当前位置:网站首页>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;
}
边栏推荐
- Atomic in golang, and cas Operations
- Let's see through the network i/o model from beginning to end
- UI控件Telerik UI for WinForms新主题——VS2022启发式主题
- MySQL script batch queries all tables containing specified field types in the database
- Body mass index program, entry to write dead applet project
- 2022 Google CTF segfault Labyrinth WP
- What are the differences between Oracle Linux and CentOS?
- Analysis of mutex principle in golang
- 免费白嫖的图床对比
- Boot - Prometheus push gateway use
猜你喜欢
线段树(SegmentTree)
Dynamic planning idea "from getting started to giving up"
【案例分享】网络环路检测基本功能配置
Js逆向——捅了【马蜂窝】的ob混淆与加速乐
2022 Google CTF SEGFAULT LABYRINTH wp
Boot - Prometheus push gateway use
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
boot - prometheus-push gateway 使用
云呐|工单管理办法,如何开展工单管理
UI控件Telerik UI for WinForms新主题——VS2022启发式主题
随机推荐
2022 Google CTF SEGFAULT LABYRINTH wp
免费白嫖的图床对比
[batch dos-cmd command - summary and summary] - view or modify file attributes (attrib), view and modify file association types (Assoc, ftype)
Segmenttree
Byte P7 professional level explanation: common tools and test methods for interface testing, Freeman
Using the entry level of DVA in taro3.*
JTAG debugging experience of arm bare board debugging
"Exquisite store manager" youth entrepreneurship incubation camp - the first phase of Shunde market has been successfully completed!
力扣1037. 有效的回旋镖
Analysis of mutex principle in golang
从零开始匹配vim(0)——vimscript 简介
gnet: 一个轻量级且高性能的 Go 网络框架 使用笔记
Taro2.* applet configuration sharing wechat circle of friends
NEON优化:性能优化经验总结
Supersocket 1.6 creates a simple socket server with message length in the header
Spark TPCDS Data Gen
[case sharing] basic function configuration of network loop detection
【芯片方案设计】脉搏血氧仪
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
第三方跳转网站 出现 405 Method Not Allowed