0

code problem

//find the sum of the series 1 +11 + 111 + 1111 + .. n terms #include <iostream> #include <cmath> using namespace std; int main() { //Getting user input int x,sum,n; x=0;sum=0; cout<<"Enter the number of terms you wanna add"<<endl; cin>>n; //LOOP for (int i=0; i<n; i++){ x += pow(10,i); sum += x; } cout<<"Sum is: "<<sum<<endl; return 0; } I cannot get the right output after the third term. what is the problem in the code?

13th May 2020, 1:38 PM
Kumudu_Mohottala
Kumudu_Mohottala - avatar
8 Answers
+ 2
I got 122
13th May 2020, 2:10 PM
Kumudu_Mohottala
Kumudu_Mohottala - avatar
+ 2
working.. thank you everyone.... I think I have missed something
13th May 2020, 2:13 PM
Kumudu_Mohottala
Kumudu_Mohottala - avatar
+ 1
But from third term it gives incorrect outputs
13th May 2020, 2:06 PM
Kumudu_Mohottala
Kumudu_Mohottala - avatar
+ 1
I ll check it
13th May 2020, 2:10 PM
Kumudu_Mohottala
Kumudu_Mohottala - avatar
0
Huh I ran your code and it actually works...
13th May 2020, 2:05 PM
Telo Philippe
Telo Philippe - avatar
0
Your code gives me 123 for the third term, isn't that right ?
13th May 2020, 2:08 PM
Telo Philippe
Telo Philippe - avatar
0
I just copy-pasted your code, that's not normal 😁
13th May 2020, 2:11 PM
Telo Philippe
Telo Philippe - avatar
0
No problem 😉
13th May 2020, 2:15 PM
Telo Philippe
Telo Philippe - avatar