CPP program convert octal to decimal

 In this program, you will learn about the conversion of octal to decimal number in the cpp programming language.

In order to understand the following code must have the knowledge of the following topics:

cpp while loop

cpp input method

cpp output method


Code:

#include <iostream>

#include <cmath>

using namespace std;

int main()

{

   int octalNumber,originalnumber;

   cout << "Enter an octal number "<<endl;

   cin >> octalNumber;


  originalnumber=octalNumber;


    int decimalNumber = 0, i = 0, remainder;


    while (octalNumber != 0) 

    {

        remainder = octalNumber % 10;

        octalNumber /= 10;

        decimalNumber += remainder * pow(8, i);

        i++;

    }


     cout << originalnumber << " in octal = " << decimalNumber << " in decimal"<<endl;

   return 0;

}

Output:

Enter an octal number

12345

12345 in octal = 5349 in decimal

In the above code, while loop is used to convert the number, separate the number by modulus 10, divide with 10 again to decrease the number and multiple by the power of 8 with unit place by using a power function.at the end of the loop,add in the decimal number. In this way, loop iterate in order to convert.

If you have any problem in the understanding of the above program write a query in below comment section.

Comments

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

Flutter layout