Friday, 15 February 2013

c++ - for-Loop to add integers -



c++ - for-Loop to add integers -

here instructions:

write programme accepts integer input keyboard , computes sum of integers 1 integer. example, if 7 input, sum: 1 + 2 + 3 + 4 + 5 + 6 + 7 computed. utilize while or loop perform calculations. print out result after sum has been calculated. note: if input big integer, not right results.

what can't figure out how add together of integers together. help appreciated.

//preprocessor directives int main () { //declare , initialize variables int n, i; int total; //user input cout << "enter integer: "; cin >> n; //compute sum of integers 1 n total=0; (i = 1; <= n; i++) cout << i; homecoming 0; }

you add together using += operator:

for (i = 1; <= n; i++) total += i; cout << total;

note short total = total + i.

c++ for-loop

No comments:

Post a Comment