Explain the difference between procedural programming and object-oriented programming, and provide examples of each.

1 Answers
Answered by suresh

Procedural Programming vs Object-Oriented Programming

Procedural programming and object-oriented programming are two different paradigms used in software development. Here is an explanation of the key differences between the two:

Procedural Programming:

In procedural programming, the focus is on writing procedures or functions that perform operations on data. The program is structured in a linear manner, with functions being called sequentially. Data is typically stored in variables and passed between functions as needed.

Example of procedural programming language: C language. Here is a simple example of a procedural program in C that calculates the sum of two numbers:

```c
#include

int sum(int a, int b) {
return a + b;
}

int main() {
int result = sum(5, 3);
printf("The sum is: %d", result);
return 0;
}
```

Object-Oriented Programming:

Object-oriented programming focuses on creating objects that encapsulate data and behavior. Objects are instances of classes, which define the structure and behavior of the objects. Inheritance, polymorphism, and encapsulation are key concepts in object-oriented programming.

Example of object-oriented programming language: Java. Here is a simple example of an object-oriented program in Java that models a car:

```java
public class Car {
private String make;
private String model;

public Car(String make, String model) {
this.make = make;
this.model = model;
}

public String getMake() {
return make;
}

public String getModel() {
return model;
}

public void startEngine() {
System.out.println("Engine started for " + make + " " + model);
}
}

public class Main {
public static void main(String[] args) {
Car myCar = new Car("Toyota", "Camry");
System.out.println("Make: " + myCar.getMake());
System.out.println("Model: " + myCar.getModel());
myCar.startEngine();
}
}
```

These examples provide a basic understanding of the differences between procedural and object-oriented programming paradigms.

Answer for Question: Explain the difference between procedural programming and object-oriented programming, and provide examples of each.