Bird
0
0
DSA Cprogramming~30 mins

Check if Number is Prime in DSA C - Build from Scratch

Choose your learning style9 modes available
Check if Number is Prime
📖 Scenario: You are helping a friend who wants to know if a number is prime. A prime number is a number greater than 1 that has no divisors other than 1 and itself.We will write a simple program in C to check if a given number is prime.
🎯 Goal: Build a C program that checks if a number is prime by testing divisibility from 2 up to the number minus one.
📋 What You'll Learn
Create an integer variable called number with the value 29
Create an integer variable called is_prime and set it to 1 (true)
Use a for loop with variable i from 2 to number - 1
Inside the loop, check if number is divisible by i
If divisible, set is_prime to 0 (false) and break the loop
Print "Prime" if is_prime is 1, otherwise print "Not Prime"
💡 Why This Matters
🌍 Real World
Prime numbers are used in cryptography, computer security, and coding theory.
💼 Career
Understanding prime checking helps in algorithm design and problem solving in software development.
Progress0 / 4 steps
1
Create the number variable
Create an integer variable called number and set it to 29.
DSA C
Hint

Use int number = 29; inside main().

2
Create the is_prime variable
Create an integer variable called is_prime and set it to 1 inside main() after number.
DSA C
Hint

Use int is_prime = 1; to assume the number is prime initially.

3
Check divisibility with a for loop
Use a for loop with variable i from 2 to number - 1. Inside the loop, check if number % i == 0. If yes, set is_prime to 0 and break the loop.
DSA C
Hint

Use for (int i = 2; i < number; i++) and inside check if (number % i == 0).

4
Print if the number is prime or not
Use an if statement to print "Prime" if is_prime is 1, otherwise print "Not Prime".
DSA C
Hint

Use if (is_prime == 1) and printf("Prime\n"), else print "Not Prime".