Posted on by Kalkicode
Code Mathematics

Find first digit of a number

The given problem is to find the first digit of a given integer number in constant time. We need to extract the first digit of the number without using any loops or string manipulations.

Problem Statement

Given an integer number, we want to find its first digit. If the number is positive, the first digit is the leftmost non-zero digit. If the number is negative, the first digit is the leftmost non-zero digit after the negative sign.

Explanation with Example

Let's take an example to illustrate the problem. Consider the number 7452. The first digit of this number is 7. For the number 1633, the first digit is 1. For the number -81633, the first digit is 8, and for 348952, the first digit is 3.

Idea to Solve the Problem

To solve this problem in constant time, we can use mathematical operations to extract the first digit. We need to calculate the number of digits in the given number and then divide the number by 10 raised to the power of the number of digits minus one. This will give us the first digit of the number.

Pseudocode

Here's the pseudocode to find the first digit of a number:

1. Function absValue(num)
2.     if num < 0
3.         return -num
4.     end if
5.     return num
6. End Function

7. Function firstDigit(num)
8.     v = absValue(num)
9.     digits = floor(log10(v))
10.    result = floor(v / 10^digits)
11.    Display "Given Number : num"
12.    Display "First Digit  : result"
13. End Function

14. Main
15.    Call firstDigit(7452)
16.    Call firstDigit(1633)
17.    Call firstDigit(-81633)
18.    Call firstDigit(348952)
19. End Main

Algorithm Explanation

  1. The absValue function returns the absolute value of the given number.
  2. In the firstDigit function, we find the absolute value of the given number and store it in v.
  3. We calculate the number of digits in v and store it in digits.
  4. To find the first digit, we divide v by 10 raised to the power of digits (minus one). The result is stored in result.
  5. We display the given number and the first digit using the printf function.

Code Solution

// 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

Resultant Output Explanation

The program executes the firstDigit function for each test case and displays the given number and its first digit. The output shows the correct first digit for each input number.

Given Number : 7452
First Digit  : 7

Given Number : 1633
First Digit  : 1

Given Number : -81633
First Digit  : 8

Given Number : 348952
First Digit  : 3

Time Complexity

The time complexity of the code is constant (O(1)) because it performs a fixed number of mathematical operations, irrespective of the size of the input number. The log10 and pow functions typically have constant time complexity, and the number of digits in an integer is proportional to log10(v), where v is the absolute value of the input number. Therefore, the overall time complexity remains constant.

Comment

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