Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

Look at the below code:

#include <iostream>

using namespace std;

int main() {   

  int n;

  int i;  

  int k; 

  cout << "please insert n";

  cin >> n; k=0 ;

  for (i = n; i > 1; i--) {

    cout << "/n "<< k << "+" << i << "=" << i + k++;

  }

  return 0;     

}  

I'm attempting to imitate the above code in python, however I don't know what I'm getting incorrectly. I don't know how to begin at a number, at that point decrement until the condition is met. This is what I have up until now:

k=0

n=4

for i in range(n)

 if i > 1 :

  i-=1

  k+=1

print(i+k++) 

Where I went wrong?

1 Answer

0 votes
by (26.4k points)

Try the following code:

for(i=n,k=0; i > 1 ; i--,k++){

   cout << "\n "<< k << "+" << i << "=" << i + k;

}

You meant \n (newline) in C++ not /n.

Want to become a expert in Python? Join the python course fast!

Watch this video tutorial for more details.

Related questions

0 votes
1 answer
asked Sep 23, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
asked Sep 23, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
asked Aug 1, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
asked Aug 1, 2020 in Python by ashely (50.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...