Problem:-
program to find sum of first 5 natural number using for loop.
Algorithm
Step1:- Start.
Step2:-Read i,sum=0.
Step3:-for i=1 to 5.
Step4:-sum=sum+i
Step6:-print sum.
Step7:-Stop.
Flowchart
C code
Output after compile CTRL+F9 and run CTRL+F5
- Initially sum=0.
- First time loop executed sum=0+1=1. Now sum hold value 1.
- second time loop executed sum=1+2=3.
- Third time loop executed sum=3+3=6.
- Fourth time loop execute sum=6+4=10.
- Fifth time loop executed sum=10+5=15.
- Now i=6 which is greatet than 5 so loop stop.
- Print sum value using printf function sum=15.
0 Comments