Results 1 to 13 of 13
Thread: have problem with my program !
- 12-10-2012, 05:06 PM #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.As you see I'm always the same input number .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"); } }
So please help me to solve this problem .Last edited by freedom12; 12-10-2012 at 05:57 PM.
- 12-10-2012, 05:10 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: have problem with my program !
Where are you adding the digits of the number?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-10-2012, 05:33 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Re: have problem with my program !
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"); } }
- 12-10-2012, 05:36 PM #4
Re: have problem with my program !
Please go through the Forum Rules, particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-10-2012, 05:58 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
- 12-10-2012, 07:20 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 .
- 12-10-2012, 07:26 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 12-10-2012, 08:09 PM #8
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
- 12-10-2012, 08:12 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: have problem with my program !
That part of the code only obtains an int from the user ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-10-2012, 08:30 PM #10
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Re: have problem with my program !
Last edited by freedom12; 12-10-2012 at 08:39 PM.
- 12-10-2012, 08:43 PM #11
Senior Member
- Join Date
- Nov 2012
- Posts
- 223
- Rep Power
- 1
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
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.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); } }
- 12-10-2012, 09:31 PM #12
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Re: have problem with my program !
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.
- 12-11-2012, 02:39 PM #13
Member
- Join Date
- Dec 2012
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
Problem with program
By karolek0901 in forum New To JavaReplies: 3Last Post: 11-27-2012, 05:02 AM -
problem with this program
By bunty83 in forum New To JavaReplies: 7Last Post: 11-01-2011, 11:05 PM -
Small problem with problem with Java, C++ parse program.
By dragstang86 in forum New To JavaReplies: 4Last Post: 10-30-2011, 03:43 AM -
Problem in Program
By Abbinormal in forum New To JavaReplies: 9Last Post: 01-08-2010, 03:38 AM -
Gui program problem..
By mingming2009 in forum Advanced JavaReplies: 7Last Post: 04-03-2009, 05:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks