C programming rookie exercises 100 questions (01-10)

Time:2021-6-22

[exercise 1] output “Hello, world!”

Title:
Output “Hello, world!”
1. Analysis
Use printf() to output “Hello, world!”.
2. Procedure:

#include 
int main()
{
    printf("Hello, World!");  //  Double quotation marks are required before and after strings in printf()
    return 0;
}

3. Input and output:

Hello, World!

4. Expand knowledge:
Printf() is a function defined in the header file stdio. H. the calling format is:

printf(“”, );


[exercise 2] output integers

Title:
Output integer
1. Analysis
Use printf() and% d to format the output integer.
2. Procedure:

#include 
int main()
{
    int intNum;  //  Declare int variable
    Printf ("enter an integer):"// Printf() output string
    scanf("%d", &intNum);  //  Scanf() formats the input, and% d represents the integer format
    Printf ("the input integer is% d", intnum))// Printf() displays formatted input
    return 0;
}

3. Input and output:

Enter an integer: 255
The input integer is 255

4. Expand knowledge:
Scanf() is a function defined in the header stdio. H, which reads the formatted input from stdin.


[exercise 3] output single character

Title:
Output single character
1. Analysis
Use printf() and% C to format one character.
2. Procedure:

#include 
int main()
{
    char oneChar;  //  Declare char variable
    oneChar = 'C';  //  Assigning values to variables
    Printf ("the value of onechar is% C", onechar))// Printf() formats the output
    return 0;
}

3. Input and output:

The value of onechar is C

4. Expand knowledge:

Basic data type
Int: integer
Short: short integer
Long: long integer
Float: floating point (single precision)
Double: floating point (double)
Char: character type
Void: no type


[exercise 4] output floating point numbers

Title:
Output floating point number
1. Analysis
Use printf() and% F to output floating-point numbers.
2. Procedure:

#include 
int main()
{
    float fVar1,fVar2;  //  Declare floating point variables   
    fVar1 = 12.00123;  //  Assigning values to variables   
    Printf ("the value of fvar1 is% F / N", fvar1))// Printf() formats the output 
    fVar2 = 123.001234;  //  Assigning values to variables   
    Printf ("the value of fvar2 is% F / N", fvar2))// Printf() formats the output 
    return 0;
}

3. Input and output:

The value of fvar1 is 12.00123
The value of fvar2 is 123.001236

AttentionThe value and assignment of fvar1Exactly the sameBut the value of fvar2 is not the same as the assignmentNot exactly the same. This is because float is a single precision floating-point number, which has 4 bytes in memory and 7 significant digits. If the number of significant digits exceeds 7, the excess will be rounded automatically.


[exercise 5] output double precision numbers

Title:
Output double precision number
1. Analysis
Use printf() and% e to output double numbers.
2. Procedure:

#include 
{
    double dVar1,dVar2;  //  Declare floating point variables   
    dVar1 = 12.00123;  //  Assigning values to variables   
    Printf ("the value of dvar1 is% LF / N", dvar1))// Formatted output: long real number 
    Printf ("the value of dvar1 is% Le, dvar1)"// Formatted output: scientific counting method 
    dVar2 = 123.001234;  //  Assigning values to variables   
    Printf ("the value of dvar2 is% LF / N", dvar2))// Formatted output: long real number 
    Printf ("the value of dvar2 is% Le, dvar2)"// Formatted output: scientific counting method 
    return 0;
}

3. Input and output:

The value of dvar1 is 12.001230
The value of dvar1 is 1.200123e + 001
The value of dvar2 is 123.001234
The value of dvar2 is 1.230012e + 002

AttentionDouble is a double precision floating-point number with 8 bytes in memory and 16 significant digits. The output of dvar2 in scientific counting format (LE) is similar to the assignmentNot exactly the sameThis is only caused by the limitation of the number of significant digits when formatting the output, but it is related to the assignment in the actual storage and calculationExactly the sameTherefore, when outputting in long real format (LF), it is consistent with assignmentExactly the same


Add two numbers

Title:
Add two numbers
1. Analysis
Analysis uses scanf() to receive input, printf() and% d to format output integers.
2. Procedure:

#include 
int main()
{
    int firstNum, secondNum, sumNum;    
    Printf ("enter two integers (separated by spaces):"); 
    //Scanf () formats the input and receives two integers from the user
    scanf("%d %d", &firstNum, &secondNum); 
    //Add two integers
    sumNum = firstNum + secondNum; 
    //Output results
    printf("%d + %d = %d", firstNum, secondNum, sumNum);
    return 0;
}

3. Input and output:

Enter two numbers (separated by spaces): – 1 99
-1 + 99 = 98

AttentionSince the input format defined in scanf() function is% d (there is a space between the two% d), you must add a space between the two integers in strict accordance with the defined format, otherwise you may make an error. Similarly, if you define the input format as% d% D, you must add a comma between the two integers you enter.


[exercise 7] multiplication of two floating point numbers

Title:
Multiplication of two floating point numbers
1. Analysis
Enter two floating-point numbers to calculate the product.
2. Procedure:

#include 
int main()
{
    double firstNum, secondNum, productNum;
    Printf ("enter two floating-point numbers (separated by spaces):");
    scanf("%lf %lf", &firstNum, &secondNum);   
    productNum = firstNum * secondNum;  //  Multiplication of two floating point numbers
    printf("%.2lf * %.2lf = %.2lf\n", firstNum,secondNum,productNum); 
    printf("%lf * %lf = %lf\n", firstNum,secondNum,productNum);
    return 0;
}

3. Input and output:

Enter two floating-point numbers (separated by spaces): 1.2345 66
1.23 * 66.00 = 81.48
1.234500 * 66.000000 = 81.477000

Attention(1) The variable secondnum is defined as a floating-point number. Although the input is an integer, it is also stored as a floating-point number( 2) %. 2lf indicates that the output format is to retain 2 significant digits, but does not affect the accuracy of the stored data.


[exercise 8] character to ASCII

Title:
Character to ASCII
1. Analysis
ASCII defines 128 characters:
(1) 0-31, 127 (delete key) are control characters
(2) White space characters: space (32), tab, vertical tab, line feed, carriage return.
(3) Display characters: A-Z, A-Z, 0-9, ~,!, @,,%, ^, &, #, $, *, (,), -, +, {,}, [,], ‘, “,,,,?, /, |,,:, And don.

2. Procedure:

#include 
int main()
{
    char oneChar;  //  Declare char variable
    
    Printf ("enter a character):"; 
    scanf("%c", &oneChar);  //  Read user input
    Printf (% C's ASCII is% d ", onechar, onechar); //% C C character format
    return 0;
}

3. Input and output:

Enter a character: a
The ASCII of a is 65


[exercise 9] divide two numbers

Title:
Division of two numbers
1. Analysis
Divide two numbers, if there is a remainder, output the remainder
2. Procedure:

#include 
int main()
{
    int dividend, divisor, quotient, remainder;
    
    Printf ("input divisor, divisor (separated by space):"); 
    //Scanf () formats the input and receives two integers from the user
    scanf("%d %d", &dividend, &divisor);  //  Input divisor, divisor
   
    quotient = dividend / divisor;  //  A numerator that divides the integral part of the denominator
    remainder = dividend % divisor;  // %  Is the remainder of a modular operation
    Printf ("quotient =% D, remainder =% d", quotient, maintainer) ";
    return 0;
}

3. Input and output:

Input divisor, divisor (separated by space): 50 10 output: quotient = 5, remainder = 0
Input divisor, divisor (separated by space): 51 10 output: quotient = 5, remainder = 1
Input divisor, divisor (separated by space): 55 10 output: quotient = 5, remainder = 5
Input divisor, divisor (separated by space): 58 10 output: quotient = 5, remainder = 8

AttentionThe quotient obtained by the division of ‘/’ is the integral part of the divisor and the divisor, which is not rounded.


[exercise 10] numerical comparison

Title:
Numerical comparison
1. Analysis
Two integer variables are defined, and if is used to compare two values
2. Procedure:

#include 
int main()
{
    int firstNum, secondNum; 

    Printf ("enter two integers (separated by spaces):"); 
    scanf("%d %d", &firstNum, &secondNum); 
    
    if(firstNum > secondNum)
        Printf ("% d greater than% d", firstnum, secondnum) ";
    else if(firstNum < secondNum)
        Printf ("% d less than% d", firstnum, secondnum) "; 
    else
        Printf ("% d equals% d", firstnum, secondnum));
    return 0;
}

3. Input and output:

Enter two integers (separated by spaces): – 1 99
-1 less than 99


Copyright note: the title and routine of this paper are from the rookie tutorial. The author rewrites the program, notes and examples, and adds the analysis of difficulties.