New Posts  All Forums:Forum Nav:

Remainder Help!

post #1 of 8
Thread Starter 
Write a program which reads in any positive integer representing money in cents. The program will calculate and then display the most compact set of coins (i.e. the fewest coins). It is assumed that the denominations available are $1, 50¢, 20¢, 10¢ and 1¢.

For example, if the user keys in the number 10.78, the program will calculate and display the following:

No of one-dollar coin - 10
No of 50-cents coin - 1
No of 20-cents coin - 1
No of 10-cents coin - 0
No of 5-cents coin - 1
No of 1-cents coin - 3

Total number of coins = 16

Hi guys, can help with this? been trying it for a few day yet still cant solve it.
post #2 of 8
What language? Java, Python, C++, VB?
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
post #3 of 8
Thread Starter 
Quote:
Originally Posted by tom.slick View Post

What language? Java, Python, C++, VB?

Sorry,sorry. Its Java. Hehe
post #4 of 8
You can solve this with recursion. Something like:
Code:
     /**
         * Calculates how many coins of a certain value will fit into a certain total.
         *
         * @param total the total in cents (e.g. $10.78 = 1078)
         * @param coinValue the value of the coin in cents (e.g. $1 = 100)
         * @return the number of coins
         */
        public static int numberOfCoins(int total, int coinValue) {
                int remainder = total - coinValue;
                if (remainder < 0) {
                        return 0;
                } else {
                        return 1 + numberOfCoins(remainder, coinValue);
                }
        }

Note this only works for a single coin value and doesn't provide a way to save the remainder for the next coin value. But hopefully you can use this as a starting point. smile.gif
post #5 of 8
Thread Starter 
Quote:
Originally Posted by jhaze84 View Post

You can solve this with recursion. Something like:
Code:
     /**
         * Calculates how many coins of a certain value will fit into a certain total.
         *
         * @param total the total in cents (e.g. $10.78 = 1078)
         * @param coinValue the value of the coin in cents (e.g. $1 = 100)
         * @return the number of coins
         */
        public static int numberOfCoins(int total, int coinValue) {
                int remainder = total - coinValue;
                if (remainder < 0) {
                        return 0;
                } else {
                        return 1 + numberOfCoins(remainder, coinValue);
                }
        }

Note this only works for a single coin value and doesn't provide a way to save the remainder for the next coin value. But hopefully you can use this as a starting point. smile.gif

Alright, noted. But in the mean time im stuck in this Question.


1. Write a program using nested for loops to print the following sequence as output:

Output:
5
66
777
8888
99999

Code:
public static void main(String[] args) {
 
        int noOfRow = 5;

        for(int i = 1; i<=5;i++)
        {
            for(int row=1;row<=noOfRow;row++)
            {
                for(int column=1;column<=row;column++)
                {
                    System.out.print(i);
                }
                System.out.println();
            }   

        }
    }
}
It doenst print out as what the question want, helpppp
post #6 of 8
This may not look the best or be the most efficient, but it works.

Code:
import java.util.Scanner;

public class Coins
{
    public static void main(String[] args)
    {
        // get user input
        Scanner user_input = new Scanner(System.in);
        System.out.print("Amount :");
        double userInput = user_input.nextDouble();
        
        // initialize number of coins to 0
        int one_dollar_coins = 0;
        int fifty_cent_coins = 0;
        int twenty_cent_coins = 0;
        int ten_cent_coins = 0;
        int five_cent_coins = 0;
        int one_cent_coins = 0;
        int totalCoins = 0;

        // convert input to string, get the tenths and hundredths as integers
        String userInputAsString = String.valueOf(userInput);        
        int tenths = Integer.parseInt(userInputAsString.substring(userInputAsString.length()-2,userInputAsString.length()-1));      
        int hundredths = Integer.parseInt(userInputAsString.substring(userInputAsString.length()-1,userInputAsString.length()));      
        
        // use Math.floor() to get the number of one dollar coins 
        if (userInput > 1)
        {
            one_dollar_coins = (int)Math.floor(userInput);
        }
        
        // check to see if we need a fifty cent coin
        // we will only need one at most, then
        // subtract the value if needed
        if (tenths >= 5)
        {
            fifty_cent_coins = 1;
            tenths = tenths - 5;
        }
        
        // do the same for the twenty cent coins,
        // but there can be 2 so use a loop
        // and checked twice,  You could also 
        // just do an if/elif 
        for (int a=0;a<2;a++)
        {
            if (tenths >= 2)
            {
                twenty_cent_coins++;
                tenths = tenths - 2;
            }
        }
        
        // check for ten cent coin
        // again one at most
        if (tenths >= 1)
        {
            ten_cent_coins = 1;
            tenths = tenths - 1;
        }
        
        // now the hundredths digit
        // you can have only one five
        // cent piece
        if (hundredths >= 5)
        {
            five_cent_coins = 1;
            hundredths = hundredths - 5;
        }
        
        // for the one cent coins, what ever
        // is left after removing the 
        // five cent piece will be
        // your one cent pieces
        one_cent_coins = hundredths;
        
        // total the coins
        totalCoins = one_cent_coins + five_cent_coins + ten_cent_coins + twenty_cent_coins + fifty_cent_coins + one_dollar_coins;

        // print results
        System.out.println("No of one-dollar coin - " + one_dollar_coins);
        System.out.println("No of fifty-cent coin - " + fifty_cent_coins);
        System.out.println("No of twenty-cent coin - " + twenty_cent_coins);
        System.out.println("No of ten-cent coin - " + ten_cent_coins);
        System.out.println("No of five-cent coins - " + five_cent_coins);
        System.out.println("No of one-cent coins - " + one_cent_coins);
        System.out.println("");
        System.out.println("Total number of coins = " + totalCoins);
    }
}
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
post #7 of 8
Thread Starter 
Quote:
Originally Posted by tom.slick View Post

This may not look the best or be the most efficient, but it works.

Code:
import java.util.Scanner;

public class Coins
{
    public static void main(String[] args)
    {
        // get user input
        Scanner user_input = new Scanner(System.in);
        System.out.print("Amount :");
        double userInput = user_input.nextDouble();
        
        // initialize number of coins to 0
        int one_dollar_coins = 0;
        int fifty_cent_coins = 0;
        int twenty_cent_coins = 0;
        int ten_cent_coins = 0;
        int five_cent_coins = 0;
        int one_cent_coins = 0;
        int totalCoins = 0;

        // convert input to string, get the tenths and hundredths as integers
        String userInputAsString = String.valueOf(userInput);        
        int tenths = Integer.parseInt(userInputAsString.substring(userInputAsString.length()-2,userInputAsString.length()-1));      
        int hundredths = Integer.parseInt(userInputAsString.substring(userInputAsString.length()-1,userInputAsString.length()));      
        
        // use Math.floor() to get the number of one dollar coins 
        if (userInput > 1)
        {
            one_dollar_coins = (int)Math.floor(userInput);
        }
        
        // check to see if we need a fifty cent coin
        // we will only need one at most, then
        // subtract the value if needed
        if (tenths >= 5)
        {
            fifty_cent_coins = 1;
            tenths = tenths - 5;
        }
        
        // do the same for the twenty cent coins,
        // but there can be 2 so use a loop
        // and checked twice,  You could also 
        // just do an if/elif 
        for (int a=0;a<2;a++)
        {
            if (tenths >= 2)
            {
                twenty_cent_coins++;
                tenths = tenths - 2;
            }
        }
        
        // check for ten cent coin
        // again one at most
        if (tenths >= 1)
        {
            ten_cent_coins = 1;
            tenths = tenths - 1;
        }
        
        // now the hundredths digit
        // you can have only one five
        // cent piece
        if (hundredths >= 5)
        {
            five_cent_coins = 1;
            hundredths = hundredths - 5;
        }
        
        // for the one cent coins, what ever
        // is left after removing the 
        // five cent piece will be
        // your one cent pieces
        one_cent_coins = hundredths;
        
        // total the coins
        totalCoins = one_cent_coins + five_cent_coins + ten_cent_coins + twenty_cent_coins + fifty_cent_coins + one_dollar_coins;

        // print results
        System.out.println("No of one-dollar coin - " + one_dollar_coins);
        System.out.println("No of fifty-cent coin - " + fifty_cent_coins);
        System.out.println("No of twenty-cent coin - " + twenty_cent_coins);
        System.out.println("No of ten-cent coin - " + ten_cent_coins);
        System.out.println("No of five-cent coins - " + five_cent_coins);
        System.out.println("No of one-cent coins - " + one_cent_coins);
        System.out.println("");
        System.out.println("Total number of coins = " + totalCoins);
    }
}

Hi, Thank you for answering my question.

However, Is there any other method to do this line ?
Code:
 // convert input to string, get the tenths and hundredths as integers
        String userInputAsString = String.valueOf(userInput);        
        int tenths = Integer.parseInt(userInputAsString.substring(userInputAsString.length()-2,userInputAsString.length()-1));      
        int hundredths = Integer.parseInt(userInputAsString.substring(userInputAsString.length()-1,userInputAsString.length()));   
cos all I have learn is all the basic , if-else, method, array, repetition,
post #8 of 8
Quote:
Originally Posted by SevantynauG View Post

Hi, Thank you for answering my question.

However, Is there any other method to do this line ?
Code:
 // convert input to string, get the tenths and hundredths as integers
        String userInputAsString = String.valueOf(userInput);        
        int tenths = Integer.parseInt(userInputAsString.substring(userInputAsString.length()-2,userInputAsString.length()-1));      
        int hundredths = Integer.parseInt(userInputAsString.substring(userInputAsString.length()-1,userInputAsString.length()));   
cos all I have learn is all the basic , if-else, method, array, repetition,
Code:
        int userInputAsInt = (int)(userInput * 100) % 100;
        int tenths = (int)Math.floor(userInputAsInt/10);
        int hundredths = (int)userInputAsInt % 10;        
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming