-2

enter image description here

I have to write a recurcive function without for or while that prints all binary numbers from 0 to a given integer:

e.g. if the integer was 7 output is this:

(000)
(001)
(010)
(011)
(100)
(101)
(110)
(111)

Here's the code I have tried:

#include <stdio.h>

/* function declaration */
int print_binary_number(int decimalNo,int count,int arr[]);

int main ()
{
    int a;

    printf("\nPlease enter an integer:\n");
    scanf("%d",&a);

    print_binary_number(a,0,0);
    return 0;
}

int print_binary_number(int decimalNo,int count,int arr[])
{
    int binaryNo,remainder,factor = 1,c;

    if (decimalNo == 0) {
        return 0;
        printf("000");
    }

    if(decimalNo != 0){

        remainder = decimalNo % 2;
        arr[count]=remainder;
        binaryNo = (binaryNo + (remainder * factor));
        factor = factor * 10;

        print_binary_number(decimalNo - 1,count,arr);
        c=1+print_binary_number(decimalNo / 2,count++,arr);

    }
    printf("%d",arr[count]);
    return 0;
}
18
  • 1
    What have you tried so far? Why are you tagging C# and C? Choose a language. Commented May 14, 2017 at 0:36
  • i know both languages and i need help in c or c#, i wrote an program but it doesnt work :: Commented May 14, 2017 at 0:39
  • Post the code that doesn't work. Commented May 14, 2017 at 0:40
  • i edit the post Commented May 14, 2017 at 0:41
  • 1
    What exactly is your qustion? Doesn´t the code you´ve posted work as you expected? What results do you get? Commented May 14, 2017 at 1:27

2 Answers 2

-1

This example covers your following needs:

  • Recursion
  • Display binary numbers in range
  • You can specify how many bits to display

#include <stdio.h>

void print_binary_numbers(int from, int to, int bits);
void print_binary(int n, int bits);

int main (void){
    int n;
    printf("Please enter an integer: ");
    scanf("%d", &n);
    print_binary_numbers(0, n, 8);
    return 0;
}

/**
 * @param bits Controls the number of displayed bits.
 */
void print_binary(int n, int bits) {
    while (--bits >= 0){
        printf("%s", n & (1 << bits) ? "1" : "0");
    }
}

void print_binary_numbers(int from, int to, int bits) {
    print_binary(from, bits);
    printf("\n");
    if (from < to) {
        print_binary_numbers(from + 1, to, bits);
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

it must a recurcive function without while .. thank you for trying
Recursion is in the first example
Convert.ToString probably uses a loop under the covers - may not be allowed??
Well, internally it uses an extern method. referencesource.microsoft.com/#mscorlib/system/…
I has added detailed implementation (recursive / cyclic approach)
|
-1

By C /* compile with -O2 option */

#include <stdio.h>

void print_binary_number(int decimalNo, int width, char arr[]);

int main (void){
    int a;
    char arr[32] = {0};

    printf("\nPlease enter an integer:\n");
    scanf("%d", &a);
    int width = 0;
    for(unsigned c = a; c; c >>= 1)
        ++width;
    print_binary_number(a, width, arr);
    return 0;
}

void print_binary_number(int decimalNo, int width, char arr[]){
    for(int i = width-1; i>=0; --i)
        printf("%d", arr[i]);
    printf("\n");
    if(!decimalNo)
        return ;
    for(int i = 0, carry = 1, c; carry; ++i, carry = c){
        c = arr[i] & carry;
        arr[i] ^= carry;
    }
    print_binary_number(decimalNo-1, width, arr);
}

2 Comments

pos += sprintf(&tmp[pos], "%d", (n & ((int)1<<bits)) ? 1 : 0); what this do?
@majdnassar Why are you asking me?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.