Ticker

6/recent/ticker-posts

C program to find sum of first 5 natural number using for loop

 


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
 




In this c program use two variable first is i that is counter and sum that store the value of total sum of first five natural number. for loop is use because statement is executed five times.Here sum of first five natural number 1+2+3+4+5=15.

  • 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.


Post a Comment

0 Comments