While Loop in C language

Hello everyone !!!!!! Hope you all must be doing well.
“Don’t wait for opportunity. Create it.!!! ”
“While ( ! ( Succeed = try() ) ) ;”
This means that we should keep trying until and unless we get successful in our life 🙂
In last article – For Loop in C language we have seen For Loop in detail.
Today we are going to have a look on While Loop.
Soo let’s get started 🙂
What is While Loop????
1)It is a entry controlled loop.
2)Here, statement(s) may be a single statement or a block of statements. The condition may be any expression and The loop iterates while the condition is true.
3)When the condition becomes false, the program control passes to the line immediately following the loop.
Syntax of While Loop:-
while(condition)
{
statement(s);
}
How exactly the while Loop is executed??
1)The while loop executes as long as the given logical expression evaluates to true.
2) When expression evaluates to false, the loop stops.
3) The expression is checked at the beginning of each iteration.
4)The execute statements inside the body of the while loop statement are not executed if the expression evaluates to false when entering the loop. It is necessary to update the loop condition inside the loop body to avoid an indefinite loop.
Flowchart:- Flowchart will make you understand more effectively 👇

Now will move towards some simple programs using while Loop👇
Q1) Write a ‘C’ program to print numbers from 1 to 10.
#include<stdio.h>
main ()
{
int i = 1;
while( i <=10 )
{
printf(“value of a: %d\n”, i);
i++;
}
}
Output:-
value of a:1
value of a:2
value of a:3
value of a:4
value of a:5
value of a:6
value of a:7
value of a:8
value of a:9
value of a:10
Q2) Write a ‘C’ program to print numbers from 10 to 1.
#include<stdio.h>
main ()
{
int i = 10;
while( i >=1 )
{
printf(“value of a: %d\n”, i);
i–;
}
}
Output:-
value of a:10
value of a:9
value of a:8
value of a:7
value of a:6
value of a:5
value of a:4
value of a:3
value of a:2
value of a:1
Q3) Write a ‘C’ program to accept an integer and display its sum of digits.
#include<stdio.h>
main( )
{
int num, rem, sum = 0;
printf(“Enter the number:”);
scanf(“%d”, &num);
while (num > 0)
{
rem = num % 10;
sum = sum + rem;
num = num/10;
}
printf (“Sum of the digits = %d”, sum);
}
Output:-
Enter the number:123
sum of the digits =6 (1+2+3=6)
Q4) Write a ‘C’ program to accept a number and reverse it.
#include<stdio.h>
main ()
{
int num, rev=0, r;
printf(“enter number”);
scanf(“%d”, &num);
while(num>0)
{
r=num%10;
rev=rev*10+r;
num=num/10;
}
printf(“Reversed number is %d”, rev);
}
Output:-
enter number:4567
Reversed number is 7654
Q5) Write a C program to print multiplication table of given number.
#include<stdio.h>
int main()
{
int i=1,number=0,b=9;
printf(“Enter a number: “);
scanf(“%d”,&number);
while(i<=10)
{
printf(“%d \n”,(number*i));
i++;
}
return 0;
}
Output:-
Enter a number: 9
9
18
27
36
45
54
63
72
81
90
That’s it for today. Will see more interesting topics in our upcoming articles soo keep supporting 🙂
And yes if you have any doubt, please feel free to ask. I’ll try to solve your doubts 🙂
Will meet in next article till than goodbye………. Have a wonderful day ahead 🙂
Thanks for sharing with Output Alafia. Good 1
Thank you sir😊