Question

Write in Java Write code which checks validity of a 3-digit positive integer entered by the...

Write in Java

Write code which checks validity of a 3-digit positive integer entered by the user.
The number is considered valid (true) if the sum of the first two digits is less than the last.
Otherwise it is invalid (false).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Java program to check the validity of 3-digit number

public class ThreeDigitValidity {  

   public static void main(String[] args) {
  
       Scanner scan = new Scanner(System.in);
       int num;
       // Input of number
       System.out.print("Enter a 3-digit number: ");
       num = scan.nextInt();
       if(num > 99 && num < 1000) /// check that the number entered is 3-digit
       {
          int temp = num;
           // get the last digit
           int last = temp%10;
           temp = temp/10; // remove the last digit from the number
           int sum = 0;
           // loop to calculate the sum of first 2 digit
           while(temp > 0)
          {
               sum += temp%10;
               temp = temp/10;
           }
         
           // if sum of first 2 digit < last digit, then valid else invalid
           if(sum < last)
               System.out.println(num+" is valid");
          else
               System.out.println(num+" is not valid");
              
}
  }
}
//end of program

Output:

Enter a 3-digit number: 105 105 is valid

Enter a 3-digit number: 234 234 is not valid

Add a comment
Know the answer?
Add Answer to:
Write in Java Write code which checks validity of a 3-digit positive integer entered by the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that uses a random number generator to generate a two digit positive integer...

    Write a program that uses a random number generator to generate a two digit positive integer and allows the user to perform one or more of the following operations: a. Double the number. b. Reverse the digits of the number. c. Raise the number to the power of 2, 3, or 4. d. Sum the digits of the number. e. If the number is a two digit number, then raise the first digit to the power of the second digit....

  • In Java Write a method named changeEvens which accepts a 4-digit positive integer and substitutes even...

    In Java Write a method named changeEvens which accepts a 4-digit positive integer and substitutes even digits with zero and returns a new integer. The new integer could have less digits. If the given integer is not a 4-digit integer or it is a negative integer the method returns zero. Examples: changeEvens(1000)-> 1000 changeEvens(-1000)-> 0 changeEvens(8764)-> 700 changeEvens(5829)-> 5009 changeEvens(1012)-> 1010

  • Write an application in java that reads in a five-digit integer and determines whether it is...

    Write an application in java that reads in a five-digit integer and determines whether it is a palindrome. If the number is not five digits long, display an error message and allow the user to enter a new value. Tips: 1. Determine the number of digits in the value input by the user , i.e. prompt the user to enter the number of digits of the number. Use a while loop to determine whether the user input contains the proper...

  • C++ Write a program that checks whether the first digit of the entered number equals the...

    C++ Write a program that checks whether the first digit of the entered number equals the last digit.

  • Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program...

    Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program that works as described in the following scenario: The user enters a credit card number. The program displays whether the credit card number is valid or invalid. Here are two sample runs: Enter a credit card number as a long integer: 4388576018410707 4388576018410707 is valid Enter a credit card number as a long integer: 4388576018402626 4388576018402626 is invalid To check the validity of the...

  • Write a java application, Date, that reads an 8-digit integer value (indicating a date) from the...

    Write a java application, Date, that reads an 8-digit integer value (indicating a date) from the keyboard; the first two digits indicate the month, the second two digits represent the day and the last 4 digits represent the year. After calculating the month, the application will implement a switch statement to output the month. After displaying the date, the application will implement a nested if else statement to output the appropriate century. i.e Prev17th century, 18th century, 19th century, 20th...

  • java programming. One. Write a method public boolean hasOnlyoddDigits(int n) that returns true if its parameter...

    java programming. One. Write a method public boolean hasOnlyoddDigits(int n) that returns true if its parameter n contains only odd digits (1, 3, 5, 7, or 9), and returns false otherwise. Zero is counted as an even digit whenever it appears inside the number. Remember that for a positive integer n, the expression (n % 10) gives its last digit, and the expression (n / 10) gives its other digits. Correct answer true false false false 357199 7540573 97531000

  • Write java program to check that a (16-digit) credit card number is valid. A valid credit...

    Write java program to check that a (16-digit) credit card number is valid. A valid credit card number will yield a result divisible by 10 when you: Form the sum of all digits. Add to that sum every second digit, starting with the second digit from the right. Then add the number of digits in the second step that are greater than four. The result should be divisible by 10. For example, consider the number 4012 8888 8888 1881. The...

  • Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and re...

    Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and return their total. The main method then prints the digits total with proper label as shown below Method Sum )is of type integer and takes an integer value. The method recursively adds up the digits and returns their total. Document your code and use proper prompts for...

  • Programming Problem HW1 Question 1

    Write a function createNum() that takes as a parameter an integer n, builds an n -digit integer using input from the user, and returns the integer. The function repeatedly prompts the user to enter single, positive (>0) digits and creates a number out of them, with the first valid digit entered being the most significant and the last valid digit entered being the least significant. If the user enters a zero, a negative number, more than one digit, or anything...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT