当前位置:网站首页>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;
}
边栏推荐
- 力扣1037. 有效的回旋镖
- Receive user input, height BMI, BMI detection small business entry case
- 动态规划思想《从入门到放弃》
- Tensorflow 1.14 specify GPU running settings
- Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000
- Neon Optimization: About Cross access and reverse cross access
- Let's see through the network i/o model from beginning to end
- Force buckle 1037 Effective boomerang
- 身体质量指数程序,入门写死的小程序项目
- Taro 小程序开启wxml代码压缩
猜你喜欢
子网划分、构造超网 典型题
Part VI, STM32 pulse width modulation (PWM) programming
【信号与系统】
Do you understand this patch of the interface control devaxpress WinForms skin editor?
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Build your own website (17)
C language - array
golang中的Mutex原理解析
线段树(SegmentTree)
[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
随机推荐
405 method not allowed appears when the third party jumps to the website
云呐|工单管理办法,如何开展工单管理
Implementation principle of waitgroup in golang
Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
树莓派/arm设备上安装火狐Firefox浏览器
让我们,从头到尾,通透网络I/O模型
Neon Optimization: an instruction optimization case of matrix transpose
阿里云中mysql数据库被攻击了,最终数据找回来了
[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
Windows installation mysql8 (5 minutes)
NEON优化:性能优化常见问题QA
Anfulai embedded weekly report no. 272: 2022.06.27--2022.07.03
Tensorflow GPU installation
THREE.AxesHelper is not a constructor
JTAG principle of arm bare board debugging
Using the entry level of DVA in taro3.*
Installation and testing of pyflink
子网划分、构造超网 典型题
界面控件DevExpress WinForms皮肤编辑器的这个补丁,你了解了吗?