前言
C语言是大多数小白走上程序员道路的第一步,在了解基础语法后,你就可以来尝试解决以下的题目。放心,本系列的文章都对新手非常友好。
Tips:题目是英文的,但我相信你肯定能看懂
一、prompts the user to enter the width and height of a rectangle and displays the perimeter, area, and the length of diagonal.Here is a sample run:
Enter the width and height of a rectangle: 4.25 7.26
The perimeter is 23.02
The area is 30.86
The length of the diagonal is 8.41
#includeint main() { printf("Enter the width and height of a rectangle: "); float a,b; scanf("%f%f",&a,&b); printf("The perimeter is %.2fn",(2*a+2*b)); printf("The area is %.2fn",a*b); printf("The length of the diagonal is %.2fn",sqrt(a*a+b*b)); return 0; }
二、prompts for the user’s weekly pay in dollars and the hours worked to be entered through the keyboard as floating-point values.
Here is a sample run:
Enter the weekly pay in dollars: 120.8
Enter worked hours: 40
Your average hourly pay rate is 3 dollars and 2 cents.
#include
int main()
{
float a,b;
printf("Enter the weekly pay in dollars: ");
scanf("%f",&a);
printf("Enter worked hours: ");
scanf("%f",&b);
printf("Your average hourly pay rate is %d dollars and %.0f cents.n",(int)(a/b),(a/b-(int)(a/b))*100);
return 0;
}
三、asks the user to enter a height in centimeters and then displays the height in centimeters and in feet and inches (1 foot =0.3048m=12inches=30.48cm). Fractional centimeters and inches should be allowed.
Here is a sample run:
Enter a height in centimeters: 182
182.0 cm = 5 feet, 11.7 inches
#include
int main()
{
float a,b;
printf("Enter a height in centimeters: ");
scanf("%f",&a);
printf("%.1fcm = %d feet, %.1f inches",a,(int)(a/30.48),(a/30.48-(int)(a/30.48))*12);
return 0;
}
四、prompts the user to enter the coordinates of two points (x1, y1) and (x2, y2), and displays the slope and the length of the line connects the two points.
Here is a sample run:
Enter the coordinates for two points: 4.5 -5.5 6.6 -6.5
The slope for the line that connects two points (4.5, -5.5) and (6.6, -6.5) is -0.48
The length for the line that connects two points (4.5, -5.5) and (6.6, -6.5) is 2.33
#include
#include
int main()
{
float a,b,c,d;
printf("Enter the coordinates for two points: ");
scanf("%f%f%f%f",&a,&b,&c,&d);
printf("nThe slope for the line that connects two points (%.1f, %.1f) and (%.1f, %.1f) is %.2fn",a,b,c,d,(b-d)/(a-c));
printf("The length for the line that connects two points(%.1f, %.1f) and (%.1f, %.1f) is %.2fn",a,b,c,d,sqrt((a-c)*(a-c)+(b-d)*(b-d)));
return 0;
}
五、Write a program that reads in investment amount, annual interest rate, and number of years
Here is a sample run:
Enter investment amount: 1000
Enter annual interest rate: 4.25
Enter number of years: 1
Accumulated value is 1043.34
int main()
{
float a,b,c;
printf("Enter investment amount: ");
scanf("%f",&a);
printf("Enter annual interest rate: ");
scanf("%f",&b);
printf("Enter number of years: ");
scanf("%f",&c);
printf("Accumulated value is %.2f",a*pow((1+b/12/100),c*12));
return 0;
}
六、
Write a program to compute the ideal weight for both males and females. According to one study, the ideal weight for a female is 100 pounds plus 5 pounds for each inch in height over 5 feet. For example, the ideal weight for a female who is 5'3" would be 100 + 15 = 115 pounds. For a male the ideal weight is 106 pounds plus 6 pounds for each inch in height over 5 feet. For example, the ideal weight for a male who is 6'2" would be 106 + 14*6 =190 pounds. Your program should ask the user to enter his/her height in feet and inches (both as integers—so a person 5'3" would enter the 5 and the 3). It should then compute and print both the ideal weight for a female and the ideal weight for a male.
Enhance the Program a Bit: The weight program would be a bit nicer if it didn't just give one number as the ideal weight for each sex. Generally a person's weight is okay if it is within about 15% of the ideal. Add to your program so that in addition to its current output it prints an okay range for each sex—the range is from the ideal weight minus 15% to the ideal weight plus 15%. You may do this by introducing new variables and assignment statements OR directly within your print statements.
#include
int main()
{
int a,b;
printf("Enter a height in feet and inches: ");
scanf("%d%d",&a,&b);
printf(" The ideal weight for a female is from %f to %fn",(100+5*((a-5)*12+b))*0.85,(100+5*((a-5)*12+b))*1.15);
printf(" The ideal weight for a male is from %f to %fn",(106+6*((a-5)*12+b))*0.85,(106+6*((a-5)*12+b))*1.15);
return 0;
}
Here is a sample run:
Enter the weekly pay in dollars: 120.8
Enter worked hours: 40
Your average hourly pay rate is 3 dollars and 2 cents.
Here is a sample run:
Enter a height in centimeters: 182
182.0 cm = 5 feet, 11.7 inches
Here is a sample run:
Enter the coordinates for two points: 4.5 -5.5 6.6 -6.5
The slope for the line that connects two points (4.5, -5.5) and (6.6, -6.5) is -0.48
The length for the line that connects two points (4.5, -5.5) and (6.6, -6.5) is 2.33
Here is a sample run:
Enter investment amount: 1000
Enter annual interest rate: 4.25
Enter number of years: 1
Accumulated value is 1043.34
Write a program to compute the ideal weight for both males and females. According to one study, the ideal weight for a female is 100 pounds plus 5 pounds for each inch in height over 5 feet. For example, the ideal weight for a female who is 5'3" would be 100 + 15 = 115 pounds. For a male the ideal weight is 106 pounds plus 6 pounds for each inch in height over 5 feet. For example, the ideal weight for a male who is 6'2" would be 106 + 14*6 =190 pounds. Your program should ask the user to enter his/her height in feet and inches (both as integers—so a person 5'3" would enter the 5 and the 3). It should then compute and print both the ideal weight for a female and the ideal weight for a male.
Enhance the Program a Bit: The weight program would be a bit nicer if it didn't just give one number as the ideal weight for each sex. Generally a person's weight is okay if it is within about 15% of the ideal. Add to your program so that in addition to its current output it prints an okay range for each sex—the range is from the ideal weight minus 15% to the ideal weight plus 15%. You may do this by introducing new variables and assignment statements OR directly within your print statements.
总结
以上就是本文全部内容,你学会了吗?



