You are on page 1of 2

CC-112 Programming Fundamentals SPRING 2022

Assignment 02
Objectives:
 To comprehend basic programming skills
 Get familiar with the use of repetition control structure

Prerequisite Skills:
 Knowledge of C’s basic constructs (expressions, operators, if-else control structure)
 Understanding about working of repetition control structures (for, while, do while)

Problem 1 – Finding approximate value of e

Overview

Euler’s number, e, is used as the base of natural logarithms. It can be approximated using the
following formula.

e = 1 / 0! +1 / 1! + 1/ 2! + 1/ 3! +.....................+ 1/ (n-1)! + 1/ n!

Problem Statement

Write a program that approximate e using a loop that terminates when the difference between two
successive values of e is less than or equal to n where n is a floating-point number i.e., 0.00001 or
0.001

Sample Input and Output:

Input Output
0.0000001 2.718282
0.01 2.708333

Assignment 02 Page 1 of 2
CC-112 Programming Fundamentals SPRING 2022

Problem 2 – Calculation of Arithmetic, Geometric and Harmonic


Means
Overview

Statisticians use many different algorithms in addition to the arithmetic average. Two other
averages are the arithmetic and harmonic mean. The geometric mean of a set of n numbers, x1 , x2 ,
x3 , x4 , ……………. xn-1, xn is defined by the following formula:

n  x1 * x2 * x3 * ........ * xn 
The harmonic mean is defined by the following formula:
n
 1 1 1 1 

 x  x  x  ........ x  
 1 2 3 n 

Problem Statement
Write a program that reads series of positive numbers and calculates the average, geometric and
harmonic means.

Sample Input and Output:


The input will be terminated if user enters -1, which will not include in the computations

Input Output
6 Arithmetic mean = 13
20 Geometric mean = 11.771
16 Harmonic mean = 10.551
10
-1
17 Arithmetic mean = 33
22 Geometric mean = 22.231
33 Harmonic mean = 14.453
88
5
-1

Extra Credit:
Extra credit will be awarded for the output formatting (alignment, floating point precision)

File(s) to be submitted:
euler.c, mean.c source code of the programs will be submitted to Teacher Assistant in the
electronic format i.e. softcopy.

Deadline: September 11, 2022. (11:59pm)

Assignment 02 Page 2 of 2

You might also like