While loop in c :
The while loop is a simple loop, it executes as long as the condition is true. This loop terminates when the condition is false while the general syntax of the loop is given below.
initial variable declaration;
while(condition)
{
// statements
// increment
}
|
![]() |
| while loop in c |
If the condition is false at first, then the compiler loop is not entered at all, the loop is completely skipped. Let us try to understand the while loop with an example.
Table of One :
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=10)
{
printf("%d\n",i);
i++;
}
getch();
}
In the example above, the first 'i' variable is done with a value of 1, the condition within the loop is conditioned until the loop executes until the number 10 is less or equal to 10. The value of the number variable is being printed in every iteration inside the loop. After this, the number variable is being incremented. This program generates the output given below.
OUTPUT :
1 2 3 4 5 6 7 8 9 10
If the initial variable is not incremented, the condition will never be false. In this case, the loop will run infinitely time.
More examples :
Print digits of an integer value in reverse order :
#include<stdio.h>
#include<conio.h>
void main()
{
int no,b;
printf("Enter any number=\n");
scanf("%d",&no);
printf("The Reverse is given below\n");
while(no!=0)
{
b=no%10;
printf("d",b);
no=no/10;
}
getch();
}
Output :
Enter any number=
798
The reverse is given below
897
A check number is palindrome or not :
#include<stdio.h>#include<conio.h>
void main()
{
int no,b,rev=0,cpy;
printf("Enter any number=\n");
scanf("%d",&no);
cpy=no;
while(no!=0)
{
b=no%10;
rev=rev*10+b;
no=no/10;
}
if(cpy==rev)
printf("palindrome");
else
printf("not palindrome");
getch();
}
Output :
Enter any number=
5885
palindrome
Find sum of digits of integer value :
#include<stdio.h>
#include<conio.h>
void main()
{
int no,b,sum=0;
printf("Enter any number=\n");
scanf("%d",&no);
cpy=no;
while(no!=0)
{
b=no%10;
sum=sum+b;
no=no/10;
}
printf("A Total sum of digits=%d",sum);
getch();
}
Output :
Enter any number=
785
A total sum of digits=20
Find multiply of digits of integer number:
#include<stdio.h>#include<conio.h>
void main()
{
int no,b,multi=1;
printf("Enter any number=\n");
scanf("%d",&no);
cpy=no;
while(no!=0)
{
b=no%10;
multi=multi+b;
no=no/10;
}
printf("A Total multiply of digits=%d",multi);
getch();
}
Output :
Enter any number=
325
A Total multiply of digits=30
Print first and last digit of integer number :
#include<stdio.h>#include<conio.h>
void main()
{
int no,b,f;
printf("Enter any number=\n");
scanf("%d",&no);
cpy=no;
while(no!=0)
{
b=no%10;
no=no/10;
}
printf("first digits=%d" and last digit=%d",b ,f);
getch();
}
Output:
Enter any number=
4859
first digit=4 and last digit
We assure you that you will not fount any problem in this tutorial. But if there is any mistake or error, you can contact us on sseffort@gmail.com also you cal follow us on Instagram-:@ss-effort

0 Comments
Please do not enter any spam link in the comment box