Write a program to accept a 5 digits number using the concept of function argument of main ( ) method and find out the sum of first digit, middle digit and last digit of that 5 digit number. Display the results with proper message. Example: N = 10238 Sum is 1 + 2 + 8 = 11
Answers
Program in Java :-
import java.util.Scanner;
public class FiveDigits {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Please enter a five digit number : ");
int n = sc.nextInt();
if (n <= 99999 && n >= 10000) {
int firstDigit = n / 10000;
int middleDigit = (n / 100) % 10;
int lastDigit = n % 10;
int sum = firstDigit + middleDigit + lastDigit;
System.out.println("Sum is " + firstDigit + " + " + middleDigit + " + " + lastDigit + " = " + sum);
}
else {
System.out.println("Invalid");
}
sc.close();
}
}