- 2

Java Primitive Operators - Tour Agency

Stuck on this for a day now, don't know if I'm even on the right track with it as my maths problem solving wouldn't be the best without involving code. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int distance = read.nextInt(); int var = 1000; double result = distance % var; System. out. println(result); } }

22nd Sep 2021, 6:30 PM
Eoin O'Hagan
Eoin O'Hagan - avatar
7 odpowiedzi
+ 4
Eoin O'Hagan As I understand, you can write sth like, int onekm = 1000; int remaining = distance % onekm; int result = (distance - remaining) / onekm; System.out.println(result); System.out.println(remaining); Try and see what will happen!
22nd Sep 2021, 8:03 PM
mesarthim
mesarthim - avatar
+ 2
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int distance = read.nextInt(); int KM=distance/1000; //1 kilometer is 1000 meters int M=distance%1000; System.out.println(KM); System.out.println(M); } }
29th Nov 2021, 2:12 PM
Kamalesh Kumar J V
+ 1
worked a treat, thank you
22nd Sep 2021, 8:21 PM
Eoin O'Hagan
Eoin O'Hagan - avatar
+ 1
Awesome - thank you
17th Nov 2021, 12:14 AM
truqoddess
truqoddess - avatar
0
If you want to calculate the result as double, you should use double variables. So, try this double result = (double)distance % (double)var; Also, I don't remeber if remaining operator "%" can be used for double variables. If not, try this, int result = distance % var; In addition, I don't think you must use a double variable, int is enough and acceptable for your problem, I think. I hope, it helps. Happy coding!
22nd Sep 2021, 6:35 PM
mesarthim
mesarthim - avatar
0
hmmm I see what you mean, unfortunately it still didnt work though. Here is the problem to give you more of an idea of whats expected. You are a manager a a tour agency and need to determine the distances between cities. The given program takes distance in meters as input. Task Complete the code to output the distance in kilometers and meters each on a new line. Sample Input 3644 Sample Output 3 644 Explanation 1 kilometer is 1000 meters, therefore 3644 meters is equal to 3 kilometers and 644 meters. Use the / operator to calculate the count of kilometers and % operator for meters.
22nd Sep 2021, 7:47 PM
Eoin O'Hagan
Eoin O'Hagan - avatar
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int distance = read.nextInt(); int KM=distance/1000; //1 kilometer is 1000 meters int M=distance%1000; System.out.println(KM); System.out.println(M); } } Good Luck
26th Jan 2022, 6:21 AM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar