Open In App

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 paradigm. It features close relation to machine architecture. It is based on Von Neumann architecture. It works by changing the program state through assignment statements. It performs step by step task by changing state. The main focus is on how to achieve the goal. The paradigm consist of several statements and after execution of all the result is stored.

Advantages: 

  1. Very simple to implement
  2. It contains loops, variables etc.

Disadvantage:  

  1. Complex problem cannot be solved
  2. Less efficient and less productive
  3. Parallel programming is not possible
Examples of Imperative programming paradigm:
C : developed by Dennis Ritchie and Ken Thompson
Fortran : developed by John Backus for IBM
Basic : developed by John G Kemeny and Thomas E Kurtz

#include <iostream>

int main() {
    // Array to store marks
    int marks[5] = { 12, 32, 45, 13, 19 };

    // Variable to store the sum of marks
    int sum = 0;

    // Variable to store the average
    float average = 0.0;

    // Calculate the sum of marks
    for (int i = 0; i < 5; i++) {
        sum = sum + marks[i];
    }

    // Calculate the average
    average = sum / 5.0;

    // Output the average
    std::cout << "Average of five numbers: " << average << std::endl;

    return 0;
}
#include <stdio.h>

int main() {
    // Array to store marks
    int marks[5] = { 12, 32, 45, 13, 19 };

    // Variable to store the sum of marks
    int sum = 0;

    // Variable to store the average
    float average = 0.0;

    // Calculate the sum of marks
    for (int i = 0; i < 5; i++) {
        sum = sum + marks[i];
    }

    // Calculate the average
    average = (float)sum / 5.0;

    // Output the average
    printf("Average of five numbers: %.2f\n", average);

    return 0;
}
public class Main {
    public static void main(String[] args) {
        // Array to store marks
        int[] marks = {12, 32, 45, 13, 19};

        // Variable to store the sum of marks
        int sum = 0;

        // Variable to store the average
        float average = 0.0f;

        // Calculate the sum of marks
        for (int i = 0; i < 5; i++) {
            sum = sum + marks[i];
        }

        // Calculate the average
        average = sum / 5.0f;

        // Output the average
        System.out.println("Average of five numbers: " + average);
    }
}
def main():
    # Array to store marks
    marks = [12, 32, 45, 13, 19]

    # Variable to store the sum of marks
    total_marks = sum(marks)

    # Calculate the average
    average = total_marks / len(marks)

    # Output the average
    print("Average of five numbers:", average)

if __name__ == "__main__":
    main()
#this code is added by Utkarsh
// Array to store marks
let marks = [12, 32, 45, 13, 19];

// Variable to store the sum of marks
let sum = 0;

// Variable to store the average
let average = 0.0;

// Calculate the sum of marks
for (let i = 0; i < 5; i++) {
    sum = sum + marks[i];
}

// Calculate the average
average = sum / 5.0;

// Output the average
console.log("Average of five numbers: " + average);

Output
Average of five numbers: 24.2

Imperative programming is divided into three broad categories: Procedural, OOP and parallel processing. These paradigms are as follows:

Examples of Procedural programming paradigm:
C : developed by Dennis Ritchie and Ken Thompson
C++ : developed by Bjarne Stroustrup
Java : developed by James Gosling at Sun Microsystems
ColdFusion : developed by J J Allaire
Pascal : developed by Niklaus Wirth

#include <iostream>
using namespace std;
int main()
{
    int i, fact = 1, num;
    cout << "Enter any Number: ";
    cin >> number;
    for (i = 1; i <= num; i++) {
        fact = fact * i;
    }
    cout << "Factorial of " << num << " is: " << fact << endl;
    return 0;
}
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Create a Scanner object for reading input
        Scanner scanner = new Scanner(System.in);

        // Prompt user to enter a number
        System.out.println("Enter any Number: ");

        // Read number from user input
        int num = scanner.nextInt();

        // Initialize factorial to 1
        int fact = 1;

        // Calculate factorial using a for loop
        for (int i = 1; i <= num; i++) {
            fact = fact * i;
        }

        // Print the factorial of the number
        System.out.println("Factorial of " + num + " is: " + fact);
    }
}
# Prompt user to enter a number
num = int(input("Enter any Number: "))

fact = 1  # Initialize factorial variable

# Calculate factorial
for i in range(1, num + 1):
    fact = fact * i

# Print the factorial
print("Factorial of", num, "is:", fact)
// Prompt the user for input
let num = prompt("Enter any Number: ");

// Initialize the factorial value to 1
let fact = 1;

// Calculate the factorial of the number
for (let i = 1; i <= num; i++) {
    fact = fact * i;
}

// Print the factorial of the number
console.log("Factorial of " + num + " is: " + fact);

Then comes OOP,

Advantages: 

Examples of Object Oriented programming paradigm:
Simula : first OOP language
Java : developed by James Gosling at Sun Microsystems
C++ : developed by Bjarne Stroustrup
Objective-C : designed by Brad Cox
Visual Basic .NET : developed by Microsoft
Python : developed by Guido van Rossum
Ruby : developed by Yukihiro Matsumoto
Smalltalk : developed by Alan Kay, Dan Ingalls, Adele Goldberg

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;

// Class Signup
class Signup {
    int userid;
    string name;
    string emailid;
    char sex;
    long mob;

public:
    // Function to create and object using
    // the parameters
    void create(int userid, string name, string emailid,
                char sex, long mob)
    {
        cout << "Welcome to GeeksforGeeks\nLets create "
                "your account\n";
        this->userid = 132;
        this->name = "Radha";
        this->emailid = "radha.89@gmail.com";
        this->sex = 'F';
        this->mob = 900558981;
        cout << "your account has been created" << endl;
    }
};

// Driver Cpde
int main()
{
    cout << "GfG!" << endl;

    // Creating Objects
    Signup s1;
    s1.create(22, "riya", "riya2@gmail.com", 'F', 89002);

    return 0;
}
import java.io.*;

class GFG {
    public static void main(String[] args)
    {
        System.out.println("GfG!");
        Signup s1 = new Signup();
        s1.create(22, "riya", "riya2@gmail.com", 'F',
                  89002);
    }
}

class Signup {
    int userid;
    String name;
    String emailid;
    char sex;
    long mob;

    public void create(int userid, String name,
                       String emailid, char sex, long mob)
    {
        System.out.println(
            "Welcome to GeeksforGeeks\nLets create your account\n");
        this.userid = 132;
        this.name = "Radha";
        this.emailid = "radha.89@gmail.com";
        this.sex = 'F';
        this.mob = 900558981;
        System.out.println("your account has been created");
    }
}
class Signup:
    def __init__(self):
        self.userid = 0
        self.name = ""
        self.emailid = ""
        self.sex = ""
        self.mob = 0

    def create(self, userid, name, emailid, sex, mob):
        print("Welcome to GeeksforGeeks\nLets create your account\n")
        self.userid = 132
        self.name = "Radha"
        self.emailid = "radha.89@gmail.com"
        self.sex = 'F'
        self.mob = 900558981
        print("your account has been created")


if __name__ == "__main__":
    print("GfG!")
    s1 = Signup()
    s1.create(22, "riya", "riya2@gmail.com", 'F', 89002)
using System;

class GFG {
    static void Main(string[] args)
    {
        Console.WriteLine("GfG!");
        Signup s1 = new Signup();
        s1.create(22, "riya", "riya2@gmail.com", 'F',
                  89002);
    }
}

class Signup {
    public int userid;
    public string name;
    public string emailid;
    public char sex;
    public long mob;

    public void create(int userid, string name,
                       string emailid, char sex, long mob)
    {
        Console.WriteLine(
            "Welcome to GeeksforGeeks\nLets create your account\n");
        this.userid = 132;
        this.name = "Radha";
        this.emailid = "radha.89@gmail.com";
        this.sex = 'F';
        this.mob = 900558981;
        Console.WriteLine("your account has been created");
    }
}
// This code is contributed by akshatve2zi2
class Signup {
    constructor(userid, name, emailid, sex, mob) {
        this.userid = userid;
        this.name = name;
        this.emailid = emailid;
        this.sex = sex;
        this.mob = mob;
    }
    
    create(userid, name, emailid, sex, mob) {
        console.log("Welcome to GeeksforGeeks\nLets create your account\n");
        this.userid = 132;
        this.name = "Radha";
        this.emailid = "radha.89@gmail.com";
        this.sex = 'F';
        this.mob = 900558981;
        console.log("your account has been created");
    }
}

console.log("GfG!");
let s1 = new Signup();
s1.create(22, "riya", "riya2@gmail.com", 'F', 89002);
// This code is contributed by akshatve2zi2

Output
GfG!
Welcome to GeeksforGeeks
Lets create your account

your account has been created

2. Declarative programming paradigm: 
It is divided as Logic, Functional, Database. In computer science the declarative programming is a style of building programs that expresses logic of computation without talking about its control flow. It often considers programs as theories of some logic.It may simplify writing parallel programs. The focus is on what needs to be done rather how it should be done basically emphasize on what code is actually doing. It just declares the result we want rather how it has be produced. This is the only difference between imperative (how to do) and declarative (what to do) programming paradigms. Getting into deeper we would see logic, functional and database.

predicates
sumoftwonumber(integer, integer).
clauses
sumoftwonumber(0, 0).
sumoftwonumber(N, R) :-
N > 0,
N1 is N - 1,
sumoftwonumber(N1, R1),
R is R1 + N.

Examples of Functional programming paradigm:
JavaScript : developed by Brendan Eich
Haskell : developed by Lennart Augustsson, Dave Barton
Scala : developed by Martin Odersky
Erlang : developed by Joe Armstrong, Robert Virding
Lisp : developed by John Mccarthy
ML : developed by Robin Milner
Clojure : developed by Rich Hickey

The next kind of approach is of Database.

CREATE DATABASE databaseAddress;
CREATE TABLE Addr (
PersonID int,
LastName varchar(200),
FirstName varchar(200),
Address varchar(200),
City varchar(200),
State varchar(200)
);

Article Tags :