# Thread: have problem with my program !

1. Member
Join Date
Dec 2012
Posts
8
Rep Power
0

## 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 .

Last edited by freedom12; 12-10-2012 at 05:57 PM.

2. ## Re: have problem with my program !

Where are you adding the digits of the number?

kind regards,

Jos

3. Member
Join Date
Dec 2012
Posts
8
Rep Power
0

## Re: have problem with my program !

Originally Posted by JosAH
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. ## Re: have problem with my program !

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

db

5. Member
Join Date
Dec 2012
Posts
8
Rep Power
0

## Re: have problem with my program !

Originally Posted by DarrylBurke
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. Member
Join Date
Dec 2012
Posts
8
Rep Power
0

## Re: have problem with my program !

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

7. ## Re: have problem with my program !

Originally Posted by freedom12
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

8. Member
Join Date
Dec 2012
Posts
8
Rep Power
0

## Re: have problem with my program !

Originally Posted by JosAH
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

PHP Code:
``` int number,sum;

number=in.nextInt();```
particularly in variable "number"

9. ## Re: have problem with my program !

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

kind regards,

Jos

10. Member
Join Date
Dec 2012
Posts
8
Rep Power
0

## Re: have problem with my program !

Originally Posted by JosAH
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. Senior Member
Join Date
Nov 2012
Posts
257
Rep Power
8

## 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. Member
Join Date
Dec 2012
Posts
8
Rep Power
0

## Re: have problem with my program !

Originally Posted by monkeyjr97
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. Member
Join Date
Dec 2012
Posts
8
Rep Power
0

## 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.

#### Posting Permissions

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