While Loop in C Language

Hello friends how are you, today in this blog i will teach you what is while loop statement, syntax of while loop , how while loop statement works and many programs using while loop statement in a very simple way.

Let's start

To run the particular block of code continuously until a required condition is fulfill is called looping. It is used to perform looping operation. When the condition will become false the execution of loop will be stopped.

👉Syntax


Its body will execute until the given condition is true.
👉Example 1 

#include<stdio.h>
int main()
{
int i=1;
while(i<=10)
{
 printf("%d ",i);
 i++;
}
}
/*
### output ###
1 2 3 4 5 6 7 8 9 10
*/

In the above program i is a variable which is initialized with 1,condition goes to 10 and it is incremented by 1 so the output will be 1 to 10.
👉Program: Factorial program 
The product of all positive number which is less than or equal to a given positive number is called factorial of that number.

For example if we want to find factorial of 5 then it will be product of 1,2,3,4 and 5.
 
To make a factorial program we will take three variables , one for number , second for running loop and third one for  storing the product of numbers.

After taking user input of number we will run loop from 1 to number itself and then we will find product of number from 1 to number and store it to a variable.

After multiplying number from 1 to number we will print the product as a output of program.

#include<stdio.h>
int main()
{
 //variables declaration
 int no,fact=1,i=1;
 //taking user input
 printf("Enter any number\n");
 scanf("%d",&no);
 //loop for multiplying numbers
 while(i<=no)
 {
 	//incrementing factor count
 	fact=fact*i;
 	//incrementing loop
 	i++;
 }
 //printing result
 printf("Factorial of %d is %d",no,fact);
 
}
/*
###Output###
Enter any number
5
Factorial of 5 is 120
*/

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.

 If you have not subscribed my website then please subscribe my website. 

Try to learn something new and teach something new to other. Thanks.

Post a Comment

0 Comments