Sum of 4 numbers...!?

• 02-14-2010, 02:08 PM
Sum of 4 numbers...!?
Hi everyone

I've a project to do,and i need some help.The project is "We have to add as input a double and than the input number must express as sum of four other numbers.It's analogy of monies.For example let's say we have €48.5 and we have to seperate this as sum of monies as 5€,3€,2€,1€ and cents".Hope that's clearly explained.
Regards
• 02-14-2010, 02:14 PM
Fubarable
Quote:

Hi everyone

I've a project to do,and i need some help.The project is "We have to add as input a double and than the input number must express as sum of four other numbers.It's analogy of monies.For example let's say we have €48.5 and we have to seperate this as sum of monies as 5€,3€,2€,1€ and cents".Hope that's clearly explained.
Regards

Very clearly. What's not explained however is your specific question. What have you done so far? Where exactly are you stuck?
• 02-14-2010, 02:54 PM
Let me to give you an example.
We suppose that we have a number between (11-50)€.so we have to express it using 5€,1€ and 1 cent.

example: 48.5 € = 5 € * 9 + 1 € *3 + 1 cent * 5
Im confused how to separate the input(double) number.
• 02-14-2010, 03:58 PM
I've already done but there is only one problem left.How to make cents as int numbers for example we have 0.7 cents to make as 7 cents without decimal point.

Take a look at my work.

Code:

```package tester; import java.text.DecimalFormat; import javax.swing.JOptionPane; public class Sum {         public static void main(String[] args)         {                 DecimalFormat formater = new DecimalFormat("0.0");                   String s = JOptionPane.showInputDialog("Write a number between 1 and 50 : ");                   double inputNum  = Double.parseDouble(s);                   if(inputNum<10)                   {                                          double cent = (inputNum%1);                           int oneEuro = (int)(inputNum%2);                           int twoEuro =(int) inputNum/2;                                                                       System.out.println("1 € * "+oneEuro);                           System.out.println("2 € * "+ twoEuro);                           System.out.println("cents "+formater.format(cent));                                            }                   else if(inputNum>10 && inputNum<50)                   {                           double cent  = inputNum%1;                                                    int oneEuro = (int) (inputNum%5);                           int fiveEuro = (int) inputNum/5;                                                      System.out.println("5 € * "+ fiveEuro);                           System.out.println("1 € * "+oneEuro);                           System.out.println("Cents: "+formater.format(cent));                          }                                     else                   {                           System.out.println("Please write a smaller number!");                   }         } }```
• 02-14-2010, 04:20 PM
Fubarable
What happens if you multiply cents by 100? If you want to truncate the results, then you could cast the result as an int:

Code:

`int centsDisplay = (int) (100 * cent);`
• 02-14-2010, 04:27 PM
Thanks man.We just need to multiply by 10.
Code:

` int centsDisplay = (int) (10 * cent);`
I appreciate your effort so much.
Thanks again.
• 02-14-2010, 04:31 PM
m00nchile
I do think you should multiply by 100, after all, 0.7€ is 70 cents, not 7.
• 02-14-2010, 04:36 PM
Fubarable
Quote:

` int centsDisplay = (int) (10 * cent);`