At Westonci.ca, we make it easy to get the answers you need from a community of informed and experienced contributors. Discover in-depth answers to your questions from a wide network of experts on our user-friendly Q&A platform. 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).