2024
Programming Paradigms Language
Prepared by raja abdul muiz
Raja abdul muiz 0
Question no 1:
Implement and explain Four Programming Paradigms Language/Algorithms to solve array
function/code of any practical application?
Answer:
Introduction of Programming Paradigms
Paradigm can also be termed as method to solve some problem or do some task. Programming
paradigm is an approach to solve problem using some programming language or also we can say
it is a method to solve a problem using tools and techniques that are available to us following
some approach. There are lots for programming language that are known but all of them need
to follow some strategy when they are implemented and this methodology/strategy is
paradigms. Apart from varieties of programming language there are lots of paradigms to fulfill
each and every demand. They are discussed below:
1. Imperative programming paradigm:
It is one of the oldest programming
To demonstrate the implementation of four programming paradigms, we have to solve a practical
problem: finding the average of numbers in an array. This is a simple yet versatile problem that can be
approached using different paradigms. Below are implementations and explanations of how this
problem can be solved using Imperative, Object-Oriented, Functional, and Declarative programming
paradigms.
1. Imperative Programming Paradigm
1
The imperative paradigm focuses on “how” to achieve the solution by providing step-by-step
instructions. It uses loops, variables, and control structures to manipulate program state.
Implementation:
Explanation:
The program explicitly defines variables (sum and average) and uses a for loop to iterate through
each element in the array.
Each step is manually controlled: summing up elements, dividing by the count, and printing the
result.
This approach closely mirrors how computers execute instructions at a low level.
2. Object-Oriented Programming Paradigm
The object-oriented paradigm organizes code into objects that encapsulate data (attributes) and
behavior (methods). It emphasizes reusability, modularity, and abstraction.
Implementation:
2
Explanation:
The ArrayOperations class encapsulates both data (numbers) and behavior (calculateAverage
method).
The calculateAverage method abstracts away the logic for calculating averages.
This paradigm promotes modularity by separating concerns into reusable objects.
3. Functional Programming Paradigm
The functional paradigm treats computation as the evaluation of mathematical functions without
changing state or mutable data. It emphasizes immutability and higher-order functions.
Implementation:
from functools import reduce
# Define a function to calculate average using functional programming
def calculate_average(numbers):
# Use reduce to calculate the sum of elements in the list
total_sum = reduce(lambda x, y: x + y, numbers)
# Calculate average by dividing total_sum by length of list
return total_sum / len(numbers)
# Input array
numbers = [10, 20, 30, 40, 50]
# Call function and print result
3
print("Average:", calculate_average(numbers))
Explanation:
The reduce function applies a lambda function iteratively to compute the sum of all elements in
the list.
The calculation avoids mutable variables like sum, adhering to functional principles.
Functions are treated as first-class citizens (e.g., passing lambdas).
4. Declarative Programming Paradigm
The declarative paradigm focuses on “what” needs to be done rather than “how” it should be done. SQL
queries or high-level abstractions like LINQ in C# are examples.
Implementation:
// Input array
const numbers = [10, 20, 30, 40, 50];
// Use built-in methods map(), reduce(), etc., declaratively
const calculateAverage = arr => arr.reduce((sum, num) => sum + num) / arr.length;
// Call function and log result
console.log("Average:", calculateAverage(numbers));
Explanation:
The code specifies what needs to happen (reduce sums up values; division calculates average),
leaving how it happens abstracted away.
Built-in methods like reduce() handle iteration internally without explicit loops or state
management.
Declarative programming prioritizes readability over control flow details.
Comparison Across Paradigms
Feature Imperative Object-Oriented Functional Declarative
Focus How tasks are Encapsulation & Immutability & What needs to be
performed Reuse Functions done
State Mutable Encapsulated Immutable Abstracted
Management
Code Moderate High High Very High
Readability
Reusability Low High Moderate Moderate