Question

In: Computer Science

write a java program that allows the user to insert two number consists of only 3...

write a java program that allows the user to insert two number consists of only 3 digits and print it and all a number between it but don't print any number that has digits equal to a number 4. like (211,212,213,215,...,350), if the user print number that not equal to 3 digits like ( 23,2298,1) print invalid number

Solutions

Expert Solution

Code screenshot:

Sample Outputs:

Code to copy:

import java.util.Scanner;
import java.text.DecimalFormat;

public class limit
{
public static void main(String args[])
{
// declare an object of Scanner s to take user input
Scanner s = new Scanner(System.in);
// a boolean to check whether any number without 4 as a digit exists in the range
boolean nonumbers = true;
// receive input for first number
System.out.print("Input first number: ");
int a = s.nextInt();
// receive input for second number
System.out.print("Input second number: ");
int b = s.nextInt();
// check if both number are 4 digits
if(a<=999 && a>=100 && b<=999 && b>=100)
{
// declare low and high
int low, high;
// check if a greater than b
if(a>b)
{
// then assign high as a and low as b
high = a;
low = b;
}
else
{
// otherwisem, assign high as b and low as a
low = a;
high = b;
}
// iterate from low to high
for(int i = low; i<=high; i++)
{
// preserve the value of current i to number
int number = i;
// loop till number greater than 0
while (number > 0)
{
// if the last digit of current value of number is 4
if (number % 10 == 4)
// Break loop
break;
// divide number by 10 to get next digit in next iteration
number = number / 10;
}
// if now number is 0 that means all digits of i is differant from 4
// because otherwise the loop would have break in between resulting
// in number having value above 0
if(number==0)
{
// then print value of i
System.out.print(i + ", ");
// reset nonumbers as we found a number withou 4 as digit
nonumbers = false;
}
}
}
else
{
// print invalid numbers
System.out.println("Invalid numbers. Please input two 3 digit numbers");
}
if(nonumbers == true)
// if nonumbers is still true, print no numbers in range
System.out.println("No numbers within range without 4 as a digit");
}
}


Related Solutions

Java Programming: Write a program that allows the user to compute the power of a number...
Java Programming: Write a program that allows the user to compute the power of a number or the product of two numbers. Your program should prompt the user for the following information: • Type of operation to perform • Two numbers (the arguments) for the operation to perform The program then outputs the following information: • The result of the mathematical operation selected. Menu to be displayed for the user: MATH TOOL 1. Compute the power of a number 2....
Please write the code JAVA Write a program that allows the user to enter the last...
Please write the code JAVA Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is: Candidate      Votes Received                                % of Total Votes...
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm Explain step by step
Write a java program that prompt the user to insert last term’s courses and their individual...
Write a java program that prompt the user to insert last term’s courses and their individual mark and store them in an appropriate multidimensional array.
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
in java Write a contacts database program that presents the user with a menu that allows...
in java Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...
Write a java code to (A) Enter the number of children, (B) Insert names from user,...
Write a java code to (A) Enter the number of children, (B) Insert names from user, (C) Print these names. upload source code with a screenshotfor output in one file with cover page
Write a program that allows the user to search the array to find the number entered
Write a program that allows the user to search the array to find the number entered
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT