Question

C++ Give me an example of how you could use an if statement. The example should...

C++

Give me an example of how you could use an if statement. The example should include a summary (Algorithm) of the program, and how you would use the if statement. I am including an example aswell.

For example, suppose you had a program to determine if a number is positive, negative or zero. Read in a number and then use the if statement to determine if the number is positive (>0) or negative (<0) or zero (==0):

if (x>0)
//display positive message
else if (x<0)
   //display negative message
else
//display zero message

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <iomanip>
using namespace std;

int main(){
    int x;
    cout<<"Enter an integer: ";
    cin>>x;
    if (x>0)
        //display positive message
        cout<<x<<" is positive"<<endl;
    else if (x<0)
        //display negative message
        cout<<x<<" is negative"<<endl;
    else
        //display zero message
        cout<<x<<" is zero"<<endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ Give me an example of how you could use an if statement. The example should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT