Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 241451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:45:16+00:00 2026-05-11T20:45:16+00:00

From Java Malik textbook- determine if an number is divisible by 11.. Code Solution

  • 0

From Java Malik textbook- determine if an number is divisible by 11..

Code Solution provided:

import java.util.*;

public class Divby11
{
    static Scanner console = new Scanner(System.in);

    public static void main (String[] args)
    {
        int num, temp, sum;
        char sign;

        System.out.print("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;       //remove the last digit
        }
        while (num > 0);

        if (sum % 11 == 0)
            System.out.println(temp + " is divisible by 11");
        else
            System.out.println(temp + " is not divisible by 11");
    }

Why go through all the effort above and just say…

  if (sum % 11 == 0)
            System.out.println(temp + " is divisible by 11");
        else
            System.out.println(temp + " is not divisible by 11");

Can any of you experts see why the author would do it this way (long way)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:45:17+00:00Added an answer on May 11, 2026 at 8:45 pm

    This code example isn’t actually dividing by eleven. If you see, it’s alternating between adding and subtracting each digit, then checks at the very end if the result is divisible by 11.

    For example, look at the following number and how this algorithm works with it:

    Start with sum=0, sign='+', num=517
    First iteration: sum=7, sign='-', num=51
    Second iteration: sum=6, sign='+', num=5
    Final iteration: sum=11, sign='-', num=0
    

    The final result is divisible by eleven.

    EDIT: The algorithm does indeed look to be implementing the divisibility rule for 11 as dfa mentions in his answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 117k
  • Answers 117k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the DataGridViewComboBoxColumn, there is a property called DisplayStyle. Set… May 11, 2026 at 10:46 pm
  • Editorial Team
    Editorial Team added an answer I have FINALLY worked around this. I took a page… May 11, 2026 at 10:46 pm
  • Editorial Team
    Editorial Team added an answer You can use the SecureWebPages assembly and web.config entries from… May 11, 2026 at 10:46 pm

Related Questions

from my small knowlegde from java se i want to try a tutorial on
When launching a process from Java, both stderr and stdout can block on output
I am coming from Java and am currently working on a C# project. What
How can I write from Java to the Windows Event Log?

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.