
Understanding Prime Numbers with a Simple Flowchart and Code
What is a Prime Number? A prime number is a number that is divisible only by 1 and itself. For example: 2, 3, 5, 7 are prime numbers. If a number has more than two factors, it is called a non-prime number. For example: 4, 6, 8 are not prime. Logic Behind Prime Number Checking To check whether a number is prime: Start with a number (let’s call it no) Assume it is prime initially Check divisibility from 2 up to half of the number If the number is divisible by any value → it is NOT prime If no divisors are found → it is PRIME Flowchart Python Code no = int(input("Enter a number: ")) div = 2 flag = True while div <= no // 2: if no % div == 0: print("Not Prime") flag = False break div = div + 1 if flag == True: print("Prime") Output: Java Code import java.util.Scanner; public class PrimeCheck { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int no = sc.nextInt(); int div = 2; boolean flag = true; while (div <= no / 2) { if
Continue reading on Dev.to Python
Opens in a new tab



