Вы находитесь на странице: 1из 6

Home Assignment on Loops

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):

Sample Input(s) Corresponding Output(s)


5
2.0000
12345
6
3.4892
3 1 2.5 2.3 1.1 6.6
5
8.0000
1 3 5 7 9 11

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.

Sample Input(s) Corresponding Output(s)


153 Armstrong Number
371 Armstrong Number
407 Armstrong Number
125 Not an Armstrong Number

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.

Sample Input(s) Corresponding Output(s)


1
1 1
4
1 2 1
1 3 3 1
1
1 1
5 1 2 1
1 3 3 1
1 4 6 4 1

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!

Sample Input(s) Corresponding Output(s)


1 1
2 -1
4 19

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.

Sample Input(s) Corresponding Output(s)


5 10 0
2 15 1
7 5 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.

Sample Input(s) Corresponding Output(s)


0.5 30
0.867 60
-1 270
1 90

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.

Sample Input(s) Corresponding Output(s)


28 Perfect Number
8128 Perfect Number
18 Not a Perfect Number

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

Sample Input(s) Corresponding Output(s)


1 0.693097
0.5 0.405532
0 0.000000
1 0.688172

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.

Sample Input(s) Corresponding Output(s)


1 2
3 4
3 5
3 4
1 2
1 2
3 4
5 6
7 8
5 9
7 8
5 6
3 4
1 2

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]

Sample Input(s) Corresponding Output(s)


-150 -0.866017
30 0.866054
60 0.499965
90 0.000000

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.

Sample Input(s) Corresponding Output(s)


1
2 3
4 5
4 6 7
8 9
10 11
12
1
2 3
3 4 5
6 7
8

Вам также может понравиться