当前位置:网站首页>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;
}
边栏推荐
- C语言实例_3
- Installation and testing of pyflink
- What are the differences between Oracle Linux and CentOS?
- go-zero微服务实战系列(九、极致优化秒杀性能)
- Neon Optimization: performance optimization FAQ QA
- [chip scheme design] pulse oximeter
- What does security capability mean? What are the protection capabilities of different levels of ISO?
- ESP Arduino (IV) PWM waveform control output
- Case development of landlord fighting game
- 云呐-工单管理制度及流程,工单管理规范
猜你喜欢
随机推荐
云呐|工单管理软件,工单管理软件APP
Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
ARM裸板调试之JTAG原理
UI control telerik UI for WinForms new theme - vs2022 heuristic theme
【C语言进阶篇】指针的8道笔试题
7.6 simulation summary
Openjudge noi 1.7 10: simple password
Data type of pytorch tensor
golang中的Mutex原理解析
2022 Google CTF SEGFAULT LABYRINTH wp
[chip scheme design] pulse oximeter
Implementation principle of waitgroup in golang
负载均衡性能参数如何测评?
Can the system hibernation file be deleted? How to delete the system hibernation file
Niuke cold training camp 6B (Freund has no green name level)
ESP Arduino (IV) PWM waveform control output
Match VIM from zero (0) -- Introduction to vimscript
go-zero微服务实战系列(九、极致优化秒杀性能)
Oracle:CDB限制PDB资源实战
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which