Write algorithm and c program to find sum of n natural numbers
Answers
The program output is also shown below.
* C program to find the sum of 'N' natural numbers.
int i, num, sum = 0;
printf("Enter an integer number \n");
scanf ("%d", &num);
for (i = 1; i <= num; i++)
sum = sum + i;
printf ("Sum of first %d natural numbers = %d\n", num, sum);
Algorithm and c program to find the sum of n natural numbers is given below .
Explanation:
Algorithm of find the sum of n natural number is given below
Step1: Declared the variable "i" and sum and initialize sum with 0
Step2 : Read the number of terms u wanted suppose it will stored in "n1".
Step3: begin the loop i=1 to i<n1
sum =sum +i
increment the value of i by 1
Step4: Display sum
Program to find sum of n natural numbers is given below
#include <stdio.h> // header file
int main() // main method
{
int n1,sum=0,i;// variable declaration
printf(" Enter the terms:");
scanf("%d",&n1); // Read the input by user
for(i=1;i<n1;++i) // iterating the loop
{
sum=sum+i; // adding terms
}
printf("%d",sum);// display sum
return 0;
}
Output:
Enter the terms:5
10
Following are the description of program:
- Declared a variable n1 ,"i" and also "sum".
- Initialized sum with 0 .
- Read the value by user in n1.
- Iterating the loop to calculating the sum.
- Finally display the sum .
Learn More:
- brainly.in/question/7744519