473,574 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Divisibility by 3 program

5 New Member
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 14910
BigDaddyLH
1,216 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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
jeremyhilg08
5 New Member
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
kedmotsoko
8 New Member
<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
jeremyhilg08
5 New Member
///CORRECTION
else if((number%3==0 && sum%3!=0)||(num ber%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 Recognized Expert Top Contributor
///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
kedmotsoko
8 New Member
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
3582
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 Newsgroup Readers: If you circulate copies of this report to groups of computer programmers at different universities etc. around the world then they might...
0
6103
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 file as folows. I need help to resolve them ASAP: cl /c /nologo /MDd /W3 /Od /GR /GM /Zi /GX /D "_DEBUG" /D " WIN32" /D "_W INDOWS" /D "_WINDLL"...
11
2587
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 >> Fork(MyProgram3, SomeFile); If not would this be something of interest to others? Thanks in advance,
1
3252
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 like this: return 0; }
7
7371
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 integer is prime if it is not divisible by an odd integer less than or equal to the square root of the number.) I was given this assignment in my...
9
4523
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 on linux platform and my target is ARM processor. But i guess it should not matter. Actually i need to know both RAM & ROM usage.
7
13260
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 the rank and suit of its second card. Note that the two cards in a hand may be entered in any order; it’s not necessarily the case that the highest...
2
19331
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 we set about doing that. Every program starts with a specification, this may be a several hundred page document from your latest client or one...
0
8063
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7822
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5302
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3742
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2249
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1347
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1062
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.