当前位置:网站首页>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;
}
边栏推荐
- THREE.AxesHelper is not a constructor
- 云呐|工单管理软件,工单管理软件APP
- The cost of returning tables in MySQL
- pytorch之数据类型tensor
- [牛客] B-完全平方数
- Maidong Internet won the bid of Beijing life insurance to boost customers' brand value
- Atomic in golang, and cas Operations
- 移植DAC芯片MCP4725驱动到NUC980
- NEON优化:矩阵转置的指令优化案例
- 【C语言进阶篇】指针的8道笔试题
猜你喜欢

2022 Google CTF SEGFAULT LABYRINTH wp

pytorch之数据类型tensor

HMM 笔记
![[牛客] B-完全平方数](/img/bd/0812b4fb1c4f6217ad5a0f3f3b8d5e.png)
[牛客] B-完全平方数

【案例分享】网络环路检测基本功能配置

动态规划思想《从入门到放弃》
![[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr](/img/4a/0dcc28f76ce99982f930c21d0d76c3.png)
[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr

云呐|工单管理软件,工单管理软件APP
![[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)](/img/40/dc45df3cd3ee7642277eff899bc6aa.png)
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)

JTAG debugging experience of arm bare board debugging
随机推荐
实现mysql与ES的增量数据同步
[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
Dynamic planning idea "from getting started to giving up"
2022 Google CTF SEGFAULT LABYRINTH wp
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
THREE.AxesHelper is not a constructor
NEON优化:关于交叉存取与反向交叉存取
taro3.*中使用 dva 入门级别的哦
ARM裸板调试之JTAG调试体验
[100 cases of JVM tuning practice] 04 - Method area tuning practice (Part 1)
Neon Optimization: an instruction optimization case of matrix transpose
How to evaluate load balancing performance parameters?
力扣1037. 有效的回旋镖
ARM裸板调试之JTAG原理
Atomic in golang, and cas Operations
2022 Google CTF SEGFAULT LABYRINTH wp
Docker method to install MySQL
[牛客] B-完全平方数
Neon Optimization: About Cross access and reverse cross access
Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000