0

so I was writing a program about printing out the consecutive sum of positive integers starting from 1 and the thing is that I wanted to cout the final sum in a certain form that prints out to me the 'n' I entered at the start of the program but instead it prints out the 'n' after all the calculations have been made.

#include <iostream>
using namespace std;
int main()
{
int sum,n;
cout << "Enter a number to be added consecutively starting from 1: ";
cin >> n;

do
{
    sum = sum + n;
    n--;
}
while(n > 0);


cout<<"The consecutive sum starting from 1 to "<< n <<"="<< sum <<endl;

return 0;

}

so what I mean is that in the last cout it prints out 'n' as 1 since that's when 'n' stops going into the do-while loop again. I want to print out the 'n' entered at the begining as an input/

thanks in advance! (still learning)

thanks to everyone for all the help and replies! after reading your replies I wanted to improve it

#include <iostream>
int main()
{
int sum,n;

sum = 0;
std::cout<<"\nEnter a number to be added consecutively starting from 1:";
std::cin>>n;
int t=n;

for (n ; n>0 ; n--)
{
sum = sum + n;

}

std::cout<<"\nThe consecutive sum starting from 1 to "<<t<<" = "<<sum;

return 0;

}

thank you!

Omar K.
  • 15
  • 1
  • 4
  • 3
    Use a for loop? Store `n` in another variable? I think you got a bit tunnel-visioned. – StoryTeller - Unslander Monica Dec 24 '17 at 14:14
  • 1
    sum isn’t initialized. –  Dec 24 '17 at 14:15
  • Your major problem includes not initializing sum as @manni66 said and store your first entry in another variable as StoryTeller stated – Clement Osei Tano Dec 24 '17 at 14:17
  • Note that you don't need your loop, you can compute [arithmetic sum](https://en.wikipedia.org/wiki/Arithmetic_progression) directly. – Jarod42 Dec 24 '17 at 14:25
  • 1
    And the usual one: [please don't use `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Quentin Dec 24 '17 at 14:31
  • Keeping the loop (else `sum = n * (n + 1) / 2;`), using increasing iterator instead of decreasing one is generally simpler: `int t=n; for (n ; n>0 ; n--) { sum += n; }` -> `for (int i = 0 ; i != n + 1; ++i) { sum += i;}` – Jarod42 Dec 24 '17 at 21:32

2 Answers2

1

Store the value of n into some other variable and use that variable for displaying that value! or do this

 #include <iostream>
using namespace std;
int main()
{
int sum,n;
cout << "Enter a number to be added consecutively starting from 1: ";
cin >> n;
cout<<"The consecutive sum starting from 1 to "<< n ;

do
{
    sum = sum + n;
    n--;
}
while(n > 0);


cout<<"="<< sum <<endl;

return 0;

}

this will give the desired output!

Dhruva-404
  • 168
  • 2
  • 10
0

You can store it in another variable and print it out at the end:

#include <iostream>
using namespace std;
int main()
{
    int sum = 0, n;
    cout << "Enter a number to be added consecutively starting from 1: ";
    cin >> n;
    int t = n;
    do
    {
        sum = sum + n;
        n--;
    } while (n > 0);


    cout << "The consecutive sum starting from 1 to " << t << "=" << sum << endl;

    return 0;

}

Plus, sum will not be automatically set to 0 (initialized) when you define it.

leyanpan
  • 433
  • 2
  • 9