Thursday, June 12, 2014

Binary Equivalent of a Number using Recursion - C Program

Problem Question


A positive integer is entered through the keyboard, write a function to find binary equivalent of this number using recursion.

Explanation of Problem


We need to design a recursive function that could find the binary equivalent of a positive integer. It is a simple program. All our function needs to do is print the (number modulus 2) at each iteration, while we keep feeding the next call with number/2.

Code


#include <stdio.h>
/**@Title: BinaryEquivalent Recursive.c*
*@Language: ANSI C*
*@Compiler: GNU GCC*
*@IDE: Code::Block 13.12*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 12-06-2014*
*/
void binaryEquiRec(long a)
{
  if (a)
  {
    int num = a % 2;
    binaryEquiRec(a / 2);
    printf("%d", num);
  }
}

int main()
{
  long number;
  printf("\n\nEnter a number: ");
  scanf("%ld", &number);
  printf("\nBinary equivalent of %ld is: ", number);
  binaryEquiRec(number);
  printf("\n\n");
  system("pause");
  return 0;
}


Explanation of Code


#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H

int main() -> The entry point of the program where the execution starts. This function has to be named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.

scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.

if (a) -> Since in each recursive call, we divide the number fed to the function by 2 (binaryEquiRec(a / 2);), this if condition guarantees that the function would return in case 'a' becomes zero, which would mean 'a / 2 = 0', which is rue iff 'a' is zero. I keep dividing the number by 2 in each call because since we use the statement, int num = a % 2; to set 'num' as 'a % 2', it means we should reject the current 2's multiple from the number to keep on going with our calculation (the current multiple has already contributed).

printf("%d", num); -> This statement is used to print the binary equivalent digit by digit. I have called this after the recursive call to get the number in correct order, else I would get the reverse of the binary equivalent. So what happens is that, as soon as the if condition fails, the last function call returns, that means the MSD of the binary equivalent is returned, which must be published as the leading bit.

system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Output(s)




Download Source Code


No comments:

Post a Comment

Need help?