luc049.c (998B)
1 /* A positive integer is entered through the keyboard, write a function to find the binary equivalent of this number: 2 (1) Without using recursion 3 (2) Using recursion 4 */ 5 /* Let Us C, Chap- 10 (Recursive), Qn No.: B(a) */ 6 /* This file is auto-generated by a bot. */ 7 /* This code is not compiled; it is for reference only. */ 8 9 10 #include <stdio.h> 11 #include <math.h> 12 #include <stdlib.h> 13 14 int binary_non_rec(int); 15 void binary_rec(int); 16 17 int main() 18 { 19 int num, bin; 20 21 printf("Enter a positive integer: "); 22 scanf("%d", &num); 23 24 bin = binary_non_rec(num); 25 printf("Binary (Non-Recursive): %d\n", bin); 26 27 printf("Binary (Recursive): "); 28 binary_rec(num); 29 printf("\n"); 30 31 return 0; 32 } 33 34 int binary_non_rec(int n) 35 { 36 int rem, i = 1, bin = 0; 37 while (n != 0) 38 { 39 rem = n % 2; 40 n = n / 2; 41 bin = bin + rem * i; 42 i = i * 10; 43 } 44 return bin; 45 } 46 47 void binary_rec(int n) 48 { 49 if (n > 1) 50 binary_rec(n / 2); 51 52 printf("%d", n % 2); 53 }