Posted on by Kalkicode
Code Number

# Find nth last digit of a number

In this article, we will discuss how to find the nth last digit of a given number. The problem requires us to retrieve the digit at a specific position from the right (starting from 1) in a given integer. We will provide a program to implement the solution and explain it step-by-step.

## Problem Statement

Given an integer number and an integer n, we need to find the nth last digit of the given number. If the nth last digit does not exist, we will return -1.

## Example

Let's take the number 45731 and find the nth last digits for different values of n:

1. For n = 1, the 1st last digit is 1.
2. For n = 2, the 2nd last digit is 3.
3. For n = 3, the 3rd last digit is 7.
4. For n = 4, the 4th last digit is 5.
5. For n = 5, the 5th last digit is 4.

## Pseudocode

1. Define a function get_digit(number, digit) to find the nth last digit of the given number.
2. Initialize a loop that runs until the number becomes 0 or the digit value becomes 0.
3. Inside the loop, check if digit is equal to 1. a. If true, return the last digit (number % 10).
4. Decrement digit by 1 and divide the number by 10 to move to the next digit from the right.
5. If the loop completes without finding the nth digit, return -1.
6. Define a function find_digit(number, digit) to display the nth last digit of the number.
7. If the number is negative, make it positive to simplify the calculation for negative numbers.
8. Call the get_digit() function to find the nth last digit and print the result.

## Algorithm Explanation

1. We define the get_digit() function to find the nth last digit in the number.
2. The function iterates through the number from the right while decrementing the digit value.
3. When digit becomes 1, the function returns the last digit (number % 10).
4. The function handles negative numbers by converting them to positive before processing.
5. If the digit does not exist (number is smaller than n), the function returns -1.
6. The find_digit() function calls get_digit() and prints the result with appropriate messages.
7. Test cases are provided to demonstrate the functionality of the code.

## Code Solution

Here given code implementation process.

//C Program
//Find nth last digit of a number
#include <stdio.h>

//return nth digit in number when if digit is exist
int get_digit(int number, int digit)
{
while (number != 0 && digit > 0)
{
if (digit == 1)
{
//get digit
return number % 10;
}
digit--;
number /= 10;
}
return -1;
}
void find_digit(int number, int digit)
{
printf("%d' digit of number %d is ", digit, number);
if (number < 0)
{
number = -number;
}
//if nth digit is -1 means digit are not exist
printf(" %d\n", get_digit(number, digit));
}
int main()
{
//Test Case
find_digit(45731, 3); //7
find_digit(-49731, 4); //9
find_digit(1234, 5); //-1
find_digit(123, 3); //1
find_digit(147911, 5); //4
return 0;
}

#### Output

3' digit of number 45731 is  7
4' digit of number -49731 is  9
5' digit of number 1234 is  -1
3' digit of number 123 is  1
5' digit of number 147911 is  4
/*
C++ Program
Find nth last digit of a number
*/
#include<iostream>

using namespace std;

class MyNumber
{
public:
//return nth digit in number when if digit is exist
int get_digit(int number, int digit)
{
while (number != 0 && digit > 0)
{
if (digit == 1)
{
return
//get digit
number % 10;
}
digit--;
number /= 10;
}
return -1;
}
void find_digit(int number, int digit)
{
cout << "" << digit << "' digit of number " << number << " is ";
if (number < 0)
{
number = -number;
}
cout << " " << this->get_digit(number, digit) << "\n";
}
};
int main()
{
MyNumber obj =  MyNumber();
//Test Case
obj.find_digit(45731, 3);//7
obj.find_digit(-49731, 4);//9
obj.find_digit(1234, 5);//-1
obj.find_digit(123, 3);//1
obj.find_digit(147911, 5);
return 0;
}

#### Output

3' digit of number 45731 is  7
4' digit of number -49731 is  9
5' digit of number 1234 is  -1
3' digit of number 123 is  1
5' digit of number 147911 is  4
/*
Java Program
Find nth last digit of a number
*/
public class MyNumber
{
//return nth digit in number when if digit is exist
public int get_digit(int number, int digit)
{
while (number != 0 && digit > 0)
{
if (digit == 1)
{
return
//get digit
number % 10;
}
digit--;
number /= 10;
}
return -1;
}
public void find_digit(int number, int digit)
{
System.out.print("" + digit + "' digit of number " + number + " is ");
if (number < 0)
{
number = -number;
}
//if nth digit is -1 means digit are not exist
System.out.print(" " + get_digit(number, digit) + "\n");
}
public static void main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Case
obj.find_digit(45731, 3); //7
obj.find_digit(-49731, 4); //9
obj.find_digit(1234, 5); //-1
obj.find_digit(123, 3); //1
obj.find_digit(147911, 5); //4
}
}

#### Output

3' digit of number 45731 is  7
4' digit of number -49731 is  9
5' digit of number 1234 is  -1
3' digit of number 123 is  1
5' digit of number 147911 is  4
/*
C# Program
Find nth last digit of a number
*/
using System;
public class MyNumber
{
//return nth digit in number when if digit is exist
public int get_digit(int number, int digit)
{
while (number != 0 && digit > 0)
{
if (digit == 1)
{
return number % 10;
}
digit--;
number /= 10;
}
return -1;
}
public void find_digit(int number, int digit)
{
Console.Write("" + digit + "' digit of number " + number + " is ");
if (number < 0)
{
number = -number;
}
//If nth digit is -1 means digit are not exist
Console.Write(" " + get_digit(number, digit) + "\n");
}
public static void Main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Case
obj.find_digit(45731, 3);
obj.find_digit(-49731, 4);
obj.find_digit(1234, 5);
obj.find_digit(123, 3);
obj.find_digit(147911, 5);
}
}

#### Output

3' digit of number 45731 is  7
4' digit of number -49731 is  9
5' digit of number 1234 is  -1
3' digit of number 123 is  1
5' digit of number 147911 is  4
<?php
/*
Php Program
Find nth last digit of a number
*/
class MyNumber
{
//return nth digit in number when if digit is exist
public 	function get_digit(\$number, \$digit)
{
while (\$number != 0 && \$digit > 0)
{
if (\$digit == 1)
{
return
//get digit
\$number % 10;
}
\$digit--;
\$number = intval(\$number / 10);
}
return -1;
}
public 	function find_digit(\$number, \$digit)
{
echo("". \$digit ."' digit of number ". \$number ." is ");
if (\$number < 0)
{
\$number = -\$number;
}
//If nth digit is -1 means digit are not exist
echo(" ". \$this->get_digit(\$number, \$digit) ."\n");
}
}

function main()
{
\$obj = new MyNumber();
//Test Case
\$obj->find_digit(45731, 3);
\$obj->find_digit(-49731, 4);
\$obj->find_digit(1234, 5);
\$obj->find_digit(123, 3);
\$obj->find_digit(147911, 5);
}
main();

#### Output

3' digit of number 45731 is  7
4' digit of number -49731 is  9
5' digit of number 1234 is  -1
3' digit of number 123 is  1
5' digit of number 147911 is  4
/*
Node Js Program
Find nth last digit of a number
*/
class MyNumber
{
//return nth digit in number when if digit is exist
get_digit(number, digit)
{
while (number != 0 && digit > 0)
{
if (digit == 1)
{
//get digit
return number % 10;
}
digit--;
number = parseInt(number / 10);
}
return -1;
}
find_digit(number, digit)
{
process.stdout.write("" + digit + "' digit of number " + number + " is ");
if (number < 0)
{
number = -number;
}
//If nth digit is -1 means digit are not exist
process.stdout.write(" " + this.get_digit(number, digit) + "\n");
}
}

function main(args)
{
var obj = new MyNumber();
//Test Case
obj.find_digit(45731, 3);
obj.find_digit(-49731, 4);
obj.find_digit(1234, 5);
obj.find_digit(123, 3);
obj.find_digit(147911, 5);
}
main();

#### Output

3' digit of number 45731 is  7
4' digit of number -49731 is  9
5' digit of number 1234 is  -1
3' digit of number 123 is  1
5' digit of number 147911 is  4
#   Python 3 Program
#   Find nth last digit of a number

class MyNumber :
# return nth digit in number when if digit is exist
def get_digit(self, number, digit) :
while (number != 0 and digit > 0) :
if (digit == 1) :

# get digit
return (number % 10)

digit -= 1
number = int(number / 10)

return -1

def find_digit(self, number, digit) :
print("", digit ,"' digit of number ", number ," is ", end = "")
if (number < 0) :
number = -number

# If nth digit is -1 means digit are not exist
print(" ", self.get_digit(number, digit) ,"\n", end = "")

def main() :
obj = MyNumber()
# Test Case
obj.find_digit(45731, 3)
obj.find_digit(-49731, 4)
obj.find_digit(1234, 5)
obj.find_digit(123, 3)
obj.find_digit(147911, 5)

if __name__ == "__main__": main()

#### Output

3 ' digit of number  45731  is   7
4 ' digit of number  -49731  is   9
5 ' digit of number  1234  is   -1
3 ' digit of number  123  is   1
5 ' digit of number  147911  is   4
#   Ruby Program
#   Find nth last digit of a number

class MyNumber

# return nth digit in number when if digit is exist
def get_digit(number, digit)

while (number != 0 && digit > 0)

if (digit == 1)
# get digit
return number % 10
end
digit -= 1
number /= 10
end
return -1
end
def find_digit(number, digit)

print("", digit ,"' digit of number ", number ," is ")
if (number < 0)

number = -number
end
# If nth digit is -1 means digit are not exist
print(" ", self.get_digit(number, digit) ,"\n")
end
end
def main()

obj = MyNumber.new()
# Test Case
obj.find_digit(45731, 3)
obj.find_digit(-49731, 4)
obj.find_digit(1234, 5)
obj.find_digit(123, 3)
obj.find_digit(147911, 5)
end
main()

#### Output

3' digit of number 45731 is  7
4' digit of number -49731 is  9
5' digit of number 1234 is  -1
3' digit of number 123 is  1
5' digit of number 147911 is  4
/*
Scala Program
Find nth last digit of a number
*/
class MyNumber
{
//return nth digit in number when if digit is exist
def get_digit(num : Int, mark: Int): Int = {
var number : Int = num;
var digit : Int = mark;
while (number != 0 && digit > 0)
{
if (digit == 1)
{
//get digit
return number % 10;
}
digit -= 1;
number = (number / 10).toInt;
}
return -1;
}
def find_digit(num: Int, digit: Int): Unit = {
var number : Int = num;
print("" + digit + "' digit of number " + number + " is ");
if (number < 0)
{
number = -number;
}
//If nth digit is -1 means digit are not exist
print(" " + get_digit(number, digit) + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Case
obj.find_digit(45731, 3);
obj.find_digit(-49731, 4);
obj.find_digit(1234, 5);
obj.find_digit(123, 3);
obj.find_digit(147911, 5);
}
}

#### Output

3' digit of number 45731 is  7
4' digit of number -49731 is  9
5' digit of number 1234 is  -1
3' digit of number 123 is  1
5' digit of number 147911 is  4
/*
Swift Program
Find nth last digit of a number
*/
class MyNumber
{
//return nth digit in number when if digit is exist
func get_digit(_ number: inout Int, _ digit: inout Int) -> Int
{
while (number != 0 && digit > 0)
{
if (digit == 1)
{
//get digit
return number % 10;
}
digit -= 1;
number /= 10;
}
return -1;
}
func find_digit(_ num:  Int, _ mark: Int)
{
var number : Int = num;
var digit : Int = mark;
print("", digit ,"\" digit of number ", number ," is ", terminator: "");
if (number < 0)
{
number = -number;
}

//If nth digit is -1 means digit are not exist
print(" ", self.get_digit(&number, &digit) ,"\n", terminator: "");
}
}
func main()
{
let obj: MyNumber = MyNumber();
//Test Case
obj.find_digit(45731, 3);
obj.find_digit(-49731, 4);
obj.find_digit(1234, 5);
obj.find_digit(123, 3);
obj.find_digit(147911, 5);
}
main();

#### Output

3 " digit of number  45731  is   7
4 " digit of number  -49731  is   9
5 " digit of number  1234  is   -1
3 " digit of number  123  is   1
5 " digit of number  147911  is   4

## Resultant Output Explanation

1. For the number 45731:
• 3rd last digit (n=3) is 7.
• 4th last digit (n=4) is 5.
• 5th last digit (n=5) is 4.
2. For the number -49731:
• 4th last digit (n=4) is 9.
3. For the number 1234:
• There is no 5th last digit (n=5), so the output is -1.
4. For the number 123:
• 3rd last digit (n=3) is 1.
5. For the number 147911:
• 5th last digit (n=5) is 4.

## Time Complexity

The time complexity of this code is O(n), where n is the position of the digit to be found. In the worst case, the loop runs until n or the number becomes 0. Since the number of digits in a number is proportional to log10(n), the time complexity can be approximated as O(log n).

## 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.

Categories
Relative Post