C program to print all prime numbers from 1 to 100

Solution with out using functions(Using For Loop)
Solution with out using functions(Using While Loop)
Solution using functions without passing arguments



C program to print all prime numbers from 1 to 100(Using For Loop)

Solution : Without using function
Program/Source Code

#include <stdio.h>

int main()
{
    int n=2,i,z=0;
    for(n=2;n<=100;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:

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,



C program to print all prime numbers from 1 to 100(Using While Loop)

Solution : Without using function
Program/Source Code

#include <stdio.h>

int main()
{
    int n=2,i,z=0;
    while(n<=100)
    {
        i=2;
        z=0;
        while(i<n)
        {
            if(n%i==0)
            {
                z=1;
                break;
            }
             i++;
        }
        if(z==0)
        printf("%d ",n);
        n++;
    }

    return 0;
}

Output:

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,



C program to print all prime numbers from 1 to 100

Solution : Using function Without Passing Arguments
Program/Source Code

#include <stdio.h>
void check()
{
    int n=2,i,z=0;
    for(n=2;n<=100;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:

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,

Previous:

Next:

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