Write a java program to store mobile number in a variable and print the last 4 digits
Answers
Answered by
0
Answer:
This is the required Java program for the question.
import java.util.*;
public class MobileNumber {
public static void main(String[] args) {
int m;
Scanner sc=new Scanner(System.in);
System.out.print("Enter your mobile number: ");
m=sc.nextInt();
if((m+"").length()==10 || (m+"").length()==12)
System.out.println("Last 4 digits: "+m%10000);
else
System.out.println("Not a valid number.");
sc.close();
}
}
Explanation:
- Here, the concept of operators is used. We are asked to enter a valid pho.ne number and display the last 4 digits. To do this, we will simply divide the number by 10000 and get the remainder. To obtain the remainder, we will use % (modulus) operator. After calculating the last 4 digits, we will display it.
See the attachment for output ☑.
Attachments:
Similar questions