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
- Program in C & C++ to print the binary equivalent 1 to nth number.
- Program in C++ to Convert Binary to Decimal number Using Function
- Program in C to convert decimal into octal number.
- Program in C++ to Convert Hexadecimal to Decimal number Using Function
- Program in C++ to Convert Decimal to Hexadecimal number Using Function
- Program in C to Convert Decimal to Binary number.
- Program in C to print the binary equivalent 1 to nth number.
- Program in C and C++ to Convert Decimal to Binary number Using recursive function
- Program in C and C++ to Convert Binary to Decimal number Using recursive function
- Program in C and C++ to Convert Decimal to Octal number Using recursive function
- Program in C and C++ to Convert Octal to Decimal number Using recursive function
- Program in C and C++ to Convert Decimal to Hexadecimal number Using recursive function
0 Comments