Question

In: Computer Science

Java Code!!!! A five-digit number is said to be friendly if: the leftmost digit is divisible...

Java Code!!!!

A five-digit number is said to be friendly if:

  • the leftmost digit is divisible by 1 and
  • the leftmost two digits are divisible by 2 and
  • the leftmost 3 digits are divisible by 3 and
  • the leftmost 4 digits are divisible by 4 and
  • leftmost 5 digits (the five-digit number itself) is divisible by 5.

For example, the number 42325 is a friendly number:

  • 4 is divisible by 1 and
  • 42 is divisible by 2 and
  • 423 is divisible by 3 and
  • 4232 is divisible by 4 and
  • 42325 is divisible by 5.

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.

Solutions

Expert Solution

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


Related Solutions

what is the best code to construct a C++ program that finds a five digit number;...
what is the best code to construct a C++ program that finds a five digit number; This number should reverses the order of its digits when multiplied by four. Also, how many five digits numbers are there in which the sum of the digits is even.
write a java code, please do not use method and demo Consider a four digit number...
write a java code, please do not use method and demo Consider a four digit number such as 6587. Split it at two digits, as 65 and 87. Sum of 65 and 87 is 152. Sum of the digits of 152 is 8. Given the starting and ending four digit numbers and a given target number such as 10, write a program to compute and print the number of instances where the sum of digits equals the target.
A number is a Universal Product Code (UPC) if its last digit agrees with the following...
A number is a Universal Product Code (UPC) if its last digit agrees with the following computations: • The sum of the odd position digits (not including the last) is M. That is we add the first digit to the third digit to the fifth digit etc. • The sum of the even position digits (not including the last) is N. • c = (3M + N)%10. • If c = 0 then the check digit is 0. • If...
In Java: "The program should be able to read a two-digit number d from the standard...
In Java: "The program should be able to read a two-digit number d from the standard input using Scanner class and outputs the second digit followed by a string literal "<-->" followed by the first digit. Run your program and make sure it prints 2<-->5 when d=52"
1a) How many five digit number can be written with the digits 1,2,3,4 if there is...
1a) How many five digit number can be written with the digits 1,2,3,4 if there is an even number of twos? 1b) How many five digit number can be written with the digits 1,2,3,4 if two can be used either 4 times, or none?
The number 52430 is a 5-digit number whereas 03201 is not a 5-digit number. Determine the...
The number 52430 is a 5-digit number whereas 03201 is not a 5-digit number. Determine the number of 5-digit multiples of 5 that can be created using the digits 0 to 9, if digits may not be repeated
A thief steals an ATM card and must randomly guess thecorrect five​-digit pin code...
2. A thief steals an ATM card and must randomly guess the correct five-digit pin code from a 5​-key keypad. Repetition of digits is allowed. What is the probability of a correct guess on the first​ try? The number of possible codes is___. the probability that correct code is given on the first try is?   3. If you know the names of the remaining seven students in the spelling​ bee, what is the probability of randomly selecting an order and...
A scientist is studying human memory. Subjects are shown a five-digit number for different lengths of...
A scientist is studying human memory. Subjects are shown a five-digit number for different lengths of time and then must write the number down. The subject either correctly recalls the number or fails to recall it. (Answer using R CODING) The results are as follows: times <- c(10.73, 9.9, 9.61, 8.7, 8.56, 8.31, 8.18, 7.86, 7.63, 6.99, 6.66, 6.1, 5.92, 5.84, 5.67, 5.64, 5.56, 5.29, 5.1, 5.09, 4.92, 4.81, 2.86, 2.13, 2.05, 1.95, 1.67, 1.67, 1.38, 1.02) correct <- c(1,...
Provide the Java code to compute the sum, average, maximum number and minimum number if I...
Provide the Java code to compute the sum, average, maximum number and minimum number if I have a string sentence of double numbers. Assume that the length of the string sentence is not known. It can be of any length. To split a string based on the comma character use the following. String sentence = "A,B,C,D,E"; String[] stringsArray = receivedSentence.split(","); Then stringsArray is an array of five elements such that: stringsArray[0] = 'A' stringsArray[1] = 'B' stringsArray[2] = 'C' stringsArray[3]...
​​​​​​​ASAP PLEASE Write a python code that prompts a user to input a 10-digit phone number....
​​​​​​​ASAP PLEASE Write a python code that prompts a user to input a 10-digit phone number. The output of your code is the phone number’s  area code, prefix and line number separated with a space on a single line e.g INPUT 2022745512 OUTPUT 202 274 5512 write a code that will prompt a user to input 5 integers and output its largest value. PYTHON e.g INPUT 2 4 6 1 3 OUTPUT Largest value is: 6
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT