# Summation number from 1 to n

Page 1 of 1

## 9 Replies - 1384 Views - Last Post: 05 January 2010 - 09:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=148239&amp;s=72fea65eea0fadec76263fe006d75b2d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jenson200986

Reputation: 0
• Posts: 90
• Joined: 06-May 09

# Summation number from 1 to n

Posted 04 January 2010 - 07:08 PM

I have code for calculate number 1 to n with using sum. Here is my code:

``` void main
int a[],n,i,sum=0;
printf("Enter n Value!");
scanf("%d", &n);
printf("\n Enter the number");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<n;i++){
sum+=a[i];
printf("\n Sum of the five number is %d", sum);
}
```

I want to know that is it the right code or not?
If it's wrong so could any one give me the right one?

This post has been edited by jenson200986: 04 January 2010 - 07:14 PM

Is This A Good Question/Topic? 0

## Replies To: Summation number from 1 to n

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Summation number from 1 to n

Posted 04 January 2010 - 07:11 PM

Why do you need an array?

Furthermore, why is the array not initialized/have a size?

### #3 jenson200986

Reputation: 0
• Posts: 90
• Joined: 06-May 09

## Re: Summation number from 1 to n

Posted 04 January 2010 - 07:17 PM

so, could you please give me other answer that can calculate number from 1 to n using sum?
for example, if i input 100 into n number the result will output:

1+2+3+4+......+100=?

### #4 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Summation number from 1 to n

Posted 04 January 2010 - 07:19 PM

Was the above supposed to be pseudo code?

Once I establish a baseline I'll provide more specific help:

```Get input
from 1 to input
sum each number to a total variable
display total

```

You have the right idea, you don't need an array to do it. Especially since there's nothing in the array you have to sum, except garbage values.

### #5 jenson200986

Reputation: 0
• Posts: 90
• Joined: 06-May 09

## Re: Summation number from 1 to n

Posted 04 January 2010 - 11:28 PM

So this is the right one:
``` void main(){
int n,i,sum=0;
printf("Enter n Value \n");
scanf("%d", &n);
for(i=0;i<n;i++){
sum= i + n;
printf( i &"+" & n & "=" & sum);}
}
```

Is it correct or not?

### #6 sarmanu

• D.I.C Lover

Reputation: 967
• Posts: 2,362
• Joined: 04-December 09

## Re: Summation number from 1 to n

Posted 05 January 2010 - 04:54 AM

If you want the sum from 1 to n, why are you using:
```for(i=0;i<n;i++) // 0 to n - 1

```

```for (i=1;i<=n;i++) // 1 to n, this is what you want

```

The second problem:
```sum= i + n;

```

Let's give 'n' the value 4.
i is 1 first, so sum will be: 1 + 3 = 4;
i is incremented, and is 2 now, so sum will be: 2 + 3 = 5;
i is incremented, and is 3 now, so sum will be: 3 + 3 = 6;
i is incremented, and is 4 now, so sum will be: 4 + 3 = 7;
Looks like the output is not correct, since 1 + 2 + 3 + 4 is 10, and not 7.

The correct summation formula should be:
```sum = sum + i;

```

Based on my above example, take the pencil and a paper and try this formula. It will be fun :-P

This post has been edited by sarmanu: 05 January 2010 - 04:55 AM

### #7 bluman

Reputation: 3
• Posts: 7
• Joined: 05-January 10

## Re: Summation number from 1 to n

Posted 05 January 2010 - 11:32 AM

If you want 1+2+3+...+n this is the formula to calculate it: sum = n*(n+1)/2;

### #8 rijaalu

Reputation: 0
• Posts: 11
• Joined: 05-January 08

## Re: Summation number from 1 to n

Posted 05 January 2010 - 08:37 PM

bluman, on 5 Jan, 2010 - 10:32 AM, said:

If you want 1+2+3+...+n this is the formula to calculate it: sum = n*(n+1)/2;

You can use array to sum from 1 to n numbers in C++.

```#include <iostream.h>

using namespace std;

int main()
{

cout <<"Enter the value of n ::\n";
cin >> n;

int a[n];
int sum = 0;

for(int i =0; i < n; i++)
{
sum +=a[i];

}

cout <<"the sum of the numbers is ::" << sum <<endl;

system("pause");
return 0;

}

```

** Edit **

### #9 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Summation number from 1 to n

Posted 05 January 2010 - 09:09 PM

rijaalu, on 5 Jan, 2010 - 10:37 PM, said:

bluman, on 5 Jan, 2010 - 10:32 AM, said:

If you want 1+2+3+...+n this is the formula to calculate it: sum = n*(n+1)/2;

You can use array to sum from 1 to n numbers in C++.

```#include <iostream.h>

using namespace std;

int main()
{

cout <<"Enter the value of n ::\n";
cin >> n;

int a[n];
int sum = 0;

for(int i =0; i < n; i++)
{
sum +=a[i];

}

cout <<"the sum of the numbers is ::" << sum <<endl;

system("pause");
return 0;

}

```

** Edit **

interesting example... you didn't really think it though did you?

first off this line: int a[n]; will only work in some compilers... most will not let this pass.

secondly, since you don't initialize the array a[] you have no idea what the sum will be...

Though as you quote pointed out at the top, there is no need to do a loop when such a calculation can be done in 1 step. A kid in elementary school can work out the formula for the sum of 1 though N...

### #10 GenHornet18

Reputation: 36
• Posts: 629
• Joined: 19-October 09

## Re: Summation number from 1 to n

Posted 05 January 2010 - 09:11 PM

That code does not abide by the syntax of C++. For example,

1.) you forgot to define and initialize 'n'
2.) the array type you have created must have a constant size when compiled, you can't create one with an undefined size.
3.) the array 'a' is not initialized to anything, so you will not receive the output you expect. This is contrast with the fact that you are modifying the element number in your loop not the data.
4.) system("pause") is hefty resource call

No arrays are unnecessary here, the problem needn't be so hard.