New course! Every coder should learn Generative AI!
Try a free lesson0
C++ Transponation
Hi everyone am facing problems with the code for the below question pls help me anyone You are making a program for a bus service. A bus can transport 50 passengers at once. Given the number of passengers waiting in the bus station as input, you need to calculate and output how many empty seats the last bus will have. Using c++ Without if clauses. I tried this so far But it seems to be wrong int main() int seats; int passenger; seats = 50; cin >> passenger; cout << passenger % seats << endl; Return 0;
6 Answers
+ 3
i know its not the right way but it worked:
#include <iostream>
using namespace std;
int main() {
//your code goes here
int x;
int y = 50;
int sum;
cin >> x;
x = x % 50;
sum = x - y;
cout << -sum << endl;
return 0;
}
+ 2
You are quite close, however, you are missing the last step. Until now, you have calculated how many passengers the last bus will transport, via
passenger % seats
But the task wants you to output the number of empty seats for the last bus, which would be the total number of seats subtracted by the amount of passengers on that bus. Try to incorporate this into your solution.
0
Shadow can you show me a better way?
0
The term -( x - y ) is equivalent to ( y - x ), but other than that, there isn't really a much better way. You can shorten it of course to a single line, i.e.
50 - ( passenger % 50 )
but that is practically the same.
0
okay thanks mate :)
- 1
Anyone tell me how run this program?