Pages

Saturday, February 14, 2015

C program to print first n natural numbers and their sum


#include<stdio.h>
#include<conio.h>

void main()
{
int n,i,sum=0;
clrscr(); //to clear the screen
printf("Enter the value of n:");
scanf("%d",&n);

for(i=1;i<=n;++i)
{
printf("
%d",i);

sum+=i;
}
printf("

Sum of first n natural numbers is:%d",sum);

getch(); //to stop the screen
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.