Chapter 2 Introducting C-Programming Exercises

Reading about C isn’t enough. You should try writing one or two simple programs to see whether writing a program goes as smoothly as it looks in this chapter. A few suggestions follow, but you should also try to think up some problems yourself. You’ll find answers to selected programming exercises on the publisher’s website. 
 


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): 

Gustav Mahler   // First print statement 
Gustav          // Second print statement
Mahler          // Still the second print statement  
Gustav Mahler   // Third and fourth print statements     
#include "stdio.h"

int main() {
    printf("Chintsai Hwo\n");
    printf("Chintsai\nHwo\n");
    printf("Chintsai");
    printf(" Hwo");
    return 0;
}

2.    Write a program to print your name and address.  

#include "stdio.h"

int main() {
    printf("Chintsai Hwo\n");
    printf("China");
    return 0;
}

 
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() {
    int age;
    int day;
    age = 23;
    day = age * 365;
    printf("An age of %d years is %d days\n", age, day);
    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 named 
 jolly()  that prints the “jolly good” message once, and one named  deny()  that prints the final line once.   

#include "stdio.h"

void jolly(void);

void deny(void);

int main() {
    jolly();
    jolly();
    jolly();
    deny();
}

void jolly(void) {
    printf("For he is a jolly good fellow!\n");
}

void deny(void) {
    printf("Which nobody can deny!\n");
}

 5.    Write a program that produces the following output: 

Brazil, Russia, India, China

India, China,

Brazil, Russia   

Have the program use two user-defined functions in addition to  main() : one named  br()  that prints “Brazil, Russia” once, and one named  ic()  that prints “India, China” once. Let  main()  take care of any additional printing tasks.   

#include "stdio.h"

void br(void);

void ic(void);

int main() {
    br();
    printf(", ");
    ic();
    printf("\n");
    ic();
    printf(",\n");
    br();
}

void br(void) {
    printf("Brazil, Russia");
}

void ic(void) {
    printf("India, China");
}

 6.   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() {
    int toes;
    toes = 10;
    printf("Toes is %d.\nTwice toes is %d.\nToes squared is %d\n", toes, toes ^ 2);
    return 0;
}

7.    Many studies suggest that smiling has benefits. 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() {
    smile();
    smile();
    smile();
    printf("\n");
    smile();
    smile();
    printf("\n");
    smile();
}

void smile(void) {
    printf("Smile!");
}

8.  In C, one function can call another. Write a program that calls a function named  one_ three() . This function should display the word  one  on one line, call a second function named  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() {
    printf("starting now:\n");
    one_three();
    printf("done!\n");
    return 0;
}

void one_three(void) {
    printf("one\n");
    two();
    printf("three\n");
}

void two(void) {
    printf("two\n");
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九成宫醴泉铭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值