38.9k views
0 votes
Write a C++ program that reads a number and determines whether the number is positive, negative or zero using Switch operator method

1 Answer

1 vote

Answer:

#include <iostream>

using namespace std;

template <typename T> int sgn(T val) {

return (T(0) < val) - (val < T(0));

}

int main()

{

cout << "Enter a number: ";

int number;

cin >> number;

switch (sgn(number)) {

case -1: cout << "The number is negative";

break;

case 0: cout << "The number is zero";

break;

case 1: cout << "The number is positive";

break;

}

}

Step-by-step explanation:

You need the special sgn() function because a switch() statement can only choose from a list of discrete values (cases).

User King Midas
by
8.3k points

No related questions found

Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.