Results 1 to 13 of 13
  1. #1
    freedom12 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default how to get the sum of separate numbers (problem)

    Hello, I have a problem that I don't know how to get the sum of separate numbers .


    the program I built

    the question is

    Write a Java application that reads from the user a positive integer less than 2147483648.
    Then separates the integer into its individual digits and prints a message indicating if the
    sum of its digits is odd or even.
    Example:
    If the user enters 35271, the message should be: The sum of the digits of 35271 is even.
    Explanation:
    3 + 5 + 2 + 7 + 1 = 18 which is even.
    PHP Code:
     public static void main(String[] args){
    
       Scanner in= new Scanner (System.in);
    
    
         int number,sum;
          
         number=in.nextInt();
         
       
         while (number >0){
             
           sum= number %10;  
              number= number /=10;
                
           System.out.print( sum);
           
           
         // if (number %2 == 0)   {
    
    // System.out.println("The sum of the digits of is even"); 
     
     }
          
     //else {
     //System.out.println("The sum of the digits is odd"); 
         
       
     }
             
          
             
    }
    As you see I'm always the same input number .



    So please help me to solve this problem .
    Last edited by freedom12; 12-10-2012 at 05:57 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: have problem with my program !

    Where are you adding the digits of the number?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    freedom12 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: have problem with my program !

    Quote Originally Posted by JosAH View Post
    Where are you adding the digits of the number?

    kind regards,

    Jos
    I didn't copy the whole program so , I forgot to but "import java.util.Scanner;"


    PHP Code:
    import java.util.Scanner;
    public class Q4_601110813 {
        
    
     public static void main(String[] args){
    
       Scanner in= new Scanner (System.in);
    
    
         int number,sum;
          
         number=in.nextInt();
         
       
         while (number >0){
             
           sum= number %10;  
              number= number /=10;
                
           System.out.print( sum);
           
           
         // if (number %2 == 0)   {
    
    // System.out.println("The sum of the digits of is even"); 
     
     }
          
     //else {
     //System.out.println("The sum of the digits is odd"); 
         
       
     }
             
          
             
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: have problem with my program !

    Please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    freedom12 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: have problem with my program !

    Quote Originally Posted by DarrylBurke View Post
    Please go through the Forum Rules, particularly the third paragraph.

    db
    I'm sorry , can you fix it to " how to get the sum of separate numbers (problem)"
    Last edited by freedom12; 12-10-2012 at 06:03 PM.

  6. #6
    freedom12 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: have problem with my program !

    please it's a simple problem + I want the solution as soon as possible .

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: have problem with my program !

    Quote Originally Posted by freedom12 View Post
    please it's a simple problem + I want the solution as soon as possible .
    Don't try to hurry us, it is of no use at all; and my question still stands (see reply #2)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    freedom12 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: have problem with my program !

    Quote Originally Posted by JosAH View Post
    Don't try to hurry us, it is of no use at all; and my question still stands (see reply #2)

    kind regards,

    Jos
    NP ^^ , Ok

    I add input in

    PHP Code:
     int number,sum;
           
         number=in.nextInt();
    particularly in variable "number"

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: have problem with my program !

    That part of the code only obtains an int from the user ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    freedom12 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: have problem with my program !

    Quote Originally Posted by JosAH View Post
    That part of the code only obtains an int from the user ...

    kind regards,

    Jos
    If so , what should I do . I tried to change int to long , put no change !.

    second , as I know it's similar if you take number from the user or initialize it ,so what did you mean ?

    ---------
    Last edited by freedom12; 12-10-2012 at 08:39 PM.

  11. #11
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: have problem with my program !

    you are asking for one int, so user might input 70237 for example, which would set that as the number variable.

    a quick way to do this, probly not the best but loop put it in a do while so it keeps asking the user for ints and create a running total for the ints so they keep on adding:

    then have the while statement saying if user inputs 0 it quits the method. so something like

    Java Code:
       public static void main(String[] args){
            Scanner in= new Scanner (System.in);
    
            int number,sum;
            int runningTot=0;
            do{
                System.out.println("enter number to add, type 0 to quit");
                number=in.nextInt();
                
                runningTot += number;
                System.out.println("current total is " + runningTot); //you will need to take this basic info and do what you wish to it
            }while(number !=0);
        }
    }
    BUT IF I WAS YOU ID CREATE SEPARATE METHODS FOR THIS, I WOULDN'T DO IT ALL FROM THE MAIN or call it from different classes, it makes it all easier to manage.

  12. #12
    freedom12 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: have problem with my program !

    Quote Originally Posted by monkeyjr97 View Post
    you are asking for one int, so user might input 70237 for example, which would set that as the number variable.

    a quick way to do this, probly not the best but loop put it in a do while so it keeps asking the user for ints and create a running total for the ints so they keep on adding:

    then have the while statement saying if user inputs 0 it quits the method. so something like

    Java Code:
       public static void main(String[] args){
            Scanner in= new Scanner (System.in);
    
            int number,sum;
            int runningTot=0;
            do{
                System.out.println("enter number to add, type 0 to quit");
                number=in.nextInt();
                
                runningTot += number;
                System.out.println("current total is " + runningTot); //you will need to take this basic info and do what you wish to it
            }while(number !=0);
        }
    }
    BUT IF I WAS YOU ID CREATE SEPARATE METHODS FOR THIS, I WOULDN'T DO IT ALL FROM THE MAIN or call it from different classes, it makes it all easier to manage.
    Thank you , it was helpful, but it wasn't what I asked for!

    I want a program that separate the number then add them , for example :

    if the input were 566 it means 5+6+6= 17 , then to determine if it's odd number or even !

    and I have the program , but I want to use while loop instead of all these digits


    PHP Code:
    import java.util.scanner;
    public class intnumbers {
    public static void main (string [] args) {
        int sum=0;
        int digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8,
                digit9, digit10;
        scanner input = new scanner(system.in);
        system.out.print("enter the posistve integer leass than 2147483648 ");
        int no = input.nextint();
        while (no <=   2147483648 ) {
         digit1=no%10; no/=10;  
         digit2=no%10; no/=10;
         digit3=no%10; no/=10;
         digit4=no%10; no/=10; 
         digit5=no%10; no/=10;  
         digit6=no%10; no/=10;  
         digit7=no%10; no/=10;  
         digit8=no%10; no/=10;
         digit9=no%10; no/=10;  
         digit10=no%10; no/=10;  
         total=total+digit1 + digit2 + digit3 + digit4 + digit5 + digit6 + digit7 + digit8 +
                digit9 + digit10; }
         switch (sum%2) {
             case 0 : System.out.println("the sum no is even");
             case 1 : System.out.println("the sum no is odd");
                 
         }
        }
        
    }
    Last edited by freedom12; 12-10-2012 at 09:38 PM.

  13. #13
    freedom12 is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: have problem with my program !

    Thank you very much for trying to help me . I've done the program , and I'll ask you if I face any problem .

    Best wishes.

Similar Threads

  1. Problem with program
    By karolek0901 in forum New To Java
    Replies: 3
    Last Post: 11-27-2012, 05:02 AM
  2. problem with this program
    By bunty83 in forum New To Java
    Replies: 7
    Last Post: 11-01-2011, 11:05 PM
  3. Small problem with problem with Java, C++ parse program.
    By dragstang86 in forum New To Java
    Replies: 4
    Last Post: 10-30-2011, 03:43 AM
  4. Problem in Program
    By Abbinormal in forum New To Java
    Replies: 9
    Last Post: 01-08-2010, 03:38 AM
  5. Gui program problem..
    By mingming2009 in forum Advanced Java
    Replies: 7
    Last Post: 04-03-2009, 05:15 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
  •