Course Programming Fundamentals (C language) 1 LAB 5 – Repetition Statements Main content 1 Get used to simple repetition statements 2 Get used to nested repetition statements 3 Start more complex dat[.]
Trang 1Course: Programming Fundamentals (C language)
LAB 5 – Repetition Statements Main content:
1 Get used to simple repetition statements
2 Get used to nested repetition statements
3 Start more complex data processing
4 Check coding styles
Practice:
1 Get used to simple repetition statements
1.1 Write a C program to display the following number series on the screen:
1.1.1 The series has N numbers with a natural number N input by a user from the
keyboard
N = 12:
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23 1.1.2 The series has N numbers with a natural number N input by a user from the
keyboard
N = 8:
0, 2, 6, 14, 30, 62, 126, 254 1.1.3 The series has the numbers less than a natural number N input by a user from the
keyboard
N = 500:
1, 4, 13, 40, 121, 364 1.1.4 The series has the numbers less than a natural number N input by a user from the
keyboard
N = 20:
3, 7, 11, 15, 19
1.2 Write a C program to process an integer number input by a user from the keyboard with the following requests:
1.2.1 How many digits are there in the given number?
1.2.2 Display the given number in a histogram as follows:
1.2.3 Calculate and return the total sum of all the odd digits in the given number
1.2.4 Calculate and return the total sum of all the digits at odd positions in the given
number It is supposed that the most left digit is counted at position 0 from the left
to the right
1.2.5 Check if the number is a prime number
1.2.6 Find and print an inversed number of the given number
Trang 2For example: An integer number input by a user is 123456
1.2.1 There are 6 digits in the number 123456
1.2.2 Histogram of the number 123456 is displayed below:
1: * 2: **
3: ***
4: ****
5: *****
6: ******
1.2.3 The total sum of all the odd digits in the number 123456 is 9
1.2.4 The total sum of all the digits at odd positions in the number 123456 is 12 1.2.5 The number 123456 is not a prime number
1.2.6 An inversed number of the number 123456 is 654321
1.3 Write a C program to calculate and display a total sum of all the even numbers sequentially input by a user from the keyboard till 0 is input Also return the number of the even numbers, the number of all the numbers, and the percentage of the even numbers over all the numbers that have been input
1.4 Write a C program to randomly generate N natural numbers in [0, 100] with a natural number N input by a user from the keyboard Calculate and display a total sum of all the odd numbers Also return the number of the odd numbers and the percentage of the odd numbers over all of N numbers that have been randomly generated
2 Get used to nested repetition statements
2.1 Write a C program to print the series of the perfect numbers less than a natural number N input by a user from the keyboard A perfect number is a number equal to the total sum of all of its factors
N = 30:
6, 28
2.2 Write a C program to print the series of the first N prime numbers with a natural number N input by a user from the keyboard A prime number is a number that has only two factors: 1 and itself
N = 5:
2, 3, 5, 7, 11
2.3 Write a C program to print the following square matrices with a size N input by a user from the keyboard
Trang 32.3.1 A square matrix of the even numbers below the non-main diagonal
10
10 8
2.3.2 The square matrix of the odd number above the non-main diagonal
9 11
11
2.3.3 The square matrix of the even numbers above the main diagonal
10 8
10
2.3.4 The square matrix of the odd numbers below the main diagonal
11
9 11
Trang 42.4 Write a C program to find the maximum value and the minimum value and their positions (row, column) in an NxM matrix of numeric values input by a user from the keyboard where
N and M are two natural numbers greater than zero also input by a user from the keyboard
3 Start more complex data processing
3.1 Write a C program to approximate a value of the following functions where x is a floating-point number in [0, 1] and N is a natural number for controlling an approximation accuracy: n<=N
3.1.1 Function ln(1+x)
3.1.2 Function tan-1x
3.2 Write a C program to generate and print an inversed 9-based complementary number of an integer number input by a user from the keyboard
Input 1: An integer number = 90145
Output 1: Its inversed 9-based complementary number = 45890
Input 2: An integer number = -213098
Output 2: Its inversed 9-based complementary number = -109687
3.3 Write a C program to generate and print a star matrix of an integer number input by a user from the keyboard Each column of the matrix contains the number of stars equal to the digit at the same position counting from zero for the most left If the given number is a positive one, it starts from the top down to the bottom Otherwise, it starts from the bottom up to the top Therefore, the size of the matrix is determined as: the number of rows is the maximum digit and the number of columns is the number of the digits
Input 1: an integer number = 90145
Output 1: a star matrix of the given number 90145 is given below:
*
*
*
Trang 5Input 2: An integer number = -213098
Output 2: A star matrix of the given number -213098 is given below:
*
3.4 Write a C program to print square matrices of stars with a size N which is an even natural number input by a user from the keyboard as follows:
3.4.1 A square matrix M1 of stars with N = 6
* * * *
* * * *
3.4.2 A square matrix M2 of stars with N = 6
* * * * * *
* * * * * *
* * * * * *
* * * * * *
4 Check coding styles
For each C program that you have written, check coding styles with the following points:
- How have you used blank lines?
- How have you used tabs for alignment?
- How have you used comments in your source code files?
- Above all, how about your naming conventions? What have you named in your source code files?