Write a c++ program to check whether a given number is prime or not?

Follow
  • Follow publicly
  • Follow privately
  • Unfollow
Best Answer
Hi,try this one...

#include<iostream.h>
int main()
{
int num;
cout << "Enter a number ";
cin >> num;
int i=2;
while(i<=num-1)
{
if(num%i==0)
{
cout << "\n" << num << " is not a prime number.";
break;
}
i++;
}
if(i==num)
cout << "\n" << num << " is a prime number.";
return 0;
}
  • 2
  • Comment

Other Answers (5)

Rated Highest
  • Rated Highest
  • Oldest
  • Newest
  • qazi ubaid answered 6 months ago
    write a program to verify that mean of the means of smple size 3, taken from a finite population 1,3,5,7 and 9 is equal to population mean..........???
    • Rate
    • Comment
  • Indranil Gupta answered 6 months ago
    #include <iostream.h>
    #include <conio.h>
    int main ()
    {
    int i=2,N;
    cout<<"Enter any number"<<endl; cin>>N;
    if (i==N)
    cout<<"Number is prime";
    else
    {
    while (N>i)
    }
    if (N%i==0)
    break;
    i++;
    if (i==N)
    cout<<"Number is prime";
    else
    cout<<"Number is not prime";
    getch();
    return 0;
    }

    Source(s):

    OWN KNOWLEDGE
    • Rate
    • Comment
  • Irum answered 7 months ago
    //Prime Numbers generation in C++
    //Using for loops and conditional structures
    #include <iostream>
    using namespace std;

    int main()
    {
    int a = 2; //start from 2
    long long int b = 1000; //ends at 1000

    for (int i = a; i <= b; i++)
    {

    for (int j = 2; j <= i; j++)
    {
    if (!(i%j)&&(i!=j)) //Condition for not prime
    {
    break;
    }

    if (j==i) //condition for Prime Numbers
    {
    cout << i << endl;

    }
    }
    }
    }
    - See more at: http://www.programmingtunes.com/generation-of-prime-numbers-c/#sthash.YoWHqYcm.dpuf
    • Rate
    • Comment
  • Destiny Maker answered 4 years ago
    The program for prime number is as follow.........

    #include<iostream.h>
    #include<conio.h>

    main()
    {
    int a,c=0,i,n;
    printf("enter the number to be checked");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    a=n%i;
    if(a=0)
    {
    c=c+1;
    }
    }
    if (c=2)
    { printf("the given number is prime"); }
    else
    printf("the given number is not prime");

    getch();
    }
    • Rate
    • Comment
  • HI answered 4 years ago
    veryfy balagurusawmy book...............
    • 1
    • Comment
  • Sign In 

    to add your answer

Who is following this question?

    %
    BEST ANSWERS
    Member Since:
    Points: Points: Level
    Total Answers:
    Points this week:
    Follow
     
    Unfollow
     
    Block
     
    Unblock