1. Member
Join Date
Dec 2009
Posts
10
Rep Power
0

Getting the remaining cents

Hi, I would like to know how to get the remaining cents from the program I've made..

Here's the program:

Java Code:
```import java.io.*;

public class centsDollars {

public static void main (String[]args) {

String x="";

try {
System.out.println("Enter amount in nickel: ");
int nickelin = Integer.parseInt(x);

System.out.println("Enter amount in penny: ");
int penniesin = Integer.parseInt(x);

double pennies = (nickelin*5)+(penniesin);
double nickel = (pennies/5);

System.out.println("You entered " +nickelin+ " nickels and " +penniesin+ " pennies.\n");
System.out.println("The total amount in penny is "+pennies+".");
System.out.println("The total amount in nickel is "+nickel+".\n");
double dollar = (pennies/100);
System.out.println("The total amount is " +dollar+" Dollar." );
}

catch (IOException e) {
System.out.println("Error!");
}
}

}```

Assuming I entered 3 nickels and 122 pennies.

This displays:

You entered 3 nickels and 122 pennies.

The total amount in penny is 137.0.

The total amount in nickel is 27.4.

The total amount is 1.37 Dollar.

How can I output the amount in dollars the same as this?:
The total amount is 1 Dollar and 37 Cents.

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,715
Rep Power
16
Forget using double. Everything here is countable so use int and reserve double for things that you would measure rather than count.

Find out about the integer division and the remainder (%) operator. Assignment, Arithmetic, and Unary Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

3. Member
Join Date
Dec 2009
Posts
10
Rep Power
0

Sorry, sir. I don't get it :(

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,715
Rep Power
16
I don't get it

Is there something specific in the tutorial section that you don't understand? Or can you not see how to apply the / and % operators to your problem?

Java Code:
```public class Cents {
public static void main(String[] args) {
int test = 12345;

System.out.println("Suppose I have " + test + " cents");
System.out.print("That would be " + (test / 100) + " dollars ");
System.out.print("and " + (test % 100) + " cents");
}
}```

5. Member
Join Date
Dec 2009
Posts
10
Rep Power
0
Hi sir. I did not know how to apply the % operator, sorry. It works fine now. Thank you very much for your help.

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,715
Rep Power
16

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•