Enter the value of n.
19
Sum=190
In this program, the user is asked to enter the value of n. Suppose you entered 19 then, count is inatialized to 1 at first. Then, the test expression in the for loop,i.e., (count<= n) becomes true. So, the code in the body of for loop is executed which makes sum to 1.
Then, the expression ++count is executed and again the test expression is checked, which becomes true. Again, the body of for loop is executed which makes sum to 3 and this process continues. When count is 20, the test condition becomes false and the for loop is terminated.
C programming while and do...while Loop
C programming loops
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied, i.e., loops are used in performing repetitive work in programming.
Suppose you want to execute some code/s 10 times. You can perform it by writing that code/s only one time and repeat the execution 10 times using loop.
There are 3 types of loops in C programming:
1. for loop
2. while loop
3. do...while loop
Syntax of while loop
while (test expression)
{
statements to be executed.
}
In the beginning of while loop, test expression is checked. If it is true, codes inside the body of while loop,i.e, code/s inside parentheses are executed and again the test expression is checked and process continues until the test expression becomes false.
Example of while loop
Write a C program to find the factorial of a number, where the number is entered by user. (Hints: factorial of n =