## Sunday, 11 April 2021

### While loop in c

A while loop allows a part of the code to be executed multiple times depending upon a given Boolean condition. it called a pre-tested loop. Then the ‘While loop’ is mostly used in the case where the number of repetitions is no known in advance.

Syntax of while loop:-

While(condition)

{

Statement;

Increament/decreasement;

}

Flowchart:-

Example:-

Problem 1

Write a C program to calculate the sum of two numbers without using the plus operator.

Algorithm

Step 1   a++;  b--;

Step 2   Repeat Step 1 until b becomes 0

Program of the above problem

#include <stdio.h>

int main()

{

int a, b;

printf("a=");

scanf("%d", &a);

printf("b=");

scanf("%d", &b);

while (b != 0)

{

a++;

b--;

}

printf("sum = %d\n", a);

return 0;

}

Output:-

Note:  This logic is design for only positive integers.

Problem 2

Write a C program to the counting of the number 1 to 10 using a while loop.

#include<stdio.h>

int main()

{

int i=1;

while(i<=10)

{

printf("%d\n", i);

i++;

}

return 0;

}

Output:-

Problem 3

Write a C program to print the 2s table using a while loop.

#include <stdio.h>

int main()

{

int a, n, i;

printf("number");

scanf("%d", &n);

(i = 1);

while (i<=n)

{

a = 2 * i;

{

printf("%d\n", a);

}

i++;

}

}

Output:-

## Friday, 9 April 2021

### Some basic C programs using Conditional Operator

Conditional operator simply returns one value when the condition is true and returns another value if the condition is false. It also knows as a ternary operator.

Syntax of a ternary operator

(condition) ? Expression: Expression;

Example

(a>0)?1:0; evaluates to true so 1.

Now understand the ternary operator with the help of some basic problem.

Problem 1

Write a C program to check the numbers are positive or negative.

#include <stdio.h>

int main()

{

int a, c;

printf("Enter the number ");

scanf("%d", &a);

c = (a>0) ? printf("positive number ") : printf("Negative number ");

// (a > 0) ? printf("positive number") : printf("Negative number");

}

Understanding the above code:

Declared two integer type variables which are a and c.

Input is taken in variable ‘a’ by  ‘scanf( )’ function.

Integer type variable c is used to store the decision based upon the condition by the compiler.

Output

Using this in the above code

(a > 0) ? printf("positive number") : printf("Negative number");

With both the code output is similar.

Problem 2

Write a C program to determine the greatest number between the three numbers.

#include <stdio.h>

int main()

{

int a, b, c, big;

printf("a=");

scanf("%d", &a);

printf("b=");

scanf("%d", &b);

printf("c=");

scanf("%d", &c);

big = a > b ? (a > c ? a : c) : (b > c ? b : c);

printf("Greatest number %d\n", big);

}

Output

## Wednesday, 7 April 2021

### Conditional Operator in C

Conditional operator is a Ternary operator available in list of operators in the C language. It requires three operands. Conditional statements are used to make decision based upon condition. Ternary operator requires two special symbols (‘?’ and ‘:’) to express it.

The program execution of conditional operator is just like if-else conditional statement.

Syntax of conditional operator

Expression 1? Expression 2: Expression 3;

Working of Ternary Operator

As in Expression 1? Expression 2:  Expression3. Expression 1 is the Boolean expression. If Expression1 evaluates to 0 which is a Boolean expression then that simply means FALSE and if the Expression1 evaluates to 1 this simply means TRUE therefore Expression3 or Expression2 will get evaluated based on Expression1.

For example

#include<stdio.h>

int main ()

{

int a, c;

printf("Enter the number  ");

scanf("%d", &a);

c = (a>5) ? printf("True") : printf("False");

}

Output:

## Tuesday, 30 March 2021

### String Format in Python

Formatting string enables the capability to add dynamic content or string to it in an easy way. It allows to update contents in a string in simpler way.

String Format using format () method

Syntax: template_string.format(positional_args, keyword_args)

The template string contains the replacement fields which can be replaced using the positional_args and keyword_args used in the method. The method returns the formatted string.

In the template string, the replacements fields are enclosed within curly braces “{  }”. Rest of the string content remains unchanged.

Using positional arguments

print('{0} {1} the {2} nicely'.format('This', 'formats', 'string'))

Here, <template_string> is '{0} {1} the {2} nicely'. The replacement fields are {0}, {1}, and {2} with zero-based positional arguments 'This', 'formats', 'string'. The replacements fields are replaced with the corresponding positional arguments by the format method.

Using keyword arguments

print('{name} is {age} years old'.format(name='Python', age= 30))

Here, the replacement fields are {name}, {age}and the corresponding keyword arguments with values. Each fields are replaced with corresponding keyword argument values.