当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
THREE. AxesHelper is not a constructor
Atomic in golang, and cas Operations
【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
Taro2.* applet configuration sharing wechat circle of friends
子网划分、构造超网 典型题
Force buckle 1037 Effective boomerang
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Openjudge noi 1.7 08: character substitution
C# 计算农历日期方法 2022
NEON优化:矩阵转置的指令优化案例
The difference between spin and sleep
Neon Optimization: performance optimization FAQ QA
力扣1037. 有效的回旋镖
Installation of torch and torch vision in pytorch
NEON优化:关于交叉存取与反向交叉存取
实现mysql与ES的增量数据同步
接收用户输入,身高BMI体重指数检测小业务入门案例
C语言实例_2
Segmenttree
搭建【Redis in CentOS7.x】








