Westonci.ca is the premier destination for reliable answers to your questions, brought to you by a community of experts. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately. Experience the convenience of finding accurate answers to your questions from knowledgeable experts on our platform.

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

Sagot :

tonb

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;

   }

}

Explanation:

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