当前位置:网站首页>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;
}
边栏推荐
- pyflink的安装和测试
- NEON优化:性能优化经验总结
- Data type of pytorch tensor
- C# 计算农历日期方法 2022
- [signal and system]
- 【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
- 从底层结构开始学习FPGA----FIFO IP的定制与测试
- How to evaluate load balancing performance parameters?
- [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)
- Vocabulary in Data Book
猜你喜欢

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

UI control telerik UI for WinForms new theme - vs2022 heuristic theme

Send template message via wechat official account

HMM notes

ClickHouse字段分组聚合、按照任意时间段粒度查询SQL

云呐|工单管理办法,如何开展工单管理

力扣1037. 有效的回旋镖

Installation of gazebo & connection with ROS

黑马笔记---异常处理

Typical problems of subnet division and super network construction
随机推荐
前置机是什么意思?主要作用是什么?与堡垒机有什么区别?
NEON优化:关于交叉存取与反向交叉存取
The difference between spin and sleep
HMM notes
云呐-工单管理制度及流程,工单管理规范
Docker method to install MySQL
taro3.*中使用 dva 入门级别的哦
Do you understand this patch of the interface control devaxpress WinForms skin editor?
[hfctf2020]babyupload session parsing engine
Neon Optimization: an instruction optimization case of matrix transpose
Realize incremental data synchronization between MySQL and ES
Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
What does security capability mean? What are the protection capabilities of different levels of ISO?
Wood extraction in Halcon
How to manage distributed teams?
Vocabulary in Data Book
The MySQL database in Alibaba cloud was attacked, and finally the data was found
黑马笔记---异常处理
Using the entry level of DVA in taro3.*
Sword finger offer II 035 Minimum time difference - quick sort plus data conversion