Results 1 to 8 of 8
Thread: Sum of 4 numbers...!?
 02142010, 02:08 PM #1Member
 Join Date
 Feb 2010
 Posts
 5
 Rep Power
 0
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

 02142010, 02:54 PM #3Member
 Join Date
 Feb 2010
 Posts
 5
 Rep Power
 0
Let me to give you an example.
We suppose that we have a number between (1150)€.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.
 02142010, 03:58 PM #4Member
 Join Date
 Feb 2010
 Posts
 5
 Rep Power
 0
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.
Java 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!"); } } }
Last edited by xmenus; 02152010 at 03:27 PM.

What happens if you multiply cents by 100? If you want to truncate the results, then you could cast the result as an int:
Java Code:int centsDisplay = (int) (100 * cent);
 02142010, 04:27 PM #6Member
 Join Date
 Feb 2010
 Posts
 5
 Rep Power
 0
Thanks man.We just need to multiply by 10.
Java Code:int centsDisplay = (int) (10 * cent);
Thanks again.
 02142010, 04:31 PM #7Senior Member
 Join Date
 Feb 2010
 Location
 Ljubljana, Slovenia
 Posts
 470
 Rep Power
 6
I do think you should multiply by 100, after all, 0.7€ is 70 cents, not 7.

Similar Threads

add all the numbers in a string
By gibson.nathan in forum New To JavaReplies: 29Last Post: 09302009, 06:14 PM 
Help with random numbers
By checkmylongboarding in forum New To JavaReplies: 2Last Post: 01122009, 06:47 AM 
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10142008, 11:46 PM 
Compare 5 numbers
By Snowboardmylife in forum New To JavaReplies: 5Last Post: 04152008, 07:04 PM 
Prime numbers
By gapper in forum New To JavaReplies: 3Last Post: 02072008, 11:09 AM
Bookmarks