Tech Study

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

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

Python Examples

Introduction: Python Examples are the basic programming concepts of python like python syntax,python data types,,python operators,python if else,python comments etc.. …

Read more

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action. The <string.h> header …

Read more