adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C and C++ to Convert Decimal number to Binary, Octal and Hexadecimal number Using recursive function


Program in C and C++ to convert Decimal to

  • Binary 
  • Octal 
  • Hexadecimal
            Using recursive function 


C Program
#include<stdio.h>
 void decToConvert(int dec, int base){
    int temp;
    if(dec == 0)
       return;
    decToConvert(dec / base, base);
    temp = (temp = dec % base ) >= 10? temp + 55 : temp + 48;
    printf("%c",temp);
 }
  int main(){
    int n;
    printf("Enter decimal number :- ");
    scanf("%d",&n);
    printf("Binary :- ");
    decToConvert(n,2);
    printf("\nOctal : ");
    decToConvert(n,8);
    printf("\nHexadecimal :- ");
    decToConvert(n,16);
    return 0;
  }
C++ Program
#include<iostream>
 using namespace std;
 void decToConvert(long int dec, int base){
    int temp;
    if(dec == 0)
       return;
    decToConvert(dec / base, base);
    temp = (temp = dec % base ) >= 10? temp + 55 : temp + 48;
    cout<<(char)temp;
 }
  int main(){
    long int n;
    cout<<"Enter decimal number :- ";
    cin>>n;
    cout<<"Binary :- ";
    decToConvert(n,2);
    cout<<"\nOctal : ";
    decToConvert(n,8);
    cout<<"\nHexadecimal :- ";
    decToConvert(n,16);
    return 0;
  }
Output :-











Related Programs



                        Post a Comment

                        0 Comments