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..
[CODE=Java]import java.util.*;
class Divisibility
{
public static void main(String args[])
{
int number;
boolean done = false;
Scanner input = new Scanner(System. in);
System.out.prin tln("Please input a 5-digit number");
int number = input.nextInt() ;
{
String numberStr = Integer.toStrin g(number); //to get the number of digits in "number"
int sum = 0;
for(int i = 0; i < numberStr.lengt h(); i++)
{
int remainder = number % 10;
sum += remainder;
number /= 10;
System.out.prin tln("Sum of the digits: "+sum);
}
if(number%3==0 && !done)
{
else
{
if(!done)System .out.println("B oth n and sum are indivisible by 3")
else
if(!done)System .out.println("T he famous statment is wrong 3")
System.out.prin tln("Both n and sum are divisible by 3")done = true;
}
System.out.prin tln("Both n and sum are divisible by 3")done = true;
}
}
}}[/CODE]
I keep getting errors when I compile. The latest one is else without if statement? Thanks for any help
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..
[CODE=Java]import java.util.*;
class Divisibility
{
public static void main(String args[])
{
int number;
boolean done = false;
Scanner input = new Scanner(System. in);
System.out.prin tln("Please input a 5-digit number");
int number = input.nextInt() ;
{
String numberStr = Integer.toStrin g(number); //to get the number of digits in "number"
int sum = 0;
for(int i = 0; i < numberStr.lengt h(); i++)
{
int remainder = number % 10;
sum += remainder;
number /= 10;
System.out.prin tln("Sum of the digits: "+sum);
}
if(number%3==0 && !done)
{
else
{
if(!done)System .out.println("B oth n and sum are indivisible by 3")
else
if(!done)System .out.println("T he famous statment is wrong 3")
System.out.prin tln("Both n and sum are divisible by 3")done = true;
}
System.out.prin tln("Both n and sum are divisible by 3")done = true;
}
}
}}[/CODE]
I keep getting errors when I compile. The latest one is else without if statement? Thanks for any help
Comment