C program to print all prime numbers between lower and upper limit

Solution with out using functions(Using For Loop)

Solution with out using functions(Using While Loop)

Solution using functions without passing arguments

Solution using functions and by passing arguments



C program to print all prime numbers between lower and upper limit(Using For Loop)

Solution : Without using function
Program/Source Code

#include <stdio.h>

int main()
{
    int n,i,z=0,lower,upper;
    printf("Enter lower and upper limits ");
    scanf("%d %d",&lower,&upper);
    for(n=lower;n<=upper;n++)
    {
        z=0;
        for(i=2;i<n;i++)
        {
            if(n%i==0)
            {
                z=1;
                break;
            }
        }
        if(z==0)
        printf("%d, ",n);
    }

    return 0;
}

Output:

Enter lower and upper limits 10
52
11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,



C program to print all prime numbers between lower and upper limit(Using While Loop)

Solution : Without using function
Program/Source Code

#include <stdio.h>

int main()
{
    int n,i,z=0,lower,upper;
    printf("Enter lower and upper limits ");
    scanf("%d %d",&lower,&upper);
    n=lower;
    while(n<=upper)
    {
        z=0;
        i=2;
        while(i<n)
        {
            if(n%i==0)
            {
                z=1;
                break;
            }
            i++;
        }
        if(z==0)
        printf("%d, ",n);
        n++;
    }

    return 0;
}

Output:

Enter lower and upper limits 50
75
53, 59, 61, 67, 71, 73,



C program to print all prime numbers between lower and upper limit

Solution : Using function Without Passing Arguments
Program/Source Code

#include <stdio.h>
void check()
{
    int n,i,z=0,lower,upper;
    printf("Enter lower and upper limits ");
    scanf("%d %d",&lower,&upper);
    for(n=lower;n<=upper;n++)
    {
        z=0;
        for(i=2;i<n;i++)
        {
            if(n%i==0)
            {
                z=1;
                break;
            }
        }
        if(z==0)
        printf("%d, ",n);
    }


}

int main()
{
    check();
    return 0;
}

Output:

Enter lower and upper limits 10
45
11, 13, 17, 19, 23, 29, 31, 37, 41, 43,



C program to print all prime numbers between lower and upper limit

Solution : Using function Passing Arguments
Program/Source Code

#include <stdio.h>
void check(int lower,int upper)
{
    int n,i,z=0;
    for(n=lower;n<=upper;n++)
    {
        z=0;
        for(i=2;i<n;i++)
        {
            if(n%i==0)
            {
                z=1;
                break;
            }
        }
        if(z==0)
        printf("%d, ",n);
    }

}

int main()
{
    int lower,upper;
    printf("Enter lower and upper limits ");
    scanf("%d %d",&lower,&upper);
    check(lower,upper);
    return 0;
}

Output:

Enter lower and upper limits 125
150
127, 131, 137, 139, 149,

C Language Programming Tutorial

C Language Tutorial Home    Introduction to C Language     Tokens     If Condition     goto statement and Labelname     Switch Statements     For loop     While Loop    Do while loop     break and continue    Functions    Recursion    Inbuild Functions    Storage Classes    Preprocessor    Arrays    Pointers     Structures and Unions    File Handling     Projects

Tutorials

Technical Questions

Interview Questions

C Programming
C++ Programming
Class 11 (Python)
Class 12 (Python)
C Language
C++ Programming
Python

C Interview Questions
C++ Interview Questions
C Programs
C++ Programs