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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:28:56+00:00 2026-06-05T08:28:56+00:00

I have written a program to print out all the numbers, whose binary pattern

  • 0

I have written a program to print out all the numbers, whose binary pattern is symmetrical(for example:1001001 , 1010101) between 1 to 1993. I had made a mistake and I found it out. However, there is some detail I can not understand yet.

The correct code:

#include <stdio.h>
main() {
    int binary[11] = {0};
    int i, n, j, k;
    for(i = 1; i <= 1993; i++) {
        n = i; k = 0;
        while(n != 0) {
            binary[k++] = n%2 ; 
            n = n/2;
        }   
        for(j = 0; j < k; j++) {
            if(binary[j] != binary[k-j-1])
            break;
        }   
        if(j == k) {
            printf("%d ", i);
            for(j = 0; j < k; j++)
                printf("%d", binary[j]);
            printf("\n");
        }   
    }
}

The wrong code:

#include <stdio.h>
main() {
    int binary[11] = {0};
    int i, n, j, k = 0; /* I have found a mistake here, k=0 should inside the for loop */     
    for(i = 1; i <= 1993; i++) {
        n = i;
        while(n != 0) {
            binary[k++] = n%2 ; 
            n = n/2;
        }   
        for(j = 0; j < k; j++) {
            if(binary[j] != binary[k-j-1])
            break;
        }   
        if(j == k) {
            printf("%d ", i);
            for(j = 0; j < k; j++)
                printf("%d", binary[j]);
            printf("\n");
        }   
    }
}

I just can’t understand why the variable “i” can not increase to 6 when “k=0” outside the for loop, and the loop will repeat again and again.

  • 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-05T08:28:58+00:00Added an answer on June 5, 2026 at 8:28 am

    In the “wrong code”, the k variable will not reset to 0 between iterations. It will however increase with every iteration of the while loop. At some point it will break the upper bound of the binary[11] array, and then the “binary[k++] = n%2 ;” statement will start to overwrite other variables on the stack, including the variable “i”. In this situation, unpredictable things will happen.

    If you step through the “wrong code” I beleive you are able to verify that this happens when i=6.

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

Sidebar

Related Questions

I have written a program to find all the possible permutations of a given
I have written a multithreaded program which does some thinking and prints out some
I have written a program that uses the Java print API to print pages
I have written a program in VB6. When I compile it and send it
I have written a program that uses qhttp to get a webpage. This works
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have written my program in python. It is written across seven files and
Visual Studio C++ 2008 / GCC 4.4.2 I have written a program to run
I have written a java program that is actually works as a gui to
I have written some sample program and DLL to learn the concept of DLL

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.