Write a C Program to add first n numbers.
1: #include<stdio.h>
2: #include<conio.h>
3: void sum(int);
4: int main()
5: {
6: int n;
7: clrscr();
8: printf("Enter a number : ");
9: scanf("%d",&n);
10: sum(n);
11: getch();
12: return 0;
13: }
14: void sum(int n)
15: {
16: int i,s=0;
17: for(i=1;i<=n;i++)
18: {
19: s+=i;
20: }
21: printf("Sum of first %d integer is %d\n",n,s);
22: }
No comments:
Post a Comment