C programming rookie exercises 100 questions (11-20)

Time:2021-6-21

[exercise 11] calculate the byte sizes of int, float, double and char

Title:
Calculates the byte sizes of int, float, double and char
1. Analysis
The sizeof operator is used to calculate the byte sizes of int, float, double and char variables.
Sizeof is a monocular operator, which gives the storage size of its operands in byte form.
2. Procedure:

#include  
int main()
{
    int integerType;
    float floatType;
    double doubleType;
    char charType;
 
    //The sizeof operator is used to calculate the byte size of a variable
    printf("Size of int: %ld bytes\n",sizeof(integerType));
    printf("Size of float: %ld bytes\n",sizeof(floatType));
    printf("Size of double: %ld bytes\n",sizeof(doubleType));
    printf("Size of char: %ld byte\n",sizeof(charType));
 
    return 0;
}

3. Input and output:

Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte


Exchange the values of two numbers

Title:
Exchange the value of two numbers
1. Analysis
This routine uses intermediate variables.
2. Procedure:

#include 
int main()
{
    float firstNum, secondNum, tempVar;
 
    Printf ("enter two floating-point numbers (separated by spaces):"); 
    scanf("%f %f", &firstNum, &secondNum); 
	
	tempVar = firstNum;
	firstNum = secondNum;
    secondNum = tempVar;

    Printf ("- firstnum after exchange =%. 2F", firstnum));
    Printf ("- secondnum after exchange =%. 2F", secondnum));
 
    return 0;
}

3. Input and output:

Enter two floating-point numbers (separated by spaces): 1 2
After exchange, firstnum = 2.00
After exchange, secondnum = 1.00


[exercise 13] judge odd / even numbers

Title:
Judge odd / even
1. Analysis
Judge whether the integer entered by the user is odd or even.
2. Procedure:

#include 
int main()
{
    int iNum;
 
    Printf ("enter an integer):";
    scanf("%d", &iNum);
 
    //Judge the remainder of the number divided by two
    if(iNum % 2 == 0)
        Printf ("% d is even.", iNum);
    else
        Printf ("% d is odd.", iNum);
 
    return 0;
}

3. Input and output:

Enter an integer: 99
99 is odd.


[exercise 14] parity judgment in interval

Title:
Judging odd / even numbers in a cyclic interval
1. Analysis
The odd / even number in the range of cyclic output interval can be judged by the remainder divided by 2.
2. Procedure:

#include  
int main()
{
    int i;
    for(i=1;i<=10;i++)
    {
        if(i%2 == 0)
            printf(" %d\n", i);
    }
    return 0;
}

3. Input and output:

2
4
6
8
10


[exercise 15] judge vowels / consonants

Title:
Judging vowels / consonants
1. Analysis
Judge whether the input letter is a vowel or a consonant. English has 26 letters, only a, e, I, O and u are vowels.
2. Procedure:

#include 
int main()
{
    char cInput;
    int isLowercaseVowel, isUppercaseVowel;
 
    Printf ("enter a letter):";
    scanf("%c",&cInput);
 
    isLowercaseVowel = ((cInput=='a') || (cInput=='e') || (cInput=='i') || (cInput=='o') || (cInput=='u'));  //  Small letter vowels
    isUppercaseVowel = ((cInput=='A') || (cInput=='E') || (cInput=='I') || (cInput=='O') || (cInput=='U'));  //  Capital vowels
 
    if (isLowercaseVowel || isUppercaseVowel)
        Printf ("% C is a vowel.", cInput);
    else
        Printf ("% C is a consonant.", cInput);
    return 0;
}

3. Input and output:

Enter a letter: s
S is a consonant

AttentionThe relational operator ‘= = = takes precedence over the logical operator’ | “, so the parenthesis () in (cinput = =’a ‘) can also be omitted.


Exercise 16: judge the largest of the three numbers

Title:
Judge the largest of the three numbers
1. Analysis
Scanf () is used to receive input, and if branch statement is used to judge condition.
2. Procedure:

#include 
int main()
{
    float fNum1, fNum2, fNum3;    
    
    Printf ("please enter 3 numbers (separated by commas):"); 
    scanf("%f,%f,%f", &fNum1, &fNum2, &fNum3); 
    
    if(fNum1>=fNum2 && fNum1>=fNum3 )
        Printf ("the maximum number is%. 2F.", fNum1);
 
    if(fNum2>=fNum1 && fNum2>=fNum3 )
        Printf ("the maximum number is%. 2F.", fNum2);
        
    if(fNum3>=fNum1 && fNum3>=fNum1 )
        Printf ("the maximum number is%. 2F.", fNum3);
    
    return 0;
}

3. Input and output:

Please enter three numbers (separated by commas): 0, 1, 2
The maximum number is 2.00

Attention1. This program is not the best way to judge the condition many times, but the idea is easy to understand and the program readability is good. 2. If the title is understood as finding the maximum number of values, this procedure is correct; But if the problem is understood as finding the largest number, not only the output result does not meet the requirements, but also the overall algorithm has bugs.


Exercise 17 quadratic equation with one variable

Title:
Quadratic equation of one variable
1. Analysis
Find the root of quadratic equation with one variable: AX2 + BX + C = 0.
Enter the values of three real numbers a, B, C, and a is not equal to 0.
2. Procedure:

#include 
#include  
int main()
{
    float fParaA,fParaB,fParaC,fRoofX1,fRoofX2,fDelta;

    Printf ("\ n univariate quadratic equation: AX2 + BX + C = 0:");
    Printf ("\ n input coefficients a, B, C (separated by commas):");
    scanf("%f %f %f",&fParaA,&fParaB,&fParaC);
    
    if(fParaA!=0)
    {
        fDelta = fParaB*fParaB - 4*fParaA*fParaC;
        if(fDelta>0)
        {
            fRoofX1=(-fParaB+sqrt(fDelta))/(2*fParaA);
            fRoofX2=(-fParaB-sqrt(fDelta))/(2*fParaA);
            printf("x1 = %0.2f, x2 = %0.2f\n",fRoofX1,fRoofX2); 
        }
        else if(fDelta==0)
        {
            fRoofX1=(-fParaB+sqrt(fDelta))/(2*fParaA);
            printf("x1 = x2 = %0.2f\n",fRoofX1); 
        }
        else
        {
            There is no real solution to the equation\ n");
        }
    }

    return 0;
}

3. Input and output:

Quadratic equation of one variable: AX2 + BX + C = 0:1
Input coefficients a, B, C (separated by commas): 1,4, – 5
x1 = 1.00, x2 = -5.00


[exercise 18] judge leap year

Title:
Judge leap year
1. Analysis
The year divisible by 4 is leap year, but the year divisible by 100 is not leap year; But the year divided by 400 is still a leap year.
2. Procedure:

#include  
int main()
{
    int iYear;
 
    Printf ("input year");
    scanf("%d",&iYear);

    //The year divided by 4 is a leap year, but the year divided by 100 is not a leap year, but the year divided by 400 is a leap year
    if((iYear%4 == 0 && iYear%100 != 0) || iYear%400 == 0)
        Printf ("% d is a leap year", iyear));
    else
        Printf ("% d is not a leap year", iyear));
    
    return 0;
}

3. Input and output:

Input year: 2021
2021 is not a leap year


[exercise 19] judge positive / negative numbers

Title:
Judge positive / negative / zero
1. Analysis
Divide two numbers, if there is a remainder, output the remainder
2. Procedure:

#include 
int main()
{
    float fNum;
 
    Printf ("enter a real number):";
    scanf("%f", &fNum);
 
    if(fNum < 0.0)
        Printf ("input%. 2F is a negative number, FNum)";
    else if(fNum > 0.0)
        Printf ("input%. 2F is a positive number, FNum)";
    else
        Printf ("input%. 2F is zero", FNum));
        
    return 0;
}

3. Input and output:

Enter a real number: 0.0
The 0.00 entered is zero

AttentionIt is possible to compare the input value with the value 0.0. However, if we compare the size of two real variables, we can not directly compare whether they are equal due to the problem of significant digits.


[exercise 20] determine whether the input characters are alphabetic

Title:
Determine whether the input character is alphabetic
1. Analysis
Both upper and lower case letters belong to the alphabet
2. Procedure:

#include 
int main()
{
    char cInput;
    Printf ("enter a character):";
    scanf("%c",&cInput);
 
    if( (cInput>='a' && cInput<='z') || (cInput>='A' && cInput<='Z'))
        Printf ("% C is a letter", cinput));
    else
        Printf ("% C is not a letter", cinput));
 
    return 0;
}

3. Input and output:

Enter a character: S
S is the letter


Copyright notice:
The title and routine are from the rookie course. The author rewrites the program, notes and examples, and adds the analysis of difficulties.