C/C++编程学习 - 第12周 ③ 判断数正负

题目链接

题目描述

小蒜蒜知道了整数分为正整数、负整数和零。
给定一个整数 N,判断其正负。

输入格式
一个整数 N(−109≤N≤109)。

输出格式
如果 N>0,输出"positive";
如果 N=0,输出"zero";
如果 N<0,输出"negative"。

Sample Input

1

Sample Output

positive

思路

判断输入的数是正数、负数还是零,对应输出"positive"、“negative"或"zero”。

C++代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	int n;
	while(cin >> n)
	{
    
    
		if(n > 0) cout << "positive" << endl;
		if(n == 0) cout << "zero" << endl;
		if(n < 0) cout << "negative" << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44826711/article/details/113120127