## 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:-