Stefza - 25 days ago 5x

Java Question

Im a bit new to programming and im stuck with a few questions I have to do.

In the questions I have to create a program which turns change (money) into "pounds:" and "pence:". How would I do this with a single double such as 154p = Pound: 1 Pence: 54.

I also have to do the same with converting feet & Inches into Meters: and Cm:. i.e. 5 foot 11 inches = 1m 80cm.

I believe there is a simple way of doing this, I just cant figure it out.

This is what I have so far for feet and inches program.

`package measure;`

import java.util.Scanner;

public class Measure {

static Scanner input = new Scanner(System.in);

public static void main(String[] args) {

System.out.println("Enter Feet: ");

int feet = input.nextInt();

System.out.println("Enter Inches: ");

int inches = input.nextInt();

double result = ((feet * 12) + inches);

double cm = result*2.54;

System.out.println((int)cm);

As you can see, I can get the full cm's but not the convert it to Meter and Cm.

Answer

You just need to convert the cm you obtained to m. You need to us the division operator `/`

and the remainder operator `%`

As an example:

```
38 = 9 x 4 + 2
38 / 9 = 4 and 38 % 9 = 2
```

For more information about these, take a look at this link: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

Therefore, adding this code to the end will do the work:

```
int meter = (int)cm / 100;
int remainingCM = (int)(cm-meter*100);
System.out.println((int)meter + "m and " + remainingCM + " cm!" );
```

Source (Stackoverflow)

Comments