Write a program to convert octal number to decimal number.
Example 1:
Input:635
Output:413
Example 2:
Input:764
Output:500
Approach
Java
public class OctalToDecimal {public static void main(String[] args) {int octal = 764;int decimal = octalToDecimal(octal);System.out.println("Decimal is " + decimal);}public static int octalToDecimal(int octal) {int decimal = 0;int i = 0;while (octal > 0) {int mod = octal % 10;decimal += mod * Math.pow(8, i);octal = octal / 10;i++;}return decimal;}}
C++
#include <bits/stdc++.h>using namespace std;//Function to convert from//octal to decimalint octalToDecimal(int octal){int decimal=0;int i=0;while(octal>0){int mod=octal%10;decimal=decimal+mod*pow(8,i);i++;octal=octal/10;}return decimal;}int main(){int octal=635;int decimal=octalToDecimal(octal);cout<<"Decimal is ";cout<<decimal<<"\n";return 0;}
No comments:
Post a Comment