Starting Vocational Training From 1-May-2024 Get Detail
Write a program in C to display n terms of natural number and their sum.
Test Data : 7
Expected Output :
The first 7 natural number is :
1 2 3 4 5 6 7
The Sum of Natural Number upto 7 terms : 28
#include <stdio.h>
void main()
{
int i,n,sum=0;
printf("Input Value of terms : ");
scanf("%d",&n);
printf(" The first %d natural numbers are: ",n);
for(i=1;i<=n;i++)
{
printf("%d ",i);
sum+=i;
}
printf(" The Sum of natural numbers upto %d terms : %d ",n,sum);
}