C Primer Plus_第5章_运算符、表达式和语句_编程练习

Practice
1. 输入分钟输出对应的小时和分钟。
#include
#define MIN_PER_H 60

int main(void)
{
int mins, hours, minutes;

printf("Convert mins to hours and minutes ");
printf("Please enter the mins: ");
scanf("%d", &mins);
while (mins > 0)
{
hours = mins / MIN_PER_H;
minutes = mins % MIN_PER_H;
printf("%d mins is %d hours and %d minutes ",
mins, hours, minutes);
printf("Please enter the next mins: ");
scanf("%d", &mins);
}
printf("ok, let's stop! ");

return 0;
}
C <wbr>Primer <wbr>Plus_第5章_运算符、表达式和语句_编程练习


2.题目略
#include

int main(void)
{
int num, i = 0;

printf("Please enter an int number: ");
scanf("%d", &num);
printf("Here we go: ");
while(i++ <= 10)
{
printf("%d  ", num+i-1);
}
printf(" ");
return 0;
}
C <wbr>Primer <wbr>Plus_第5章_运算符、表达式和语句_编程练习

3.题略
#include
#define D_PER_W 7

int main(void)
{
int day, week, days;

printf("Convert day to weeks and days ");
printf("Please enter the days: ");
scanf("%d", &day);
while(day > 0)
{
week = day / D_PER_W;
days = day % D_PER_W;
printf("%d days are %d weeks and %d days ",
day, week, days);
printf("Please enter the next days: ");
scanf("%d", &day);
}
printf("Done! ");
return 0;
}
C <wbr>Primer <wbr>Plus_第5章_运算符、表达式和语句_编程练习

4.题略
#include
#define C_P_F 36.2
#define C_P_I 16.5

int main(void)
{
float c, f, i;
printf("Please enter a height in centimeters: ");
scanf("%f", &c);
// printf("%d", (int)c);
while (c > 0)
{
f = c / C_P_F;
i = c / C_P_I;
printf("%1.1f cm = %1.1f feet, %1.1f inches. ",
c, f, i);
printf("Enter a height in centimeters (<=0 to quit): ");
scanf("%f", &c);
// printf("%d", int(c));
}
printf("bye ");
return 0;
}
C <wbr>Primer <wbr>Plus_第5章_运算符、表达式和语句_编程练习

5.题略
#include

int main (void)
{
int N, n=0;
int sum=0;

printf("计算前N个数的和(不包括0): ");
printf("Please input the N:");
scanf("%d", &N);
printf("N = %d ",N);
while(n++ < N)
sum = sum + n;
printf("前%d个数之和为%d ", N, sum);

getchar();
getchar();
return 0;
}
C <wbr>Primer <wbr>Plus_第5章_运算符、表达式和语句_编程练习

6.题略

#include
int main(void)
{
int begn = 0, stop, sum = 0;
//printf("please enter a begin number: ");
//scanf("%d", &begn);
printf("please enter a stop number:  ");
scanf("%d", &stop);
//while (begn <= stop)
while (begn++ < stop)
sum = sum + begn * begn;
printf("前%d个数的平方和为%d ",stop,sum);

return 0;
}
C <wbr>Primer <wbr>Plus_第5章_运算符、表达式和语句_编程练习

7.题略
#include
void cube (int n);

int main(void)
{
int i;
printf("Please enter a number: ");
scanf("%d", &i);
cube (i);
return 0;
}

void cube(int n)
{
int m;
m = n * n * n;
printf("cube of the number = %d ", m);
}
C <wbr>Primer <wbr>Plus_第5章_运算符、表达式和语句_编程练习
自己又改了下,效果相同

#include
int cube (int n);

int main(void)
{
int i,m;
printf("Please enter a number: ");
scanf("%d", &i);
m = cube (i);
printf("cube of the number = %d ", m);

return 0;
}

int cube(int n)
{
int m;
m = n * n * n;
return m;
}

8.题略

#include
void Temperatures (double f);

int main(void)
{
double tmp;
printf("Please enter a Fahrenheit temperature: ");
scanf("%lf", &tmp); //%lf是读取double型数据的意思
while(scanf("%lf", &tmp) == 1)//利用scanf函数返回值来作循环判断条件  !!!!!!吊炸天!!!!!! 
{
Temperatures (tmp);
printf("Please enter another f: ");
scanf("lf", &tmp);
}
printf("Done!");

return 0;
}

void Temperatures (double f)
{
double c, k;
const double X = 1.8;
const double Y = 32.0, Z = 273.16;
c = X * f + Y;
k = c + Z;
printf("the fahrenheit is %1.2f = %1.2f celsius = %1.2f kelvin ", f, c, k);
}
结果如下
C <wbr>Primer <wbr>Plus_第5章_运算符、表达式和语句_编程练习

原文地址:https://www.cnblogs.com/TomLily/p/5814582.html