C Primer Plus Chapter 2 C Language Overview - programming exercises

Posted by owner on Tue, 01 Feb 2022 12:11:31 +0100

1. Write a program and call printf() function once to print your first name and last name on one line. Again, the printf () function prints your first name and last name on two lines. Then, call the printf () function twice to print your first name and last name on one line. The output should be as follows (of course, replace the content of the instance with your name):

Gustav Mahler ← content printed for the first time
Gustav ← content printed for the second time
Mahler ← is still the content printed for the second time
Gustav Mahler ← contents of the 3rd and 4th printing

#include<stdio.h>
#define NAME"Gustav"
#define SURNAME"Mahler"
/*You can define your first and last names*/
int main(void)
{
    printf("%s %s\n",NAME,SURNAME);
    printf("%s\n%s\n",NAME,SURNAME);
    printf("%s",NAME);
    printf("%s\n",SURNAME);
    
    return 0;
}

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

#include<stdio.h>
#define NAME"Li Xiaonan"
#define ADDRESS"No.19 Tairm University, A Laer, Xin Jiang"
/*The name and address are respectively defined with precompiled instructions*/
int main(void)
{
   printf("%s\n",NAME);/*Print name*/
   printf("%s\n",ADDRESS);/*Print address*/
   
   return 0;
}

3. Write a program to convert your age into days and display these two values. There is no need to consider leap years here.

#include<stdio.h>
#define DAYS_PER_YEAR 365
/*Specify the number of days in a year with precompiling*/
int main(void)
{
   int age,days;
   age=19;
   days=age*DAYS_PER_YEAR;
   printf("You age is %d,and it is %d days.\n",age,days);
   
   return 0;
}

4. Write a program to generate 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!

In addition to the main() function, the program also calls two user-defined functions: one is called jolly(), which is used to print the first three messages, one at a time; Another function called deny() prints the last message.

#include<stdio.h>

int jolly(void);
int deny(void);
/*Function declaration*/
int main(void)
{
   jolly();
   jolly();
   jolly();
   deny();
   /*function call*/
   return 0;
}
int jolly(void)
{
/*Function definition*/
  printf("For he's a jolly good fellow!\n");
  return 0;
}
int deny(void)
{
/*Function definition*/
  printf("Which nobody can deny!\n");
  return 0;
}

5. Write a program to generate the following output:

Brazil,Russia,India,China
India,China
Brazil,Russia

In addition to main(), the program also calls two user-defined functions: one is called br(), which calls "Brazil,Russia" to print once at a time; The other is called ic(), calling "India,China" to print once at a time. The rest is done in the main() function.

#include<stdio.h>

int br(void);
int ic(void);
/*Function declaration*/
int main(void)
{
  br();
  printf(",");
  ic();
  ic();
  br();
  /*function call*/
  return 0;
}
int br(void)
{
/*Function definition*/
  printf("Brazil,Russia");
  return 0;
}
int ic(void)
{
/*Function definition*/
 printf("India,China\n");
 return 0;
}

6. Write a program, create an integer variable toes, and set toes to 10 The program also calculates twice the toes and the square of the toes. The program shall print three values and describe them separately to distinguish them.

#include<stdio.h>
int main(void)
{
   int toes;
   toes=10;
/*Define the shaping variable toes and assign a value of 10*/
   printf("The Variable toes =%d.\n",toes);
   printf("double toes =%d.\n",2*toes);
/*Calculate and print twice the toes, or calculate first and then print, for example:
int d_toes=2*toes;
printf("double toes=%d.\n",d_toes);
But it's best not to write:
toes=2*toes;
This will modify the value of toes and affect the calculation of the square of toes.
*/
   printf("toes' square=%d.\n",toes*toes);
 /*Calculate and print the square of toes.*/
    return 0;
}

7. Many studies have shown that smiling has many benefits. Write a program to generate output in the following format:

Smile!Smile!Smile!
Smile!Smile!
Smile!

The program needs to define a function, which is called to print "Smile!" once at a time, Use this function according to the needs of the program.

#include<stdio.h>

int smile(void);
/*Function declaration*/
int main(void)
{
/*function call*/
   smile();smile();smile();
   printf("\n");
   smile();smile();
   printf("\n");
   smile();
}
int smile(void)
{
/*Function definition*/
  printf("Smile!");
  return 0;
}

8. In C language, a function can call another function. Write a program and call a program named one_ Function of three(). This function prints the word "one" on one line, then calls the second function two(), and then prints the word "three" on another line. The two() function displays the word "two" on one line. The main() function is calling one_ Before the three() function, print the phrase "starting now:", and display the phrase "done!" after the call. Therefore, the output of the program is as follows:

starting now:
one
two
three
done!

#include<stdio.h>

int one_three(void);
int two(void);
/*Declaration of two functions*/
int main(void)
{
   printf("Staring now!\n");
   one_three();
   printf("Done!\n");
   return 0;
}
int one_three(void)
{
  printf("one\n");
  two();
  printf("three\n");
  return 0;
}
int two(void)
{
  printf("two\n");
  return 0;
}

Topics: C