WHILE LOOP IN C

Maha

While Loop In C

A while loop is a control flow statement that repeatedly executes a block of code as long as the specified condition is true. 

Syntax:

while (condition) 
{
// code to be executed repeatedly 
}

 Example of while loop in C to calculate the sum of numbers from 1 to 20:

Program:

#include <stdio.h>
int main() 
{
int i = 1;
int sum = 0;
while (i <= 10
{
sum += i;
i++;
}
printf("The sum of numbers from 1 to 20 is %d\n",sum);
return 0;
}

Output:

The sum of numbers from 1 to 20 is 210

Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send