C++ program to convert from Binary to Decimal, Octal to Decimal, Hexadecimal to Decimal














































C++ program to convert from Binary to Decimal, Octal to Decimal, Hexadecimal to Decimal



#include <iostream>
#include <bits/stdc++.h>
using namespace std;
//function to convert from binary to decimal
void binaryTodec(int n){
    int x=1,y,sum=0,temp=n;
    while(temp>0){
        y=temp%10;
        sum+=x*y;
        x*=2;
        temp/=10;
    }
    cout<<"Binary number entered :"<<n<<" Decimal number is "<<sum<<endl;
}
//function to convert from octal to decimal
void octalToDecimal(int k){
    int x=1,y,sum=0,temp=k;
    while(temp>0){
        y=temp%10;
        sum+=x*y;
        x*=8;
        temp/=10;
    }
     cout<<"octal number entered :"<<k<<" Decimal number is "<<sum<<endl;
}
//function to convert from hexadecimal to decimal
void hexadecimalToDecimal(string s){
    int sum=0,x=1,i;
    int n=s.size();
    for(i=n-1;i>=0;i--){
        if(s[i]>='0'&&s[i]<='9')
        {
            sum+=x*(s[i]-'0');
        }
        else if(s[i]>='A'&&s[i]<='F')
        {
            sum+=x*(s[i]-'A'+10);
        }
        x*=16;
    }
    cout<<"hexadecimal number entered :"<<s<<" Decimal number is "<<sum<<endl;
}
int main()
{
    int n,ch;
    cout<<"Enter 1 .to convert from binary to decimal"<<endl<<"2. to convert from octal to decimal "<<endl<<"3 . to convert from hexadecimal to decimal"<<endl;
    cin>>ch;
    switch (ch){
        case 1:{
        int n;
         cout<<"enter the  binary number to convert";
         cin>>n;
         binaryTodec(n);
        }
         break;
         case 2:{
         int k;
         cout<<"enter the octal number to convert";
         cin>>k;
         octalToDecimal(k);
         }
         break;
         case 3:{
            cout<<"enter the hexadecimal number to convert";
             string s;
             cin>>s;
             hexadecimalToDecimal(s);
             break;
         }
           default:
             cout<<"enter a valid choice";
    }
}
OUTPUT:
Enter 1 .to convert from binary to decimal
2. to convert from octal to decimal 
3 . to convert from hexadecimal to decimal
1
enter the  binary number to convert 10110
Binary number entered :10110 Decimal number is 22
OUTPUT:
Enter 1 .to convert from binary to decimal
2. to convert from octal to decimal 
3 . to convert from hexadecimal to decimal
2
enter the octal number to convert 55
octal number entered :55 Decimal number is 45
OUTPUT:
Enter 1 .to convert from binary to decimal
2. to convert from octal to decimal 
3 . to convert from hexadecimal to decimal
3
enter the hexadecimal number to convertAB2
hexadecimal number entered :AB2 Decimal number is 2738
OUTPUT:
Enter 1 .to convert from binary to decimal
2. to convert from octal to decimal 
3 . to convert from hexadecimal to decimal
4
enter a valid choice








More Articles of Shaik Aftab Ahmed:

Name Views Likes
C++ Program to Find the Frequency of Odd & Even Numbers in the given Matrix 394 1
C++ program to Sort a Linked List Using Merge Sort 358 1
C++ Program to Implement a Linked List representation of a Binary tree 353 1
C++ Program to Check for balanced parentheses in an expression 251 1
C++ Program to Perform Inorder, Preorder, Postorder traversals in a binary tree. 289 1
C++ program to print Indian flag 385 1
C++ program to Convert a multi level linked list to a singly linked list 269 1
C++ program to print right view of a Binary tree using queue 243 1
C++ Program to implement Huffman Coding Compression Algorithm 1660 1
C++ Program to Create a Height Balanced Binary Tree 274 1
C++ program to implement Prims algorithm 635 1
C++ Program for BFS Traversal 289 1
C++ Progam to Evaluate a Prefix Expression 460 1
C++ Program to Implement Queue using Linked List 254 1
C++ implementation of Stack using Linked list 303 1
C++ program to find the intersection point of two linked lists 339 1
C++ program to count the inversions in the given array 278 1
C++ program to perform D.N.F sort 325 1
C++ program to print all possible subsets of a String 286 1
C++ program to count the number of ones in a binary representation of a number 310 1
C++ program to print all possible subsets of a set 322 1
C++ program to find the largest word in a String 287 1
C++ Program to print a matrix in Spiral order 365 1
C++ program to convert from Binary to Decimal, Octal to Decimal, Hexadecimal to Decimal 313 1
C limits library 340 1
Program to add two Binary numbers 332 1

Comments