switch cout print with a prameter c++
Sure! Here is a response without your personal words and sentences at the beginning and end of your reply:
#include <iostream>
void printNumber(int num) {
switch (num) {
case 1:
std::cout << "One";
break;
case 2:
std::cout << "Two";
break;
case 3:
std::cout << "Three";
break;
default:
std::cout << "Number out of range";
}
}
int main() {
int number = 2;
printNumber(number);
return 0;
}
Explanation: - The program includes the iostream library for input and output operations. - The function printNumber takes an integer parameter num. - Inside the function, a switch statement is used to evaluate the value of num. - If num is 1, "One" is printed to the standard output using cout. - If num is 2, "Two" is printed. - If num is 3, "Three" is printed. - If num doesn't match any of the cases, "Number out of range" is printed. - In the main function, an integer variable number is initialized with the value 2. - The printNumber function is called with the number as the argument. - The program returns 0 to indicate successful execution.