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 7770355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:23:23+00:00 2026-06-01T16:23:23+00:00

I need some explanation for this code. This is the example code given by

  • 0

I need some explanation for this code.
This is the example code given by others.

    for ( int i = 1; i <= 8; i++ )
    {
        if(check % 2 == 0)
            sum += i;
        else
            sum -= i;
        check /= 2;            <--- Need explanation for this line.
    }

But in the Pseudo code, there is no check /= 2; procedure.

Here is the full Pseudo code.

int binary = 0;
int sum;

while(binary<256)
sum = 0;
for(go through all 8 digits)
    if the i-th digit is 0
        sum += i
    if the i-th digit is 1
        sum -= i
end for

if sum == 0
    output

binary++
end while

So, what is the purpose for that line of code?

Since sum, binary, and check is initialize as 0.

I have written this code using the Pseudocode given above.
But seems like my code will duplicate the output and one more problem, the format.

I want the output be like this format:

Enter a number : 3

-1 -2 +3 = 0
1 +2 -3 = 0 

But my currently output is:

Enter a number : 3

-1 -2 3 = 0
1 2 -3 = 0

Here is my code:

CODE IS REMOVED!

Solved!

I’m too focus on the for-loop for the output part, hence miss the while-loop for the binary, because the pseudocode is for 256 possible solutions, hence, there will be same output for the front part, example:

1 - 2 - 3 + 4 = 0                             

1 - 2 - 3 + 4 + 5 - 6 - 7 + 8 = 0

Hence, the pseudocode may give an same output. So, since the solution is in 2 ^ n where n = 1, 2, 3, ... form, so change the

 while( binary < 256 )   --->     while ( binary < Math.pow(2, input))

should solve it.

The format and the duplicate of the answer are solved.

  • 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-06-01T16:23:24+00:00Added an answer on June 1, 2026 at 4:23 pm

    This is the way to go through all digits. The most right digit is retrieved by check % 2, and after checking it, you shift check one digit (bit) to the right by check /= 2 (equals to check = check / 2;)

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

Sidebar

Related Questions

I need some explanation about the following piece of code: It is used for
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
Need some help to solve this. I have a gridview and inside the gridview
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with
example: For logging, my code uses log4j . but other jars my code is
I'm looking at this example about sound generation on iOS because I need to
For example, I have some data source (probably, database) from where I need to
Need some help about with Memcache. I have created a class and want to
Need some guidance figuring out what went wrong. I've been using mysql, phpmyadmin for

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.