Write a C program to create Book Details using structure

Write a C program to check whether a number is Prime number or not using while & for loop

Introduction

Write a C program to check whether a number is Prime number or not using while & for loop

Check whether a number is Prime number or not using while loop

#include<stdio.h>
int main()
{
    int num, i, f;

    //Readind a number from user
    printf("Enter any number: ");
    scanf("%d", &num);

    f = 0;
    i = 2;
    while(i <= num/2)
    {
        if(num%i == 0)
        {
            f=1;
            break;
        }
        i++;
    }
    if(f == 0)
        printf("%d is a Prime Number", num);
    else
        printf("%d is Not a Prime Number", num);
 return 0;
}

Check whether a number is Prime number or not using for loop

#include<stdio.h>
int main()
{
    int num, i, f;

    //Reading a number from user
    printf("Enter a number: ");
    scanf("%d",&num);
    f=0;
    for(i=2; i <= num/2; i++)
    {
        if(num%i == 0)
        {
            f=1;
            break;
        }
    }
    if(f==0)
        printf("%d is a Prime Number", num);
    else
        printf("%d is a Not Prime Number", num);
    return 0;
}

Result

Write a C program to check whether a number is Prime number or not using while & for loop
Write a C program to check whether a number is Prime number or not using while & for loop

Add a Comment

Your email address will not be published. Required fields are marked *