Find last digit of a number
The given problem is to find the last digit of a given number. The code is implemented in Java and uses simple arithmetic operations to determine the last digit of the number, regardless of its sign (positive or negative).
Problem Statement
We want to find the last digit of a given number, whether it is positive or negative.
Explanation with Example
Let's consider the number 7452 as an example. In the code, we call the function lastDigit(7452)
. The
function starts by taking the absolute value of the input number using the absValue
function. This step
ensures that we can correctly find the last digit of both positive and negative numbers.
Next, we find the last digit by performing the modulo operation (%
) with 10 on the absolute value of the
input number. The modulo operation returns the remainder after dividing the number by 10, which happens to be the
last digit of the number.
Pseudocode
function absValue(num):
if num is less than 0:
return -num
return num
function lastDigit(num):
v = absValue(num)
result = v % 10
print("Given Number: " + num)
print("Last Digit: " + result)
Algorithm Explanation
-
The function
absValue(num)
takes a numbernum
as input and returns its absolute value by checking if it's negative. If it is, it returns the negation of the number, otherwise, it returns the number itself. -
The function
lastDigit(num)
takes a numbernum
as input and first finds the absolute value of the number by calling theabsValue
function. -
The function then calculates the last digit of the absolute value of the input number by performing the modulo operation with 10 (
v % 10
). The result is stored in the variableresult
. -
The function prints the original given number and the last digit.
Code Solution
Here given code implementation process.
// Java program for
// Find last digit of a number
public class Digits
{
public int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
public void lastDigit(int num)
{
// Get absolute value
int v = absValue(num);
// Find last digit
int result = v % 10;
// Display given number
System.out.println("\n Given Number : " + num);
// Display last digit
System.out.println(" Last Digit : " + result);
}
public static void main(String[] args)
{
Digits task = new Digits();
// Test
task.lastDigit(7452);
task.lastDigit(1633);
task.lastDigit(-8);
task.lastDigit(348952);
}
}
Output
Given Number : 7452
Last Digit : 2
Given Number : 1633
Last Digit : 3
Given Number : -8
Last Digit : 8
Given Number : 348952
Last Digit : 2
// C Program
// Find last digit of a number
// In constant time
#include <stdio.h>
int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
void lastDigit(int num)
{
// Get absolute value
int v = absValue(num);
// Find last digit
int result = v % 10;
// Display given number
printf("\n Given Number : %d", num);
// Display last digit
printf("\n Last Digit : %d\n", result);
}
int main()
{
// Test
lastDigit(7452);
lastDigit(1633);
lastDigit(-8);
lastDigit(348952);
return 0;
}
Output
Given Number : 7452
Last Digit : 2
Given Number : 1633
Last Digit : 3
Given Number : -8
Last Digit : 8
Given Number : 348952
Last Digit : 2
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Find last digit of a number
class Digits
{
public: int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
void lastDigit(int num)
{
// Get absolute value
int v = this->absValue(num);
// Find last digit
int result = v % 10;
// Display given number
cout << "\n Given Number : " << num << endl;
// Display last digit
cout << " Last Digit : " << result << endl;
}
};
int main()
{
Digits *task = new Digits();
// Test
task->lastDigit(7452);
task->lastDigit(1633);
task->lastDigit(-8);
task->lastDigit(348952);
return 0;
}
Output
Given Number : 7452
Last Digit : 2
Given Number : 1633
Last Digit : 3
Given Number : -8
Last Digit : 8
Given Number : 348952
Last Digit : 2
package main
import "fmt"
// Go program for
// Find last 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) lastDigit(num int) {
// Get absolute value
var v int = this.absValue(num)
// Find last digit
var result int = v % 10
// Display given number
fmt.Println("\n Given Number : ", num)
// Display last digit
fmt.Println(" Last Digit : ", result)
}
func main() {
var task * Digits = getDigits()
// Test
task.lastDigit(7452)
task.lastDigit(1633)
task.lastDigit(-8)
task.lastDigit(348952)
}
Output
Given Number : 7452
Last Digit : 2
Given Number : 1633
Last Digit : 3
Given Number : -8
Last Digit : 8
Given Number : 348952
Last Digit : 2
// Include namespace system
using System;
// Csharp program for
// Find last digit of a number
public class Digits
{
public int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
public void lastDigit(int num)
{
// Get absolute value
int v = this.absValue(num);
// Find last digit
int result = v % 10;
// Display given number
Console.WriteLine("\n Given Number : " + num);
// Display last digit
Console.WriteLine(" Last Digit : " + result);
}
public static void Main(String[] args)
{
Digits task = new Digits();
// Test
task.lastDigit(7452);
task.lastDigit(1633);
task.lastDigit(-8);
task.lastDigit(348952);
}
}
Output
Given Number : 7452
Last Digit : 2
Given Number : 1633
Last Digit : 3
Given Number : -8
Last Digit : 8
Given Number : 348952
Last Digit : 2
<?php
// Php program for
// Find last digit of a number
class Digits
{
public function absValue($num)
{
if ($num < 0)
{
return -$num;
}
return $num;
}
public function lastDigit($num)
{
// Get absolute value
$v = $this->absValue($num);
// Find last digit
$result = $v % 10;
// Display given number
echo("\n Given Number : ".$num.
"\n");
// Display last digit
echo(" Last Digit : ".$result.
"\n");
}
}
function main()
{
$task = new Digits();
// Test
$task->lastDigit(7452);
$task->lastDigit(1633);
$task->lastDigit(-8);
$task->lastDigit(348952);
}
main();
Output
Given Number : 7452
Last Digit : 2
Given Number : 1633
Last Digit : 3
Given Number : -8
Last Digit : 8
Given Number : 348952
Last Digit : 2
// Node JS program for
// Find last digit of a number
class Digits
{
absValue(num)
{
if (num < 0)
{
return -num;
}
return num;
}
lastDigit(num)
{
// Get absolute value
var v = this.absValue(num);
// Find last digit
var result = v % 10;
// Display given number
console.log("\n Given Number : " + num);
// Display last digit
console.log(" Last Digit : " + result);
}
}
function main()
{
var task = new Digits();
// Test
task.lastDigit(7452);
task.lastDigit(1633);
task.lastDigit(-8);
task.lastDigit(348952);
}
main();
Output
Given Number : 7452
Last Digit : 2
Given Number : 1633
Last Digit : 3
Given Number : -8
Last Digit : 8
Given Number : 348952
Last Digit : 2
# Python 3 program for
# Find last digit of a number
class Digits :
def absValue(self, num) :
if (num < 0) :
return -num
return num
def lastDigit(self, num) :
# Get absolute value
v = self.absValue(num)
# Find last digit
result = v % 10
# Display given number
print("\n Given Number : ", num)
# Display last digit
print(" Last Digit : ", result)
def main() :
task = Digits()
# Test
task.lastDigit(7452)
task.lastDigit(1633)
task.lastDigit(-8)
task.lastDigit(348952)
if __name__ == "__main__": main()
Output
Given Number : 7452
Last Digit : 2
Given Number : 1633
Last Digit : 3
Given Number : -8
Last Digit : 8
Given Number : 348952
Last Digit : 2
# Ruby program for
# Find last digit of a number
class Digits
def absValue(num)
if (num < 0)
return -num
end
return num
end
def lastDigit(num)
# Get absolute value
v = self.absValue(num)
# Find last digit
result = v % 10
# Display given number
print("\n Given Number : ", num, "\n")
# Display last digit
print(" Last Digit : ", result, "\n")
end
end
def main()
task = Digits.new()
# Test
task.lastDigit(7452)
task.lastDigit(1633)
task.lastDigit(-8)
task.lastDigit(348952)
end
main()
Output
Given Number : 7452
Last Digit : 2
Given Number : 1633
Last Digit : 3
Given Number : -8
Last Digit : 8
Given Number : 348952
Last Digit : 2
// Scala program for
// Find last digit of a number
class Digits()
{
def absValue(num: Int): Int = {
if (num < 0)
{
return -num;
}
return num;
}
def lastDigit(num: Int): Unit = {
// Get absolute value
var v: Int = absValue(num);
// Find last digit
var result: Int = v % 10;
// Display given number
println("\n Given Number : " + num);
// Display last digit
println(" Last Digit : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Digits = new Digits();
// Test
task.lastDigit(7452);
task.lastDigit(1633);
task.lastDigit(-8);
task.lastDigit(348952);
}
}
Output
Given Number : 7452
Last Digit : 2
Given Number : 1633
Last Digit : 3
Given Number : -8
Last Digit : 8
Given Number : 348952
Last Digit : 2
// Swift 4 program for
// Find last digit of a number
class Digits
{
func absValue(_ num: Int) -> Int
{
if (num < 0)
{
return -num;
}
return num;
}
func lastDigit(_ num: Int)
{
// Get absolute value
let v: Int = self.absValue(num);
// Find last digit
let result: Int = v % 10;
// Display given number
print("\n Given Number : ", num);
// Display last digit
print(" Last Digit : ", result);
}
}
func main()
{
let task: Digits = Digits();
// Test
task.lastDigit(7452);
task.lastDigit(1633);
task.lastDigit(-8);
task.lastDigit(348952);
}
main();
Output
Given Number : 7452
Last Digit : 2
Given Number : 1633
Last Digit : 3
Given Number : -8
Last Digit : 8
Given Number : 348952
Last Digit : 2
// Kotlin program for
// Find last digit of a number
class Digits
{
fun absValue(num: Int): Int
{
if (num < 0)
{
return -num;
}
return num;
}
fun lastDigit(num: Int): Unit
{
// Get absolute value
val v: Int = this.absValue(num);
// Find last digit
val result: Int = v % 10;
// Display given number
println("\n Given Number : " + num);
// Display last digit
println(" Last Digit : " + result);
}
}
fun main(args: Array < String > ): Unit
{
val task: Digits = Digits();
// Test
task.lastDigit(7452);
task.lastDigit(1633);
task.lastDigit(-8);
task.lastDigit(348952);
}
Output
Given Number : 7452
Last Digit : 2
Given Number : 1633
Last Digit : 3
Given Number : -8
Last Digit : 8
Given Number : 348952
Last Digit : 2
Resultant Output Explanation
For the given code, the output is as follows:
Given Number: 7452
Last Digit: 2
Given Number: 1633
Last Digit: 3
Given Number: -8
Last Digit: 8
Given Number: 348952
Last Digit: 2
- For the number 7452, the last digit is 2.
- For the number 1633, the last digit is 3.
- For the number -8, the last digit is 8.
- For the number 348952, the last digit is 2.
Time Complexity
The time complexity of the code is constant O(1) because the operations performed (absolute value and modulo) are constant time operations, irrespective of the size of the input number. The code performs a fixed number of operations to determine the last digit, so the time complexity remains constant.
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