Write a C C++ program to check entered number is prime or not

Write a C C++ program to check entered number is prime or not

Looping और iterative statements के बारे में और अधिक जानने के लिए देखें— Looping statements in C

Unconditional branching statements के बारे में अधिक जानकारी के लिए देखें— Break, Continue and Goto in C

Flowchart to check entered number is prime or not

Checking whether given number is prime or not flowchart

C program to check entered number is prime or not

#include<stdio.h>
int main()
{
    int num, c=2;
    char prime='t';
    printf("Enter a number: ");
    scanf("%d",&num);
    while(c<num)
    {
        if(num%c==0)
        {
            prime='f';
            break;
        }
        c++;
    }
    if(prime=='t')
    {
        printf("Prime number");
    }
    else
    {
        printf("Not a prime number");
    }
    return 0;
}

C++ program to check entered number is prime or not

#include<iostream>
using namespace std;
int main()
{
    int num, c=2;
    char prime='t';
    cout<<"Enter a number: ";
    cin>>num;
    while(c<num)
    {
        if(num%c==0)
        {
            prime='f';
            break;
        }
        c++;
    }
    if(prime=='t')
    {
        cout<<"Prime number";
    }
    else
    {
        cout<<"Not a prime number";
    }
    return 0;
}

Output of C C++ program to check entered number is prime or not

Enter a number: 5
Prime number
Enter a number: 10
Not a prime number

Other programs using while for and do while loops in C C++

  1. Generate series of natural numbers (while, for, do while)
  2. Generate series of natural numbers and their sum
  3. Generate series of odd numbers and find their sum
  4. Generate series of even numbers and find their sum
  5. Generate table of given number
  6. Find factorial of given number
  7. Find power of given number
  8. Generate multiplication table of given number
  9. HCF and LCM of two numbers
  10. Generating Fibonacci series
  11. Generating series of prime numbers
  12. Sum of digits of given number
  13. Reverse of digits of given number
  14. Checking palindrome number
  15. Checking Armstrong number
  16. Generating series of Armstrong numbers
  17. Convert decimal to binary
  18. Convert binary to decimal
Share it to:

Published by

admin

I am a computer teacher, programmer and web developer