C language programming rookie exercise 100 questions (01-10)

Posted by Diceman on Sat, 19 Feb 2022 16:56:52 +0100

[exercise 1] output "Hello, World!"

0. Title:
Output "Hello, World!"
1. Analysis:
Use printf() to output "Hello, World!".
2. Procedure:

#include <stdio.h>
int main()
{
    printf("Hello, World!");  // Double quotes are required before and after the string in printf()
    return 0;
}

3. Input and output:

Hello, World!

4. Expand knowledge:
printf() is the header file stdio The calling format of the function defined in H is:

Printf ("< format string >", < parameter table >);

[exercise 2] output integer

0. Title:
Output integer
1. Analysis:
Format the output integer using printf() and% d.
2. Procedure:

#include <stdio.h>
int main()
{
    int intNum;  // Declare int variable
    printf("Enter an integer: ");  // printf() output string
    scanf("%d", &intNum);  // scanf() format input, "% d" indicates integer format
    printf("The integer entered is: %d", intNum);  // printf() displays formatted input
    return 0;
}

3. Input and output:

Enter an integer: 255
The integer entered is 255

4. Expand knowledge:
scanf() is the header file stdio The function defined in H reads the formatted input from the standard input stdin.

[exercise 3] output a single character

0. Title:
Output single character
1. Analysis:
Use printf() and% c to format and output a character.
2. Procedure:

#include <stdio.h>
int main()
{
    char oneChar;  // Declare char variable
    oneChar = 'C';  // Assign a value to a variable
    printf("oneChar The value of is %c", oneChar);  // printf() format 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 type (single precision)
Double: floating point type (double precision)
char: character type
void: no type

[exercise 4] output floating point numbers

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

#include <stdio.h>
int main()
{
    float fVar1,fVar2;  // Declare floating point variables   
    fVar1 = 12.00123;  // Assign a value to a variable   
    printf("fVar1 The value of is %f\n", fVar1);   // printf() format output 
    fVar2 = 123.001234;  // Assign a value to a variable   
    printf("fVar2 The value of is %f\n", fVar2);   // printf() format output 
    return 0;
}

3. Input and output:

The value of fVar1 is 12.00123
The value of fVar2 is 123.001236

[attention!] The value of fVar1 is exactly the same as the assignment, but the value of fVar2 is not exactly the same as the assignment. This is because float is a single precision floating-point number, accounting for 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 number

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

#include <stdio.h>
{
    double dVar1,dVar2;  // Declare floating point variables   
    dVar1 = 12.00123;  // Assign a value to a variable   
    printf("dVar1 The value of is %Lf\n", dVar1);   // Formatted output: long real number 
    printf("dVar1 The value of is %Le\n", dVar1);   // Formatted output: scientific counting 
    dVar2 = 123.001234;  // Assign a value to a variable   
    printf("dVar2 The value of is %Lf\n", dVar2);   // Formatted output: long real number 
    printf("dVar2 The value of is %Le\n", dVar2);   // Formatted output: scientific counting 
    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

[attention!] Double is a double precision floating-point number with 8 bytes in memory and 16 significant digits. Although the output of dVar2 in scientific counting format (Le) is not exactly the same as the assignment, which is only caused by the limit of significant digits in formatted output, it is exactly the same as the assignment in actual storage and calculation, so it is exactly the same as the assignment in long real format (Lf).

[exercise 6] add two numbers

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

#include <stdio.h>
int main()
{
    int firstNum, secondNum, sumNum;    
    printf("Enter two integers(Separated by spaces): "); 
    // scanf() formats the input and receives two integers entered by 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

[attention!] Since the input format defined in scanf() function is "% d% d" (there is a space between 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] multiply two floating point numbers

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

#include <stdio.h>
int main()
{
    double firstNum, secondNum, productNum;
    printf("Enter two floating point numbers(Separated by spaces): ");
    scanf("%lf %lf", &firstNum, &secondNum);   
    productNum = firstNum * secondNum;  // Multiply 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

0. Title:
Character to ASCII
1. Analysis:
ASCII defines 128 characters:
(1) 0-31 and 127 (delete key) are control characters
(2) Blank characters: space (32), tab, vertical tab, line feed, carriage return.
(3) Displayable characters: A-Z, A-Z, 0-9, ~,!, @,%, ^, &, #, $, * (,), -, +, {,}, [,], ', ", <, >,,?, /, |, \,,:,;,. There are also stop signs and.

2. Procedure:

#include <stdio.h>
int main()
{
    char oneChar;  // Declare char variable
    
    printf("Enter a character: "); 
    scanf("%c", &oneChar);  // Read user input
    printf("%c of ASCII by %d", oneChar, oneChar);  // %c character format
    return 0;
}

3. Input and output:

Enter A character: A
ASCII of A is 65

[exercise 9] divide two numbers

0. Title:
Divide two numbers
1. Analysis:
Divide the two numbers. If there is a remainder, output the remainder
2. Procedure:

#include <stdio.h>
int main()
{
    int dividend, divisor, quotient, remainder;
    
    printf("Enter divisor and divisor(Separated by spaces): "); 
    // scanf() formats the input and receives two integers entered by the user
    scanf("%d %d", &dividend, &divisor);  // Enter divisor and divisor
   
    quotient = dividend / divisor;  // The integral part of the numerator divided by the denominator
    remainder = dividend % divisor;  // %Is a modular operation, the remainder after division
    printf("merchant = %d, remainder = %d", quotient,remainder);
    return 0;
}

3. Input and output:

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

[attention!] The quotient obtained by division "/" is the integer part of the divisor divided by the divisor, and is not rounded.

[exercise 10] numerical comparison

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

#include <stdio.h>
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 be equal to %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 come from the rookie tutorial. The author rewrites the program, notes and examples, and adds difficulty analysis.