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

7th Sep 2017, 3:21 PM
rishabh Singh
rishabh Singh - avatar
2 Respuestas
+ 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; }
7th Sep 2017, 4:04 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
For n == 3 : 1 + (1+3) + (1+3+5)
7th Sep 2017, 4:06 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar