---
title: "Prime number program: procedural and OOP"
subject: "Object Oriented Programming"
chapter: 1
key: "semester_3/object_oriented_programming:prime-procedural-oop"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/prime-procedural-oop
site: BCT Engineering
---

# Prime number program: procedural and OOP

Object Oriented Programming · Chapter 1: Introduction to Object Oriented Programming

- Asked in: 2070 Asar, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/prime-procedural-oop
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-1

## Question

Write a program to find prime number in procedural and object oriented ways.

## Short answer

The procedural version uses a separate isPrime function checking divisibility up to sqrt(n). The OOP version wraps the number inside a class with private member n, and methods read(), isPrime(), display() to bind data and behavior securely.

## Answer

A prime number is greater than 1 and divisible only by 1 and itself. To test *n*, check divisibility by every number from 2 to √n.

#### Procedural way

Data (`n`) and the function are separate; the number is passed to a function.

```cpp
#include <iostream>
using namespace std;

int isPrime(int n) {
    if (n < 2) return 0;
    for (int i = 2; i * i <= n; i++)
        if (n % i == 0) return 0;
    return 1;
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    if (isPrime(n)) cout << n << " is prime" << endl;
    else cout << n << " is not prime" << endl;
    return 0;
}
```

#### Object oriented way

The number and the function that tests it are bound together in a class.

```cpp
#include <iostream>
using namespace std;

class Number {
    int n;                       // data hidden inside the class
public:
    void read() {
        cout << "Enter a number: ";
        cin >> n;
    }
    bool isPrime() {
        if (n < 2) return false;
        for (int i = 2; i * i <= n; i++)
            if (n % i == 0) return false;
        return true;
    }
    void display() {
        if (isPrime()) cout << n << " is prime" << endl;
        else cout << n << " is not prime" << endl;
    }
};

int main() {
    Number obj;
    obj.read();
    obj.display();
    return 0;
}
```

**Sample output (both):** for input 17, `17 is prime`; for input 15, `15 is not prime`.

**Difference:** in the procedural version `n` is a free variable passed around; in the OOP version `n` is private data of the object and only its member functions can use it.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Up to which value is divisibility checked to test if n is prime?
   A. n/2 only starting at 0
   B. n*n
   C. sqrt(n)
   D. n+1
   Answer: C. Checking i from 2 while i*i <= n is enough, i.e. up to sqrt(n). (Key point: Prime test method)

2. What does the isPrime code do for n < 2?
   A. Returns prime
   B. Loops forever
   C. Divides by zero
   D. Returns not prime
   Answer: D. A prime must be greater than 1, so n < 2 returns false/0. (Key point: Edge case)

3. How does the OOP version differ from the procedural one?
   A. n is private data of the object used only by member functions
   B. n is passed to a global function
   C. n is a global variable
   D. There is no function to test primality
   Answer: A. In OOP, data and the function are bound in class Number. (Key point: Difference of styles)

4. What is printed for input 15?
   A. 15 is prime
   B. 15 is not prime
   C. 17 is prime
   D. Nothing
   Answer: B. 15 = 3 x 5; the loop finds 15 % 3 == 0. (Key point: Sample output)

## Related in Object Oriented Programming

- [Compare procedural and object oriented programming](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprocedural-vs-oop-compare) — Object Oriented Programming, chapter 1
- [Advantages of OOP over procedural programming](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoop-vs-procedural-advantages) — Object Oriented Programming, chapter 1, asked 5×
- [Simple program illustrating OOP concept](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoop-simple-program) — Object Oriented Programming, chapter 1
- [Limitations of procedural programming](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprocedural-limitations) — Object Oriented Programming, chapter 1
- [How OOP differs from POP](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aoop-vs-pop-differ) — Object Oriented Programming, chapter 1
- [Vector class addition by object argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avector-class-addition) — Object Oriented Programming, chapter 1

## Related in other subjects

- [Object oriented system and core concepts](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aoo-system-concepts-explained) — Object Oriented Analysis and Design, chapter 1
- [Algorithmic vs object-oriented decomposition](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aalgorithmic-vs-oo-decomposition) — Object Oriented Analysis and Design, chapter 1
- [Prime number checking processor](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Aspp-prime-check) — Embedded System, chapter 2
- [Characteristics and advantages of OOP](https://www.bctengineer.com/question.md?key=semester_5%2Fsoftware_engineering%3Aoop-characteristics) — Software Engineering, chapter 2
