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
Quote:
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.
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 .
Re: have problem with my program !
Where are you adding the digits of the number?
kind regards,
Jos
Re: have problem with my program !
Quote:
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;"
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");
}
}
Re: have problem with my program !
Please go through the Forum Rules, particularly the third paragraph.
db
Re: have problem with my program !
Quote:
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)"
Re: have problem with my program !
please it's a simple problem + I want the solution as soon as possible .
Re: have problem with my program !
Quote:
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
Re: have problem with my program !
Quote:
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
I add input in
Code:
int number,sum;
number=in.nextInt();
particularly in variable "number"
Re: have problem with my program !
That part of the code only obtains an int from the user ...
kind regards,
Jos
Re: have problem with my program !
Quote:
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 ?
---------
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
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.
Re: have problem with my program !
Quote:
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
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
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");
}
}
}
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.