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


Introduction

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


techstudy.org

Kindle Paperwhite High Resolution Display Tablet | Rs. 9999

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


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#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


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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

C language

     





Related Topics


Responses




Popular Posts

Facebook

Founded in 2016, Tech Study passionately delivers stylish and dynamic innovative information to programmer and technology lovers. http://www.techstudy.org/.