+ 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
36 Respostas
+ 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
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
+ 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
0
Why did you define that distance is 0 if the int distance is already 0?
0
#include <iostream>
using namespace std;
int main()
{
int distance = 0;
//your code goes here
for (;distance <= 0; distance += 40){
cout << distance << endl;
}
return 0;
}
//For that you can use this
0
Farnaz.A
Expected Output
40
80
120
160
200
0
Alex the test wants me to use for loop
0
Atul in the test it doesn't require an input :/
0
Alex I'm not copy pasting but i said already it's requires a for loop in the test!!!
I can already use while and I'm not asking for how to use while?!!