C program to check number is prime number or not

Solution Without using function(Using for loop)
Solution Without using function(Using while loop)
Solution Using functions with out passing arguments
Solution Using function by passing arguments
Solution Using function by passing arguments and returning value

For better understanding of the program one should have knowledge of



C program to check number is prime number or not

Solution : Without using function(Using for loop)
Program/Source Code

#include <stdio.h>

int main()
{
    int n,i,z=0;
    printf("Enter any no ");
    scanf("%d",&n);
    for(i=2;i<n;i++)
    {
        if(n%i==0)
        {
            z=1;
            break;
        }
    }
    if(z==0)
        printf("Number is prime");
    else
        printf("Number is not prime");

    return 0;
}

Output:

case 1:
Enter any no 25
Number is not prime

case 2:
Enter any no 3
Number is prime



C program to check number is prime number or not

Solution : Without using function(Using while loop)
Program/Source Code

#include <stdio.h>

int main()
{
    int n,i,z=0;
    printf("Enter any no ");
    scanf("%d",&n);
    i=2;
    while(i<n)
    {
        if(n%i==0)
        {
            z=1;
            break;
        }
        i++;
    }
    if(z==0)
        printf("Number is prime");
    else
        printf("Number is not prime");

    return 0;
}

Output:

Enter any no 26
Number is not prime

Enter any no 17
Number is prime



C program to check number is prime number or not

Solution : Using function Without Passing Arguments
Program/Source Code

#include <stdio.h>
/* function definition */
void prime()
{
    int n,i,z=0;
    printf("Enter any no ");
    scanf("%d",&n);
    for(i=2;i<n;i++)
    {
        if(n%i==0)
        {
            z=1;
            break;
        }
    }
    if(z==0)
        printf("Number is prime");
    else
        printf("Number is not prime");

}
int main()
{
    prime(); /* function calling */

    return 0;
}

Output:

case 1:
Enter any no 36
Number is not prime

case 2:
Enter any no 53
Number is prime



C program to check number is prime number or not

Solution : Using function By Passing Arguments
Program/Source Code

#include <stdio.h>
/* function definition */
void prime(int n)
{
    int i,z=0;
    for(i=2;i<n;i++)
    {
        if(n%i==0)
        {
            z=1;
            break;
        }
    }
    if(z==0)
        printf("Number is prime");
    else
        printf("Number is not prime");

}
int main()
{
    int n;
    printf("Enter any no ");
    scanf("%d",&n);
    prime(n); /* function calling */

    return 0;
}

Output:

case 1:
Enter any no 125
Number is not prime

case 2:
Enter any no 197
Number is prime


You May Also Like:
  • C Program to find lowest of 3 numbers
  • C Program to print factorial of a number
  • C program to print prime numbers between upper and lower limits
  • C program to check character is an alphabet or not
  • C program to convert total inches to feet and inches.
  • C program to convert decimal to binary



Next: C program to print all prime numbers from 1 to 100

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