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