Convert octal to decimal number

Here given code implementation process.

//C Program
//Convert octal to decimal number
#include <stdio.h>

void octal_to_decimal(int number)
{
	int result = 0, multiplier = 1;
	int remainder = 0;
	int flag = 0;
	printf(" Octal : %d ", number);
	if (number < 0)
	{
		//When number is negative number
		number = -number;
		flag = 1;
	}
	while (number != 0)
	{
		remainder = number % 10;
		result = (remainder * multiplier) + result;
		multiplier *= 8;
		number /= 10;
	}
	if (flag == 1)
	{
		result = -result;
	}
	printf(" Decimal : %d \n\n", result);
}
int main()
{
	//Test Cases
	octal_to_decimal(42);
	octal_to_decimal(18);
	octal_to_decimal(25);
	octal_to_decimal(-173);
	return 0;
}

Output

 Octal : 42  Decimal : 34

 Octal : 18  Decimal : 16

 Octal : 25  Decimal : 21

 Octal : -173  Decimal : -123
/*
  Java Program
  Convert octal to decimal number
*/
class MyNumber
{
	public void octal_to_decimal(int number)
	{
		int result = 0, multiplier = 1;
		int remainder = 0;
		int flag = 0;
		System.out.print(" Octal : " + number );
		if (number < 0)
		{
			//When number is negative number
			number = -number;
			flag = 1;
		}
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = number / 10;
		}
		if (flag == 1)
		{
			result = -result;
		}
		System.out.print(" Decimal : "+ result +" \n\n");
	}
	public static void main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Cases
		obj.octal_to_decimal(42);
		obj.octal_to_decimal(18);
		obj.octal_to_decimal(25);
		obj.octal_to_decimal(-173);
	}
}

Output

 Octal : 42 Decimal : 34

 Octal : 18 Decimal : 16

 Octal : 25 Decimal : 21

 Octal : -173 Decimal : -123
//Include header file
#include <iostream>
using namespace std;

/*
  C++ Program
  Convert octal to decimal number
*/

class MyNumber
{
	public: void octal_to_decimal(int number)
	{
		int result = 0, multiplier = 1;
		int remainder = 0;
		int flag = 0;
		cout << " Octal : " << number;
		if (number < 0)
		{
			//When number is negative number
			number = -number;
			flag = 1;
		}
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = number / 10;
		}
		if (flag == 1)
		{
			result = -result;
		}
		cout << " Decimal : " << result << " \n\n";
	}
};
int main()
{
	MyNumber obj = MyNumber();
	//Test Cases
	obj.octal_to_decimal(42);
	obj.octal_to_decimal(18);
	obj.octal_to_decimal(25);
	obj.octal_to_decimal(-173);
	return 0;
}

Output

 Octal : 42 Decimal : 34

 Octal : 18 Decimal : 16

 Octal : 25 Decimal : 21

 Octal : -173 Decimal : -123
//Include namespace system
using System;
/*
  C# Program
  Convert octal to decimal number
*/
class MyNumber
{
	public void octal_to_decimal(int number)
	{
		int result = 0, multiplier = 1;
		int remainder = 0;
		int flag = 0;
		Console.Write(" Octal : " + number);
		if (number < 0)
		{
			//When number is negative number
			number = -number;
			flag = 1;
		}
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = number / 10;
		}
		if (flag == 1)
		{
			result = -result;
		}
		Console.Write(" Decimal : " + result + " \n\n");
	}
	public static void Main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Cases
		obj.octal_to_decimal(42);
		obj.octal_to_decimal(18);
		obj.octal_to_decimal(25);
		obj.octal_to_decimal(-173);
	}
}

Output

 Octal : 42 Decimal : 34

 Octal : 18 Decimal : 16

 Octal : 25 Decimal : 21

 Octal : -173 Decimal : -123
<?php
/*
  Php Program
  Convert octal to decimal number
*/
class MyNumber
{
	public	function octal_to_decimal($number)
	{
		$result = 0;
		$multiplier = 1;
		$remainder = 0;
		$flag = 0;
		echo " Octal : ". $number;
		if ($number < 0)
		{
			//When number is negative number
			$number = -$number;
			$flag = 1;
		}
		while ($number != 0)
		{
			$remainder = $number % 10;
			$result = ($remainder * $multiplier) + $result;
			$multiplier *= 8;
			$number = intval($number / 10);
		}
		if ($flag == 1)
		{
			$result = -$result;
		}
		echo " Decimal : ". $result ." \n\n";
	}
}

function main()
{
	$obj = new MyNumber();
	//Test Cases
	$obj->octal_to_decimal(42);
	$obj->octal_to_decimal(18);
	$obj->octal_to_decimal(25);
	$obj->octal_to_decimal(-173);
}
main();

Output

 Octal : 42 Decimal : 34

 Octal : 18 Decimal : 16

 Octal : 25 Decimal : 21

 Octal : -173 Decimal : -123
/*
  Node Js Program
  Convert octal to decimal number
*/
class MyNumber
{
	octal_to_decimal(number)
	{
		var result = 0;
		var multiplier = 1;
		var remainder = 0;
		var flag = 0;
		process.stdout.write(" Octal : " + number);
		if (number < 0)
		{
			//When number is negative number
			number = -number;
			flag = 1;
		}
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = parseInt(number / 10);
		}
		if (flag == 1)
		{
			result = -result;
		}
		process.stdout.write(" Decimal : " + result + " \n\n");
	}
}

function main()
{
	var obj = new MyNumber();
	//Test Cases
	obj.octal_to_decimal(42);
	obj.octal_to_decimal(18);
	obj.octal_to_decimal(25);
	obj.octal_to_decimal(-173);
}
main();

Output

 Octal : 42 Decimal : 34

 Octal : 18 Decimal : 16

 Octal : 25 Decimal : 21

 Octal : -173 Decimal : -123
#   Python 3 Program
#   Convert octal to decimal number

class MyNumber :
	def octal_to_decimal(self, number) :
		result = 0
		multiplier = 1
		remainder = 0
		flag = 0
		print(" Octal : ", number, end = "")
		if (number < 0) :
			# When number is negative number
			number = -number
			flag = 1
		
		while (number != 0) :
			remainder = number % 10
			result = (remainder * multiplier) + result
			multiplier *= 8
			number = int(number / 10)
		
		if (flag == 1) :
			result = -result
		
		print(" Decimal : ", result ," \n")
	

def main() :
	obj = MyNumber()
	# Test Cases
	obj.octal_to_decimal(42)
	obj.octal_to_decimal(18)
	obj.octal_to_decimal(25)
	obj.octal_to_decimal(-173)

if __name__ == "__main__": main()

Output

 Octal :  42 Decimal :  34

 Octal :  18 Decimal :  16

 Octal :  25 Decimal :  21

 Octal :  -173 Decimal :  -123
#   Ruby Program
#   Convert octal to decimal number

class MyNumber

	def octal_to_decimal(number)
	
		result = 0
		multiplier = 1
		remainder = 0
		flag = 0
		print(" Octal : ", number)
		if (number < 0)
		
			# When number is negative number
			number = -number
			flag = 1
		end
		while (number != 0)
		
			remainder = number % 10
			result = (remainder * multiplier) + result
			multiplier *= 8
			number = number / 10
		end
		if (flag == 1)
		
			result = -result
		end
		print(" Decimal : ", result ," \n\n")
	end
end
def main()

	obj = MyNumber.new()
	# Test Cases
	obj.octal_to_decimal(42)
	obj.octal_to_decimal(18)
	obj.octal_to_decimal(25)
	obj.octal_to_decimal(-173)
end
main()

Output

 Octal : 42 Decimal : 34 

 Octal : 18 Decimal : 16 

 Octal : 25 Decimal : 21 

 Octal : -173 Decimal : -123 

/*
  Scala Program
  Convert octal to decimal number
*/
class MyNumber
{
	def octal_to_decimal(n : Int): Unit = {
        var number = n;
		var result: Int = 0;
		var multiplier: Int = 1;
		var remainder: Int = 0;
		var flag: Int = 0;
		print(" Octal : " + number);
		if (number < 0)
		{
			//When number is negative number
			number = -number;
			flag = 1;
		}
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = (number / 10).toInt;
		}
		if (flag == 1)
		{
			result = -result;
		}
		print(" Decimal : " + result + " \n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Cases
		obj.octal_to_decimal(42);
		obj.octal_to_decimal(18);
		obj.octal_to_decimal(25);
		obj.octal_to_decimal(-173);
	}
}

Output

 Octal : 42 Decimal : 34

 Octal : 18 Decimal : 16

 Octal : 25 Decimal : 21

 Octal : -173 Decimal : -123
/*
  Swift Program
  Convert octal to decimal number
*/
class MyNumber
{
	func octal_to_decimal(_ n:  Int)
	{
      	var number: Int = n;
		var result: Int = 0;
		var multiplier: Int = 1;
		var remainder: Int = 0;
		var flag: Int = 0;
		print(" Octal : ", number, terminator: "");
		if (number < 0)
		{
			//When number is negative number
			number = -number;
			flag = 1;
		}
		while (number != 0)
		{
			remainder = number % 10;
			result = (remainder * multiplier) + result;
			multiplier *= 8;
			number = number / 10;
		}
		if (flag == 1)
		{
			result = -result;
		}
		print(" Decimal : ", result ," \n\n", terminator: "");
	}
}
func main()
{
	let obj: MyNumber = MyNumber();
	//Test Cases
	obj.octal_to_decimal(42);
	obj.octal_to_decimal(18);
	obj.octal_to_decimal(25);
	obj.octal_to_decimal(-173);
}
main();

Output

 Octal :  42 Decimal :  34

 Octal :  18 Decimal :  16

 Octal :  25 Decimal :  21

 Octal :  -173 Decimal :  -123


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved