1.两个整数四则运算
/*
输入两个整数,进行加、减、乘、除、求余运算
*/
#include <stdio.h>
int add(int a, int b) // 两个整数相加
{
return a + b;
}
int sub(int a, int b) // 两个整数相减
{
return a - b;
}
int multi(int a, int b) // 两个整数相乘
{
return a * b;
}
double div(int a, int b) // 两个整数相除
{
double ans = a * 1.0 / b;
return ans;
}
int mod(int a, int b) // 求余
{
return a % b;
}
int main()
{
int m, n;
scanf("%d %d", &m, &n);
printf("%d + %d = %d\n", m, n, add(m, n));
printf("%d - %d = %d\n", m, n, sub(m, n));
printf("%d * %d = %d\n", m, n, multi(m, n));
if(n != 0)
printf("%d / %d = %.2f\n", m, n, div(m, n));
printf("%d %% %d = %d", m, n, mod(m, n));
}
执行效果:
2.两个浮点数运算
/*
输入两个整浮点数,进行加、减、乘、除
*/
#include <stdio.h>
double add(double a, double b) // 两个浮点数相加
{
return a + b;
}
double sub(double a, double b) // 两个浮点数相减
{
return a - b;
}
double multi(double a, double b) // 两个浮点数相乘
{
return a * b;
}
double div(double a, double b) // 两个浮点数相除
{
float ans = a * 1.0 / b;
return ans;
}
int main()
{
double m, n;
scanf("%lf %lf", &m, &n);
printf("%lf + %lf = %.2lf\n", m, n, add(m, n));
printf("%lf - %lf = %.2lf\n", m, n, sub(m, n));
printf("%lf * %lf = %.2lf\n", m, n, multi(m, n));
printf("%lf / %lf = %.2lf\n", m, n, div(m, n));
}
执行效果:
3.字符转ASCII码
#include <stdio.h>
#include <string.h>
int main()
{
char c1[7] = {
'a', ' b', 'c', 'd', 'e', 'f', 'g'};
char c2[7] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G'};
for(int i=0; i < strlen(c1); i++)
{
printf("%c-->%d %c-->%d\n", c1[i], c1[i], c2[i], c2[i]);
}
return 0;
}
执行效果:
4.数值比较
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
int max = a > b ? a : b;
printf("%d", max);
return 0;
}
执行效果:
5.计算int/float/double/char的字节大小
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("size of int: %d bytes\n", sizeof(a));
printf("size of float: %d bytes\n", sizeof(b));
printf("size of double: %d bytes\n", sizeof(c));
printf("size of char: %d bytes\n", sizeof(d));
}
执行效果:
6.交换两个数的值
#include <stdio.h>
int main()
{
int a, b, temp;
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("%d %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("%d %d\n", a, b);
}
执行效果:
7.判断奇偶
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
if(n % 2 == 0)
printf("%d是偶数\n", n);
else
printf("%d是奇数\n", n);
}
执行效果:
8.判断元音/辅音
#include <stdio.h>
int main()
{
char c;
scanf("%c", &c);
if((c=='a' || c=='e' || c=='i' || c=='o' || c=='u') || (c=='A' || c=='E' || c=='I' || c=='O' || c=='U'))
{
printf("%c是元音\n", c);
}
else
{
printf("%c是辅音\n", c);
}
return 0;
}
执行效果示例:
9.判断三个数中的最大最小值
#include <stdio.h>
int main()
{
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int max = a, min = a;
if(max < b)
{
max = b;
}
if(max < c)
{
max = c;
}
if(min > b)
{
min = b;
}
if(min > c)
{
min = c;
}
printf("max = %d, min = %d", max, min);
}
执行效果:
10.判断闰年
/*
闰年需满足的条件:
1.能被4整除,不能被100整除;
2.能被400整除
*/
#include <stdio.h>
int main()
{
int year;
scanf("%d",&year);
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf("%d 是闰年\n", year);
}else{
printf("%d 不是闰年\n", year);
}
return 0;
}
执行效果:
11.判断字母
#include <stdio.h>
int main()
{
char c;
scanf("%c", &c);
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
printf("%c是字母\n", c);
}
else
{
printf("%c 不是字母", c);
}
return 0;
}
执行效果:
12.求1-n的和
#include <stdio.h>
int main()
{
int n, i, sum = 0;
scanf("%d", &n);
for(i=1; i<=n; i++)
{
sum += i;
}
printf("%d", sum);
return 0;
}
执行效果:
13.输出九九乘法表
#include <stdio.h>
int main(