473,385 Members | 1,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Divisibility by 3 program

I have a question with a program. The assignment is "Write a program to verify the statement Numbers whose sum of digits is divisible by 3 represent numbers divisible by 3. Input a 5 digit integer from the keyboard. Find the sum of the digits, call it sum. Verify that either(a) both n and sum are divisible by 3 or (b) both are indivisible by 3.
Your output is:
Given number =
Sum of digits=
One of the following
a. Both number and sum are divisible by 3
b. Both number and sum are indivisible by 3
c. The famous statement is wrong

This is what I have so far..

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. class Divisibility    
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         int number;
  7.         boolean done = false;
  8.         Scanner input = new Scanner(System.in);
  9.         System.out.println("Please input a 5-digit number");
  10.         int number = input.nextInt();
  11.         {
  12.             String numberStr = Integer.toString(number); //to get the number of digits in "number"
  13.             int sum = 0;
  14.             for(int i = 0; i < numberStr.length(); i++)
  15.         {
  16.             int remainder = number % 10;
  17.             sum += remainder;
  18.             number /= 10;
  19.             System.out.println("Sum of the digits: "+sum);
  20. }
  21. if(number%3==0 && !done)
  22. {
  23. else
  24. {
  25. if(!done)System.out.println("Both n and sum are indivisible by 3")
  26. else
  27. if(!done)System.out.println("The famous statment is wrong 3")
  28. System.out.println("Both n and sum are divisible by 3")done = true;
  29. }
  30. System.out.println("Both n and sum are divisible by 3")done = true;
  31. }
  32. }
  33. }}
I keep getting errors when I compile. The latest one is else without if statement? Thanks for any help
Feb 10 '08 #1
7 14896
BigDaddyLH
1,216 Expert 1GB
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Feb 10 '08 #2
BigDaddyLH
1,216 Expert 1GB
The else without the if is right here:

Expand|Select|Wrap|Line Numbers
  1. if(number%3==0 && !done)
  2. {
  3. else
See how that else is the first statement in the if block?

Suggestion: your code would be more readable, and you would have an easier time getting the syntax right if you indented carefully. It's worth it.
Feb 10 '08 #3
The else without the if is right here:

Expand|Select|Wrap|Line Numbers
  1. if(number%3==0 && !done)
  2. {
  3. else
See how that else is the first statement in the if block?

Suggestion: your code would be more readable, and you would have an easier time getting the syntax right if you indented carefully. It's worth it.
I keep playing with it, but I can't figure out code that works. I'm stumped and out of idea. If you haven't noticed I'm brand new at Java.
Feb 11 '08 #4
<Code removed. Read our Posting Guidelines>

//sample output
Please input a 5-digit number
12345
Your Output is:
Given number: 12345
Sum of digits: 15
Both n and sum are divisible by 3
//
There you go!!
Feb 11 '08 #5
///CORRECTION
else if((number%3==0 && sum%3!=0)||(number%3!=0 && sum%3==0))
>>hope u see it

best regards,
tsokos.

It makes plenty of sense when I see it done, I'm just having trouble coming up with it myself. Thank you for the help.
Feb 11 '08 #6
BigDaddyLH
1,216 Expert 1GB
///CORRECTION
CODE REMOVED
tsokos.
tsokos,

While you might think you're doing someone a favor by writing their assignment for them, you're really not. Do you know the adage about teaching a man to fish versus giving a man a fish?
Feb 11 '08 #7
tsokos,

While you might think you're doing someone a favor by writing their assignment for them, you're really not. Do you know the adage about teaching a man to fish versus giving a man a fish?

Sorry sir...i'm new here too...i'll make sure i revise the rules.
Thanks for your advice,
Tsokos.
Feb 12 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: edgrsprj | last post by:
PROPOSED EARTHQUAKE FORECASTING COMPUTER PROGRAM DEVELOPMENT EFFORT Posted July 11, 2005 My main earthquake forecasting Web page is: http://www.freewebz.com/eq-forecasting/Data.html ...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
11
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
1
by: Eric Whittaker | last post by:
hi all, im trying to write my first c++ program. a success, but i can't get the window to stay open after user enters input. it just automatically closes. right now the end of my program looks...
7
by: brian.digipimp | last post by:
Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd...
9
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working...
7
by: ibtc209 | last post by:
I just started programming in C, and I need some help with this problem. Your program will read the information about one MiniPoker hand, namely the rank and suit of the hand’s first card, and...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.