Saturday 13 October 2012

how to find the prime number using c++(with simple coding)


how to find prime number in c++
how to find prime number in c++:
This is the source code for determining that given number is prime or not
#include<iostream.h>
#include<conio.h>
main()
{
      int x;
      for(int i=1;i<=100;i++){
      cout<<"enter the number"<<endl;
      cin>>x;
      if(x==2 || x==3 || x==5){
             cout<<"number is prime"<<endl;
             }
      else if(x%2==0 || x%3==0 || x%5==0)
      {
                cout<<"not prime"<<endl;
                }
                else
                {
                    cout<<"number is prime"<<endl;
                    }
                     
                       }
                    getch();
                    }
End

3 comments:

  1. What is getch()??

    ReplyDelete
    Replies
    1. getch() is a function to get a character from the user.
      i use this at the end to halt the program.
      if you have a parameter or argument in parentheses of getch() like; getch(c). Then input character will be stored in "c".
      you can also use system("pause") instead of getch() to halt the program.
      ----------------------------------------------------------------------------------------------------------
      i am not sign in now therefore i comment as a anonymous.

      Delete
  2. There is some problem in your source code like There is some number which isnot divided by 2 3 or 5. Like 49 it can be divided by 7 not in 2 3 or 5. I guess it will be the bug of your program

    ReplyDelete