Skip to content
DATA STRUCTURE
DATA STRUCTURE

Learn everything about Data structure

  • Algorithms
  • DSA
  • Array
  • Stack
  • Queue
  • Matrix
  • Programs
DATA STRUCTURE

Learn everything about Data structure

Algorithm to find whether a number is prime or not

YASH PAL, September 26, 2022March 6, 2023

In this tutorial, we are going to write an algorithm to verify whether a number is prime or not. using this algorithm we can write a program to verify whether a number is prime or not or find whether a number is prime or not in most programming languages like java, python, c++, and c.

[br]

Algorithm to find Prime Number.

1. Read N.
2. Set I:=2 and PRIME = 1.
3. Repeat Steps 4 to 7 while I <= N/2
4. If(N % I=0)
5.       Set PRIME:=0. and exit the loop structure.
6. Else:
7.       Set I:=I + 1.
8. [End of If Structure.]
9. [End of While loop]
10. If PRIME = 1, then:
11.       Print the number is prime.
12. Else
 13.      Print the number is not prime.
14. [End of If structure.]
15. Exit.
[br]
Here in the above algorithm, we first read an integer variable N. and then set default values of two variables I=2 and PRIME = 1 and then using the while loop we repeat steps 4 to 7. in the if condition we check if the value of N is divisible by 2 then we set the value of PRIME to 0 and exit from the loop else we set the value of I to I + 1. this process is executed till the value of I less than or equal to N/2. then we check if the value of the PRIME variable is 1 then we print the number is prime else we print the number as not prime. and then we exit from the program.
[br]
Also, Read
  1. Algorithm to Find Maximum of Two Numbers
  2. Algorithm to Find Maximum of Three Numbers
  3. Algorithm to Input percentage From user and Print grade
  4. Algorithm to Print the Series 1 to N using for loop
  5. Algorithm to Print the Series 1 to N using while loop
  6. Algorithm to Print the Series 1 to N using Do While loop
  7. Algorithm to Reverse a given Number
  8. Algorithm for Linear Search in Array
  9. Algorithm to Calculate Factorial using Function
algorithms algorithm

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

  • HackerRank Dynamic Array Solution in Python
  • HackerRank 2D Array DS solution in Python
  • HackeRank Array – DS solution in Python
  • Streaming Demystified: How Much Data Does Your Favorite Show Really Use?
  • Parenthesis Matching program in C programming
  • HackerRank Dynamic Array Solution in Python
  • HackerRank 2D Array DS solution in Python
  • HackeRank Array – DS solution in Python
  • Streaming Demystified: How Much Data Does Your Favorite Show Really Use?
  • Parenthesis Matching program in C programming
  • About US
  • Contact US
  • Data Structures and algorithms tutorials
  • Digital Communication Tutorials
  • DMCA
  • HackerRank All Algorithms problems solutions
  • HackerRank C problems solutions
  • HackerRank C++ problems solutions
  • HackerRank Java solutions
  • HackerRank Python solutions
  • Human Values Tutorials
  • Internet of Things Tutorials
  • Privacy Policy
©2025 DATA STRUCTURE | WordPress Theme by SuperbThemes