Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.Write a C++ program making use of a switch statement but without the explicit

ID: 3628832 • Letter: 1

Question

1.Write a C++ program making use of a switch statement but without the explicit use of any if statements. The program reads in an integer number from the user and then display accordingly:

i. "The value is 1", if the number is equal to 1
ii. "The value is 2", if the number is equal to 2
iii. "The value is 3", if the number is equal to 3
iv. "The value is between 4 and 9", if the number is between 4 and 9 (inclusive)
v. "The value is <= 0 or >=10", if otherwise


2.Write a C++ program with a while loop structure. This program will reads a list of non-zero integers from the standard input device, and calculates and displays the difference between the largest and the smallest of these values. The program will end when a 0 value is read. This final value 0 will not participate in the required calculation.

Explanation / Answer

#include<iostream>
using namespace std;
int main()
{
int a;
cout << " enter a integer ";
cin >>a;

cout << endl;
switch(a)
{
case 1: cout << "The value is 1"; break;
case 2: cout << "The value is 2"; break;
case 3: cout << "The value is 3"; break;
case 4:
case 5:
case 6:
case 7:
case 8:
case 9: cout << "The value is between 4 and 9"; break;
default: cout << "The value is <= 0 or >=10"; break;
}
return 0;
}

2)

#include<iostream>
using namespace std;
int main()
{
int max;
int min;
int a;
cout << " Enter Value of a ";
cin >> a;
max = min = a;
cout << " Enter value of a and 0 to end ";
cin >> a;
while ( a!=0)
{
if(a>max) max = a;
if(a<min) min = a;
cout << " Enter value of a and 0 to end ";
cin >> a;
}

cout << " Difference between largest and smallest given by " << (max-min) <<endl;

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote