7 Replies - 3434 Views - Last Post: 24 October 2010 - 02:11 PM Rate Topic: -----

#1 cyber_gurl2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 19-October 10

Sum of Numbers

Posted 24 October 2010 - 01:40 PM

Hello Everyone, I have been working on this program and here is my code and If anyone can give me a hint or some idea of how i can improve it.
So far, what i'm trying to do is find the sum of the integer i entered. so for example, i enter 5 the result will show 15. Actually, that is my problem because instead of the result showing 15 it shows 5 as the result. I know its adding 1 to each value so it shows 12345. what i want is 1+2+3+4, but my math formula is wrong. To make this work i have another formula to try and that is n= number. (n(n+1)/2. If i use this formula it doesn't work.

// Sum of Numbers
// This ogram uses a loop to get the sum of all integers
// from 1 up to the number entered.

#include <iostream>
using namespace std;
int main()
{
int number;
int num = 1;
int sum = 0;
   cout << “Enter a Positive Integer: “ << endl;
   cin  >> number;

   while(number <1)
   {
   cout << “ The number must be a postive integer: “ << endl;
   cin  >> “ number;
   }

   [b]for ( sum = 1; sum <= number; sum++)
   { sum += number[/b]
   }

   cout << “The sum of the number is: “ << endl;

   return 0;
 }


This post has been edited by Martyr2: 24 October 2010 - 01:49 PM
Reason for edit:: Please use code tags in the future, thanks!


Is This A Good Question/Topic? 0
  • +

Replies To: Sum of Numbers

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,898
  • Joined: 08-August 08

Re: Sum of Numbers

Posted 24 October 2010 - 01:52 PM

If the user enters 0, what will happen in your while loop?
Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Sum of Numbers

Posted 24 October 2010 - 01:53 PM

Look at your logic. You are resetting the sum variable every pass in the for loop.

Use a counter variable like 'i' and add it to sum.

Example:
int sum = 0;

for(int i = 1; i <= number; i++)
{
  sum += i;
}

cout << sum << endl;


Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4436
  • View blog
  • Posts: 12,307
  • Joined: 18-April 07

Re: Sum of Numbers

Posted 24 October 2010 - 01:55 PM

You can also just do your formula correctly...

// Sum of Numbers
// This ogram uses a loop to get the sum of all integers
// from 1 up to the number entered.

#include <iostream>
using namespace std;
int main()
{
    int number;
    int num = 1;
    int sum = 0;
    cout << "Enter a Positive Integer: " << endl;
    cin  >> number;

    while(number <1)
    {
        cout << " The number must be a postive integer: " << endl;
    }

    // Notice the formula in this line
    cout << "The sum of the number is: " << (number * (number + 1)) / 2 << endl;

    return 0;
 }



Pretty straight forward to me. :)
Was This Post Helpful? 1
  • +
  • -

#5 Guest_cybergurl_2010*


Reputation:

Re: Sum of Numbers

Posted 24 October 2010 - 02:00 PM

View PostCTphpnwb, on 24 October 2010 - 12:52 PM, said:

If the user enters 0, what will happen in your while loop?

It is suppose to show invalid input . Guess I have to add that too.
Was This Post Helpful? 0

#6 cyber_gurl2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 19-October 10

Re: Sum of Numbers

Posted 24 October 2010 - 02:05 PM

View Postcyber_gurl2010, on 24 October 2010 - 12:40 PM, said:

Hello Everyone, I have been working on this program and here is my code and If anyone can give me a hint or some idea of how i can improve it.
So far, what i'm trying to do is find the sum of the integer i entered. so for example, i enter 5 the result will show 15. Actually, that is my problem because instead of the result showing 15 it shows 5 as the result. I know its adding 1 to each value so it shows 12345. what i want is 1+2+3+4, but my math formula is wrong. To make this work i have another formula to try and that is n= number. (n(n+1)/2. If i use this formula it doesn't work.

// Sum of Numbers
// This ogram uses a loop to get the sum of all integers
// from 1 up to the number entered.

#include <iostream>
using namespace std;
int main()
{
int number;
int num = 1;
int sum = 0;
   cout << “Enter a Positive Integer: “ << endl;
   cin  >> number;

   while(number <1)
   {
   cout << “ The number must be a postive integer: “ << endl;
   cin  >> “ number;
   }

   [b]for ( sum = 1; sum <= number; sum++)
   { sum += number[/b]
   }

   cout << “The sum of the number is: “ << endl;

   return 0;
 }


Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3103
  • View blog
  • Posts: 10,898
  • Joined: 08-August 08

Re: Sum of Numbers

Posted 24 October 2010 - 02:10 PM

View Postcybergurl_2010, on 24 October 2010 - 04:00 PM, said:

View PostCTphpnwb, on 24 October 2010 - 12:52 PM, said:

If the user enters 0, what will happen in your while loop?

It is suppose to show invalid input . Guess I have to add that too.

Sorry, I misread it as two couts. Your while loop will work.
Was This Post Helpful? 0
  • +
  • -

#8 cyber_gurl2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 19-October 10

Re: Sum of Numbers

Posted 24 October 2010 - 02:11 PM

Thanks Everyone, for your ideas and my program is now working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1