# Getting the remaining cents

• 12-30-2009, 01:35 AM
MyOnlineChurva
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:

Code:

```import java.io.*; public class centsDollars {     public static void main (String[]args) {                 BufferedReader input = new BufferedReader (new InputStreamReader(System.in));                 String x="";                 try {             System.out.println("Enter amount in nickel: ");             x = input.readLine();             int nickelin = Integer.parseInt(x);                         System.out.println("Enter amount in penny: ");             x = input.readLine();             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:

Quote:

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?:
Quote:

The total amount is 1 Dollar and 37 Cents.
• 12-30-2009, 02:15 AM
pbrockway2
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)
• 12-30-2009, 02:35 AM
MyOnlineChurva

Sorry, sir. I don't get it :(
• 12-30-2009, 05:49 AM
pbrockway2
Quote:

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?

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");     } }```
• 01-01-2010, 02:42 AM
MyOnlineChurva
Hi sir. I did not know how to apply the % operator, sorry. It works fine now. Thank you very much for your help.
• 01-01-2010, 03:37 AM
pbrockway2
You're welcome.