229k views
3 votes
Write a program to input money in cents from user, example 12745 and display the one dollar bills and the cents. Submit source code.

1 Answer

6 votes

import java.util.*; //This is used to import the Scanner

class Main {

public static void main(String[] args) {

System.out.println("Hello world!");

Scanner ValCents = new Scanner (System.in);

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

int user_input = ValCents.nextInt();

int result = user_input/100; // This one will solve for the one dollar bills.

int remain = user_input % 100; // This one will get the remainder of cents.

System.out.println(result + " One Dollar Bills");

System.out.println(remain + " Cents");

}

}

Remember that you need to get the amount of dollar bills separately from the number of cents. This is because the int variable will always return the whole value of the number. The modulo is used to get the variable of the cents as the whole number is already taken by the "result".

User Ramiz Raja
by
4.8k points