Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Finding change in cents with int values

LordpunyTheGreatLordpunyTheGreat OhioMember Posts: 2

//
* Write a program that reads how much change is
due (in cents) and writes out that amount as dollars, quarters, dimes, nickels,
and pennies. Use integers for all variables and all arithmetic. Run 3 test cases,
including the one shown in the exercise. change this license header, choose
License Headers in Project Properties.

*/
package findingchange;

import java.util.Scanner ;

/**
*
* @author Peter Guthrie
*/
public class Findingchange {

/**

 */
public static void main(String[] args) {
    Scanner scan = new Scanner (System.in);
    int dollars = 100 ;// 100 goes into cents but how do I find quarters?
    int quarters = 25;
    int dimes = 10;
    int nickels = 5;

    System.out.println("Your change:" + cents/dollars //then What?);
}

}
System.out.println("Please enter cents value");

Comments

  • LordpunyTheGreatLordpunyTheGreat OhioMember Posts: 2

    I figured it out

    package findingchange;

    import java.util.Scanner ;

    /**
    *
    * @author Peter Guthrie
    */
    public class Findingchange {

    /**
     * @param args     
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        System.out.println("Please enter cents value:");
          // assingment of values      
        int cents = scan.nextInt(); 
        int dollars = 100 ;
        int quarters = 25;
        int dimes = 10 ;
        int nickels = 5;
        int pennies = 1;
              //math 
        int D = cents/ dollars ; //find first value 
        int DQ = cents - (D * 100); //total-(number of dollars * value of each dollar)= amounts less the a dollar  
        int Q =DQ/quarters;// repeat 
        int Qd = cents - (D*100)-(Q*25);
        int d = Qd/dimes;
        int dN = cents - (D*100)-(Q*25)-(d*10);
        int N = dN/nickels;
        int NP = cents - (D*100)-(Q*25)-(d*10)-(N*5);
        int P = NP/pennies;
        // output 
        System.out.print("Your change is: "+ D +" dollars, ");
        System.out.print( Q +" quarters, ");
        System.out.print( d +" dimes, ");
        System.out.print( N +" nickels, ");
        System.out.print("and " + P + " cents " );
    
    }    
    

    }

Sign In or Register to comment.