In: Computer Science
Design an algorithm to prompt user to enter a number. Determine the number is odd or even with Case Logic.
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