Algorithm to print prime numbers from 1 to n in c:
Here’s the algorithm to print prime numbers from 1 to n in c:
Step 1: Take the number num as input.
Step 2: Initialize a variable temp to 0.
Step 3: Start a for loop that iterates from 2 to num/2.
Step 4: Inside the loop, check if num is divisible by the current iterator value. If it is, increment temp by 1.
Step 5: After the loop, check the value of temp:
- If temp is 0: This means the number is divisible only by 1 and itself. Return “num IS PRIME”.
- Else: If temp is greater than 0, it indicates that num has divisors other than 1 and itself. Return “num IS NOT PRIME”.
Flowchart for C Program to Print Prime Numbers from 1 to n
To understand how it works, let’s look at a generic program that prints prime numbers. The flow chart for the program is as follows:
Now let’s look at the code for the same:
#include <stdio.h>
int main() {
int i, num, n, count;
// Take input for the range
printf("Enter the range: ");
scanf("%d", &n);
// Print the prime numbers in the given range
printf("The prime numbers between 1 and %d are: ", n);
for (num = 2; num <= n; num++) { // Start from 2 since 1 is not a prime number
count = 0; // Reset the count for each number
for (i = 2; i <= num / 2; i++) { // Check divisors from 2 to num/2
if (num % i == 0) { // If divisible, it's not a prime number
count++;
break; // Exit the loop if a divisor is found
}
}
// If count is still 0, the number is prime
if (count == 0) {
printf("%d ", num);
}
}
printf("\n"); // Print a newline for cleaner output
return 0; // Return 0 to indicate successful execution
}
Output:
Enter the range: 50
The prime numbers between 1 and 50 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
=== Code Execution Successful ===
The code prompts the user to enter a number n, setting the upper limit for prime numbers. It uses a loop to check each number from 2 to n. For each number, the code checks if it is divisible by any number between 2 and num/2. If no divisors are found, the number is prime and printed. The process repeats for all numbers between 2 and n, displaying all prime numbers.
C Program to Print Prime Numbers From 1 to 200 Using FOR Loop
Here’s an example program which prints all prime numbers from 1 to 200. The program uses a for loop to check each number in the range. For every number, a nested for loop checks divisibility by numbers from 2 to half of the current number. If no divisors are found, the number is prime and printed. The program skips 1 since it's not prime and uses the count to track divisibility for each number.
#include <stdio.h>
int main() {
int i, num, count;
printf("The prime numbers between 1 and 200 are: ");
for(num = 2; num <= 200; num++) { // Loop from 2 to 200
count = 0;
// Check divisors from 2 to num/2
for(i = 2; i <= num / 2; i++) {
if(num % i == 0) {
count++;
break;
}
}
// If no divisors are found, the number is prime
if(count == 0) {
printf("%d ", num);
}
}
printf("\n"); // Print newline for cleaner output
return 0; // Indicate successful program termination
}
Output:
The prime numbers between 1 and 200 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
=== Code Execution Successful ===
C Program to Print Prime Numbers From 1 to n Using FOR Loop
Now, let’s look at a program that prints prime numbers from 1 to n, but the limit is not set to 100. In this program, we write the code to identify and print all prime numbers within a given range using for loop. The program takes an upper limit n as input and iterates through numbers from 1 to n. For each number, a nested for loop checks divisibility by numbers up to half its value. The number is considered prime and printed if no divisors are found (other than 1 and itself). The program skips 1 as it's not a prime number.
Here’s the code to print prime numbers from 1 to n using a for loop:
#include <stdio.h>
int main(void) { // Use int main(void) for standard compliance
int i, num, n, count;
printf("Enter the range: ");
scanf("%d", &n);
printf("The prime numbers between 1 and %d are: ", n);
for (num = 2; num <= n; num++) { // Start from 2 as 1 is not a prime number
count = 0;
for (i = 2; i <= num / 2; i++) { // Check divisibility up to num/2
if (num % i == 0) {
count++;
break; // Exit the loop if a divisor is found
}
}
if (count == 0) { // If count is still 0, the number is prime
printf("%d ", num);
}
}
printf("\n");
return 0; // Return 0 to indicate successful execution
}
Output:
Enter the range: 200
The prime numbers between 1 and 200 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
=== Code Execution Successful ===
C Program to Print Prime Numbers From 1 to n Using WHILE Loop
In this program, we use while loop to print prime numbers from 1 to n. It has an outer while loop, which iterates through numbers from 2 to n. For each number, an inner while loop checks if it's divisible by any number between 2 and half of itself. If no divisors are found, the number is prime and printed. With the while loop, the program checks each number until reaching the upper limit n.
Here are the print prime numbers from 1 to n in c using a while loop:
#include <stdio.h>
int main(void) { // Use int main(void) for standard compliance
int n, num = 2, i, isPrime;
printf("Enter the range: ");
scanf("%d", &n);
printf("The prime numbers between 1 and %d are: ", n);
while (num <= n) {
isPrime = 1; // Assume the number is prime
i = 2;
while (i <= num / 2) { // Check divisors from 2 to num/2
if (num % i == 0) {
isPrime = 0; // Not a prime number
break;
}
i++;
}
if (isPrime && num != 1) { // If prime and not 1, print it
printf("%d ", num);
}
num++;
}
printf("\n");
return 0; // Return 0 to indicate successful execution
}
Output:
Enter the range: 50
The prime numbers between 1 and 50 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
=== Code Execution Successful ===
Conclusion
Learning how to code tasks like the c program to print prime numbers from 1 to n is essential for building a strong foundation in programming. The two versions of the prime number printing task show two different approaches. The first approach, which prints primes from 1 to 100, offers a fixed solution, while the second approach allows flexibility. So the user can define the range (1 to n) and print primes to large numbers. This difference builds real problem-solving skills as you learn to code and shows how code can be adapted to different needs. More importantly, the task teaches you to think logically and optimise code to handle loops effectively, which is a crucial skill for software development.
Mastering basics is necessary to develop programming skills and is vital for a successful career in software companies where adaptability and problem-solving are highly valued. To strengthen your foundations, you can also consider enrolling in the CCBP Academy 4.0 program so you can be job-ready by the time you finish college.
Boost Your Placement Chances by Learning Industry-Relevant Skills While in College!
Explore ProgramFrequently Asked Questions
1. What is the purpose of checking divisibility up to num/2 or sqrt(num) in prime number programs?
Checking up to num/2 (o for efficiency) can be done to reduce unnecessary computations. If a number has a divisor larger than half of itself, it would already have a smaller divisor. So, checking beyond this limit is not needed.
2. How loops differ fro loops in prime number programs?
While loops are used when the number of iterations is not predetermined, this is because they continue running as long as a condition is true. For loops can be used when you have a fixed number of iterations.
3. Why is the number 1 excluded from being a prime number?
Prime numbers are defined as numbers greater than 1 that have no divisors other than 1 and themselves. So, one is excluded.
4. How can learning to print prime numbers help in learning programming?
Printing prime numbers teaches you key concepts like loops, conditional statements, and number theory. All of these are used in bigger programs, too.
5. Can the prime number printing program be optimised further?
The program can be optimised by skipping even numbers (except 2) in the prime check.