instance of java

RSS Feed URL: http://instanceofjavaforus.blogspot.in/
Category: Java | Total Views: 1295

Latest entries from this feed

C program to find sum of n numbers using for loop

Posted on 14 May 2017 | 1:24 pm by InstanceOfJava

  • C program to find sum of n numbers using function
  • This is about to get sum of all natural numbers in C using for loop
  • For example 4: 1+2+3+4=11
  • Let us see an example program to get  sum of natural numbers using while loop.



Program #1 :  Write a C program to get sum of all natural numbers up to n using for loop

  1. // www. instanceofjava.com all rights reserved
  2.  #include<stdio.h> 
  3. #include<math.h>
  4.  
  5. int main()
  6. {
  7.     int n, i, sum = 0;
  8.     
  9.     printf("Enter a positive integer: ");
  10.     scanf("%d",&n);
  11.  
  12.     for(i=1; i <= n; ++i)
  13.     {
  14.         sum += i;   // sum = sum+i;
  15.     }
  16.  
  17.     printf("Sum = %d",sum);
  18.  
  19.    return 0;
  20. }

 Output:


sum of all natural numbers in c

 
  



Related products