12.Write a Java program to find sum of first and last digit of a number.
Static :-
class loop12
{
public static void main(String args[])
{
int n=1234,ld,ft,sum=0;
ld = n%10;
System.out.println("last number :- "+ld);
ft = n;
while(ft>=10)
{
ft = ft/10;
}
System.out.println("First number :- "+ft);
sum = ft+ld;
System.out.println("The Sum is :-"+sum);
}
}
Output :-
Dynamic :-
import java.util.*;
class loop12
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n,ld,ft,sum=0;
System.out.println("Enter the number :- ");
n=in.nextInt();
ld = n%10;
System.out.println("Last Digit :-"+ld);
ft=n;
while(ft>=10)
{
ft = ft/10;
}
System.out.println("First Digit :-"+ft);
sum = ft+ld;
System.out.println("The sum is :- "+sum);
}
}
Output :-