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!