Write a program to find the sum of first n natural numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,i=1;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("Sum=%d",sum);
getch();
}
Output:
Enter the number: 5
Sum=15
No comments: