 "For loop test" my code is not working. | Sololearn: Learn to code for FREE!

+1

# "For loop test" my code is not working.

Bon Yoyage! (C++) // “You are on a 5-hour sea voyage. The ship sails at a speed of 40km per hour. Write a program that will output how many kilometers the ship has traveled by each hour.” I'm doing this and it's not working #include <iostream> using namespace std; int main() { int distance = 0; //your code goes here for (distance <= 0; distance + 40;){ cout << distance << endl; } return 0; } Expected Output 40 80 120 160 200

+1

Remember coding is all about making mistakes and trying again .

+6

Rami Moazzen #include <iostream> using namespace std; int main() { int distance=0; int i; for(i=0;i<5;i++) { distance+=40; cout<<distance<<endl; } return 0; }

+4

#include <iostream> using namespace std; int main() { for (int i=1;i<=5;i++){ cout<<40*i<<endl; } return 0; }

+4

Farnaz.A I think so we have to print each distance in a new line

+3

Rami Moazzen Here is solution,hope it will help you speed*time=distance and we have 5 hours so distance travel in 5 hours is something like this #include <iostream> using namespace std; int main() { int distance=0; int speed=40; for (int time=1;time<=5;time++) { distance=speed*time; cout<<distance<<endl; } return 0; }

+2

Why bother with that? Simply write : int km=5*40; cout<<km; note:question is wrong ? u already know how many km u sail in one hour .

+2

Atul alright he can simply add endl I fixed it . 👌

+2

Rami Moazzen It says that it is a 5 hour trip, but your code will run indefinitely. you have to stop after 5 hours. it would make more sense in this case to loop through the days, instead of the distance: for (int day=1; day<=5; day++) { int distance = day*40; cout << distance << endl; }

+2

Rami Moazzen Speed=40 is constant but time is increasing by 1 untill time reaches to 5 so every time distance is calculated which is multiple of 40 40*1=40 40*2=80 40*3=120 40*4=160 40*5=200

+2

Tommy Simpson Go through this 👇 https://www.sololearn.com/discuss/1316935/?ref=app

+2

Nnanyere Uzoma dude just post that as ur own question not here ...

+2

Nnanyere Uzoma go to discussion tab and select the pencil icon then just post ur comment here there .

+1

#include <iostream> using namespace std; int main() { int distance = 0; //your code goes here for (distance=0;distance <= 0; distance += 40){ cout << distance << endl; } return 0; }

+1

Atul, it's not working https://code.sololearn.com/cm4RO9dTwVUR/?ref=app

+1

#include <iostream> using namespace std; int main() { int speed; int time; cin>>speed; cin>>time; for(int i=1;i<=speed;i++){ cout<<speed*time<<endl; } return 0; }

+1

Farnaz.A true, but the main thing from the quiz is teach me how to use for loop and unfortunately it's not working with me so I'm doing something wrong, and still can't figure it out :(

+1

Rami Moazzen try this code

+1

Deepesh Patel, just one more question, I'm still beginner and don't understand why you used "time++" As i understood x++ means increase by one, while here we want to increase it with 40 each time. So how did it work here?

+1

#include <iostream> using namespace std; int main() { int a; cin >> a; for (int i=1;i<=a;i++) cout << i*40 << endl; return 0; } // Hope this helps

0

This is the correct code. In terms of syntax