0% found this document useful (0 votes)
48 views

TD4 Functions

Uploaded by

baghyraalfhd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

TD4 Functions

Uploaded by

baghyraalfhd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

University of Tlemcen Author : Berrabah Sid Ahmed

Faculty of Sciences
Department of computer sciences

academic year: 2023 / 2024 1sr year of engineering


Algorithmic & Data Structures (S1)

Tutorial 4 : Functions

Exercise 1:
1. Provide the Algorithm for a function called digits_sum that reads an integer and returns the
sum of its digits.
2. Provide the Algorithm for a program that uses digits_sum to calculate the sum of the digits
of a number entered by the user. The sum, in turn, will be converted to the sum of its digits.
And so on, until the sum is only one digit long.
Example : 75  12  3.
3. Write the correspondent C code.

Exercise 2:
1. Give the algorithm for a function power that calculates and returns the nth power (positive
integer) of a real number p. n and p are provided as parameters.
2. Modify the function power to account for a negative n integer.

Exercise 3:
The natural logarithm of the number (1 + x) can be simulated by the following series:
Ln(1+x) = x – x2/2 + x3/3 – x4/4 + x5/5 - …
Give the algorithm for a function that calculates the series to a certain rank N.

Exercise 4:
A factorion is a positive integer that is equal to the sum of the factorials of its digits.
Examples:
145 = 1! + 4! + 5!
40585 = 4! + 0! + 5! + 8! + 5!

1. Provide the algorithm for a function that calculates the factorial of an integer given as a
parameter.
2. Provide the algorithm for a function that checks if a positive integer given as a parameter is a
factorion.

Exercise 5:
Give the algorithm for a procedure that determines and displays whether a positive integer
given as a parameter is an abundant, perfect, or deficient number.

A number is abundant when the sum of its divisors is greater than twice that number.
A number is perfect when the sum of its divisors is equal to twice that number.
A number is deficient when the sum of its divisors is less than twice that number.
Example :
6 is a perfect number : 1 + 2 + 3 + 6 = 2*6
8 is a deficient number : 1 + 2 + 4 + 8 < 2*8
12 is an abundant number : 1 + 2 + 3 + 4 + 6 +12 > 2*12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy