顺序结构
1、编写程序完成温度转换。
要求程序运行时从键盘输入任意的华氏温度值(浮点型),将其转换成摄氏温 度值并输出(结果保留2位小数)。提示:C:摄氏 F:华氏。C = 5/9*(F-32)。
#include<stdio.h>
int main()
{
double c,f;
scanf("%lf",&f);
c=5/9.0*(f-32);
printf("%.2lf",c);
return 0;
}
2、编写程序完成大小写英文字母的转换。
要求程序运行时,从键盘输入一个大写英文字母,输出该大写 英文字母对应的小写英文字母以及该小写英文字母后续的一个字符。
#include<stdio.h>
int main()
{
char c;
c=getchar();
putchar(c+32);
putchar(c+33);
return 0;
}
#include <stdio.h>
int main() {
char ch;
printf("请输入一个大写英文字母: ");
scanf("%c", &ch);
if(ch >= 'A' && ch <= 'Z'){
printf("%c的对应小写字母为%c,后面一个字母为%c\n", ch, ch + ('a' - 'A'), ch + ('a' - 'A')+ 1);
} else {
printf("输入的字符不是大写英文字母!\n");
}
return 0;
}
3、编写程序完成进制转换。
要求程序运行时,从键盘输入一个小于256的十进制整数,输出该数对应的 二进制数(用八位表示)如输入数据为:123 输出结果为:01111011
方法一:
#include <stdio.h>
int main()
{
int decimalnum;
printf("请输入一个八位的十进制数:");
scanf("%d", &decimalnum);
int i = 0;
int binary[8];
for (int j = 0; j < 8; j++) {
binary[j] = 0;
}
printf("转化为八位二进制为:");
while (decimalnum > 0)
{
binary[i] = (decimalnum & 1);
decimalnum >>= 1;
i++;
}
for (int j = 7 ; j >= 0; j--) {
printf("%d", binary[j]);
}
return 0;
}
方法二:
#include<stdio.h>
int main()
{
int a=0;
int d = 0;
int b = 0, c = 0;
scanf_s("%d", &a);
while (a > 0) {
b = a % 2;
c = c * 10 + b;
a = a / 2;
}
while (c != 0) {
d = d * 10 + c % 10;
c /= 10;
}
printf("%08d", d);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a;
int b0,b1,b2,b3,b4,b5,b6,b7;
scanf("%d",&a);
b0=a%2;
b1=a/2%2;
b2=a/2/2%2;
b3=a/2/2/2%2;
b4=a/2/2/2/2%2;
b5=a/2/2/2/2/2%2;
b6=a/2/2/2/2/2/2%2;
b7=a/2/2/2/2/2/2/2%2;
printf("%d%d%d%d%d%d%d%d",b7,b6,b5,b4,b3,b2,b1,b0);
system("pause");
return 0;
}
4、编写程序完成矩形面积的计算。
要求程序运行时,从键盘输入矩形的两个边长(正整数),计算矩形 的面积(正整数)并输出。
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,s;
scanf("%d%d",&a,&b);
s=a*b;
printf("%d\n",s);
system("pause");
return 0;
}
5、编写程序完成平均值的计算。
要求程序运行时,依次输入3个整数,求它们的平均值并输出,结果保留2位小数。
#include<stdio.h>
#include<stdlib.h>
int main()
{
double a,b,c;
double x;
scanf("%lf%lf%lf",&a,&b,&c);
x=(a+b+c)/3;
printf("%.2lf\n",x);
system("pause");
return 0;
}
6、编写程序,计算各位数字之和。
要求程序运行时依次输入一个三位的整数(100至999),计算并输出该数个位、十位和百位上 各位数字之和。
#include <stdio.h>
int main() {
int number, digit_sum = 0, hundreds_digit, tens_digit, ones_digit;
scanf("%d", &number);
// 提取各个位的数字
ones_digit = number % 10;
tens_digit = (number / 10) % 10;
hundreds_digit = number / 100;
// 计算各位数字之和
digit_sum = ones_digit + tens_digit + hundreds_digit;
// 输出结果
printf("%d\n", digit_sum);
return 0;
}
7、编写程序,计算球。
要求程序运行时输入圆球半径(浮点型数值),计算并输出圆球的表面积和体积(圆周率 取3.1415926,结果保留两位小数)。提示:球的表面积=4πR²,球的体积=4/3πR³。
#include<stdio.h>
#include<stdlib.h>
int main()
{
double R,s,v,πD;
scanf("%lf",&R);
πD=3.1415926;
s=4.0*πD*R*R;
v=(4.0/3)*πD*R*R*R;
printf("%.2lf %.2lf",s,v);
system("pause");
return 0;
}
8、编写程序求三角形面积 。
要求程序运行时从键盘依次输入三角形的三个边长(浮点型),计算输出三角 形的面积(结果保留两位小数)。提示:设三边长为a,b,c,p=(a+b+c)/2,则三角形面积平方=p*(pa)(p-b)(p-c);开平方可用数学函数sqrt()。
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, p, area;
scanf("%f %f %f", &a, &b, &c);
p = (a + b + c) / 2;
area = sqrt(p * (p - b) * (p - c) * (p - a));
printf("%.2f\n", area);
return 0;
}
在程序中,我们首先定义了四个浮点型变量a、b、c、p和area,分别用来存储三角形的三个边长、半周长和三角形的面积。然后,使用printf()函数提示用户输入三角形的三个边长,并使用scanf()函数从键盘读取用户输入的数据,并将它们分别存储到变量a、b、c中。
接下来,我们根据海伦公式计算出三角形的半周长p,并使用sqrt()函数计算三角形的面积。最后,使用printf()函数输出计算结果,保留两位小数。
9、编写程序求解具有两个不同实根的一元二次方程的根。
要求程序运行时从键盘依次输入一元二次方程 ax*x+bx+c=0的系数a(双精度)、b(双精度)和c(双精度),计算并输出该方程的两个实根(结果保留 两位小数)。 提示:开平方可使用数学函数sqrt()。
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
double a,b,c,dise,x1,x2,p,q;
scanf("%lf%lf%lf",&a,&b,&c);
dise=b*b-4*a*c;
p=(-b)/(2.0*a);
q=sqrt(dise)/(2.0*a);
x1=p+q;
x2=p-q;
printf("%.2lf %.2lf",x1,x2);
system("pause");
return 0;
}
10、编写程序求移动距离。
要求程序运行时从键盘依次输入初始速度(浮点型)、加速度(浮点型)和时间 (浮点型),计算指定时间后移动的距离并输出,保留2位小数。提示:设S是距离,V0是初速度,t是时 间,a是加速度。则S=V0t+1/2at²。
#include <stdio.h>
int main() {
float v0, a, t, s;
scanf("%f", &v0);
scanf("%f", &a);
scanf("%f", &t);
s = v0 * t + 0.5 * a * t * t;
printf("%.2f\n", s);
return 0;
}
程序首先定义了四个浮点型变量v0、a、t和s,分别用来存储初始速度、加速度、时间和移动的距离。然后,使用printf()函数提示用户输入初始速度、加速度和时间,并使用scanf()函数从键盘读取用户输入的数据,并将它们分别存储到变量v0、a和t中。
接下来,程序根据公式s=v0t+1/2at²计算出指定时间后移动的距离,并将结果存储到变量s中。最后,使用printf()函数输出计算结果,保留两位小数。
11、编写程序求圆的周长和面积。
要求程序运行时从键盘输入半径(浮点型),计算并输出圆的周长和 面积(圆周率取3.1415926,结果保留2位小数)。
#include<stdio.h>
#include<stdlib.h>
int main()
{
double c,s,r,πD;
scanf("%lf",&r);
πD=3.1415926;
c=2*πD*r;
s=πD*r*r;
printf("%.2lf %.2lf",c,s);
return 0;
}
12、编写程序计算运费。
要求程序运行时从键盘依次输入每公里每吨货物的基本运费(整型)、运送货 物的重量(整型)、运送距离(整型)以及折扣(双精度浮点型),计算并输出其所需运费(结果保留3 位小数)。提示:设每公里每吨货物的基本运费为p,货物重量为w,距离为s,折扣为d。总运费f的计算 公式为:f=pws*(1-d)。
#include<stdio.h>
#include<stdlib.h>
int main()
{
double p,w,s,d,f;
scanf("%lf%lf%lf%lf",&p,&w,&s,&d);
f=p*w*s*(1-d);
printf("%.3lf\n",f);
return 0;
}
在这个程序中,我们首先定义了四个变量:p(基本运费)、w(货物重量)、s(距离)和d(折扣),以及一个双精度浮点型变量f用于存储总运费。然后,我们使用printf()
函数提示用户输入所需的信息,并使用scanf()
函数从键盘读取这些信息。接下来,我们使用给定的公式计算总运费,并将结果存储在变量f中。最后,我们使用printf()
函数输出总运费,保留3位小数。
13、编写程序计算自由落体时间。
要求程序运行时从键盘输入物体高度(浮点型),计算并输出其自由 落体时掉落地面所需时间(结果保留2位小数)。提示:设h是距离,g是重力加速度(g=9.8),t是时间 的平方。则h=gt/2;开平方可使用数学函数sqrt()。
我们可以使用C语言的scanf函数从键盘读取物体高度,然后根据自由落体的公式计算出所需时间,最后使用printf函数输出结果。
以下是C语言代码实现:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
double t,g,h,t1;
scanf("%lf",&h);
g=9.8;
t=2*h/g;
t1=sqrt(t);
printf("%.2lf",t1);
return 0;
}
14、编写程序求人民币最小张数。
要求程序运行时从键盘输入指定数额现金(整型),需要在张数最少情 况下将其换取100元、50元、20元、10元、5元 和1元的人民币,各自需要多少张,请按100元、50元、20 元、10元、5元和1元的次序计算所需张数并输出。
这个问题可以使用取模运算来解决。我们可以从大到小依次检查100元、50元、20元、10元、5元和1元的人民币面额,看它们分别能覆盖多少现金,然后输出所需张数。
#include <stdio.h>
int main() {
int cash;
printf("请输入指定数额现金:");
scanf("%d", &cash);
int one_hundred = cash / 100; // 计算100元面额的人民币需要多少张
cash %= 100;
int fifty = cash / 50; // 计算50元面额的人民币需要多少张
cash %= 50;
int twenty = cash / 20; // 计算20元面额的人民币需要多少张
cash %= 20;
int ten = cash / 10; // 计算10元面额的人民币需要多少张
cash %= 10;
int five = cash / 5; // 计算5元面额的人民币需要多少张
cash %= 5;
int one = cash; // 计算1元面额的人民币需要多少张
printf("100元面额的人民币需要%d张\n", one_hundred);
printf("50元面额的人民币需要%d张\n", fifty);
printf("20元面额的人民币需要%d张\n", twenty);
printf("10元面额的人民币需要%d张\n", ten);
printf("5元面额的人民币需要%d张\n", five);
printf("1元面额的人民币需要%d张\n", one);
return 0;
}
#include<stdio.h>
int main()
{
int a,b,c,d,e,f,x,t1,t2,t3,t4,t5,t6;
scanf("%d",&x);
a=100;b=50;c=20;d=10;e=5;f=1;
t1=x/a;
t2=x%a/b;
t3=x%a%b/c;
t4=x%a%b%c/d;
t5=x%a%b%c%d/e;
t6=x%a%b%c%d%e/f;
printf("%d\n%d\n%d\n%d\n%d\n%d\n",t1,t2,t3,t4,t5,t6);
return 0;
}
部分内容来源网络,侵删!