C LAB‎ > ‎(Sem2) Data Structures‎ > ‎

Qxx1. WAP to convert decimal to binary using recursion.

posted Apr 26, 2011, 8:59 AM by Neil Mathew   [ updated Apr 26, 2011, 9:07 AM ]
SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<stdio.h>
 
void Dec2Bin(int num)
{
    int z;
    
    if(num>=0)
    {
    // conditions to find the remainder 
    // then recall (if not at end) then print
         
    if(num<2 && num>=0)
    {
    z=num;
    }
    else
    {
    z=num%2;
    num/=2;
    Dec2Bin(num);
    }  
    printf("%d",z);
 
    }
}
 
 
int main()
{
    int num;
    scanf("%d",&num);
    
    printf("\n Binary: ");
    Dec2Bin(num);
    
}
 

OUTPUT:

9

 Binary: 1001



Comments