Find first digit of a number

Here given code implementation process.

// C Program
// Find first digit of a number
// In constant time
#include <stdio.h>
#include <math.h>

int absValue(int num)
{
    if(num < 0)
    {
        return -num;
    }
    return num;
}

void firstDigit(int num)
{
    // Get absolute value
    int v = absValue(num);

    // Get number of digit - 1
    int digits = (int)log10(v);

    // Find first digit
    int result = (int)(v / pow(10, digits));

    // Display given number
    printf("\n Given Number : %d",num);

    // Display first digit
    printf("\n First Digit  : %d\n",result);
}

int main()
{   
    // Test
    firstDigit(7452);
    firstDigit(1633);
    firstDigit(-81633);
    firstDigit(348952);
    return 0;
}

Output

 Given Number : 7452
 First Digit  : 7

 Given Number : 1633
 First Digit  : 1

 Given Number : -81633
 First Digit  : 8

 Given Number : 348952
 First Digit  : 3
// Java program for
// Find first digit of a number
public class Digits
{
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void firstDigit(int num)
	{
		// Get absolute value
		int v = absValue(num);
		// Get number of digit - 1
		int digits = (int) Math.log10(v);
		// Find first digit
		int result = (int)(v / Math.pow(10, digits));
		// Display given number
		System.out.println("\n Given Number : " + num);
		// Display first digit
		System.out.println(" First Digit : " + result);
	}
	public static void main(String[] args)
	{
		Digits task = new Digits();
		// Test
		task.firstDigit(7452);
		task.firstDigit(1633);
		task.firstDigit(-81633);
		task.firstDigit(348952);
	}
}

Output

 Given Number : 7452
 First Digit : 7

 Given Number : 1633
 First Digit : 1

 Given Number : -81633
 First Digit : 8

 Given Number : 348952
 First Digit : 3
// Include header file
#include <iostream>
#include <math.h>
using namespace std;
// C++ program for
// Find first digit of a number
class Digits
{
	public: int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	void firstDigit(int num)
	{
		// Get absolute value
		int v = this->absValue(num);
		// Get number of digit - 1
		int digits = (int) log10(v);
		// Find first digit
		int result = (int)(v / pow(10, digits));
		// Display given number
		cout << "\n Given Number : " << num << endl;
		// Display first digit
		cout << " First Digit : " << result << endl;
	}
};
int main()
{
	Digits *task = new Digits();
	// Test
	task->firstDigit(7452);
	task->firstDigit(1633);
	task->firstDigit(-81633);
	task->firstDigit(348952);
	return 0;
}

Output

 Given Number : 7452
 First Digit : 7

 Given Number : 1633
 First Digit : 1

 Given Number : -81633
 First Digit : 8

 Given Number : 348952
 First Digit : 3
package main
import "math"
import "fmt"
// Go program for
// Find first digit of a number
type Digits struct {}
func getDigits() * Digits {
	var me *Digits = &Digits {}
	return me
}
func(this Digits) absValue(num int) int {
	if num < 0 {
		return -num
	}
	return num
}
func(this Digits) firstDigit(num int) {
	// Get absolute value
	var v int = this.absValue(num)
	// Get number of digit - 1
	var digits int = int(math.Log10(float64(v)))
	// Find first digit
	var result int = int((float64(v) / math.Pow(10, float64(digits))))
	// Display given number
	fmt.Println("\n Given Number : ", num)
	// Display first digit
	fmt.Println(" First Digit : ", result)
}
func main() {
	var task * Digits = getDigits()
	// Test
	task.firstDigit(7452)
	task.firstDigit(1633)
	task.firstDigit(-81633)
	task.firstDigit(348952)
}

Output

 Given Number : 7452
 First Digit : 7

 Given Number : 1633
 First Digit : 1

 Given Number : -81633
 First Digit : 8

 Given Number : 348952
 First Digit : 3
// Include namespace system
using System;
// Csharp program for
// Find first digit of a number
public class Digits
{
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void firstDigit(int num)
	{
		// Get absolute value
		int v = this.absValue(num);
		// Get number of digit - 1
		int digits = (int) Math.Log10(v);
		// Find first digit
		int result = (int)(v / Math.Pow(10, digits));
		// Display given number
		Console.WriteLine("\n Given Number : " + num);
		// Display first digit
		Console.WriteLine(" First Digit : " + result);
	}
	public static void Main(String[] args)
	{
		Digits task = new Digits();
		// Test
		task.firstDigit(7452);
		task.firstDigit(1633);
		task.firstDigit(-81633);
		task.firstDigit(348952);
	}
}

Output

 Given Number : 7452
 First Digit : 7

 Given Number : 1633
 First Digit : 1

 Given Number : -81633
 First Digit : 8

 Given Number : 348952
 First Digit : 3
<?php
// Php program for
// Find first digit of a number
class Digits
{
	public	function absValue($num)
	{
		if ($num < 0)
		{
			return -$num;
		}
		return $num;
	}
	public	function firstDigit($num)
	{
		// Get absolute value
		$v = $this->absValue($num);
		// Get number of digit - 1
		$digits = (int) log10($v);
		// Find first digit
		$result = (int)($v / pow(10, $digits));
		// Display given number
		echo("\n Given Number : ".$num.
			"\n");
		// Display first digit
		echo(" First Digit : ".$result.
			"\n");
	}
}

function main()
{
	$task = new Digits();
	// Test
	$task->firstDigit(7452);
	$task->firstDigit(1633);
	$task->firstDigit(-81633);
	$task->firstDigit(348952);
}
main();

Output

 Given Number : 7452
 First Digit : 7

 Given Number : 1633
 First Digit : 1

 Given Number : -81633
 First Digit : 8

 Given Number : 348952
 First Digit : 3
// Node JS program for
// Find first digit of a number
class Digits
{
	absValue(num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	firstDigit(num)
	{
		// Get absolute value
		var v = this.absValue(num);
		// Get number of digit - 1
		var digits = parseInt(Math.log10(v));
		// Find first digit
		var result = parseInt((v / Math.pow(10, digits)));
		// Display given number
		console.log("\n Given Number : " + num);
		// Display first digit
		console.log(" First Digit : " + result);
	}
}

function main()
{
	var task = new Digits();
	// Test
	task.firstDigit(7452);
	task.firstDigit(1633);
	task.firstDigit(-81633);
	task.firstDigit(348952);
}
main();

Output

 Given Number : 7452
 First Digit : 7

 Given Number : 1633
 First Digit : 1

 Given Number : -81633
 First Digit : 8

 Given Number : 348952
 First Digit : 3
import math
#  Python 3 program for
#  Find first digit of a number
class Digits :
	def absValue(self, num) :
		if (num < 0) :
			return -num
		
		return num
	
	def firstDigit(self, num) :
		#  Get absolute value
		v = self.absValue(num)
		#  Get number of digit - 1
		digits = int(math.log10(v))
		#  Find first digit
		result = int((v / 10 ** digits))
		#  Display given number
		print("\n Given Number : ", num)
		#  Display first digit
		print(" First Digit : ", result)
	

def main() :
	task = Digits()
	#  Test
	task.firstDigit(7452)
	task.firstDigit(1633)
	task.firstDigit(-81633)
	task.firstDigit(348952)

if __name__ == "__main__": main()

Output

 Given Number :  7452
 First Digit :  7

 Given Number :  1633
 First Digit :  1

 Given Number :  -81633
 First Digit :  8

 Given Number :  348952
 First Digit :  3
#  Ruby program for
#  Find first digit of a number
class Digits 
	def absValue(num) 
		if (num < 0) 
			return -num
		end

		return num
	end

	def firstDigit(num) 
		#  Get absolute value
		v = self.absValue(num)
		#  Get number of digit - 1
		digits = Math.log10(v).to_i
		#  Find first digit
		result = (v / 10 ** digits).to_i
		#  Display given number
		print("\n Given Number : ", num, "\n")
		#  Display first digit
		print(" First Digit : ", result, "\n")
	end

end

def main() 
	task = Digits.new()
	#  Test
	task.firstDigit(7452)
	task.firstDigit(1633)
	task.firstDigit(-81633)
	task.firstDigit(348952)
end

main()

Output

 Given Number : 7452
 First Digit : 7

 Given Number : 1633
 First Digit : 1

 Given Number : -81633
 First Digit : 8

 Given Number : 348952
 First Digit : 3
// Scala program for
// Find first digit of a number
class Digits()
{
	def absValue(num: Int): Int = {
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	def firstDigit(num: Int): Unit = {
		// Get absolute value
		var v: Int = absValue(num);
		// Get number of digit - 1
		var digits: Int = Math.log10(v).toInt;
		// Find first digit
		var result: Int = (v / Math.pow(10, digits.toDouble)).toInt;
		// Display given number
		println("\n Given Number : " + num);
		// Display first digit
		println(" First Digit : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Digits = new Digits();
		// Test
		task.firstDigit(7452);
		task.firstDigit(1633);
		task.firstDigit(-81633);
		task.firstDigit(348952);
	}
}

Output

 Given Number : 7452
 First Digit : 7

 Given Number : 1633
 First Digit : 1

 Given Number : -81633
 First Digit : 8

 Given Number : 348952
 First Digit : 3
import Foundation;
// Swift 4 program for
// Find first digit of a number
class Digits
{
	func absValue(_ num: Int) -> Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	func firstDigit(_ num: Int)
	{
		// Get absolute value
		let v: Int = self.absValue(num);
		// Get number of digit - 1
		let digits: Int = Int(log10(Float(v)));
		// Find first digit
		let result: Int = Int((Float(v) / pow(10, Float(digits))));
		// Display given number
		print("\n Given Number : ", num);
		// Display first digit
		print(" First Digit : ", result);
	}
}
func main()
{
	let task: Digits = Digits();
	// Test
	task.firstDigit(7452);
	task.firstDigit(1633);
	task.firstDigit(-81633);
	task.firstDigit(348952);
}
main();

Output

 Given Number :  7452
 First Digit :  7

 Given Number :  1633
 First Digit :  1

 Given Number :  -81633
 First Digit :  8

 Given Number :  348952
 First Digit :  3
// Kotlin program for
// Find first digit of a number
class Digits
{
	fun absValue(num: Int): Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	fun firstDigit(num: Int): Unit
	{
		// Get absolute value
		val v: Int = this.absValue(num);
		// Get number of digit - 1
		val digits: Int = Math.log10(v.toDouble()).toInt();
		// Find first digit
		val result: Int = (v / Math.pow(10.0, digits.toDouble())).toInt();
		// Display given number
		println("\n Given Number : " + num);
		// Display first digit
		println(" First Digit : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Digits = Digits();
	// Test
	task.firstDigit(7452);
	task.firstDigit(1633);
	task.firstDigit(-81633);
	task.firstDigit(348952);
}

Output

 Given Number : 7452
 First Digit : 7

 Given Number : 1633
 First Digit : 1

 Given Number : -81633
 First Digit : 8

 Given Number : 348952
 First Digit : 3


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