Question

Write code to repeatedly ask the user for a number. If the number is positive and...

Write code to repeatedly ask the user for a number.
If the number is positive and even, print out a happy message.
If the number is positive and odd, print out a sad message.
If the number is negative, print out an angry message
If they enter zero, stop asking
If they enter something that isn't an integer, print out a message telling
them they are an idiot, but keep looping

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

Since you have not provided the language of your preference, I am providing the code in Java.

CODE

import java.util.InputMismatchException;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

while(true) {

int num;

System.out.println("Enter a number (0 to exit): ");

String inp = sc.nextLine();

try {

num = Integer.parseInt(inp);

if (num > 0) {

System.out.println("Today is a very good day!!");

} else if (num < 0) {

System.out.println("Today is a very gloomy day!!");

} else {

break;

}

} catch (NumberFormatException e ) {

System.out.println("You are an idiot....");

}

}

}

}

terminated> StackListBased [Java Appli Enter a number ce to exit)a hello are an idiot... Enter a number Ce to exit): 56 Today

Add a comment
Know the answer?
Add Answer to:
Write code to repeatedly ask the user for a number. If the number is positive and...
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 an assembly code that ask the user to enter a number and put it in...

    Write an assembly code that ask the user to enter a number and put it in variable called ''num1'' and check if number in variable num1 , is positive even or positive odd or negative even or negative odd If number is positive even , put the number into variable called '' positiveEven '' and show a message '' the number is positive even'' If number is negative even , put the number into variable called '' negativeEven'' and show...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • Write a program that prompts the user for positive integers, only stopping when a negative integer...

    Write a program that prompts the user for positive integers, only stopping when a negative integer or zero is given. The program should then print out how many of the positive integers were odd. Example: Enter a positive integer (0 or negative to stop): 9 Enter a positive integer (0 or negative to stop): 4 Enter a positive integer (0 or negative to stop): 7 Enter a positive integer (0 or negative to stop): -3 You entered 2 odd integers....

  • JAVA Ask the user for integers. If the user did not enter an integer, ask again....

    JAVA Ask the user for integers. If the user did not enter an integer, ask again. (Type Safe Input) Keep a running total of the intergers entered (sum). Once the user enters 0, stop looping. Print the sum of all the numbers. Do not use try-catch. EXAMPLE OUTPUT: Enter an integer, 0 to stop> [fasdfsa] Invalid input. Enter an integer, 0 to stop> [231.342] Invalid input. Enter an integer, 0 to stop> [1] Enter an integer, 0 to stop> [2]...

  • First, ask the user for a positive integer called “length”. If the user does not provide...

    First, ask the user for a positive integer called “length”. If the user does not provide an integer, or if the integer is not positive, then ask again, by asking inside a while loop. The acceptable integer is intended to indicate the user’s desired twist length. A twist is always three lines long, and made of slash characters and X characters. For example, if length is 25, then your code should print out this: Simple output: IXI XI XI XI...

  • [C++] Declare three Boolean flags and initialize it as with false value. Ask the user to...

    [C++] Declare three Boolean flags and initialize it as with false value. Ask the user to enter a number, if the number is even make the first flag true, if the number is odd make the second flag true, and if the value entered by the user was not a number make the third flag true. Print out the values of the three flags along with a proper message telling the user about what he/she entered.

  • Question 4 (3 mark) : Write a Java program to ask the user to input an...

    Question 4 (3 mark) : Write a Java program to ask the user to input an integer number from the keyboard. The output should indicate whether it is positive, zero, or negative, an even number or an odd number. REQUIREMENTS • Your code should ask user to input an integer number, then decide its sign and parity based on this input. • Your code should use if-else statement. Your code must work exactly as the specification and the output should...

  • Write code that prompts the user to input a number. The program should then output the...

    Write code that prompts the user to input a number. The program should then output the number and a message saying whether the number i "Positive, "Negative" or "Zero". It should further output whether the number is odd or even (zero is counted as even) For example:

  • In Java Write a program that will ask the user for integers and print if the...

    In Java Write a program that will ask the user for integers and print if the integer is positive, negative or zero. The program should continue asking for integer until the user enters a negative value.

  • 5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters...

    5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. ​ 1 largest = None 2 smallest = None 3 while True: 4...

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
ADVERTISEMENT