当前位置:网站首页>C语言实例_5
C语言实例_5
2022-07-06 17:39:00 【Vicky__3021】
1.加法运算
写一个加法程序,输入整数a,b,输出他们的和。
#include<stdio.h>
int main(void)
{
int a,b,c;
scanf("%d,%d", &a,&b);
c = a+b;
printf("%d+%d=%d\n",a,b,c);
return 0;
}
2.不使用第3个变量,实现两个数的对调
不用第三个变量,实现将两个数进行对调的操作。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("a=%d b=%d\n",a,b);
a=a^b;
b=a^b;
a=a^b;
printf("a=%d b=%d\n",a,b);
return 0;
}
3.用宏定义常量
已知某物品单价为30,数量为x。求商品的总价钱。用宏定义物品的单价。
#include<stdio.h>
int main(void)
{
int x,f;
int p=30;
scanf("%d",&x);
f = x*p;
printf("%d",f);
return 0;
}
4.计算总成绩和平均成绩
编程求从键盘上输入一个学生的五门成绩,计算出该学生的总成绩和平均成绩。
#include<stdio.h>
int main(void)
{
int a,b,c,d,e;
int m;
double n;
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
m=a+b+c+d+e;
n=m/5.0;
printf("%d %.2lf",m,n);
return 0;
}
5.求三角形的面积
编程求以a、b、c为边长的三角形的面积area。
#include<stdio.h>
#include<math.h>
int main(void)
{
double a,b,c;
double s,area;
scanf("%lf %lf %lf",&a,&b,&c);
s=(a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("%.3lf",area);
/*********End**********/
return 0;
}
6.计算两个正整数的最大公约数
编程计算两个正整数的最大公约数。其中求最大公约数的函数原型已经给出,请在主函数中编程调用函数,输出最大公约数。
程序的运行示例:
12,3
3
####函数原型说明
求最大公约数的函数原型如下:
int MaxCommonFactor( int a, int b);
返回值:返回的是最大公约数;若输入的数据有任意一个不满足条件,返回值是-1。
参数:a,b是两个整型数
#include<stdio.h>
int MaxCommonFactor( int a, int b)
{
int c;
if(a<=0||b<=0)
return -1;
while(b!=0)
{
c=a%b;
a=b;
b=c;
}
return a;
}
int main(void)
{
int a,b;
scanf("%d,%d",&a,&b);
int x=MaxCommonFactor(a,b);
printf("%d",x);
return 0;
}
边栏推荐
- BFS realizes breadth first traversal of adjacency matrix (with examples)
- Do you understand this patch of the interface control devaxpress WinForms skin editor?
- 分享一个通用的so动态库的编译方法
- Make a simple graphical interface with Tkinter
- MySQL中回表的代价
- mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
- Using the entry level of DVA in taro3.*
- 黑马笔记---异常处理
- golang中的WaitGroup实现原理
- pyflink的安装和测试
猜你喜欢

BFS realizes breadth first traversal of adjacency matrix (with examples)

1123. 最深叶节点的最近公共祖先

身体质量指数程序,入门写死的小程序项目

阿里云中mysql数据库被攻击了,最终数据找回来了

系统休眠文件可以删除吗 系统休眠文件怎么删除

JTAG principle of arm bare board debugging

云呐-工单管理制度及流程,工单管理规范

Installation of gazebo & connection with ROS
![[case sharing] basic function configuration of network loop detection](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[case sharing] basic function configuration of network loop detection
![[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error](/img/a5/41d4cbc070d421093323dc189a05cf.png)
[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error
随机推荐
The MySQL database in Alibaba cloud was attacked, and finally the data was found
Installation and testing of pyflink
Do you understand this patch of the interface control devaxpress WinForms skin editor?
Supersocket 1.6 creates a simple socket server with message length in the header
NEON优化:性能优化经验总结
BFS realizes breadth first traversal of adjacency matrix (with examples)
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
黑马笔记---异常处理
go-zero微服务实战系列(九、极致优化秒杀性能)
安全保护能力是什么意思?等保不同级别保护能力分别是怎样?
C language - array
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
UI control telerik UI for WinForms new theme - vs2022 heuristic theme
树莓派/arm设备上安装火狐Firefox浏览器
Niuke cold training camp 6B (Freund has no green name level)
Installation of torch and torch vision in pytorch
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
How to manage distributed teams?
Meet in the middle
Taro 小程序开启wxml代码压缩