C program to print all prime numbers upto a 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 upto a limit(Using For Loop)

Solution : Without using function
Program/Source Code

#include <stdio.h>

int main()
{
    int n,i,z=0,limit;
    printf("Enter the limit ");
    scanf("%d",&limit);

    for(n=2;n<=limit;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 the limit 52
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,



C program to print all prime numbers upto a limit(Using While Loop)

Solution : Without using function
Program/Source Code

#include <stdio.h>

int main()
{
    int n=2,i,z=0,limit;
    printf("Enter the limit ");
    scanf("%d",&limit);

    while(n<=limit)
    {
        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 the limit 25
2, 3, 5, 7, 11, 13, 17, 19, 23,



C program to print all prime numbers upto a limit

Solution : Using function Without Passing Arguments
Program/Source Code

#include <stdio.h>
void check()
{
    int n,i,z=0,limit;
    printf("Enter the limit ");
    scanf("%d",&limit);

    for(n=2;n<=limit;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 the limit 15
2, 3, 5, 7, 11, 13,



C program to print all prime numbers upto a limit

Solution : Using function Passing Arguments
Program/Source Code

#include <stdio.h>
void check(int limit)
{
    int n,i,z=0;
    for(n=2;n<=limit;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 limit;
    printf("Enter the limit ");
    scanf("%d",&limit);
    check(limit);
    return 0;
}

Output:

Enter the limit 35
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,

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