Find last digit of a number

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


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