Sum of first and last digit in Java
Input - Question 5
In this question, we will see how to input a two digit number in Java programming using the java input and find the sum of its first and last digit. To know more about java input click on the java input lesson.
Q5) Write a program in Java to input a two digit number and find the sum of its first and last digit.
Program
import java.util.Scanner;
public class Q5
{
public static void main(String args[])
{
int n,fd,ld,sum;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a two digit number");
n=sc.nextInt();
fd=n/10;
ld=n%10;
sum=fd+ld;
System.out.println("First digit=" + fd);
System.out.println("Last digit=" + ld);
System.out.println("Sum of first and last digit=" + sum);
}
}
Output
Enter a two digit number 56 First digit=5 Last digit=6 Sum of first and last digit=11