Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Java program to write a code to detect divisibility of any number by 11

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java program to write a code to detect divisibility of any number by 11

    Introduction to my problem:

    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.

    However, the method that I have to use to determine if the enter value is divisible by 11 (with no remainder) is by using a certain method demonstrated below:

    For example suppose that n=9343752, then t=2-5+7-3+4-3+9==11. 11 is
    divisible by 11 then it follows that 8784204 is divisible by 11.

    If n=843439 then t=9-3+4-3+4-8=3. 3 is not divisible by 11 then
    843439 is not divisible by 11.

    if the user enter's a non postive integer the program should continue asking for a positive value until
    the user does so.

    My code so far:

    My code below accepts only 10-digit integers and I have struggled for it to accept any postive integers. It will not run if enter a negative 10-digit integer.


    [FONT=Arial Black][SIZE=2]public class DivisionBy11{
    public static void main(String[] args) throws IOException{
    InputStreamReader in = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(in);
    System.out.println("Please enter a positive integer");
     
    String numbers = br.readLine();
    while( Keyboard.ReadInt() <0){[COLOR=Lime]  /*I'm try to check if the enter value is a positive integer and if it's not, to prompt the user to enter a positive integer. So I've been unsuccessful.*/[/COLOR]:(
     
        System.out.println("Please enter a positive integer");
     
    }
     
    String numbers1 = numbers.substring(9,10);
    String numbers2 = numbers.substring(8,9);
    String numbers3 = numbers.substring(7,8);
    String numbers4 = numbers.substring(6,7);
    String numbers5 = numbers.substring(5,6);
    String numbers6 = numbers.substring(4,5);
    String numbers7 = numbers.substring(3,4);
    String numbers8 = numbers.substring(2,3);
    String numbers9 = numbers.substring(1,2);
    String numbers10 = numbers.substring(0,1);
    int numberi1 = Integer.parseInt(numbers1);
    int numberi2 = Integer.parseInt(numbers2);
    int numberi3 = Integer.parseInt(numbers3);
    int numberi4 = Integer.parseInt(numbers4);
    int numberi5 = Integer.parseInt(numbers5);
    int numberi6 = Integer.parseInt(numbers6);
    int numberi7 = Integer.parseInt(numbers7);
    int numberi8 = Integer.parseInt(numbers8);
    int numberi9 = Integer.parseInt(numbers9);
    int numberi10 = Integer.parseInt(numbers10);
    System.out.println("The number was " + numbers);
    float t = 0;
    float n=0;
    int myvalue = 0;
    t = numberi1 - numberi2 + numberi3 - numberi4 + numberi5 - numberi6 + numberi7 - numberi8 + numberi9 -
            numberi10;
    System.out.println(t);
    n = t / 11;
     
    if(Math.round(n)==myvalue){ [COLOR=Lime]//I'm having difficulty in check the return value is an integer[/COLOR]:-ss
        System.out.println("Yes");
    }
        else
            System.out.println("No");
     
     
        }
     
    }[/SIZE][/FONT]

    Any help will be greatly appreciated.
    Last edited by Deep_4; November 8th, 2012 at 01:35 PM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: My Division Problem

    import java.util.Scanner;
     
     
    public class DivideBy11 {
    	public static void main(String[] args){
    		int inputNumber;
     
    		System.out.println("Enter a positive number: ");
    		while((inputNumber = new Scanner(System.in).nextInt()) < 0);
     
    		String tempNumber = Integer.toString(inputNumber);
    		int total = 0;
    		boolean add = true;
     
    		for(int i = tempNumber.length() - 1; i >= 0; i--){
    			if(add){
    				total += (tempNumber.charAt(i)-48);
    				add = false;
    			}else{
    				total -= (tempNumber.charAt(i)-48);
    				add = true;
    			}
    		}
     
    		if(total == 11){
    			System.out.println("Yes.");
    		}else{
    			System.out.println("No.");
    		}
    	}
    }

    Chris

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: My Division Problem

    Have you heard of the mod operator, as well as the parseInt method? Scanner also has a nextInt methd.
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a number: ");
    int num = input.nextInt();
    if (num % 11 == 0)
    {
         System.out.println(num + " is divisible by 11");
    }
    else
    {
         System.out.println(num + " has a remainder of " + (num%11) + " when divided by 11");
    }

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: My Division Problem

    i was guessing it was a challenege set, where they cannot use mod to determine it

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: My Division Problem

    calculate the remainder by hand (not completely sure how numerically stable this is, but in theory it works)
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a number: ");
    double num = input.nextInt();
    if (num - (11 * Math.floor(num/11)) == 0)
    {
         System.out.println(num + " is divisible by 11");
    }
    else
    {
         int remainder = (int)(num - 11 * Math.floor(num/11));
         System.out.println(num + " has a remainder of " + remainder + " when divided by 11");
    }
    Last edited by helloworld922; August 8th, 2009 at 11:52 AM.

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    Java'88 (August 16th, 2009)

  7. #6
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: My Division Problem

    Thank very very much for your help Freaky Chris and hellooworld922.

    Quote Originally Posted by Freaky Chris View Post
    i was guessing it was a challenege set, where they cannot use mod to determine it
    Yes, mod operater was not allowed, thats what made this challenge really difficult for me.

    The method below had to be use as that is what the challenge ask for.

    Method:
    E.g. n=9343752, then t=2-5+7-3+4-3+9==11. 11 is
    divisible by 11 then it follows that 8784204 is divisible by 11.

    Quote Originally Posted by helloworld922
    Have you heard .... parseInt method? ..Scanner also has a nextInt methd.
    I have not yet reach the parseInt and Scanner method in my Java for Dummies, 4th edition

    Freaky Chris would example to me how this part of your code works, as I did not understand fully what is happening here.


    Thank you.

    JAVA CODE
    ...
    if(add){
    total += (tempNumber.charAt(i)-48);
    add = false;
    }else{
    total -= (tempNumber.charAt(i)-48);
    add = true;
    }

    ...

  8. #7
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: My Division Problem

    subractign 48 from the character representation of anumber gives you the numerical value of it for example, '1'-48 = 1 if that makes sense

    Chris

  9. The Following User Says Thank You to Freaky Chris For This Useful Post:

    Java'88 (August 16th, 2009)

  10. #8
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: My Division Problem

    Quote Originally Posted by Freaky Chris View Post
    subractign 48 from the character representation of anumber gives you the numerical value of it for example, '1'-48 = 1 if that makes sense

    Chris
    Yes, that make sense for me, Thanks

  11. #9
    Junior Member
    Join Date
    Aug 2009
    Location
    San Fransisco
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My Division Problem

    Yeah I agree that mod should not be used, because that is what assignment was all about.

    Acumen
    Lucid forums