当前位置:网站首页>C语言刷题 | 输入一个数输出对应的值(13)
C语言刷题 | 输入一个数输出对应的值(13)
2022-06-22 09:10:00 【C语言小火车】
例13:C语言实现当num<0时,输出result=-1;当num>0时,result=1;当num=0时,result=0。编写一个C程序,输入一个num值,要求输出相应的result值。
解题思路:用if语句检查num的值,根据num的值决定赋予result的值。由于result的可能性不是两个而是3个,因此不可能只用一个简单的if语句就可以实现,,需要用到if语句的嵌套。
源代码演示:
#include<stdio.h>//头文件
int main()//主函数
{
int num,result; //定义整型变量
printf("请输入num:");//提示语句
scanf("%d",&num);//键盘输入
if(num<0) //判断条件小于0时
{
result=-1;
}
else if(num==0)//判断条件等于0时
{
result=0;
}
else //判断条件大于0时
{
result=1;
}
printf("%d\n",result);//输出结果
return 0;//函数返回值为0
}编译结果:
请输入num:4
1
--------------------------------
Process exited after 4.581 seconds with return value 0
请按任意键继续. . .为了使程序更清晰,易读,写程序时对选择结构和循环结构应采用锯齿形的缩进形式。
边栏推荐
- 版本问题导致“无法定位程序输入点OPENSSL_sk_new_reserve于动态链接库C:\Users...\libssl-1_1-x64.dll”
- Read all files under the folder in the jar package
- 报告:在技术领域,男性更有可能获得工作面试的机会
- 【Tensorboard】所有雷区都踩,一文解决你所有问题
- copy_from_user和copy_to_user
- Interview shock 59: can there be multiple auto increment columns in a table?
- DOM编程
- Local visualization method of xshell remote server tensorboard/visdom [one-step test is effective]
- traefik ingress实践
- pip3 install xxx报错:Command 'lsb_release -a' returned non-zero exit status 1.
猜你喜欢

项目优化+上线(掌握了么?)

Solidity from introduction to practice (III)

XSS vulnerability attack
![[detailed explanation] point multiplication and cross multiplication of neural network matrix (pytorch version)](/img/17/e964216d81555d0a1203ee51d4698e.png)
[detailed explanation] point multiplication and cross multiplication of neural network matrix (pytorch version)

Use record of rabbit nest

kali木马入侵win7系统

景联文科技:机器学习AI数据集产品汇总(三)

np. Arange and np Linspace nuances (data overflow problem)

File upload attack and protection

Instanceinforeplicator class of Eureka (service registration assistant)
随机推荐
09 combination mode
yolov5 export Gpu推理模型导出
traefik ingress实践
【目标检测】|检测错误机制 Why Object Detectors Fail: Investigating the Influence of the Dataset
PHP output color image with specified height and width for web page background image
Continuous training on tensorflow breakpoint (principle + code explanation)
Interview shock 59: can there be multiple auto increment columns in a table?
User insight into the video industry in January 2022: active users began to pick up under the influence of holidays
list_head
Share 830 spider IP segments (by analyzing 1g logs)
kali木马入侵win7系统
两个线程各执行100次i++,得到的可能值
The third-party libraries commonly used in golang development are not the most complete, but more complete
【路径规划】辅助点与多段贝塞尔平滑RRT
[network security officer] an attack technology that needs to be understood - high hidden and high persistent threats
[target detection] | detection error mechanism why object detectors fail: investigating the influence of the dataset
三个不同线程顺序打印ABC十种写法【并发编程JUC】
关于EasyPoi导入Excel文件二级表头数据时@ExcelEntity实体类里的第一列数据为null的这档事
Flask blog practice - user management
Classification of SQL language