# Find nth last digit of a number

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

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