Written by Sai Sandeep Thota on January 9, 2013 in C > C++ > Programming

Find the Sum of First n Numbers using C/C++

GET ALERTS:

Get our Latest updates delivered to your mailbox!

Finding the sum of first n numbers is very easy, User gives a number then the program returns the sum of numbers. For example take number : 5, Sum of first 5 numbers is: 15.

Sum of First n Numbers using C

#include<stdio.h>
#include<conio.h>

void main()
{
	int i, n, a=0;
	clrscr();
	printf("\nSum of a given number");
	printf("\nEnter a Number:");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		a=a+i;
	}
	printf("Sum of first %d numbers is: %d", n,a);
	getch();
}

Sum of First n numbers using C++

#include<iostream.h>
#include<conio.h>

void main()
{
	int n, a=0;
	clrscr();
	cout<<"\nSum of a given number";
	cout<<"\nEnter a Number:";
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		a=a+i;
	}
	cout<<"Sum of first "<<n<<" numbers is: "<<a;
	getch();
}


Output:
sum-of-first-n-numbers

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: