Friday, 20 June 2014

C++ program to convert octal to decimal number

Here is a C++ program to convert octal to decimal. Here are the different methods to convert octal to decimal like using recursion,manipulators etc.
How to convert octal to decimal.


Method 1 : C++ program to convert octal to decimal
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    long decimal=0,rem,num,temp=1,z=0;
    cout<<"Enter the number in octal ";
    cin>>num;
    cout<<"Decimal equivalent = ";
    for(int i=0;num>0;i++)
    {
             rem=num%10;
             num/=10;
             decimal+=rem*pow(8,i);
    }
    cout<<decimal;
    return 0;
}


Method 2 : C++ program to convert octal to decimal using recursion
#include<iostream>
#include<math.h>
using namespace std;
int deci(int n,int i)
{
    if(n==0)
        return 0;
    return (n%10)*pow(8,i)+deci(n/10,i+1);
}
int main()
{
    int num;
    cout<<"Enter Octal number : ";
    cin>>num;
    cout<<"Decimal equivalent : "<<deci(num,0);
    return 0;
}



Method 3 : Another C++ program to convert octal to decimal
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
int main()
{
    int decimal=0,lt;
    char num[30];
    cout<<"Enter the number in Octal ";
    cin>>num;
    lt=strlen(num)-1;
    for(int i=lt;i>=0;i--)
        decimal+=(num[i]-48)*pow(8,lt-i);
    cout<<"Decimal equivalent "<<decimal;
    return 0;
}



Method 4 : C++ program to convert octal to decimal using function
#include<iostream>
#include<math.h>
#include<string.h>
using namespace std;
void oct(char *a,int lt)
{
    int decimal=0;
    for(int i=lt;i>=0;i--)
        decimal+=(a[i]-48)*pow(8,lt-i);
    cout<<"Decimal equivalent "<<decimal;
}
int main()
{
    int lt;
    char num[30];
    cout<<"Enter the number in octal ";
    cin>>num;
    lt=strlen(num)-1;
    oct(num,lt);
    return 0;
}



Method 5 : C++ program to convert octal to decimal using manipulators
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    cout<<"Decimal equivalent of octal number 65 is : "<<dec<<065;
    return 0;
}

No comments:

Post a Comment