i have to code a program that ask for the user to enter a positive integer and then I have to find if the value that was enter is divisible by 11 (with no remainder) and display "yes" if it is divisible and "no" if it is not. <<My program so far does this <<<

if the user enter's a non postive integer the program should continue asking for a positive value until the user does so. <<<but im having difficulty to do this,please help<<<<<<


My code so far:

public static void main(String[] args) {
		// TODO Auto-generated method stub
		int num, temp, sum;
		char sign;
		System.out.println("Enter a positive integer: ");
        num = console.nextInt();
        System.out.println();

        temp = num;

        sum = 0;
        sign = '+';

        do
        {
            switch (sign)
            {
            case '+' :
                sum = sum + num % 10;
                sign = '-';
                break;

            case '-' :
                sum = sum - num % 10;
                sign = '+';
            }

            num = num / 10;       
        }
        while (num > 0);

        if (sum % 11 == 0)
            System.out.println(" Yes, it is divisible by 11");
        else
            System.out.println("No, it is not divisible by 11");
    }
}

thnk you guys

Recommended Answers

All 4 Replies

ill try and help u a step at a time cos i can see a few problems

first, you havnt declared the class name you should have something like:

class divisible{

make sure the class name and file name are the same or it will confuse you when compiling and running it

secondly if "int num, temp, sum" is supposed to be three differnet intergers they should appear like this:

int num
int temp
int sum

Re above.
Class names should begin with a capital letter.
Class name and file name MUST match for a public class.
int num, temp, sum; is perfectly good Java.
int num
int temp
int sum
is not valid. It needs a semicolon at the end of each line.

Here's a clue for your original question:
If the user enters a negative number you want to give a message and try again, and again, and again. "Try again" tells you it's some kind of loop. It's a kind of loop that you keep doing while some condition (eg number is negative) is true.

thank you guyz, i actually think ive done it ;)...seems to be working..let me know what u think!!

import java.util.Scanner;

public class Divby11 {

	/**
	 * @param args
	 */
	static Scanner console = new Scanner(System.in);
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int num, sum;
		int num1 = 0;
		int sum1;
		char sign;
		System.out.println("Enter a positive integer: ");
        num = console.nextInt();
        
        sum = 0;
        sum1=0;
        sign = '+';

        if (num>0)
        {
            switch (sign)
            {
            case '+' :
                sum = sum + num % 10;
                sum1 = sum1 + num1 % 10;
                sign = '-';
                break;

            case '-' :
                sum = sum - num % 10;
                sum1 = sum1 - num1%10;
                sign = '+';
            }

            num = num / 10;   
            num1 = num1 / 10;
        }
        else
        	do
        {
            	System.out.println("Enter a positive integer");
            	num1 = console.nextInt();
            	
        }
        	while (num1<0);
    
        if (sum % 11 == 0)
        {
        	System.out.println("Yes, it is divisible by 11");
        }
        else if (sum1 % 11 ==0)
        {
        	System.out.println("Yes, it is divisible by 11");
        }
        	else
            System.out.println("No, it is not divisible by 11");
        
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.