C Primer Plus 编程练习代码——第2章

来源:互联网 发布:咫尺网络 编辑:程序博客网 时间:2024/06/10 09:54

1.Write a program that uses one printf() call to print your first name and last name on one line, uses a second printf() call to print your first and last names on two separate lines, and uses a pair of printf() calls to print your first and last names on one line. The output should look like this (but using your name):

#include <stdio.h>int main(void) {    printf("Steve Jobs\n"); //print first and last name in one line    printf("Steve\nJobs\n"); //print first and last name in seperated lines    printf("Steve "); //print first and last name in one line using a pair of printf    printf("Jobs");    return 0;}

2.Write a program to print your name and address.(Omitted)

3.Write a program that converts your age in years to days and displays both values. At this point, don't worry about fractional years and leap years.

#include <stdio.h>int main(void) {    int ageInYear, ageInDay;        ageInYear = 26;    ageInDay = ageInYear * 365;    printf("My age is %d-year-old, which is %d days", ageInYear, ageInDay);    return (0);}

4.Write a program that produces the following output:
For he's a jolly good fellow!
For he's a jolly good fellow!
For he's a jolly good fellow!
Which nobody can deny!
Have the program use two user-defined functions in addition to main(): one that prints the "jolly good" message once, and one that prints the final line once.

#include <stdio.h>void myPrint1(void);void myPrint2(void);int main(void) {    int i = 1;        for(i; i <= 3; i++){    printf("For he's a ");    myPrint1();    printf("fellow.\n");    }            myPrint2();    return (0);}void myPrint1(void){    printf("jolly good ");}void myPrint2(void){    printf("Which nobody can deny!\n");}

5.Write a program that creates an integer variable called toes. Have the program set toes to 10. Also have the program calculate what twice toes is and what toes squared is. The program should print all three values, identifying them.

#include <stdio.h>int main(void) {    int toe = 10;        printf("The toe is %d, twice toe is %d, and toe squarded is %d", toe, toe * 2, toe * toe);    return 0;}
6.Write a program that produces the following output:
Smile!Smile!Smile!
Smile!Smile!
Smile!
Have the program define a function that displays the string Smile! once, and have the program use the function as often as needed.

#include <stdio.h>void smile(void);int main(void) {        smile(); smile(); smile(); printf("\n");            smile();smile();printf("\n");        smile();        return 0;}void smile(void){    printf("smile!");}
7.Write a program that calls a function named one_three(). This function should display the word one on one line, call the function two(), and then display the word three on one line. The function two() should display the word two on one line. The main() function should display the phrase starting now: before calling one_three() and display done! after calling it. Thus, the output should look like the following:
starting now:
one
two
three
done!

#include <stdio.h>void one_three(void);void two(void);int main(void) {    printf("Starting now: \n");    one_three();    printf("Done!");    return 0;}void one_three(void) {    printf("one\n");    two();    printf("three\n");}void two(void) {    printf("two\n");}



原创粉丝点击