Assignment 2
Assignment 2
Assignment 2
Computer &
Technology
Prepared by Muhammad Hashir khan
Roll No: 241274
Submitted to MR Ahsan Ali
Page 0|4
Assignment 2:
Question no 1:
Implement and explain Four Programming Paradigms Language/Algorithms to
solve array function/code of any practical application
Answer:
Four Programming Paradigms: Language/Algorithms to Solve Array
Functions:
In programming, paradigms refer to the different styles or approaches to writing code. Each paradigm
offers unique ways of thinking about and solving problems. Here, we will explore four programming
paradigms—imperative, functional, object-oriented, and declarative—by implementing an array function
that calculates the sum of all even numbers in an array. This practical application can be useful in various
scenarios such as data analysis or financial calculations.
1. Imperative Programming;
Imperative programming focuses on how to perform tasks using statements that change a program’s state.
It involves writing sequences of commands for the computer to execute.
Example Implementation in Python:
def sum_even_numbers_imperative(arr):
total = 0
for number in arr:
if number % 2 == 0:
total += number
return total
Example usage
array = [1, 2, 3, 4, 5, 6]
result = sum_even_numbers_imperative(array)
print(result)
Output:
12
Explanation:
We initialize a variable total to zero.
Page 1|4
We iterate through each element in the array.
For each element, we check if it is even (using modulo operation).
If it is even, we add it to total.
Finally, we return the computed sum.
2. Functional Programming
Functional programming treats computation as the evaluation of mathematical functions and avoids
changing-state and mutable data. It emphasizes the use of pure functions.
Example Implementation in Python:
def is_even(n):
return n % 2 == 0
def sum_even_numbers_functional(arr):
return sum(filter(is_even, arr))
Example usage
array = [1, 2, 3, 4, 5, 6]
result = sum_even_numbers_functional(array)
print(result) #
Output:
12
Explanation:
We define a helper function is_even that checks if a number is even.
The filter function applies is_even to each element of the array and returns only those that are
true.
We then use sum to calculate the total of these filtered values.
3. Object-Oriented Programming (OOP)
Object-oriented programming organizes code into objects that combine data and behavior. This paradigm
promotes encapsulation and reusability.
Example Implementation in Python:
class ArrayProcessor:
def __init__(self, arr):
self.arr = arr
Page 2|4
def sum_even_numbers(self):
total = sum(number for number in self.arr if number % 2 == 0)
return total
# Example usage
array_processor = ArrayProcessor([1, 2, 3, 4, 5, 6])
result = array_processor.sum_even_numbers()
print(result)
Output: 12
Explanation:
We create a class ArrayProcessor that takes an array as input during initialization.
The method sum_even_numbers computes the sum of even numbers using a generator expression
within the sum function.
This encapsulates functionality related to array processing within an object.
4. Declarative Programming
Declarative programming focuses on what the program should accomplish without specifying how to
achieve it. SQL is a common example of this paradigm.
Explanation:
In this pseudocode representation similar to SQL syntax:
o We select the sum of numbers from an array where each number satisfies the condition of
being even.
o This approach abstracts away how data is processed and focuses on what result we want.
Page 3|4