Is there a shorter way I can add 6 numbers without using a loop?
Here is my code:
sum = 1 + 2 + 3 + 4 + 5 + 6;
Yes, this is homework, but it is acceptable to submit the assignment the long way. I just want to reduce 6 variable declarations to 1, if possible (without using a loop). The textbook doesn't show a way, so either I overlooked it or it's not possible. . .
#include <iostream>
int sumtorial( int value )
{
if ( value )
{
std::cout << value ;
if (value - 1)
std::cout << " + " ;
value += sumtorial(value-1) ;
}
return value ;
}
int main()
{
int result = sumtorial(6) ;
std::cout << " = " << result << '\n' ;
}