g. You have two JoRels tO De 24 hours delay. Once the homework is discussed in c
ID: 3721961 • Letter: G
Question
g. You have two JoRels tO De 24 hours delay. Once the homework is discussed in class no joker is accepted 1. Write a program that generates a set of numbers based on differed criteria. Use a switch statement to handle the user choice and for loop to generate the numbers. You need to use do/while to repeat the menus for the user. Sample Run Enter an interval (a, b): 1 10 1. Odd Numbers 2. Even Numbers 3. Numbers can be divided by 3 4. Exit Choose an option: 1 The new sequence is 1 3 5 79 1. Odd Numbers 2. Even Numbers 3. Numbers can be divided by 3 4. Exit Choose an option: 3 The new sequence is 3 6 9 1. Odd Numbers 2. Even Numbers 3. Numbers can be divided by 3 4. Exit Choose an option:4 Thank You!!Explanation / Answer
#include <iostream>
using namespace std;
int main() {
int i,a,b,option = 0;
cout<<" Enter an interval (a,b) : ";
cin>>a>>b;
do
{
cout<<" 1. Odd Numbers ";
cout<<" 2. Even Numbers ";
cout<<" 3. Numbers can be divided by 3 ";
cout<<" 4. Exit ";
cout<<" Choose an option : ";
cin>>option;
if(option == 4) // exit the loop
break;
cout<<" The new sequence is ";
switch(option)
{
case 1 : for(i= a;i<=b;i++)
if( i%2 != 0) // i is not divisible by 2, odd number
cout<<i<<" ";
break;
case 2 : for(i= a;i<=b;i++)
if( i%2 == 0) // i is divisible by 2, even number
cout<<i<<" ";
break;
case 3: for(i= a;i<=b;i++)
if( i%3 == 0)// divisible by 3
cout<<i<<" ";
break;
case 4: break;
default: cout<<" Invalid choice ";
break;
}
}while(option != 4);
return 0;
}
Output:
Enter an interval (a,b) :1 10
1. Odd Numbers
2. Even Numbers
3. Numbers can be divided by 3
4. Exit
Choose an option :1
The new sequence is 1 3 5 7 9
1. Odd Numbers
2. Even Numbers
3. Numbers can be divided by 3
4. Exit
Choose an option :2
The new sequence is 2 4 6 8 10
1. Odd Numbers
2. Even Numbers
3. Numbers can be divided by 3
4. Exit
Choose an option :3
The new sequence is 3 6 9
1. Odd Numbers
2. Even Numbers
3. Numbers can be divided by 3
4. Exit
Choose an option :4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.