Question

In: Computer Science

Design an algorithm to prompt user to enter a number. Determine the number is odd or...

Design an algorithm to prompt user to enter a number. Determine the number is odd or even with Case Logic.

Solutions

Expert Solution

Algorithm

step 1 : start

step 2 : Input the number from user , say num.

step 3: perform modulus operation of this number by 2 in switch case . ie switch( num % 2). (This will return only two case values either 0 or 1).

step 4: for case value 0 , print "the number is even " and then exit from program.

step 5 : for case value 1 , print "the number is odd " and then exit from program.

step 6 : stop

if you are willing to know its program , here it is , in c++

program

#include <iostream>

using namespace std;

int main()
{
int num;
cout<<"Enter a number:";
cin>>num;
switch(num%2)
{
case 0:cout<<num<<" is even number";
break;
case 1:cout<<num<<" is odd number";
break;

//default case is not required since this switch case only return either 0 or 1
}
}

output

Enter a number:5

5 is odd number

Enter a number:6

6 is even number

if you find this answer useful, please rate positive , thankyou


Related Solutions

In C++ Prompt user to enter two integers •Determine whether the first number is divisible by...
In C++ Prompt user to enter two integers •Determine whether the first number is divisible by the second. If the second number is zero, the program should not do division •Output the remainder of the two numbers •Compare the two integers, display the integers in non-decreasing order.
Prompt the user to enter an integer Then, prompt the user to enter a positive integer...
Prompt the user to enter an integer Then, prompt the user to enter a positive integer n2. Print out all the numbers that are entered after the last occurrence of n1 and whether each one is even or odd If n1 does not occur or there are no values after the last occurrence of n1, print out the message as indicated in the sample runs below. Sample: Enter n1: -2 Enter n2: 7 Enter 7 values: -2 3 3 -2...
Write a PowerShell script which will prompt user to enter the number of the day of...
Write a PowerShell script which will prompt user to enter the number of the day of the week (e.g. 1,2,3,4,5,6,7) and return the day of the week. (e.g. Sunday...etc.) (Hint: Sunday is the 1st day of the week).
4. Prompt user to enter a double number from console. Round this number so it will...
4. Prompt user to enter a double number from console. Round this number so it will keep 2 places after the decimal point. Display new number. For example, number 12.3579 will round to 12.36; number 12.4321 will round to 12.43 This is meant to be written in Java 14.0
The program will prompt the user to enter a number between [10 .. 20] (inclusive). After...
The program will prompt the user to enter a number between [10 .. 20] (inclusive). After verifying that the number is within the proper range, you will generate that many random numbers and place them into a dynamically allocated array. Your program will then display the contents of the array (with 4 numbers per line). Next, you will sort the values in descending order (from largest to smallest). Lastly, you will display the sorted numbers (again, with 4 per line)....
c++ code: prompt user to enter a number then add loop that checks a list of...
c++ code: prompt user to enter a number then add loop that checks a list of numbers then display numbers that are higher or equal to the user's input.
Prompt user to enter an integer number from console. Use 2 methods to multiply this number...
Prompt user to enter an integer number from console. Use 2 methods to multiply this number by factor 7, display result. This is required to be done in MIPS Assembly Language.
Prompt user to enter an integer number from console. Use 2 methods to multiply this number...
Prompt user to enter an integer number from console. Use 2 methods to multiply this number by factor 7, display result. This is required to be done in MIPS Assembly Language.
Program must be OOP design.   Prompt the user to enter 10 doubles and fill an array...
Program must be OOP design.   Prompt the user to enter 10 doubles and fill an array Print the array Fill an array with 10 random numbers and print the array Sort the array -- then print Delete index[ 3] of the array and re-print the array JAVA This is my program so far. I am having trouble with my delete element method. import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class ArrayPlay { Random rand = new Random();//assign random number variable...
C++ Prompt the user for the number of guests attending the event. Determine and report the...
C++ Prompt the user for the number of guests attending the event. Determine and report the number of large, medium, and small pizzas you need to order. For every 7 guests, order one large pizza. For every 3 guests left over, order one medium pizza. For every 1 guest left over, order one small pizza. Part 2 - Serving Size Compute and report the total area of pizza (in square inches) you need to purchase. Do not round these values....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT