1.温度计华氏转摄氏度
#include<stdio.h>
int main()
{
printf("请输入华氏温度:");
float f, c;
scanf_s("%f", &f);
c = (f - 32) /1.8;
printf("转为摄氏温度为:%f", c);
}
2.计算存款利息
#include<stdio.h>
int main()
{
float r1 = 0.1, r2 = 0.2, r3 = 0.3, p1, p2, p3;
p1 = 1000 * (1 + r1);
p2 = 1000 * (1 + r2);
p3 = 1000 * (1 + r3 / 2) * (1 + r3 / 2);
printf("p1=%f\np2=%f\np3=%f\n", p1, p2, p3);
return 0;
}
3.给出三角形变成,求面积
#include<stdio.h>
#include<math.h>
int main()
{
printf("请输入三角形的边长:\n");
float a, b, c;
scanf_s("%f%f%f", &a, &b, &c);
if (a + b < c || a + c < b || b + c < a)
{
printf("请重新输入!");
}
float p,s;
p = (a + b + c) * 0.5;
s = sqrt(p * (p - a) * (p - b) * (p - c));
printf("三角形面积为:%f", s);
return 0;
}
4.输入二元一次方程的参数,求二元一次方程的解
#include<stdio.h>
#include<math.h>
int main()
{
printf("please enter value:");
float a, b, c;
scanf_s("%f%f%f", &a, &b, &c);
float d;
d = b * b - 4 * a * c;
if (d < 0)
{
printf("please anew value:");
}
float p, q;
p = -(b / 2 * a);
q = sqrt(b * b - 4 * a * c);
float x1, x2;
x1 = p + q;
x2 = p - q;
printf("this result is:x1=%f,x2=%f", x1, x2);
return 0;
}
5.输入两个实数,由小到大输出
#include<stdio.h>
int main()
{
int a, b;
int c;
printf("please enter two number:");
scanf_s("%d%d", &a, &b);
if (a > b)
{
c = a;
a = b;
b = c;
}
printf("From small to large is:%5d%5d", a, b);
return 0;
}
6.输入三个数,按照由小到大输出
#include<stdio.h>
int main()
{
printf("please enter three value:");
float a, b, c;
float z;
scanf_s("%f%f%f", &a, &b, &c);
if (a > b)
{
z = a;
a = b;
b = a;
}
if (a > c)
{
z = a;
a = c;
c = z;
}
else if(b<c)
{
printf("from small to large is:%5f\t%5f\t%5f\t", a, b, c);
}
else
{
z = b;
b = c;
c = z;
}
printf("from small to large is:%5f\t%5f\t%5f\t", a, b, c);
return 0;
}
7.输入一个字符,如果是大写字母的话转为小写字母,否则直接输出
#include<stdio.h>
int main()
{
char a,b;
printf("please enter a char:");
a = getchar();
if ('A' <= a && a <= 'Z')
{
b = a + 32;
printf("this char Converted to :%c", b);
}
else
printf("this char is :%c", a);
return 0;
}
8.输入一个跃阶函数,输入一个x后输出相应的y
#include<stdio.h>
int yuejie(float a)
{
if (a > 0)
return 1;
if (a = 0)
return 2;
if (a < 0)
return 3;
}
int main()
{
float x;
printf("please a value:");
scanf_s("%f", &x);
float y = yuejie(x);
printf("this result is : %f", y);
return 0;
}
9.根据成绩划分不同等级(Switch)
#include<stdio.h>
int main()
{
char grade;
printf("please enter a grade:");
scanf_s("%c", &grade);
switch (grade)
{
case 'a':printf("score is above 85"); break;
case 'b':printf("score is 70~84\n"); break;
case 'c':printf("score is 60~69\n"); break;
case 'd':printf("score is below 60\n"); break;
default:printf("enter grade is error");
}
return 0;
}
10.计算1+2+3+4......+100(do while)完成
#include<stdio.h>
int main()
{
int sum=0, i=1;
do
{
sum += i;
i++;
} while (i < 101);
printf("The result of this equation is:%d", sum);
return 0;
}
11.1000名学生进行募捐,总数达到十万时结束,统计此时捐款人数以及平均每人捐款数目
#include<stdio.h>
#include<math.h>
int main()
{
int i,n=0;
float sum=0;
float ave,v;
for (i = 1; i < 1001; i++)
{
printf("please enter number of contributions:");
scanf_s("%f", &v);
sum += v;
n++;
if (sum > 100000)
{
break;
}
}
printf("all people have:%d\n", n);
ave = sum / n;
printf("average amount is:%f", ave);
return 0;
}
12.输出100-200间不能被3整除的数
#include<stdio.h>
int main()
{
int i;
printf("The number of eligible ones is:");
for (i = 100; i < 201; i++)
{
if (i % 3 == 0)
{
continue;
}
else
{
printf("%5d\t", i);
}
}
return 0;
}
13.输出4x5矩阵
#include<stdio.h>
int main()
{
int i, j;
int n = 0;
printf("please enter 4x5 array:");
for (i = 1; i <= 4; i++)
for (j = 1; j <= 5; j++)
{
if (n % 5 == 0)
{
printf("\n");
}
printf("%d\t", i * j);
n++;
}
return 0;
}
14.用公式求π的近似值,直到发现某一项的绝对值小于10-6为止
15.求斐波那契数列前四十个数
#include<stdio.h>
int main()
{
int arr[40];
int i,j;
printf("Fibonacci :\n");
for (i = 0; i < 40; i++)
{
if (i < 2)
{
arr[i] = 1;
j = i + 1;
printf("Item %d:", j);
printf("%-7d\n", arr[i]);
}
else
{
arr[i] = arr[i - 1] + arr[i - 2];
j = i + 1;
printf("Item %d:", j);
printf("%-7d\n", arr[i]);
}
}
return 0;
}
16.译密码,按以下规律将电文变成密码:将字母A变成字母E,a变成e,即变成其后的第4个字母,W变成A,X变成B,Y变成C,Z变成D。
#include<stdio.h>
int main()
{
char a;
int i;
printf("please enter char:");
scanf_s("%c", &a);
if ('V' >= a >= 'A' || 'v' >= a >= 'a')
{
a += 4;
printf("after translation:%c\n", a);
}
else if ('Z' >= a >= 'W' || 'z' >= a >= 'w');
{
a -= 22;
printf("after translation:%c\n", a);
}
return 0;
}
17.将一个二维数组行列对换,存入另一个数组
#include<stdio.h>
int main()
{
int a[2][3]={1,2,3,4,5,6}, b[3][2];
int i, j,n=1;
printf("arr a :\n");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++,n++)
{
printf("%d\t", a[i][j]);
if (n % 3 == 0)
{
printf("\n");
}
b[j][i] = a[i][j];
}
}
printf("\n");
printf("arr b :\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++,n++)
{
printf("%d\t", b[i][j]);
if (n % 2 == 0)
{
printf("\n");
}
}
}
return 0;
}
18.求一个3x4数组最大值,并计算行和列
#include<stdio.h>
int main()
{
int i, j;
int h,l, max;
int arr[3][4] = { {1,2,3,4},{9,6,7,5,},{-1,-2,-3,-4} };
printf("3行4列arr:\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
printf("%5d\t", arr[i][j]);
}
printf("\n");
}
max = arr[0][0];
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
if (arr[i][j] > max)
{
max = arr[i][j];
h = i;
l = j;
}
}
printf("max=%d\n", max);
printf("行号是:%d\n", h + 1);
printf("列号是:%d\n", l + 1);
return 0;
}
19.有三个字符串,求最大值
#include<stdio.h>
#include<string.h>
int main()
{
char a[5],b[5],c[5];
printf("please char:\n");
gets(a);
gets(b);
gets(c);
if ((strcmp(a, b)) > 0 && (strcmp(a, c)) > 0)
{
printf("the biggest is:");
puts(a);
}
else if ((strcmp(b, a)) > 0 && (strcmp(b, c)) > 0)
{
printf("the biggest is:");
puts(b);
}
else if ((strcmp(c, b)) > 0 && (strcmp(c, a)) > 0)
{
printf("the biggest is:");
puts(c);
}
return 0;
}
20.输入一串单词,求单词数量
#include<stdio.h>
int main()
{
char a[100];
char* ch = a;
int b = 0;
int word = 0;
printf("please input the words:\n");
gets(a);
for (; *ch != '\0'; ++ch)
{
if (*ch == ' ')
{
word = 0;
}
else if (word == 0)
{
b++;
word = 1;
}
}
printf("there are %d words\n", b);
return 0;
}
21.输入两个整数,输出最大者(使用自建函数)
#include<stdio.h>
void main()
{
printf("please enter two number:");
int max(int x, int y);
int a, b, c;
scanf_s("%d,%d", &a, &b);
c = max(a, b);
printf("Max is %d\n", c);
}
int max(int x, int y)
{
int z;
z = x > y ? x : y;
return(z);
}
22.输入两个实数,用一个函数求和
#include<stdio.h>
int main()
{
float summ(float x, float y);
float a, b, c;
printf("请输入两个数:\n");
scanf_s("%f%f", &a, &b);
c = summ(a, b);
printf("sum=%f\n", c);
return 0;
}
float summ(float x, float y)
{
float z;
z = x + y;
return z;
}
23.输入四个整数,用函数的嵌套找出其中最大值
#include<stdio.h>
int main()
{
int max4(int a, int b, int c, int d);
int a, b, c, d, max;
printf("please enter four number:");
scanf_s("%d%d%d%d", &a, &b, &c, &d);
max = max4(a, b, c, d);
printf("max =%d\n", max);
return 0;
}
int max4(int a, int b, int c, int d)
{
int max2(int a, int b);
int z;
z = max2(a, b);
z = max2(z, c);
z = max2(z, d);
return z;
}
int max2(int a, int b)
{
return a > b ? a : b;
}
24.五个人,第五个人比第四个人大两岁,以此类推,第一个人十岁,求第五个人年龄
#include<stdio.h>
int age(int n)
{
if (n == 1)
{
return 10;
}
else
{
return age(n - 1) + 2;
}
}
int main()
{
int n = age(5);
printf("第五个人年龄为:%d", n);
return 0;
}
25.递归求n!
#include<stdio.h>
int jiecheng(int x)
{
if (x == 1 || x == 0)
{
return 1;
}
else
{
return jiecheng(x - 1) * x;
}
}
int main()
{
int n;
printf("请输入你想计算的阶乘数:");
scanf_s("%d", &n);
int v = jiecheng(n);
printf("阶乘为:%d", v);
return 0;
}
26.一个一维数组放了十个学生成绩,求平均分,最高和最低分
#include<stdio.h>
int main()
{
float average(float arr[10]);
float score[10], aver;
int i;
printf("请输入10学生的成绩:\n");
for (i = 0; i < 10; i++)
{
scanf_s("%f", &score[i]);
}
aver = average(score);
return 0;
}
float average(float arr[10])
{
int i;
int max, small;
int a=arr[0];
float aver, sum = 0;
for (i = 0; i < 10; i++)
{
sum += arr[i];
}
aver = sum / 10;
for (i = 1; i < 10; i++)
{
if (a > arr[i])
{
max = a;
small = arr[i];
a = arr[i];
}
else
{
max = arr[i];
small = a;
a = arr[i];
}
}
printf("最大值为:%d\n最小值为:%d\n平均分为:%f", max, small, aver);
return 0;
}
27.输出1到5的阶乘值。(使用静态局部变量static)
3include<stdio.h>
int main()
{
int jiecheng(int x);
int i, n;
for (i = 1; i <= 5; i++)
{
printf("%d! = %d\n", i, jiecheng(i));
}
return 0;
}
int jiecheng(int x)
{
static int n = 1;
n = n * x;
return n;
}
28.输入a和b两个整数,按先大后小的顺序输出a和b(以指针作为函数参数)
#include<stdio.h>
int main() {
int big(int* p1, int* p2);
int a, b;
int* p1, * p2;
printf("请输入两个整数:");
scanf_s("%d%d", &a, &b);
p1 = &a;
p2 = &b;
if (a < b)
{
big(p1, p2);
printf("max=%d,min=%d", a, b);
}
else
printf("max=%d,min=%d", a, b);
return 0;
}
int big(int* p1, int* p2)
{
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
return 0;
}
29.输入3个整数a,b,c,要求按由大到小的顺序将它们输出。(用函数实现)
#include<stdio.h>
int main()
{
int a, b, c;
int max3(int x, int y, int z);
printf("请输入三个数:");
scanf_s("%d%d%d", &a, &b, &c);
max3(a, b, c);
return 0;
}
int max3(int x, int y, int z)
{
int max2(int x, int y);
if (x > y && x > z)
{
int max2(y, z);
printf("max=%d\nmid=%d\nsmall=%d\n", x,y,z);
}
else if (y > x && y > z)
{
int m;
m = x;
x = y;
y = m;
int max2(y, z);
printf("max=%d\nmid=%d\nsmall=%d\n", x, y, z);
}
else if (z > x && z > y)
{
int n;
n = x;
x = z;
z = n;
int max2(y, z);
printf("max=%d\nmid=%d\nsmall=%d\n", x, y, z);
}
return 0;
}
30.将数组a中n个整数按相反顺序存放。(用数组名作函数参数)
#include<stdio.h>
int main()
{
int i, a[10] = { 1,2,3,4,5,6,7,8,9,10 };
printf("输出原数组\n");
for (i = 0; i < 10; i++)
{
printf("%3d", a[i]);
}
printf("\n");
int ni(int a[], int n);
ni(a, 10);
for (i = 0; i < 10; i++)
{
printf("%3d", a[i]);
}
printf("\n");
return 0;
}
int ni(int a[], int n)
{
int i, k;
for (i = 0; i < n/2; i++)
{
k = a[i];
a[i] = a[n - 1 - i];
a[n - 1 - i] = k;
}
return 0;
}
31.把学生信息(包括学号、姓名、性别、住址)放在一个结构体中, 输入两个学生的学号、姓名和成绩,输出成绩较高的学生的学号、姓名和成绩
#include<stdio.h>
struct xinxi
{
int num;
char name[50];
char sex;
char add[100];
int score;
};
int main()
{
struct xinxi s[2] = { {101,"li","m","baoji",99},{102,"zhang","w","qishan",98} };
printf("成绩高的人为:\n");
if (s[0].score > s[1].score)
{
printf("学号:%d姓名:%s成绩:%d", s[0].num, s[0].name, s[0].score);
}
else if (s[1].score > s[0].score)
{
printf("学号:%d姓名:%s成绩:%d", s[1].num, s[1].name, s[1].score);
}
return 0;
}
32.有3个候选人,每个选民只能投票选一人,要求编一个统计选票的程序,先后输入被选人的名字,最后输出各人得票结果。
#include<stdio.h>
#include<string.h>
struct jipiao
{
char name[20];
int sum;
};
int main()
{
int i, j;
struct jipiao leader[3] = { {"li",0},{"zhang",0},{"sun",0} };
char leader_name[5];
for (i = 1; i <= 5; i++)
{
gets(leader_name);
for (j = 0; j < 3; j++)
{
if (strcmp(leader_name, leader[j].name) == 0)
leader[j].sum++;
}
}
printf("得票结果:\n");
for (i = 0; i < 3; i++)
{
printf("%s:%5d\n", leader[i].name, leader[i].sum);
}
return 0;
}
33.有n个学生的信息(包括学号、姓名、成绩),要求按照成绩的高低顺序输出各学生的信息。
#include<stdio.h>
struct xinxi
{
int num;
char name[50];
int score;
}s[3] = { {101,"li",99},{102,"zhang",98},{103,"wang",97} };
int main()
{
int i;
int max2(int x, int y);
if (s[0].score > s[1].score && s[0].score > s[2].score)
{
printf("成绩顺序由大到小为:\n");
printf("学号:%d姓名:%s成绩:%d", s[0].num, s[0].name, s[0].score);
printf("学号:%d姓名:%s成绩:%d", s[1].num, s[1].name, s[1].score);
printf("学号:%d姓名:%s成绩:%d", s[2].num, s[2].name, s[2].score);
}
if (s[1].score > s[0].score && s[1].score > s[2].score)
{
i = s[0].score;
s[0].score = s[1].score;
s[1].score = i;
max2(s[1].score, s[2].score);
}
if (s[2].score > s[0].score && s[2].score > s[1].score)
{
i = s[0].score;
s[0].score = s[2].score;
s[2].score = i;
max2(s[1].score, s[2].score);
}
return 0;
}
max2(int x, int y)
{
int k;
if (x > y)
{
printf("成绩顺序由大到小为:\n");
printf("学号:%d姓名:%s成绩:%d", s[0].num, s[0].name, s[0].score);
printf("学号:%d姓名:%s成绩:%d", s[1].num, s[1].name, s[1].score);
printf("学号:%d姓名:%s成绩:%d", s[2].num, s[2].name, s[2].score);
}
if (x < y)
{
k = x;
x = y;
y = k;
printf("成绩顺序由大到小为:\n");
printf("学号:%d姓名:%s成绩:%d", s[0].num, s[0].name, s[0].score);
printf("学号:%d姓名:%s成绩:%d", s[1].num, s[1].name, s[1].score);
printf("学号:%d姓名:%s成绩:%d", s[2].num, s[2].name, s[2].score);
}
return 0;
}