While loop statement

While loop statement:

In while loop control statement, loop is executed until condition becomes false.

while

 

Example 1

#include <stdio.h>

int main()

{

int j=3;

while(j<10)

{

printf(“%d\n”,j);

j++;

}

}

Output

3 4 5 6 7 8 9

Example 2

#include<stdio.h>

int main()

{

int value1 = 1;

while(value1<=3)

{

printf(“Value is %d\n”, value1);

value1++;

}

return 0;

}

output

Value is 1 Value is 2 Value is 3

writea pragram to find factorial of a number

#include

int main()

{

int number1;

long long factorial1;

printf(“Enter an integer: “);

scanf(“%d”,&number1);

factorial1 = 1; // loop terminates when number is less than or equal to 0

while (number1 > 0)

{

factorial1 *= number1; // factorial = factorial*number;

–number1;

}

printf(“Factorial= %d”, factorial1); return 0;

}

Output

Enter an integer: 6

Factorial = 720

 

Leave a Reply

Your email address will not be published. Required fields are marked *