Wednesday, 30 July 2014

Display sum of first n positive nos.

#include<stdio.h>
#include<conio.h>
void main()
{
        int i, n, sum=0;
        clrscr();
printf(“Enter total no. to sum : ”);
scanf(“%d”, &n);
for(i=1;i<=n;i++)
{
                sum=sum+i;
}
printf(“Sum of first %d nos. is %d”, n,sum);
getch();
}


OUTPUT :
Enter total no. to sum : 10

Sum of first 10 nos. is 55

No comments:

Post a Comment