Write an algorithm, draw corresponding flow chart and write an interactive program which prompts the user with the following options on the opening menu: 1) To accept two integers and check whether they are equal or not 2) To check whether a given number is even or odd 3) To check whether a given number is a positive number or a negative number 4) Quit Enter your choice:Note: Use SWITCH..CASE statement.
Question
Write an algorithm, draw corresponding flow chart and write an interactive program which prompts the user with the following options on the opening menu: 1) To accept two integers and check whether they are equal or not 2) To check whether a given number is even or odd 3) To check whether a given number is a positive number or a negative number 4) Quit Enter your choice:Note: Use SWITCH..CASE statement.
Solution
Algorithm:
- Start
- Display the menu options to the user
- Accept the user's choice
- Use a switch case statement to perform the following operations based on the user's choice:
- Case 1: Accept two integers from the user and check if they are equal or not. If they are equal, print "The numbers are equal". If they are not equal, print "The numbers are not equal".
- Case 2: Accept a number from the user and check if it is even or odd. If the number is even, print "The number is even". If the number is odd, print "The number is odd".
- Case 3: Accept a number from the user and check if it is positive or negative. If the number is positive, print "The number is positive". If the number is negative, print "The number is negative".
- Case 4: Quit the program.
- If the user enters a choice that is not in the menu, print "Invalid choice".
- End
Flowchart:
[Start] --> [Display Menu] --> [Accept User Choice] --> |Switch Case Statement| --> [Case 1: Accept two integers, Check if equal, Print result] --> [End] --> [Case 2: Accept number, Check if even or odd, Print result] --> [End] --> [Case 3: Accept number, Check if positive or negative, Print result] --> [End] --> [Case 4: Quit] --> [End] --> [Default: Print "Invalid choice"] --> [End]
Interactive Program (in Python):
while True:
print("Menu:")
print("1. Check if two integers are equal")
print("2. Check if a number is even or odd")
print("3. Check if a number is positive or negative")
print("4. Quit")
choice = int(input("Enter your choice: "))
if choice == 1:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if num1 == num2:
print("The numbers are equal")
else:
print("The numbers are not equal")
elif choice == 2:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("The number is even")
else:
print("The number is odd")
elif choice == 3:
num = int(input("Enter a number: "))
if num >= 0:
print("The number is positive")
else:
print("The number is negative")
elif choice == 4:
break
else:
print("Invalid choice")
This program will continue to run and display the menu until the user chooses to quit by entering 4.
Similar Questions
Create a program in Java that will accept two integers. If the sum of the integers is odd, display the higher integer, else, display the lower integer.
Create a program that prompts you for two numbers (positive or negative values should be possible), multiplies them and then adds a third number that also must be entered.
.Write a C program that prompts a user for two integers and the appropriate operator(i.e. +,-,/,*) then uses switch case to determine the appropriate operation.
Prompt two integers, a and b, and use logical operators to determine whether both values are non-zero. Display 'True' if both values are non-zero; otherwise, display 'False'.
Write a program to check whether a number is positive or negative. Take an integer, num, as an input from the console using input() function and print the result to the console as shown in the sample test case. Sample Input and Output 1: num: 02 positive Sample Input and Output 2: num: -52142 negative Sample Input and Output 3: num: 0 zero
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.