0
Write a program to print sum of series (1)+(1+3)+(1+3+5)+(1+3+5+7).....n using go to statement
using goto
2 Respostas
+ 3
#include <iostream>
using namespace std;
int main() {
unsigned sum = 0, subsum = 0, n;
try{
cin >> n;
}catch(...){
cout << "Bad input" << endl;
return -1;
}
unsigned i = 0;
loop:sum += subsum += 2*i + 1;
if(++i < n)
goto loop;
cout << sum << endl;
return 0;
}
+ 1
For n == 3 :
1 + (1+3) + (1+3+5)