In: Computer Science
Java Code!!!!
A five-digit number is said to be friendly if:
For example, the number 42325 is a friendly number:
Write a program that will accept a 5 digit number (i.e, 10000 - 99999) from the user and prints to the screen a message telling the user if the number entered is or is not a friendly number. If the number entered is not a 5 digit number print to the screen an appropriate message and end the program. You must use an if/else statement which must have a condition constructed using boolean logic to test if the number is friendly.
import java.util.Scanner;
public class FriendlyNumber {
public static void main(String[] args) {
System.out.println("Enter number:
");
Scanner sc = new
Scanner(System.in);
int num = sc.nextInt();
if(num<10000 ||
num>99999){
System.out.println("Invalid number");
return;
}
boolean flag = true;
if (num % 5 != 0) {
flag =
false;
} else {
//removing last digit by dividing with 10
num = num /
10;
if (num % 4 !=
0) {
flag = false;
} else {
//removing last digit by dividing with 10
num = num / 10;
if (num % 3 != 0) {
flag = false;
} else {
//removing last digit by dividing with 10
num = num / 10;
if (num % 2 != 0) {
flag =
false;
}
}
}
}
if(flag)
System.out.println("Friendly Number");
else
System.out.println("Not a Friendly Number");
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me