Do While loop in C programming with examples

 

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;
do
{
 printf("%d ",i);
 i++;
}
while(i<=10);
}
/*
### 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
 do
 {
 	//incrementing factor count
 	fact=fact*i;
 	//incrementing loop
 	i++;
 }
 while(i<=no);
 //printing result
 printf("Factorial of %d is %d",no,fact);
 
}
/*
###Output###
Enter any number
5
Factorial of 5 is 120
*/

Here I am requesting if you found this post was helpful for you then let me know by your comment in below comment box and share it with your friend. Also you can contact me on Instagram @rajusunagar_

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thank you!!.......

Post a Comment

0 Comments