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)
Aucun commentaire:
Enregistrer un commentaire