Skip to search.

Breaking News Visit Yahoo! News for the latest.

×Close this window

  1. Home >
  2. All Categories >
  3. Computers & Internet >
  4. Programming & Design >
  5. Resolved Question
Wilson Stypes Wilson Stypes
Member since:
November 07, 2009
Total points:
178 (Level 1)

Resolved Question

Show me another »

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

2441139 by 2441139
Member since:
March 25, 2008
Total points:
1,135 (Level 3)

Best Answer - Chosen by Voters

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;
}
100% 1 Vote
  • 2 people rated this as good

There are currently no comments for this question.

Other Answers (2)

  • Destiny Maker by Destiny Maker
    Member since:
    September 13, 2010
    Total points:
    495 (Level 2)
    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();
    }
    0% 0 Votes
  • HI by HI
    Member since:
    August 07, 2009
    Total points:
    157 (Level 1)
    veryfy balagurusawmy book...............
    0% 0 Votes
    • 1 person rated this as good

Answers International

Yahoo! does not evaluate or guarantee the accuracy of any Yahoo! Answers content. Click here for the Full Disclaimer.

Help us improve Yahoo! Answers. Send Feedback