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

write a java code. Consider a four digit number such as 6587. Split it at two...
write a java code. 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.
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.
Write a java program The last digit of a credit card number is the check digit,...
Write a java program The last digit of a credit card number is the check digit, which protects againts transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Follwing steps explains the algorithm in determining if a credit card number is a valid card.  Starting from the right most digit, form the sum of every other digit. For example, if...
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...
Java question - How can the code below be modified to accept multi digit input. String...
Java question - How can the code below be modified to accept multi digit input. String e = "1+9+8"; int r = e.charAt(0)-'0'; for (int i = 1; i < e.length(); i+=2){    if (e.charAt(i) == '+'){ r += e.charAt(i+1)-'0'; } else{ r -= e.charAt(i+1)-'0'; } The only built in String methods that can be used are lowercase(), length(), and charAt(). Arrays and parseInt() cannot be used. So we want to know how we can get an answer if a...
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?
(Prime Numbers) An integer is said to be prime if it is divisible by only 1...
(Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write pseudocode and function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and print all the prime numbers between 1 and 1000. Display 10 numbers per line. Twin primes...
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"
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT