Prime Numbers from 1 to 100: Program and Explanation

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.

Prime Numbers from 1 to 100: Program and Explanation

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.

What is a Prime Number?

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:

  • 2 is a prime number because it is divisible only by 1 and 2.
     
  • 5 is prime because its only divisors are 1 and 5.
     
  • 6 is not prime because it has three divisors: 1, 2, and 3.
     

Note that 1 is not considered a prime number. This is a common misconception, but it is excluded from the definition by convention.

Prime Numbers Between 1 and 100

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.

Logic to Find Prime Numbers from 1 to 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:

  1. Loop through numbers from 1 to 100.
     
  2. For each number, check how many numbers divide it.
     
  3. If the number has exactly two divisors (1 and itself), it's a prime number.

However, this brute-force method can be optimized.

Optimization Tip:

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.

C Program to Display Prime Numbers from 1 to 100

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;

}

Output:

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

Explanation of the Code

Let’s break this down step-by-step:

  • #include <stdio.h> includes the standard input-output library.
     
  • #include <math.h> allows us to use the sqrt() function to optimize our inner loop.
     
  • We start a for loop from 2 to 100 (since 1 is not prime).
     
  • For each number i, we assume it is prime by setting flag = 1.
     
  • Then, we check if any number j (from 2 to √i) divides i completely. If it does, it's not a prime number, and we set flag = 0.
     
  • After the inner loop, if flag is still 1, we print the number because it's a prime.
     

This approach is far more efficient than checking every number up to i.

Importance of Learning Prime Number Programs

You might wonder why such a simple program holds importance. Here are a few reasons:

  1. Foundation for Algorithms: Prime numbers are the basis of many algorithms, especially in number theory and cryptography.
     
  2. Improves Logical Thinking: Writing this kind of program strengthens your problem-solving and logic-building abilities.
     
  3. Interview Favorite: It’s a frequently asked coding question in interviews for entry-level software roles.
     
  4. Pattern Recognition: Helps you understand loops, conditional statements, and nested structures clearly.

If you want to learn more about such fundamental programming techniques and enhance your skills, consider taking a structured course.

Learn Programming with Uncodemy

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:

  • Understand core programming logic.
     
  • Master loops, conditionals, arrays, and functions.
     
  • Solve practical problems like finding prime numbers, factorials, Fibonacci series, and more.
     

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.

Common Mistakes to Avoid

While writing a program to find the prime no 1 to 100, beginners often make some typical mistakes:

1. Starting from 1:

Remember, 1 is not a prime number. Always start from 2.

2. Not Breaking the Loop:

Once you know a number isn’t prime, break out of the loop immediately. It improves efficiency.

3. Not Using Square Root Optimization:

Checking all the way to i in the inner loop makes the code slower. Use sqrt(i) instead.

4. Forgetting to Include Math Library:

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.

Other Ways to Find Prime Numbers from 1 to 100

Besides the method shown above, there are other techniques that can be used to find primes:

1. Sieve of Eratosthenes:

A highly efficient algorithm for finding all primes smaller than a given number. It’s more advanced but great for larger datasets.

2. Recursion:

You can write a recursive function to check if a number is prime and use that inside a loop.

3. Using Arrays:

You can store prime status in an array for further processing or usage.

Applications of Prime Numbers

Understanding prime numbers isn’t just for programming assignments—they are widely used in real life too.

  • Cryptography: Public key encryption algorithms like RSA are based on large prime numbers.
     
  • Hashing: Some hash functions use primes to reduce collision.
     
  • Random Number Generation: Algorithms often use primes to improve randomness.
     
  • Digital Security: Protecting passwords and data through encryption often involves large primes.
     

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.

Final Thoughts

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.

Placed Students

Our Clients

Partners

...

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses