Write a C program and flowchart to print sum of n natural number

For writing this program you need to have knowledge of the following C Programming topics :

Algorithm :

  1. START
  2. Read the value of n
  3. add n to sum variable until the value of n is greater than that
  4. Decrease the value of n by 1
  5. Write the final value of n
  6. STOP

C Program :

  1. /* Write a C program and flowchart to print sum of n natural number */
  2. #include <stdio.h>
  3.  
  4.  
  5. void main()
  6. {
  7. int n, sum;
  8. printf("Enter the natural number");
  9. scanf("%d", &n);
  10.  
  11. while(n>0)
  12. {
  13. sum=sum+n;
  14. n--;
  15. }
  16. printf("%d",sum);
  17. }

Result :

Download Files used in this program :

Related Programs :

Similar Posts