write a java program to find sum of first and last digit of the number?

To find sum of first and last digit of the number here we first create while loop to check number is greater than 10 after that divide number from 10 to get first quotient.this is will be first digit number.
Now to find last digit number we divide number by 10 and reminder will be last digit.after that we sum of both first and last digit number.

				
					public class SumFirstLastNumber {
	public static void main(String[] args) {
		int number = 56789;
		int sum = 0;
		int firstDigit = number;
		while (firstDigit >= 10) {
			firstDigit = firstDigit / 10;
		}
		System.out.println("first digit number:- " + firstDigit);
		int lastDigit = number % 10;
		System.out.println("last digit number:- " + lastDigit);
		sum = firstDigit + lastDigit;
		System.out.println("sum of first and last digit of a number:- " + sum);
	}
}
				
			

Output :-
first digit number:- 5
last digit number:- 9
sum of first and last digit of a number:- 14

Leave a Comment

Your email address will not be published. Required fields are marked *