do...while loops in c programming
Loops are used in programming to repeat a specific block of code. After reading this tutorial, you will learn how to create a while and do...while loop in C programming.
Loops are used in programming to repeat a specific block until some end condition is met. There are three loops in C programming:
- for loop
- while loop
- do...while loop
while loop
The syntax of a while loop is:
while (testExpression) { //codes }
where,
testExpression
checks the condition is true or false before each loop.example
// Program to find factorial of a number
// For a positive integer n, factorial = 1*2*3...n
#include <stdio.h>
int main()
{
int number;
long long factorial;
printf("Enter an integer: ");
scanf("%d",&number);
factorial = 1;
// loop terminates when number is less than or equal to 0
while (number > 0)
{
factorial *= number; // factorial = factorial*number;
--number;
}
printf("Factorial= %lld", factorial);
return 0;
}
Output
Enter an integer: 5
Factorial = 120
Comments
Post a Comment