Adsense 728 * 15

Search

Custom Search

C Program to Print the Sum of First Ten Natural Numbers

Write a program to print the sum of first ten natural numbers. In this programming question, we have to write a code to print the sum of 10 natural numbers.

We don't have to take any input for this program, as we have to calculate the sum of first 10 numbers. There are other variation of this program is to write a c program to print the sum of first N natural numbers.

There are two approaches to solve this program.

i) Use for loop to iterate from 1 to 10 and add numbers.
ii) We can use mathematical formula to calculate the sum of first ten natural numbers.


C Program to Print the Sum of First Ten Natural Numbers

In first example, we are going to use for loop to calculate sum of first ten numbers.

#include <stdio.h>
 
int main() {
 
   int i, sum=0;
 
   //Run a loop from 1 to 10
   for(i = 1; i <= 10; i++) {
     //Add the value
     sum = sum + i;
   }
 
   printf("Sum of 10 numbers is %d",sum);
 
   return 0;
}

Program to find the sum of n natural numbers using recursion

Program to print prime numbers between 1 to 100

 In second example, we use mathematical formula to calculate the sum of first ten numbers.


#include <stdio.h>

int main() {
 
  int i,sum=0;
 
  int n = 10;
 
  /* Sum of n numbers.
     where the value of n is 10 
  */
 
  sum = (n*(n+1))/2;
 
 
  printf("Sum of 10 numbers is %d",sum);
 
 
  return 0;

}


Programming questions on strings

No comments:

Post a Comment