Prime numbers are one of the most fundamental concepts in mathematics and computer science. They play a vital role in number theory, cryptography, and algorithm design. For aspiring programmers, understanding how to write a program that identifies prime numbers from 1 to 100 is a classic exercise that helps build both logical reasoning and coding skills. In this article, we’ll walk through what prime numbers are, how to find them in the range from 1 to 100, and then break down an efficient C program that does just that.

Whether you’re preparing for a college coding test, technical interview, or simply trying to improve your logic-building skills, this article will provide a clear and practical guide. We’ll also recommend a great programming course from Uncodemy to help you deepen your coding knowledge further.
A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In simpler terms, a number is prime if it is only divisible by 1 and the number itself.
For example:
Note that 1 is not considered a prime number. This is a common misconception, but it is excluded from the definition by convention.
Before we dive into the code, let's first list all the prime no 1 to 100 manually. This gives us a benchmark to check if our program works correctly.
Here’s the list:
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
There are 25 prime numbers between 1 and 100.
To find prime numbers between 1 and 100 using programming logic, we need a method to check whether each number in that range is prime. The logic can be summarized in the following steps:
However, this brute-force method can be optimized.
We don’t need to check all numbers up to n to determine if n is prime. Instead, we can check only up to √n because if a number has a factor greater than its square root, it must also have a corresponding factor smaller than the square root.
Let’s now take a look at a C program that uses the optimized logic to print all the prime numbers from 1 to 100.
Copy Code
c
CopyEdit
#include <stdio.h>
#include <math.h>
int main() {
int i, j, flag;
printf("Prime numbers from 1 to 100 are:\n");
for(i = 2; i <= 100; i++) {
flag = 1; // Assume i is prime
for(j = 2; j <= sqrt(i); j++) {
if(i % j == 0) {
flag = 0; // i is not prime
break;
}
}
if(flag == 1) {
printf("%d ", i);
}
}
return 0;
}css
CopyEdit
Prime numbers from 1 to 100 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
Let’s break this down step-by-step:
This approach is far more efficient than checking every number up to i.
You might wonder why such a simple program holds importance. Here are a few reasons:
If you want to learn more about such fundamental programming techniques and enhance your skills, consider taking a structured course.
If you're a beginner or even an intermediate programmer who wants to improve your C or C++ programming skills, Uncodemy offers highly-rated courses that cover everything from the basics to advanced problem-solving.
Their course on C Programming for Beginners is perfect if you want to:
By learning with Uncodemy, you'll gain both the theoretical knowledge and hands-on experience needed to tackle real-world coding challenges. Check out their course to strengthen your foundational coding skills and build confidence in writing optimized programs.
While writing a program to find the prime no 1 to 100, beginners often make some typical mistakes:
Remember, 1 is not a prime number. Always start from 2.
Once you know a number isn’t prime, break out of the loop immediately. It improves efficiency.
Checking all the way to i in the inner loop makes the code slower. Use sqrt(i) instead.
Using sqrt() without #include <math.h> causes compilation errors.
By keeping these in mind, your code will not only run correctly but also perform better.
Besides the method shown above, there are other techniques that can be used to find primes:
A highly efficient algorithm for finding all primes smaller than a given number. It’s more advanced but great for larger datasets.
You can write a recursive function to check if a number is prime and use that inside a loop.
You can store prime status in an array for further processing or usage.
Understanding prime numbers isn’t just for programming assignments—they are widely used in real life too.
So, by mastering a simple program like printing the prime no 1 to 100, you’re actually stepping into one of the most powerful areas of computing.
Understanding how to find prime numbers from 1 to 100 is an essential exercise for anyone starting with programming. It not only teaches core concepts like loops and conditionals but also prepares you for more complex logic-building problems.
The program we discussed uses an optimized method that checks divisibility only up to the square root of each number, making it both simple and efficient. Whether you're practicing for an exam, preparing for a coding interview, or learning for fun, this program is a must-know.
And if you're looking to deepen your programming skills, don’t miss the Uncodemy C Programming Course, which offers structured learning and real-world problem solving.
Start small, practice regularly, and you’ll be amazed at how quickly your coding confidence grows.
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR