Professional Documents
Culture Documents
Esy Homework 04
Esy Homework 04
Embedded Systems
Problem Set 04: C programming
61FIT2ESY
HANU-Faculty of Information Technology Embedded Systems
Homework
Students should use the textbook [1] and Dev–C++ to solve problems below.
n! = n × (n − 1) × (n − 2) × . . . × 1, for n ≥ 1, n! = 1 for n = 0.
1. Write a program that reads a nonnegative integer and computes and prints its factorial.
2. Write a program that estimates the value of the mathematical constant e by using the
formula:
1 1 1
e = 1 + + + + ...
1! 2! 3!
3. Write a program that computes the value of ex by using the formula:
x x2 x3
ex = 1 + + + + ...
1! 2! 3!
0, 1, 1, 2, 3, 5, 8, 13, 21, . . .
begins with the terms 0 and 1 and has the property that each succeeding term is the sum of
the two preceding terms.
1. Write a nonrecursive function fibonacci(n) that calculates the nth Fibonacci number.
Use unsigned int for the function’s parameter and unsigned long long int for its
return type.
2. Determine the largest Fibonacci number that can be printed on your system.
1. Create an array with all elements initialized to 1 (true). Array elements with prime
indices will remain 1. All other array elements will eventually be set to zero.
2. Starting with array index 2 (index 1 is not prime), every time an array element is
found whose value is 1, loop through the remainder of the array and set to zero every
element whose index is a multiple of the index for the element with value 1. For array
index 2, all elements beyond 2 in the array that are multiples of 2 will be set to zero
(indices 4, 6, 8, 10, and so on.). For array index 3, all elements beyond 3 in the array
that are multiples of 3 will be set to zero (indices 6, 9, 12, 15, and so on.).
When this process is complete, the array elements that are still set to 1 indicate that the
index is a prime number. Write a program that uses an array of 1,000 elements to determine
and print the prime numbers between 1 and 999. Ignore element 0 of the array.
References
[1] Paul Deitel and Harvey Deitel, C How to Program, Pearson, 8th edition, 2016, Global
Edition.