题目字太多了略🤣有问题欢迎留言讨论~
IDE: Xcode
5-1
#include <stdio.h>
#define MIN_TO_HOUR 60
int main()
{
int time, min, hour; //int默认是有符号型
printf("Please enter time(min):");
scanf("%d", &time);
while(time > 0){
getchar();
hour = time / MIN_TO_HOUR;
min = time % MIN_TO_HOUR;
printf("equal to %dhour %dmin\n", hour, min);
printf("Please enter time(min):");
scanf("%d", &time);
}
return 0;
}
Output:
5-2
#include <stdio.h>
int main()
{
int n = 0;
int number;
printf("please enter a number:");
scanf("%d", &number);
while (n++ <= 10){
printf("%d ", number++);
}
return 0;
}
Output:
5-3
#include <stdio.h>
#define DAY_TO_WEEK 7
int main()
{
int time, week, day; //int默认是有符号型
printf("Please enter time(day):");
scanf("%d", &time);
while(time > 0){
getchar();
week = time / DAY_TO_WEEK;
day = time % DAY_TO_WEEK;
printf("%d days are %d weeks, %d days.\n", time, week, day);
printf("Please enter time(day):");
scanf("%d", &time);
}
return 0;
}
Output:
5-4
#include <stdio.h>
#define INCH_TO_FOOT 12 //1英尺=12英寸
#define CM_TO_INCH 2.54 //1英寸=2.54厘米
int main()
{
float height;
float inch;
int foot;
printf("Enter a height in centimeters:");
scanf("%f", &height);
while(height > 0){
getchar();
inch = height / CM_TO_INCH;
foot = inch / INCH_TO_FOOT;
inch = inch - foot * INCH_TO_FOOT;
printf("%.1f cm = %d feet, %.1f inches\n", height, foot, inch);
printf("Enter a height in centimeters (<=0 to quit): ");
scanf("%f", &height);
}
printf("bye\n");
return 0;
}
Output:
5-5
#include <stdio.h>
int main()
{
int count, sum = 0;
printf("please enter days:");
scanf("%d", &count);
getchar();
while (count > 0){
sum += count;
count--;
}
printf("you make $%d totally in these days.\n", sum);
return 0;
}
Output:
5-6
#include <stdio.h>
int main()
{
int count, sum = 0;
printf("please enter days:");
scanf("%d", &count);
getchar();
while (count > 0){
sum += count * count;
count--;
}
printf("you make $%d totally in these days.\n", sum);
return 0;
}
Output:
5-7
#include <stdio.h>
double cube(double n)
{
return (n * n * n);
}
int main()
{
double number;
double number_cube;
printf("Please enter a number(type:double):");
scanf("%lf", &number); //lf用于存储double
getchar();
number_cube = cube(number);
printf("the cube of number is: %g\n", number_cube); //g,自动判断用f还是e,和输出宽度
return 0;
}
Output:
5-8
#include <stdio.h>
int main()
{
int mod, number, remainder;
printf("This program computes moduli.\n");
printf("Enter integer to serve as the second operand:");
scanf("%d", &mod);
getchar();
while(1){
printf("Now enter the first operand(< = 0 to quit):");
scanf("%d", &number);
getchar();
if( number <= 0) break;
remainder = number % mod;
printf("%d %% %d is %d\n", number, mod, remainder);
}
printf("Done\n");
return 0;
}
Output:
5-9
#include <stdio.h>
void Temperatures(double fahrenheit)
{
const double f_to_c = 32.0;
const double c_to_k = 273.16;
double centigrade, kelvin; //Fahrenheit华氏温度,centigrade摄氏温度,kelvin开氏温度
centigrade = 5.0 / 9.0 * (fahrenheit - f_to_c);
kelvin = centigrade + c_to_k;
printf("%.2lf°F = %.2lf°C = %.2lf°K\n", fahrenheit, centigrade, kelvin);
}
int main()
{
double temp;
printf("please enter temperature(fahrenheit):");
while( 1 == scanf("%lf", &temp)){
getchar();
Temperatures(temp);
printf("please enter temperature(fahrenheit):");
}
printf("Done\n");
return 0;
}
Output: