Skip to content

Commit a27637b

Browse files
sachincoolthe-cybersapien
authored andcommitted
Add Decimal_To_Octal (#109)
* Add Decimal_To_Octal * Rename Decimal_To_Octal to Decimal_To_Octal.kt * Rename Octal_To_Decimal to Octal_To_Decimal.kt
1 parent e375729 commit a27637b

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ Ravi Kant Garg (garg-ravi24)
44
Sumit Gupta(Invincisumit)
55
Shifali Gupta (Gupta-shifali)
66
Mohit Sharma (ms10398)
7+
Harshit Luthra (sachincool)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fun main(args: Array<String>) //Main function
2+
{
3+
val decimal = 18 //Let's Take a number for Example
4+
val octal = DecimalToOctal(decimal)
5+
println("$decimal in decimal = $octal in octal") //calling decimal and octal inside ""
6+
}
7+
8+
fun DecimalToOctal(decimal: Int): Int // Our main function which converts decimal to octal
9+
{
10+
var decimal = decimal
11+
var octalNumber = 0
12+
var i = 1
13+
14+
while (decimal != 0) {
15+
octalNumber += decimal % 8 * i
16+
decimal /= 8
17+
i *= 10
18+
}
19+
20+
return octalNumber
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fun main(args: Array<String>) //Main Function
2+
{
3+
val octal = 18
4+
val decimal = OctalToDecimal(octal)
5+
println("$octal in octal = $decimal in decimal") //This is the method to print variable inside Quotes
6+
}
7+
8+
fun OctalToDecimal(octal: Int): Int // Conversion Function With Return type Int
9+
{
10+
var octal = octal
11+
var decimalNumber = 0
12+
var i = 0
13+
14+
while (octal != 0) {
15+
decimalNumber += (octal % 10 * Math.pow(8,i).toInt()) //Easy Peasy right ? We need to Convert The Math.pow to Integer.
16+
++i
17+
octal /= 10
18+
}
19+
20+
return decimalNumber //No Semicolon Weeeeee!!
21+
}

0 commit comments

Comments
 (0)