Java Primitive Operators - Tour Agency | Sololearn: Learn to code for FREE!

-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); } }

9/22/2021 6:30:35 PM

Eoin O'Hagan

+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!

+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); } }

+1

worked a treat, thank you

+1

Awesome - thank you

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!

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.

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