Write a java Program to calculate the sum of first and last digit of a number.
9. Write a java Program to calculate the sum of first and last digit of a number.
import java.util. *;
class sumlast{
public static void main(String args[]){
Scanner sc = new Scanner(System.in );
System.out.print("Enter value:");
int x = sc.nextInt();
int fdigit, ldigit, sum;
fdigit=0;
ldigit=0;
ldigit= x % 10;
while (x != 0){
fdigit= x % 10;
x /= 10;
}
sum = fdigit + ldigit;
System.out.println("The sum of last and first digit is: " + sum);
}
}
Enter value:2356 The sum of last and first digit is: 8
Post a Comment