Take you to brush (Nioke.com) C language 100 questions (fourth day)

✅Author’s introduction: Hello everyone, I am @I have to type code every day, a player who is a material transfer coder, I hope to work hard and make progress together!
📃Personal homepage: @The personal homepage who has to type [code every]
day🔥Series of columns: Niuke.com C language brushing column

Table of contents

Exercise 1: Alignment of Niu Niu

Exercise 2: Binary A+B

Exercise 3: Niu Niu learns addition

Exercise 4: Single Digits of Floating Point Numbers

Exercise 5: Niu Niu Buying Movie Tickets

Exercise 6: Calculating division with remainders

concluding remarks

Exercise 1: [Alignment of Niu Niu]

describe

Read in 3 integers, Niuniu tries to align the output right after the two numbers occupy the width of 8 spaces.

Enter description:

Enter three integers, separated by spaces.

Output description:

Output 3 integers with the second and third digits occupying 8 spaces and right-justify the output

Example 1

Enter: 2 7 5

Output: 2 7 5

# include  <stdio.h> 
int  main ()
 {
     int a,b,c;
     scanf ( "%d%d%d" ,&a,&b,&c);
     // Method 1: just count 8 spaces 
    printf ( " %d %d %d" ,a,b,c);
     // Method 2: Use the column width: %m.nd, m represents the column width, n represents how many decimal places are reserved 
    // Note: The default of using the column width is the whole For right-aligned, if you want to left-align and add -, you can 
    printf ( "%-8d%-8d%-8d" ,a,b,c);
     return  0 ;
}

Exercise 2: Binary [A+B]

describe

Input a hexadecimal number a, and an octal number b, and output the decimal result of a+b (range – 231~231-1).

Enter description:

A line, a hexadecimal number a, and an octal number b, separated by a space.

Output description:

One line, the decimal result of a+b.

Example 1

Input: 0x12 05

Output: 23

Remark:

Hexadecimal usually starts with 0x, eg 0xFF. Octal, usually starting with 0, such as 07.

# include  <stdio.h> 
int  main ()
 {
     int x,y;
     // %x: hexadecimal %o: octal 
    scanf ( "%0x%o" ,&x,&y);
     // inside the computer Binary, so it can be added directly and printed out in %d decimal form 
    printf ( "%d\n" ,x+y);
     return  0 ;
}

Exercise 3: [Niu Niu learns addition]

describe

Given two integers, ask to output the sum of these two integers

Enter description:

Enter two integers a, b (0 <= a, b <= 1000)

Output description:

output an integer

Example 1

Input: 1 2

output: 3

Supplement: addition (+), subtraction (-), multiplication (*), division (/), remainder (%), the following operations can be used!

#include <stdio.h>
#define SUM(x,y) ((x)+(y))

int Add(int x,int y){
    return x+y;
}

int  main ()
 {
     // Method 1: write 
    int x,y directly;
     scanf ( "%d%d" ,&x,&y);
     printf ( "%d\n" ,x+y);

    // Method 2: Encapsulate it into a function, it is recommended to write like this in the future, and develop a good habit 
    int sum = Add(x,y);
     printf ( "%d\n" ,sum);

    // Method 3: define the macro 
    printf ( "%d\n" ,SUM(x,y));

    return 0;
}

Exercise 4: [Single Digits of Floating Point Numbers]

describe

Given a floating point number, ask to get the single digit of the floating point number.

Data range: 0 < n ≤200 

Enter description:

A line, including a float.

Output description:

A line containing an integer, the single digit corresponding to the input floating-point number.

Example 1

Enter: 13.141

output: 3

# include  <stdio.h> 
int  main ()
 {
     float x = 0.0 ;
     scanf ( "%f" ,&x);
     // first cast the type, then take the remainder 
    printf ( "%d\n" ,( int )x % 10 );

    return 0;
}

Exercise 5: [Niu Niu Buying Movie Tickets]

describe

Niu Niu is buying movie tickets for his friends. Knowing that the ticket price of a movie is 100 yuan, what is the total ticket price of x friends?

Enter description:

Enter a positive integer x, indicating the number of Niu Niu’s friends

Output description:

Output the total fare (Niu Niu does not need to buy a ticket for himself)

Example 1

Input: 1

output: 100

# include  <stdio.h> 
int  main ()
 {
     // input x as friend 
    int x= 0 ;
     scanf ( "%d" ,&x);
     // total fare 
    printf ( "%d\n" , 100 *x );
     return  0 ;
}

Exercise 6: [Calculating division with remainders]

describe

Given two integers a and b (0 < a, b < 10,000), compute the integer quotient and remainder of dividing a by b.

Enter description:

A line, including two integers a and b, followed by the dividend and the divisor (non-zero), separated by spaces.

Output description:

A line containing two integers, the integer quotient and the remainder, separated by a space.

Example 1

Input: 15 2

Output: 7 1

# include  <stdio.h> 
int  main ()
 {
     int x,y;
     scanf ( "%d%d" ,&x,&y);
     // Use / to get the quotient, use % to get the remainder 
    printf ( "%d %d\ n" ,x/y,x%y);

    return 0;
}

concluding remarks

Today’s sharing is here! Sign up to join the quiz army through the link below! The real interview questions of various big factories are waiting for you!

Leave a Comment

Your email address will not be published. Required fields are marked *