Find number of digits in a number

Here given code implementation process.

// C Program
// Find number of digits in a number
#include <stdio.h>

//Find the length of number
void digits_length(int number)
{
  int result = 0;
  printf("Number : %d  ",number);
  if(number==0)
  {
    //When number is zero, then digit length is one
    result = 1;
  }
  if(number < 0)
  {
    //When number is negative number
    number = -number;
  }
  while(number!=0)
  {
    result++;
    number/=10;
  }
  printf("Digit : %d  \n",result);
}
int main()
{
  //Test Case
  digits_length(84211);
  digits_length(-1111);
  digits_length(100);
  digits_length(123451);
  return 0;
}

Output

Number : 84211  Digit : 5
Number : -1111  Digit : 4
Number : 100  Digit : 3
Number : 123451  Digit : 6
/*
  C++ Program
  Find number of digits in a number
*/
#include<iostream>

using namespace std;
class MyNumber
{
	public:
		//Find the length of number
		void digits_length(int number)
		{
			int result = 0;
			cout << "Number : " << number << " ";
			if (number == 0)
			{
				//When number is zero, then digit length is one
				result = 1;
			}
			if (number < 0)
			{
				//When number is negative number
				number = -number;
			}
			while (number != 0)
			{
				result++;
				number /= 10;
			}
			cout << "Digit : " << result << " \n";
		}
};
int main()
{
	MyNumber obj =  MyNumber();
	//Test Case
	obj.digits_length(84211);
	obj.digits_length(-1111);
	obj.digits_length(100);
	obj.digits_length(123451);
	return 0;
}

Output

Number : 84211 Digit : 5
Number : -1111 Digit : 4
Number : 100 Digit : 3
Number : 123451 Digit : 6
/*
  Java Program
  Find number of digits in a number
*/
public class MyNumber
{
	//Find the length of number
	public void digits_length(int number)
	{
		int result = 0;
		System.out.print("Number : " + number + " ");
		if (number == 0)
		{
			//When number is zero, then digit length is one
			result = 1;
		}
		if (number < 0)
		{
			//When number is negative number
			number = -number;
		}
		while (number != 0)
		{
			result++;
			number /= 10;
		}
		System.out.print("Digit : " + result + " \n");
	}
	public static void main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Case
		obj.digits_length(84211);
		obj.digits_length(-1111);
		obj.digits_length(100);
		obj.digits_length(123451);
	}
}

Output

Number : 84211 Digit : 5
Number : -1111 Digit : 4
Number : 100 Digit : 3
Number : 123451 Digit : 6
/*
  C# Program
  Find number of digits in a number
*/
using System;
public class MyNumber
{
	//Find the length of number
	public void digits_length(int number)
	{
		int result = 0;
		Console.Write("Number : " + number + " ");
		if (number == 0)
		{
			//When number is zero, then digit length is one
			result = 1;
		}
		if (number < 0)
		{
			//When number is negative number
			number = -number;
		}
		while (number != 0)
		{
			result++;
			number /= 10;
		}
		Console.Write("Digit : " + result + " \n");
	}
	public static void Main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Case
		obj.digits_length(84211);
		obj.digits_length(-1111);
		obj.digits_length(100);
		obj.digits_length(123451);
	}
}

Output

Number : 84211 Digit : 5
Number : -1111 Digit : 4
Number : 100 Digit : 3
Number : 123451 Digit : 6
<?php
/*
  Php Program
  Find number of digits in a number
*/
class MyNumber
{
	//Find the length of number
	public 	function digits_length($number)
	{
		$result = 0;
		echo("Number : ". $number ." ");
		if ($number == 0)
		{
			//When number is zero, then digit length is one
			$result = 1;
		}
		if ($number < 0)
		{
			//When number is negative number
			$number = -$number;
		}
		while ($number != 0)
		{
			$result++;
			$number = intval($number / 10);
		}
		echo("Digit : ". $result ." \n");
	}
}

function main()
{
	$obj = new MyNumber();
	//Test Case
	$obj->digits_length(84211);
	$obj->digits_length(-1111);
	$obj->digits_length(100);
	$obj->digits_length(123451);
}
main();

Output

Number : 84211 Digit : 5
Number : -1111 Digit : 4
Number : 100 Digit : 3
Number : 123451 Digit : 6
/*
  Node Js Program
  Find number of digits in a number
*/
class MyNumber
{
	//Find the length of number
	digits_length(number)
	{
		var result = 0;
		process.stdout.write("Number : " + number + " ");
		if (number == 0)
		{
			//When number is zero, then digit length is one
			result = 1;
		}
		if (number < 0)
		{
			//When number is negative number
			number = -number;
		}
		while (number != 0)
		{
			result++;
			number = parseInt(number / 10);
		}
		process.stdout.write("Digit : " + result + " \n");
	}
}

function main(args)
{
	var obj = new MyNumber();
	//Test Case
	obj.digits_length(84211);
	obj.digits_length(-1111);
	obj.digits_length(100);
	obj.digits_length(123451);
}
main();

Output

Number : 84211 Digit : 5
Number : -1111 Digit : 4
Number : 100 Digit : 3
Number : 123451 Digit : 6
#   Python 3 Program
#   Find number of digits in a number

class MyNumber :
	# Find the length of number
	def digits_length(self, number) :
		result = 0
		print("Number : ", number ," ", end = "")
		if (number == 0) :
			# When number is zero, then digit length is one
			result = 1
		
		if (number < 0) :
			# When number is negative number
			number = -number
		
		while (number != 0) :
			result += 1
			number = int(number / 10)
		
		print("Digit : ", result ," \n", end = "")
	

def main() :
	obj = MyNumber()
	# Test Case
	obj.digits_length(84211)
	obj.digits_length(-1111)
	obj.digits_length(100)
	obj.digits_length(123451)


if __name__ == "__main__": main()

Output

Number :  84211  Digit :  5
Number :  -1111  Digit :  4
Number :  100  Digit :  3
Number :  123451  Digit :  6
#   Ruby Program
#   Find number of digits in a number

class MyNumber

	# Find the length of number
	def digits_length(number)
	
		result = 0
		print("Number  :", number ," ")
		if (number == 0)
		
			# When number is zero, then digit length is one
			result = 1
		end
		if (number < 0)
		
			# When number is negative number
			number = -number
		end
		while (number != 0)
		
			result += 1
			number /= 10
		end
		print("Digit  :", result ," \n")
	end
end
def main()

	obj = MyNumber.new()
	# Test Case
	obj.digits_length(84211)
	obj.digits_length(-1111)
	obj.digits_length(100)
	obj.digits_length(123451)
end
main()

Output

Number  :84211 Digit  :5 
Number  :-1111 Digit  :4 
Number  :100 Digit  :3 
Number  :123451 Digit  :6 
/*
  Scala Program
  Find number of digits in a number
*/
class MyNumber
{
	//Find the length of number
	def digits_length(num: Int): Unit = {
		var result: Int = 0;
      	var number: Int = num;
		print("Number : " + number + " ");
		if (number == 0)
		{
			//When number is zero, then digit length is one
			result = 1;
		}
		if (number < 0)
		{
			//When number is negative number
			number = -number;
		}
		while (number != 0)
		{
			result += 1;
			number = (number / 10).toInt;
		}
		print("Digit : " + result + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case
		obj.digits_length(84211);
		obj.digits_length(-1111);
		obj.digits_length(100);
		obj.digits_length(123451);
	}
}

Output

Number : 84211 Digit : 5
Number : -1111 Digit : 4
Number : 100 Digit : 3
Number : 123451 Digit : 6
/*
  Swift Program
  Find number of digits in a number
*/
class MyNumber
{
	//Find the length of number
	func digits_length(_ num:  Int)
	{
		var result: Int = 0;
      	var number: Int = num;
		print("Number : ", number ," ", terminator: "");
		if (number == 0)
		{
			//When number is zero, then digit length is one
			result = 1;
		}
		if (number < 0)
		{
			//When number is negative number
			number = -number;
		}
		while (number != 0)
		{
			result += 1;
			number /= 10;
		}
		print("Digit : ", result ," \n", terminator: "");
	}
}
func main()
{
	let obj: MyNumber = MyNumber();
	//Test Case
	obj.digits_length(84211);
	obj.digits_length(-1111);
	obj.digits_length(100);
	obj.digits_length(123451);
}
main();

Output

Number :  84211  Digit :  5
Number :  -1111  Digit :  4
Number :  100  Digit :  3
Number :  123451  Digit :  6


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