Convert number to digits array

Here given code implementation process.

// C Program 
// Convert number to digits array
#include <stdio.h>

// Converting a given number to digits array
void digitArray(int num)
{
	int number = num;
	if (number < 0)
	{
		// When number is negative
		// Convert into positive number
		number = -number;
		num = -num;
	}
	int size = 0;
	int i = 0;
	// Get the size
	while (number != 0)
	{
		number /= 10;
		size++;
	}
	// This are used to store result value
	int digits[size];
	number = num;
	i = size - 1;
	// Get number digit
	while (number != 0)
	{
		digits[i] = number % 10;
		number /= 10;
		i--;
	}
	i = 0;
	// Display calculated result
	while (i < size)
	{
		printf("  %d", digits[i]);
		++i;
	}
	printf("\n");
}
int main(int argc, char const *argv[])
{
  	// Test case
	digitArray(987650);
	digitArray(12345);
	return 0;
}

Output

  9  8  7  6  5  0
  1  2  3  4  5
/*
    Java Program
    Convert number to digits array
*/
public class  NumberToDigits
{
    // Converting a given number to digits array
    public void digitArray(int num)
    {
        int number = num;

        if(number < 0)
        {
            // When number is negative
            // Convert into positive number
            number = - number;
        }

        // Convert number to string
        String temp = ""+number; 
		
        // Get number of digits
        int size = temp.length();
		
        // This are used to store result value
        int []digits = new int[size];

        // Loop controlling variable
        int i = 0;

        // Execute Loop through of digit size
        for (i = 0; i < size ; ++i ) 
        {
          	// 48 is ASCII of zero
            digits[i] = temp.charAt(i) - 48;
        }

        // Display calculated result
        for (i = 0; i < size ; ++i ) 
        {
            System.out.print("  "+digits[i]); 
        }
        System.out.print("\n"); 
    }
  
    public static void main(String []args) 
    {
        NumberToDigits task = new NumberToDigits();
        // Test case
        task.digitArray(987650);
        task.digitArray(12345);
    }
}

Output

  9  8  7  6  5  0
  1  2  3  4  5
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ Program
    Convert number to digits array
*/
class NumberToDigits
{
    public:
        // Converting a given number to digits array
        void digitArray(int num)
        {
            int number = num;
            if (number < 0)
            {
                // When number is negative
                // Convert into positive number
                number = -number;
            }
            // Convert number to string
            string temp = to_string(number);
            // Get number of digits
            int size = temp.size();
            // This are used to store result value
            int digits[size];
            // Loop controlling variable
            int i = 0;
            // Execute Loop through of digit size
            for (i = 0; i < size; ++i)
            {
                // 48 is ASCII of zero
                digits[i] = temp[i] - 48;
            }
            // Display calculated result
            for (i = 0; i < size; ++i)
            {
                cout << "  " << digits[i];
            }
            cout << "\n";
        }
};
int main()
{
    NumberToDigits task = NumberToDigits();
    // Test case
    task.digitArray(987650);
    task.digitArray(12345);
    return 0;
}
// In case your c++ compiler not work to to_string
// Then so compile program by using this command
// g++ -std=c++11 source.cpp [Compile]
// Note source.cpp is code file
// ./a.out [Run]

Output

  9  8  7  6  5  0
  1  2  3  4  5
// Include namespace system
using System;
/*
    C# Program
    Convert number to digits array
*/
public class NumberToDigits
{
	// Converting a given number to digits array
	public void digitArray(int num)
	{
		int number = num;
		if (number < 0)
		{
			// When number is negative
			// Convert into positive number
			number = -number;
		}
		// Convert number to string
		String temp = "" + number;
		// Get number of digits
		int size = temp.Length;
		// This are used to store result value
		int[] digits = new int[size];
		// Loop controlling variable
		int i = 0;
		// Execute Loop through of digit size
		for (i = 0; i < size; ++i)
		{
			// 48 is ASCII of zero
			digits[i] = temp[i] - 48;
		}
		// Display calculated result
		for (i = 0; i < size; ++i)
		{
			Console.Write("  " + digits[i]);
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		NumberToDigits task = new NumberToDigits();
		// Test case
		task.digitArray(987650);
		task.digitArray(12345);
	}
}

Output

  9  8  7  6  5  0
  1  2  3  4  5
<?php
/*
    Php Program
    Convert number to digits array
*/
class NumberToDigits
{
	// Converting a given number to digits array
	public	function digitArray($num)
	{
		$number = $num;
		if ($number < 0)
		{
			// When number is negative
			// Convert into positive number
			$number = -$number;
		}
		// Convert number to string
		$temp = "".$number;
		// Get number of digits
		$size = strlen($temp);
		// This are used to store result value
		$digits = array_fill(0, $size, 0);
		// Loop controlling variable
		$i = 0;
		// Execute Loop through of digit size
		for ($i = 0; $i < $size; ++$i)
		{
			// 48 is ASCII of zero
			$digits[$i] = ord($temp[$i]) - 48;
		}
		// Display calculated result
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $digits[$i];
		}
		echo "\n";
	}
}

function main()
{
	$task = new NumberToDigits();
	// Test case
	$task->digitArray(987650);
	$task->digitArray(12345);
}
main();

Output

  9  8  7  6  5  0
  1  2  3  4  5
/*
    Node Js Program
    Convert number to digits array
*/
class NumberToDigits
{
	// Converting a given number to digits array
	digitArray(num)
	{
		var number = num;
		if (number < 0)
		{
			// When number is negative
			// Convert into positive number
			number = -number;
		}
		// Convert number to string
		var temp = "" + number;
		// Get number of digits
		var size = temp.length;
		// This are used to store result value
		var digits = Array(size).fill(0);
		// Loop controlling variable
		var i = 0;
		// Execute Loop through of digit size
		for (i = 0; i < size; ++i)
		{
			// 48 is ASCII of zero
			digits[i] = (temp[i]).charCodeAt(0) - 48;
		}
		// Display calculated result
		for (i = 0; i < size; ++i)
		{
			process.stdout.write("  " + digits[i]);
		}
		process.stdout.write("\n");
	}
}

function main()
{
	var task = new NumberToDigits();
	// Test case
	task.digitArray(987650);
	task.digitArray(12345);
}
main();

Output

  9  8  7  6  5  0
  1  2  3  4  5
#  Python 3 Program
#  Convert number to digits array

class NumberToDigits :
	#  Converting a given number to digits array
	def digitArray(self, num) :
		number = num
		if (number < 0) :
			#  When number is negative
			#  Convert into positive number
			number = -number
		
		#  Convert number to string
		temp = str(number)
		#  Get number of digits
		size = len(temp)
		#  This are used to store result value
		digits = [0] * (size)
		#  Loop controlling variable
		i = 0
		#  Execute Loop through of digit size
		while (i < size) :
			#  48 is ASCII of zero
			digits[i] = ord(temp[i]) - 48
			i += 1
		
		i = 0
		#  Display calculated result
		while (i < size) :
			print("  ", digits[i], end = "")
			i += 1
		
		print(end = "\n")
	

def main() :
	task = NumberToDigits()
	#  Test case
	task.digitArray(987650)
	task.digitArray(12345)

if __name__ == "__main__": main()

Output

   9   8   7   6   5   0
   1   2   3   4   5
#  Ruby Program
#  Convert number to digits array

class NumberToDigits 
	#  Converting a given number to digits array
	def digitArray(num) 
		number = num
		if (number < 0) 
			#  When number is negative
			#  Convert into positive number
			number = -number
		end

		#  Convert number to string
		temp = number.to_s
		#  Get number of digits
		size = temp.length()
		#  This are used to store result value
		digits = Array.new(size) {0}
		#  Loop controlling variable
		i = 0
		#  Execute Loop through of digit size
		while (i < size) 
			#  48 is ASCII of zero
			digits[i] = (temp[i]).ord - 48
			i += 1
		end

		i = 0
		#  Display calculated result
		while (i < size) 
			print("  ", digits[i])
			i += 1
		end

		print("\n")
	end

end

def main() 
	task = NumberToDigits.new()
	#  Test case
	task.digitArray(987650)
	task.digitArray(12345)
end

main()

Output

  9  8  7  6  5  0
  1  2  3  4  5
/*
    Scala Program
    Convert number to digits array
*/
class NumberToDigits
{
	// Converting a given number to digits array
	def digitArray(num: Int): Unit = {
		var number: Int = num;
		if (number < 0)
		{
			// When number is negative
			// Convert into positive number
			number = -number;
		}
		// Convert number to string
		var temp: String = "" + number;
		// Get number of digits
		var size: Int = temp.length();
		// This are used to store result value
		var digits: Array[Int] = Array.fill[Int](size)(0);
		// Loop controlling variable
		var i: Int = 0;
		// Execute Loop through of digit size
		while (i < size)
		{
			// 48 is ASCII of zero
			digits(i) = temp(i) - 48;
			i += 1;
		}
		i = 0;
		// Display calculated result
		while (i < size)
		{
			print("  " + digits(i));
			i += 1;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: NumberToDigits = new NumberToDigits();
		// Test case
		task.digitArray(987650);
		task.digitArray(12345);
	}
}

Output

  9  8  7  6  5  0
  1  2  3  4  5
/*
    Swift 4 Program
    Convert number to digits array
*/
class NumberToDigits
{
	// Converting a given number to digits array
	func digitArray(_ num: Int)
	{
		var number: Int = num;
		if (number < 0)
		{
			// When number is negative
			// Convert into positive number
			number = -number;
		}
		// Convert number to string
		let temp = Array(String(number));
		// Get number of digits
		let size: Int = temp.count;
		// This are used to store result value
		var digits: [Int] = Array(repeating: 0, count: size);
		// Loop controlling variable
		var i: Int = 0;
		// Execute Loop through of digit size
		while (i < size)
		{
			// 48 is ASCII of zero
			digits[i] =  Int(UnicodeScalar(String(temp[i]))!.value) - 48;
			i += 1;
		}
		i = 0;
		// Display calculated result
		while (i < size)
		{
			print("  ", digits[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
}
func main()
{
	let task: NumberToDigits = NumberToDigits();
	// Test case
	task.digitArray(987650);
	task.digitArray(12345);
}
main();

Output

   9   8   7   6   5   0
   1   2   3   4   5
/*
    Kotlin Program
    Convert number to digits array
*/
class NumberToDigits
{
	// Converting a given number to digits array
	fun digitArray(num: Int): Unit
	{
		var number: Int = num;
		if (number<0)
		{
			// When number is negative
			// Convert into positive number
			number = -number;
		}
		// Convert number to string
		var temp: String = "" + number;
		// Get number of digits
		var size: Int = temp.count();
		// This are used to store result value
		var digits: Array<Int> = Array(size){0};
		// Loop controlling variable
		var i: Int = 0;
		// Execute Loop through of digit size
		while (i<size)
		{
			// 48 is ASCII of zero
			digits[i] = temp[i].toInt() - 48;
			i += 1;
		}
		i = 0;
		// Display calculated result
		while (i < size)
		{
			print("  " + digits[i]);
			i += 1;
		}
		print("\n");
	}
}

fun main(args: Array<String>): Unit
{
	var task: NumberToDigits = NumberToDigits();
	// Test case
	task.digitArray(987650);
	task.digitArray(12345);
}

Output

  9  8  7  6  5  0
  1  2  3  4  5


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