C programming code to sum numbers from 1 to n

Share This Topic:

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

  1. Accept the last number, n
  2. 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();
}

Share this article:
Previous Post: C Programming to display number patterns in right angle

December 1, 2014 - In Academics, Computers

Next Post: C programming code to display the first n odd numbers

December 2, 2014 - In Academics, Computers

Related Posts

Leave a Reply

Your email address will not be published.