Академический Документы
Профессиональный Документы
Культура Документы
Section A1
Problem No 1
Variance Calculation
Write a C program to find the variance of a list of n numbers. First you have to take the value n
as input from the user, which is an integer. Then you have to take n real numbers as input from
the user. Then print out the variance (σ 2 ) of those numbers. Show 4 digits after the decimal point.
Recall that:
n 2
X (xi − x̄)
σ2 =
i=0
n
Use of array is prohibited in this task. (Hint: Expand the term inside the square...)
See the following example (minor precision error would be acceptable):
Problem No 2
Armstrong Number
Write a C program that takes an integer n and determines whether it is an Armstrong number or
not. An Armstrong number of three digits is an integer such that the sum of the cubes of its digits
is equal to the number itself. For example, 371 is an Armstrong number since 33 + 73 + 13 = 371.
1
Home Assignment on Loops
Section A2
Problem No 1
Series of Sum
Write a C program that takes a real number x as input. You need to apply Maclaurin series
expansion to compute the value of e−x . Terminate your summation when the absolute value of the
nth term is less than 10−4 . Print 6 digits after the decimal point in your output. Recall that, the
expansion is as follows.
x x2 x3
e−x = 1 − + − + ...
1! 2! 3!
Sample Input(s) Corresponding Output(s)
1 0.367857
2 0.135379
0.5 0.606510
Problem No 2
Number Triangle
Write a C program that takes an integer n and prints the first n rows of Pascal’s triangle as shown
in the sample input-output table.
2
Home Assignment on Loops
Section B1
Problem No 1
Series of Sum
Write a C program that takes a non-negative integer and computes the sum of the following series
up to nth term. You cannot use nested loops for this problem.
1 1 (1 + 3) 1 (1 + 3) (1 + 3 + 5) 1 (1 + 3) (1 + 3 + 5) (1 + 3 + 5 + 7)
− + − + ...
1! 2! 3! 4!
Problem No 2
Binary Numbers
Write a C program that takes two integer numbers as input and determines the number of posi-
tions where both numbers contain same bits in their binary representation. Consider the rightmost
position of two binary representations as the 1st position. For example, the binary representation
of the numbers 11 and 7 are 1011 and 111 respectively. So, there are two positions where the bits
are same. So, the answer is 2.
3
Home Assignment on Loops
Section B2
Problem No 1
Series of Sum
Write a C program that takes a real number x as input, where −1 < x < 1. You need to apply
trigonometric series expansion to compute the value of sin−1 x. Terminate your summation when
the absolute value of the nth term is less than 10−4 . Recall that, the expansion is as follows.
1 x3 1 3 x5 1 3 5 x7
sin−1 x = x + + + + ...
2 3 24 5 246 7
This expression will return the desired angle in radian value. For better understanding, you have
to print the corresponding degree value which should be a non-negative integer.
Problem No 2
Perfect Number
Write a C program that takes an integer n as input and determines whether n is a perfect number
or not. Remember that a perfect number is a number such that the sum of its divisors (except the
number itself) is equal to the number. Example of a perfect numbers is 496. The divisors of 496 is
1, 2, 4, 8, 16, 31, 62, 124, 248, 496 and 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 = 496.
4
Home Assignment on Loops
Section C1
Problem No 1
Series of Sum
Write a C program that takes a real number x as input where −1 < x ≤ 1. You need to apply
Maclaurin series expansion to compute the value of ln (1 + x). Terminate your summation when
the absolute value of the nth term is less than 10−4 . Print 6 digits after the decimal point in your
output. Recall that, the expansion is as follows.
x2 x3 x4
ln (1 + x) = x − + − + ...
2 3 4
Problem No 2
Number Triangle
Write a C program that takes an integer n and prints the left adjacent number triangle as shown
in the sample input-output table.
5
Home Assignment on Loops
Section C2
Problem No 1
Series of Sum
Write a C program that takes a real number x as input, which represents an angle in degree. You
need to apply Maclaurin series expansion to compute the value of cos x. Terminate your summation
when the absolute value of the nth term is less than 10−4 . Print 6 digits after the decimal point in
your output. Recall that, the expansion is as follows (where x is expressed in radian).
x2 x4 x6
cos x = 1 − + − + ...
2! 4! 6!
[In case you need it, approximate value of π is 3.14159265]
Problem No 2
Number Triangle
Write a C program that takes an integer n and prints the left adjacent number triangle as shown
in the sample input-output table.