C program to print prime numbers between 1-100

devquora
devquora

Posted On: Jan 27, 2023

C program to print prime numbers between 1-100

 

Overview:

In this tutorial, I will teach you What is a Prime Number? and What do you mean by Factors of a number? Then, I will demonstrate How to find prime Numbers within a specific range? Moreover, I will show you the logic to check if the given number is prime or not. After all, I will show a C Program to find prime numbers within range with output. After all, I will explain the internal execution of this program.

Table of contents:

  1. What is a Prime Number?
  2. What do you mean by Factor of a number?
  3. Demonstration to check prime number with some examples
  4. Main Logic to find prime Numbers within a specific range
  5. C program to find prime Numbers within a specific range
  6. Conclusion

What is a Prime Number?

A Prime Number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Examples of the Prime Numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, etc.

In other words, Prime numbers are the positive integers having only two factors, 1 and the integer itself.

For example, factors of 8 are 1,2,4, and 8. But factors of 11 are only 1 and 11. Hence, 11 is a prime number but 8 is not the prime number.  1 is never a prime number.

What do you mean by the Factor of a number?

Factors are made up of divisor and dividend. In other words, you can say that the factors are the numbers from whom a number is divisible. For instance, Factors of 23 ia 1 and 23 but 1, 2, 3, 4, 24 are the factors of 24.

 

Demonstration to check prime number with some examples

Demo Of Prime

Main Logic to check if the given number is prime or not

  • Step 1: We remembered that 1 is not prime therefore, Take a for loop with an initial variable i from 2 to 100.
  • Step 2: Check if the current number i is prime or not
  • Step 3: If i is divisible by any number other than 1 and self then it is not a prime number.
  • Step 4: If the number is prime then print
  • Step 5: Else exit.

C program to print prime numbers between 1-100 with output

/* C Program to Print Prime Numbers between 1 to 100 using For Loop*/
#include 
int main()
{
    int number, j, isPrime; 
    printf("All prime numbers between 1 to 100 %d are:\n ");
    /* Find all Prime numbers between 1 to 100 */
    for(number=2; number<=100; number++)
    {
        /* Assume that the current number is Prime */
        isPrime = 1; 
        /* Check if the current number i is prime or not */
        for(j=2; j<=number/2; j++)
        {
            /* If i is divisible by any number other than 1 and self
             * then it is not prime number
            */
            if(number%j==0)
            {
                isPrime = 0;
                break;
            }
        }
        /* If the number is prime then print */
        if(isPrime==1)
        {
            printf("%d,", number);
        }
    }
    return 0;
}

The output of the Program:

Demo of Prime within Range

Conclusion:

This section shows the explanation of the program with output.

The internal working of this program depends upon the previous demonstration to find prime numbers within the range.

This Program runs a loop from 2 to 100 and checks for each current number within the range that it is prime or not. After the successful execution of the program, it displays the list of all prime numbers from 2 to 100. As we know that 1 is not a prime number so we don't need to check it.

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..