Skip to content

Commit 3907cea

Browse files
committed
Octal to Decimal
1 parent ee2e055 commit 3907cea

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

InterviewPrograms/src/com/java/convertor/OctalToDecimal.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,59 @@
2020
* the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
2121
* It also requires a dot (decimal point) to
2222
* represent decimal fractions.
23+
*
24+
* Steps
25+
* ------
26+
* - get input (hexadecimal) from the user
27+
* - create a variable, lets say pow (default value is 0)
28+
* - create a variable, lets say decimal (default value is 0)
29+
* - get the last digit of the octal number (last_digit = num%10)
30+
* - multiply the last digit with power of 8 to pow variable
31+
* - add the above result with decimal variable
32+
* - increment the pow variable
33+
* - continue all the above steps for all the digits of octal
34+
*
35+
* say the octal is = 123
36+
* decimal = 1 * 8^2 + 2 * 8^1 + 3 * 8^0
37+
* decimal = 1 * 64 + 2 * 8 + 3 * 1
38+
* decimal = 64 + 16 + 3
39+
* decimal = 83
40+
*
41+
* say the octal is = 144
42+
* decimal = 1 * 8^2 + 4 * 8^1 + 4 * 8^0
43+
* decimal = 1 * 64 + 4 * 8 + 4 * 1
44+
* decimal = 64 + 32 + 4
45+
* decimal = 100
46+
*
2347
*/
48+
2449
public class OctalToDecimal {
2550
public static void main(String[] args) {
51+
// int octal = 123;
52+
// int octal = 144;
53+
int octal = 1000;
54+
System.out.println("The Octal Number is : "+octal);
55+
int pow = 0;
56+
int decimal = 0;
2657

58+
while(octal > 0){
59+
int unitDigit = octal % 10;
60+
decimal = (int) (decimal + (unitDigit*Math.pow(8, pow)));
61+
pow++;
62+
octal = octal / 10;
63+
}
64+
System.out.println("The Decimal Value is : "+decimal);
2765
}
2866
}
2967
/*
3068
OUTPUT
69+
70+
The Octal Number is : 123
71+
The Decimal Value is : 83
72+
73+
The Octal Number is : 144
74+
The Decimal Value is : 100
75+
76+
The Octal Number is : 1000
77+
The Decimal Value is : 512
3178
*/

InterviewPrograms/src/com/java/strings/CountVowels.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
* Words without vowels are
3030
* sky, spy, try, fly, lynch, myth, dry, why, sync, shy, ply,
31-
* by, cry, crypt, fry, gym, psych,
31+
* by, cry, crypt, fry, gym, psych, spy
3232
*/
3333

3434
public class CountVowels {

0 commit comments

Comments
 (0)