Question:

Write a program in c++ to enter number n and perform the following operations according to users choice:?

by  |  earlier

0 LIKES UnLike

1) check if number is positive or negative or zero

2)check if the number is prime or composite

3)check if the number is odd or even

4)exit

please help me, its very urgent

 Tags:

   Report

2 ANSWERS


  1. #include <iostream.h>

    class Input

    {

        int n;

        

      public:

        Input(){}

    Input(int i)

    {

    n=i;

    }

        int IsPrime(void);

        int IsEven(void);

        int IsPositive(void);

    };

    int Input::IsPositive()

    {

    if((n>0))

    return 1;

    return 0;

    }

    int Input::IsEven()

    {

    if((n%2)==0)

    return 1;

    return 0;

    }

    int Input::IsPrime()

    {

    int flag=1;

    long val;

    if( (IsEven()==1)&& (IsPositive()==1))

    {

    for(int i=n/2; i>2;i--)

    {

    if((val=n%i)>0) {flag=0; break;}

    }

    return flag;

    }

    else

    {

    return 0;

    }

    }

    void main()

    {

      Input obj(2);

    if (obj.IsPositive()){ cout << " No is Positive ";} else {cout << " No is Negative ";}

    if (obj.IsEven()==1){ cout << " No is Even";} else {cout << " No is Odd";}

    if (obj.IsPrime()==1){ cout << " No is Prime";} else {cout << " No is Composite";}

    }

    //kuldeepraaj@timescalling.com

    //kuldeepraaj@gmail.com

    //kuldeepraaj@yahoo.co.in

    //kuldeepraaj@ymail.com

    //kuldeepraaj@indiatimes.com


  2. PLEASE CHOOSE  BEST ANSWER IF U CAN :)

    #include <iostream.h>

    #include <conio.h>

    void main()

    {

      clrcsr();

      int choice, num, flag  = 1;

    cout << "1) check if number is +ve , -ve or 0\n";

    cout<<  "2) chk is prime or not\n";

    cout<<  "3) odd or even\n";

    cout << "4) exit\n";

      cout <<  "Enter ur choice\n";

      cin  >> choice;

      cout <<  "Enter any number\n";

    cin >> num;

      switch(choice)

    {

      case 1:

                 if(num >0 )  

                   cout <<  "number is +ve "        ;

                   elseif(num <  0)

                      cout << "number is -ve";

                    else

                        cout <<  "number is equal to 0";

                    break;

    case 2:

               for(int i = 2; i  < num; i++)

              {

                   flag  = 1;

                   if(num % i  == i)

                    {

                       cout <<  "PRIME";

                       flag = 0;

                   }

              }

           if(flag )

          cout<<  "NON PRIME";

          break;

    case 3:

                if(num % 2 == 0)

               cout <<  "EVEN";

                 else

                cout <<  "ODD";

                default:

                  cout << "program exit\n";

                 break;

    }

      getch();

    }

    PLZZZZZZZZZ GIVE ME POINTS.................... BY CHOSSING BEST ANSWER

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions