ENSTA - Ecole Nationale Supérieure des Technologies Avancées
Cité Diplomatique
Ex Centre Biomédical Dergana
Bordj El Kiffan, Alger.
TD 02
7th November 2024
1. Sum of Two Numbers
Objective: Write an algorithm that takes two numbers as input and outputs their sum.
Hint: Use addition to calculate the sum of two variables.
2. Print Numbers from 1 to N
Objective: Create an algorithm that prints numbers from 1 to a given integer N.
Hint: Use a loop that increments a counter up to N.
3. Reverse a String
Objective: Write an algorithm that takes a string as input and outputs it in reverse.
Hint: Loop through the string from the end to the beginning.
4. Count the Number of Digits
Objective: Create an algorithm that counts the number of digits in a given integer.
Hint: Use division to separate each digit.
5. Sum of Digits of a Number
Objective: Write an algorithm that calculates the sum of the digits of a given number.
Hint: Use modulus and division to extract and sum each digit.
6. Find the Minimum and Maximum in an Array
Objective: Write an algorithm that finds the smallest and largest values in an array.
Hint: Use a loop to compare each element to the current minimum and maximum values.
7. Check for Prime Number
● Objective: Write an algorithm that checks if a given number n is a prime number.
● Hint: A prime number has no divisors other than 1 and itself. Try dividing n by numbers up
to the square root of n.
8. Fibonacci Sequence Generator
● Objective: Create an algorithm that generates the first N numbers in the Fibonacci
sequence.
● Hint: Start with 0 and 1 and add each pair of numbers to get the next number in the
sequence.
9. Count Vowels in a String
● Objective: Write an algorithm that counts the number of vowels in a given string.
● Hint: Use a loop to check each character, and count vowels like a, e, i, o, u.
10. Calculate Power (Exponentiation)
● Objective: Write an algorithm to calculate x raised to the power of y (x^y).
● Hint: Use a loop to multiply x by itself y times.
11. Check Palindrome
● Objective: Write an algorithm to check if a given string is a palindrome (reads the same
forwards and backwards).
● Hint: Compare characters from the beginning and end of the string moving toward the
center.
12. Array Sum
● Objective: Write an algorithm that calculates the sum of all elements in an array.
● Hint: Use a loop to iterate through the array and keep a running total.
13. Count Occurrences of a Character
● Objective: Write an algorithm to count how many times a specific character appears in a
string.
● Hint: Use a loop to compare each character in the string to the target character.
14. Remove Duplicates from an Array
● Objective: Write an algorithm to remove duplicate elements from an array.
● Hint: Use a new array or list to store unique values, and check if each element is already
in the list before adding.
15. Bubble Sort
● Objective: Write an algorithm to sort an array of numbers in ascending order using the
bubble sort technique.
● Hint: Swap adjacent elements if they are out of order, repeating until no more swaps are
needed.
16. Simple Interest Calculator
● Objective: Write an algorithm to calculate simple interest. The formula is SI = (P * R
* T) / 100, where P is the principal, R is the rate, and T is time.
● Hint: Use the provided formula and take input for P, R, and T.