Sunday, February 19, 2012

C Program to find first sum of n numbers - c programs


C Program to find first sum of n numbers - c programs


// write a c program to find first sum of n numbers
//print sum of n numbers using c program




//C Program to find first sum of n numbers using while loop


##include<stdio.h>
#include<conio.h>
main()
{
int num, n,sum=0,i=0;
printf("enter the number ");
scanf("%d",&n);
printf("enter %d numbers",n);
while(i<n)

scanf("%d",&num);
sum=sum+num;
i=i+1;
}
printf("the sum of %d numbers is %d",n,sum);
getch();
}
}


//C Program to print first sum of n numbers without using loop


##include<stdio.h>
#include<conio.h>
main()
{
int n,sum;
printf("enter the number ");
scanf("%d",&n);
sum=n*(n+1)/2;
printf("sum of first n numbers is %d ",sum);
getch();
}

No comments:

Post a Comment