Adsense 728 * 15

Search

Custom Search

Program to print sum of first ten numbers


Write a program to print the sum of first ten numbers.

To print sum of 10 numbers we can use loop to add numbers. Alternatively we can also use mathematical formula to calculate sum of series.

Program to print sum of n numbers.

Program to print the sum of first ten numbers


Method 1: Using for loop to calculate sum of first ten numbers.



#include <stdio.h>
 
int main() {
 
 int i,sum=0;
 
 for(i=1;i<=10;i++){
 
  sum = sum + i;
 }
 
 printf("Sum of 10 numbers is %d",sum);
 
 
 return 0;
}

Method 2- Using sum of series.
#include <stdio.h>

int main() {
 
 int i,sum=0;
 
 /* Taking the value of n is 10. As we are calculating sum of 10 numbers.
    You can take the value of n as user input as well. */
 
 int n = 10;
 
 /* Sum of n numbers. */
 
 sum = (n*(n+1))/2;
 
 
 printf("Sum of 10 numbers is %d",sum);
 
 
 return 0;
}



No comments:

Post a Comment