C programming code to sum numbers from 1 to n
Course Details
Field of Study: Computer Science and Engineering
Course Name: C Programming
Course Description: C programming code to sum numbers from 1 to n
Algorithm
- Accept the last number, n
- Add numbers from 1 to n using for loop
Code
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,sum=0;
printf(“Enter a number.\n”);
scanf(“%d”,&n);
for(int i=1;i<=n;i++){
sum=sum+i;
}
printf(“Sum=%d”,sum);
getch();
}