a for loop | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

a for loop

ok using a for loop to calculate and display the the sum of 1 to n. where n is an input by the user . the result from the for loop should be some thing like 1+2+3+4=10 when n =4

9th Mar 2017, 5:32 PM
itumeleng
itumeleng - avatar
2 Antworten
+ 1
#include <iostream> using namespace std; int main() { int x; cout<<"Enter a number n to calculate the sum from 1 to n"<<endl; for (int i =1 ; i<=x; i++) { if(i==x){ cout<< "Sum of 1 through " << i << " is " << i*(i+1)/2<<endl; } } } //Write this code and it will work :)
9th Mar 2017, 6:39 PM
Nazareth Haroutioun Keshishian
Nazareth Haroutioun Keshishian - avatar
+ 1
Thank you
9th Mar 2017, 6:57 PM
itumeleng
itumeleng - avatar