《C语言程序设计现代方法:第2版 修订版》第六章课后编程题,水平有限,仅供参考。 1
#include <stdio.h>
int main (void)
{
float num,max=0;
printf("Enter a number:");
scanf("%f",&num);
while(num>0){
printf("Enter a number:");
scanf("%f",&num);
if(num>max){
max=num;
}
}
printf("\nThe largest number number entered was %.2f",max);
return 0;
}
2
#include <stdio.h>
int main (void)
{
int num1,num2,temp;
printf("Enter two integers:");
scanf("%d %d",&num1,&num2);
if(num1==0||num2==0){
return 0;
}
if(num1<num2){
temp=num1;
num1=num2;
num2=temp;
}
while(num2%num1!=0){
temp=num2%num1;
num2=num1;
num1=temp;
}
printf("Greatest common divisor: %d",num1);
return 0;
}
3
#include <stdio.h>
int main (void)
{
int num1,num2,temp,result1,result2;
printf("Enter two integers:");
scanf("%d/%d",&num1,&num2);
result1=num1,result2=num2;
if(num1==0||num2==0){
return 0;
}
if(num1<num2){
temp=num1;
num1=num2;
num2=temp;
}
while(num2%num1!=0){
temp=num2%num1;
num2=num1;
num1=temp;
}
printf("In lowest terms: %d/%d",result1/num1,result2/num1);
return 0;
}
4
#include <stdio.h>
int main (void)
{
float commission,value;
printf("Enter value of trade: ");
scanf("%f",&value);
if(value==0){
return 0;
}
while(value!=0){
if(value<2500.00f)
commission=30.00f+.017f*value;
else if(value<6250.00f)
commission=56.00f+.0066f*value;
else if(value<20000.00f)
commission=76.00f+.0034f*value;
else if(value<50000.00f)
commission=100.00f+.0022f*value;
else if(value<500000.00f)
commission=155.00f+.0011f*value;
else
commission=255.00f+.0009f*value;
if(commission<39.00f)
commission=39.00f;
printf("Commission:$%.2f\n",commission);
printf("\nEnter value of trade: ");
scanf("%f",&value);
}
return 0;
}
5
#include <stdio.h>
int main (void)
{
int num,count;
printf ("Enter a number: ");
scanf ("%d", &num);
printf ("The reversal is: ");
do{
count=num%10;
num/=10;
printf("%d",count);
}while(num!=0);
return 0;
}
6
#include <stdio.h>
int main (void)
{
int num,begin;
printf("Enter a number: ");
scanf("%d",&num);
if(num!=2){
for(begin=2;begin*begin<=num;begin+=2){
printf("%d\n",begin*begin);
}
}
else{
printf("4");
}
return 0;
}
7
#include <stdio.h>
int main (void)
{
int i,n,odd,square;
printf("This program prints a table of squares.\n");
printf("Enter number of entries in table: ");
scanf("%d",&n);
odd=3;
for(i=1,square=1;i<=n;odd+=2,i++){
printf("%10d%10d\n",i,square);
square+=odd;
}
return 0;
}
8
#include <stdio.h>
int main (void)
{
int i,days,start;
printf("Enter number of days in month:");
scanf("%d",&days);
printf("Enter staring day of the week (1=Sun, 7=Sat):");
scanf("%d",&start);
for(i=0;i<start-1;i++){
printf(" ");
}
for(i=1;i<=days;i++){
printf("%2d",i);
printf(" ");
if((start+i-1)%7==0)
printf("\n");
}
return 0;
}
9
#include <stdio.h>
int main (void)
{
int times,i;
float amount,rate,monthly_payment;
printf("Enter amount of loan:");
scanf("%f",&amount);
printf("Enter interest rate:");
scanf("%f",&rate);
printf("Enter monthly payment:");
scanf("%f",&monthly_payment);
printf("Enter the times: ");
scanf("%d",×);
for(i=0;i<times;i++){
amount=(amount-monthly_payment)+amount*rate/1200;
printf("Banlance remaining after first payment:$%.2f\n",amount);
}
return 0;
}
10
#include <stdio.h>
int main (void)
{
int day,month,year,day_s,month_s,year_s;
printf("Enter a date (mm/dd/yy):");
scanf("%d/%d/%d",&month,&day,&year);
day_s=day,month_s=month,year_s=year;
while(month!=0&&day!=0&&year!=0){
if(year_s>year){
day_s=day,month_s=month,year_s=year;
}
else if(year_s==year){
if(month_s>month){
day_s=day,month_s=month,year_s=year;
}
else if(month_s==month){
if(day_s>day){
day_s=day,month_s=month,year_s=year;
}
else if(day_s==day){
day_s=day,month_s=month,year_s=year;
}
}
}
printf("Enter a date (mm/dd/yy):");
scanf("%d/%d/%d",&month,&day,&year);
}
printf("%d/%d/%d is the earliest date",month_s,day_s,year_s);
return 0;
}
11
#include <stdio.h>
int main (void)
{
int num,count;
float sum,temp,step;
printf("Enter a number:");
scanf("%d",&num);
sum=1;
for(count=1;count<=num;count++){
temp=1;
for(step=count;step>=1;step--){
temp*=step;
}
sum+=1/temp;
}
printf("The value is %f",sum);
return 0;
}
12
#include <stdio.h>
int main (void)
{
int num,count;
float sum,temp,step,stop;
printf("Enter a number:");
scanf("%d",&num);
printf("Enter a number you want to stop:");
scanf("%f",&stop);
sum=1;
for(count=1;count<=num;count++){
temp=1;
for(step=count;step>=1;step--){
temp*=step;
}
if(1/temp<stop){
break;
}
else{
sum+=1/temp;
}
}
printf("The value is %f",sum);
return 0;
}
|