Introduction
The while loop in c programming is a very important loop. It has some advantages than for loop. In the for loop, we must know the number of iterations in advance. But there are some occasions where don’t know the number of iterations. In this case, we can use while loop.
Also read: C Program to Find the Sum of Cubes of Elements in an Array
Syntax
The syntax of the while loop in c programming is given as
while(expression)
{
body of while loop
}
statement-n;
From the above syntax, we have to use while keyword. The expression is nothing but a Boolean expression that means it evaluates to a true or false value. If the value of an expression is true, then the body of while loop will be executed, otherwise, this body will be skipped and control will directly transfer to statement-n which is exactly below the while loop body. Here, statement-n means a number of statements. This body of while loop will be repeated until the expression is true.
Here, the scope of while loop in c is a single-line statement. What does this mean? If you have to repeat only a single statement, then there is no need to write curly brackets. But, if there are more than one statements you want to repeat, then write all these statements inside the curly brackets, the otherwise only a single statement will be repeated.
For example, see the following code.
i=1;
while(i<=10)
{
printf("%d ",i);
i++;
}
The output of the above code is
1 2 3 4 5 6 7 8 9 10
Here, the initial value of i is 1. In the above c program code, while loop has an expression i.e. i<=10. That means the body of while loop will be executed until the value of i remains less or equal to 10. When the value of i becomes greater than 10, then the body of while loop will not be executed.
Also read: Floyd Triangle in C Programming
In the body of while loop, we have written two statements. One is for printing the values of i and the another statement is for updating the value of i. If we don’t update the value of the variable i, then this loop will be executed infinite times.
Now, I am going to write five important programs in c to show the use of while loop.
Example 1: Print 1 to 50 using while loop in c
In this c program, we have to print values from 1 2 3 up to 50. See the following program.
#include<stdio.h>
int main()
{
int i=1;
while(i<=50)
{
printf("%d ",i);
i++;
}
return 0;
}
We are not reading anything from the user. But suppose, instead of 50, we have to print the numbers up to n. The value n means any number. So, that number can be read from the user.
For example,
#include<stdio.h>
int main()
{
int i=1,n;
printf("Enter any number\n");
scanf("%d",&n);
while(i<=n)
{
printf("%d ",i);
i++;
}
return 0;
}
In while expression, we have written n instead of 50. The value of n can be anything.
Example 2: Print multiples of 5 in C using while loop
In this c program, we have to print the values like 5 10 15 and so on. I am going to make changes in the above program. Change the value of i from 1 to 5 because we need to start printing from 5. Now, instead of i++, write i=i+5. Why? Because, we need to print the multiples of 5. The while condition remains the same. See the following program.
#include<stdio.h>
int main()
{
int i=5,n;
printf("Enter any number\n");
scanf("%d",&n);
while(i<=n)
{
printf("%d ",i);
i=i+5;
}
return 0;
}
I hope, you are not getting confused. Let’s see another interesting examples.
Also read : C Program to Display Numbers From 1 to n Except 6 and 9
Example 3: Factorial Program in C While Loop
We can calculate factorial of any number using any loop or recursion. Here, we are not going in detail about factorial. If we want to know in detail, then click Factorial Program in C.
See the following program.
#include<stdio.h>
int main()
{
int fact=1,i=1,n;
printf("Enter any number\n");
scanf("%d",&n);
while(i<=n)
{
fact = fact * i;
i++;
}
printf("\nFactorial of %d is %d", n,fact);
return 0;
}
These are all examples, where we are using while just like for loop. The benefits of while over for loop is I explained above. Now, we will see such examples here.
Example 4: Reverse a Number in C using while loop.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rem,rev=0;
int n,an;
printf("Enter any Number\n");
scanf("%d",&n);
an=n;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf("Reverse of %d is %d",an,rev);
return 0;
}
If we want to know more about this program, then click on Reverse a Number in C.
In the above c program, we don’t know, how many times this while loop will be executed. It depends on the value of n.
Let’s see one more example.
Example 5: Strong Number in C using while loop.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, sum=0,fact,n,num,rem;
printf("Enter a Number\n");
scanf("%d",&n);
num=n;
while(n>0)
{
rem=n%10;
fact=1;
for(i=1;i<=rem;i++)
{
fact=fact*i;
}
sum=sum+fact;
n=n/10;
}
if(num==sum)
{
printf("%d is a Strong Number",num);
}
else
{
printf("%d is not a Strong Number",num);
}
return 0;
}
For more details about this program, click on Strong Number in C.
I hope, you have understood each and every example. If you need any assistance or want to give feedback, then please contact me.
Thanks for reading.