1.将分钟转换成 小时 和 分钟, 使用while循环让用户重复输入,直到输入0退出。
#include<stdio.h> int main(void) { const int AHoursOfMinutes = 60; int minutes; int m_hour; int m_minute; do { printf("请输入时间(分钟)"); scanf_s("%d", &minutes); m_hour = minutes / AHoursOfMinutes; m_minute = minutes % AHoursOfMinutes; printf("%d小时 %d分钟\n", m_hour, m_minute); } while (minutes != 0); return 0;
}2.用户输入一个整数,程序打印它和它后面的10个数字。
#include<stdio.h> int main(void) { int n; printf("请输入一个整数"); scanf_s("%d", &n); for (int i = n; i <= n + 10; i++) printf("%d ", i); getchar(); getchar()a; return 0;
3.输入天数,转换成周数和天数,用户输入非正整数时退出。 这题和第1题差不多
#include<stdio.h> int main(void) { int days; int weeks; int day; do { printf("输入天数"); scanf_s("%d", &days); weeks = days / 7; day = days % 7; printf("%d周,%d天\n", weeks, day); } while (days > 0); getchar(); getchar(); return 0; }
4.输入身高(cm),转换成英寸。
#include<stdio.h> int main(void) { float height = 1; float inchs; int feet; while (height > 0) { printf("输入身高(厘米)"); scanf_s("%f", &height); if (height <= 0) break; inchs = height / 2.54; feet = inchs / 12; inchs -= feet * 12; printf("%f cm = %d feet, %f inches\n", height, feet, inchs); } printf("Bye"); getchar(); getchar(); return 0; }
5.6 略
7.计算立方
#include<stdio.h> double cube(double dd) { return (dd * dd * dd); } int main(void) { double dd; printf("输入一个浮点数"); scanf_s("%lf", &dd); printf("%lf", cube(dd)); getchar(); getchar(); return 0; }
8.编写求模的程序
#include<stdio.h> int main(void) { int first; int second; printf("求模:\n"); printf("输入一个整数作为%右边的值"); scanf_s("%d", &second); printf("输入第一个数"); scanf_s("%d", &first); printf("%d %% %d is %d\n", first, second, first%second); while (first > 0) { printf("输入下个数(<=0 时退出)"); scanf_s("%d", &first); if (first <= 0) break; printf("%d %% %d is %d\n", first, second, first%second); } printf("Done"); return 0; }
#include<stdio.h> void Temperatures(double f) { double CT = 5.0 / 9.0*(f - 32.0); double KT = CT + 273.16; printf("%.2lf °F , %.2lf °C ,%.2lf K\n", f, CT, KT); } int main(void) { double FT; printf("输入一个华式温度 F (q退出):"); while (scanf_s("%lf", &FT) == 1) { Temperatures(FT); printf("输入一个华式温度 F (q退出):"); } return 0; }