Problem 2 • Loops
Author: Er. Manoj Kumar • Computer Science Fundamentals
Check Whether an Integer is Prime or Composite
📋
Problem Statement
Given a positive integer N, determine whether it is a Prime number, Composite number, or Neither (<= 1) using an optimized trial division loop running up to sqrt(N).
📊
Variable Dictionary / Table
| Variable Name | Data Type | Role & Purpose |
|---|---|---|
| N | Integer (Long Long) | Input integer to test for primality |
| i | Integer (Long Long) | Loop divisor candidate (2, 3, 5, 7...) |
| isPrime | Boolean | Primality flag |
📝
Step-by-Step Formal Algorithm
Step 1: Start.
Step 2: Read integer N.
Step 3: If N <= 1:
Print 'Neither Prime nor Composite' and Stop.
Step 4: If N == 2 or N == 3:
Print 'Prime Number' and Stop.
Step 5: If N % 2 == 0:
Print 'Composite Number' and Stop.
Step 6: Initialize i = 3.
Step 7: While (i * i <= N):
If (N % i == 0):
Print 'Composite Number' and Stop.
i = i + 2.
Step 8: Print 'Prime Number'.
Step 9: Stop.
🔀
Flowchart (Interactive Graphical Diagram)
flowchart TD
Start([Start]) --> In[/"Read integer N"/]
In --> Chk1{"Is N <= 1 ?"}
Chk1 -- Yes --> Out1[/"Print 'Neither Prime nor Composite'"/]
Out1 --> Stop([Stop])
Chk1 -- No --> Chk2{"Is N == 2 or N == 3 ?"}
Chk2 -- Yes --> Out2[/"Print 'Prime Number'"/]
Out2 --> Stop
Chk2 -- No --> ChkEven{"Is N % 2 == 0 ?"}
ChkEven -- Yes --> OutComp[/"Print 'Composite Number'"/]
OutComp --> Stop
ChkEven -- No --> Init["Set i = 3"]
Init --> LoopCond{"Is i * i <= N ?"}
LoopCond -- Yes --> DivTest{"Does N % i == 0 ?"}
DivTest -- Yes --> OutComp2[/"Print 'Composite Number'"/]
OutComp2 --> Stop
DivTest -- No --> Inc["i = i + 2"]
Inc --> LoopCond
LoopCond -- No --> OutPrime[/"Print 'Prime Number'"/]
OutPrime --> Stop
🖥️
Flowchart (ANSI Terminal ASCII Chart)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read N |
+-----------------+
|
v
/ \
/ N <= 1?\ ---YES--> [Print Neither] --> [STOP]
\ /
| NO
v
/ \
/ N==2,3?\ ---YES--> [Print Prime] --> [STOP]
\ /
| NO
v
/ \
/ N%2==0?\ ---YES--> [Print Composite]--> [STOP]
\ /
| NO
v
+-----------------+
| Set i = 3 |
+-----------------+
|
v
/ \
/i*i <= N?\ ---NO---> [Print Prime] --> [STOP]
\ /
| YES
v
/ \
/ N%i==0?\ ---YES--> [Print Composite]--> [STOP]
\ /
| NO
v
+-----------------+
| i = i + 2 | ----> (Loop to i*i <= N)
+-----------------+
🧪
Dry-Run Execution Trace Table
| Execution Step / Case | Variables State 1 | Variables State 2 | Condition Evaluation | Output / Result |
|---|---|---|---|---|
| Test Case N=29 | N=29, i=3 | 3*3=9 <= 29, 29%3!=0 | i becomes 5; 5*5=25 <= 29, 29%5!=0; i becomes 7; 7*7=49 > 29 | Loop terminates -> 29 is PRIME |
| Test Case N=49 | N=49, i=3, 5, 7 | 7*7=49 <= 49, 49%7 == 0 | Divisor found at i=7 | Early Exit -> 49 is COMPOSITE |
⚡
Interactive C++ Compiler & Runner
Live In-BrowserEdit the code directly with the Edit Code button, provide custom inputs, and press Run Code to compile and execute in the live terminal console below!
📄
problem_2.cpp
GCC 13.2 • C++20
#include <iostream>
using namespace std;
int main() {
long long n;
cout << "Enter a positive integer: ";
if (!(cin >> n)) {
cout << "Invalid input! Please enter a valid integer.\n";
return 1;
}
if (n <= 1) {
cout << n << " is NEITHER prime nor composite (by definition).\n";
return 0;
}
if (n == 2 || n == 3) {
cout << n << " is a PRIME number.\n";
return 0;
}
if (n % 2 == 0) {
cout << n << " is a COMPOSITE number (divisible by 2).\n";
return 0;
}
bool isPrime = true;
for (long long i = 3; i * i <= n; i += 2) {
if (n % i == 0) {
isPrime = false;
cout << n << " is a COMPOSITE number (divisible by " << i << ").\n";
break;
}
}
if (isPrime) {
cout << n << " is a PRIME number.\n";
}
return 0;
}
⌨️ Standard Input (cin):
Presets:
💻 Terminal Output
• Ready to run
Input: 997 Output: 997 is a PRIME number.
Developer & Exam Key Takeaway
Testing up to sqrt(N) drops time complexity from O(N) to O(sqrt(N)), enabling instant primality verification even for numbers up to 10^12.