当前位置:网站首页>C language instance_ five
C language instance_ five
2022-07-07 01:25:00 【Vicky__ three thousand and twenty-one】
1. Addition operation
Write an addition program , Input integer a,b, Output their and .
#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. Don't use the second 3 A variable , Realize the exchange of two numbers
No third variable , Realize the operation of exchanging two numbers .
#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. Define constants with macros
It is known that the unit price of an item is 30, The number of x. Find the total price of the goods . Define the unit price of the item with a macro .
#include<stdio.h>
int main(void)
{
int x,f;
int p=30;
scanf("%d",&x);
f = x*p;
printf("%d",f);
return 0;
}
4. Calculate the total score and average score
Programming to enter a student's five grades from the keyboard , Calculate the student's total score and average score .
#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. Find the area of the triangle
Programming requires a、b、c Is the area of a triangle with side length 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. Calculate the greatest common divisor of two positive integers
Programming to calculate the maximum common divisor of two positive integers . The prototype of the function for finding the maximum common divisor has been given , Please call the function in the main function , Output the greatest common divisor .
Running example of program :
12,3
3
#### Function prototype description
The prototype of the function for finding the greatest common divisor is as follows :
int MaxCommonFactor( int a, int b);
Return value : Returns the greatest common divisor ; If any of the input data does not meet the conditions , The return value is -1.
Parameters :a,b Are two integer numbers
#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;
}
边栏推荐
- [JS] obtain the N days before and after the current time or the n months before and after the current time (hour, minute, second, year, month, day)
- 云呐-工单管理制度及流程,工单管理规范
- Oracle: Practice of CDB restricting PDB resources
- C语言实例_3
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
- Gnet: notes on the use of a lightweight and high-performance go network framework
- Case development of landlord fighting game
- Let's see through the network i/o model from beginning to end
- Boot - Prometheus push gateway use
- Using the entry level of DVA in taro3.*
猜你喜欢
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
云呐|工单管理软件,工单管理软件APP
云呐-工单管理制度及流程,工单管理规范
AI 从代码中自动生成注释文档
Do you understand this patch of the interface control devaxpress WinForms skin editor?
【信号与系统】
The MySQL database in Alibaba cloud was attacked, and finally the data was found
Typical problems of subnet division and super network construction
ClickHouse字段分组聚合、按照任意时间段粒度查询SQL
LLDP兼容CDP功能配置
随机推荐
Neon Optimization: performance optimization FAQ QA
Implementation principle of waitgroup in golang
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Metauniverse urban legend 02: metaphor of the number one player
【案例分享】网络环路检测基本功能配置
Atomic in golang, and cas Operations
子网划分、构造超网 典型题
让我们,从头到尾,通透网络I/O模型
ARM裸板调试之JTAG调试体验
Lldp compatible CDP function configuration
736. Lisp 语法解析 : DFS 模拟题
golang中的WaitGroup实现原理
What are the differences between Oracle Linux and CentOS?
The cost of returning tables in MySQL
Spark TPCDS Data Gen
身体质量指数程序,入门写死的小程序项目
如何管理分布式团队?
【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
THREE. AxesHelper is not a constructor