Results 1 to 8 of 8
  1. #1
    xmenus is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by xmenus View Post
    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?

  3. #3
    xmenus is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    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.

  4. #4
    xmenus is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    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; 02-15-2010 at 02:27 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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);

  6. #6
    xmenus is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks man.We just need to multiply by 10.
    Java Code:
     int centsDisplay = (int) (10 * cent);
    I appreciate your effort so much.
    Thanks again.

  7. #7
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I do think you should multiply by 100, after all, 0.7 is 70 cents, not 7.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by xmenus View Post
    Thanks man.We just need to multiply by 10.
    Java Code:
     int centsDisplay = (int) (10 * cent);
    I appreciate your effort so much.
    Thanks again.
    You're welcome, but please remind me not to do any financial transactions with you. ;)

Similar Threads

  1. add all the numbers in a string
    By gibson.nathan in forum New To Java
    Replies: 29
    Last Post: 09-30-2009, 06:14 PM
  2. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 05:47 AM
  3. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  4. Compare 5 numbers
    By Snowboardmylife in forum New To Java
    Replies: 5
    Last Post: 04-15-2008, 07:04 PM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •