How do I go about writing positive, neg, zero, and even or odd???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joeyke
    New Member
    • Feb 2010
    • 15

    How do I go about writing positive, neg, zero, and even or odd???

    Write a complete C++ program, including comments, to do the following:

    Ask the user to enter a number, and then read in the number from the keyboard. Print out the number and also output whether the number is positive, negative or zero, and also output whether the number is even or odd.
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Notice that these categories are not mutually exclusive. Take them one at a time:
    • How do you test if a number is positive?
    • How do you test if a number is negative?
    • How do you test if a number is zero?
    • How do you test if a number is even?
    • How do you test if a number is odd?

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      Edit: Pardon me donbock but you were not there when I started klanking away.
      The people here don`t take kindly to spoon feeding the mentally lazy however:
      1. If the number can be divided by 2 with 0 remainder it is even and if not it is odd.
      2. If the number is less than 0 it is negative.
      3. If number when multiplied by 1 == 0, it is 0 (zero)
      In future have a go yourself and post your code identifying a specific difficulty
      Specifically get your thought processes working. As they say if you don`t use it you loose it!

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        Have you tried to write the code...? What problem you have in your code.?

        Regards
        Dheeraj Joshi

        Comment

        • joeyke
          New Member
          • Feb 2010
          • 15

          #5
          line 27 and line 31 have an errors but i don't know what they are

          Code:
          #include <iostream>
          
          using namespace std;
          
          int main ()
          {
              int numb1;
              int numb2;
          
          
              cout<<" enter one number: ";
              cin>>numb1;
              for (numb1=numb1+0;numb1>0;)
              {
                  cout<<numb1<<" is positive";
              }
          
              for (numb1=numb1+0;numb1<0;)
              {
                  cout<<numb1<<" is negative";
              }
                  for (numb1=numb1+0;numb1=0;)
              {
                  cout<<numb1<<" is zero";
          }
          for (numb1=numb1+0;numb1%2=0;) //line 27
              {
                  cout<<" and is positive";
              }
              for (numb1=numb1+0;numb1%2><0;) //line 31
              {
                  cout<<" and  is negative ";
              }
          system ("pause");
          return 0;
          
          }
          Last edited by Banfa; Feb 26 '10, 09:44 AM. Reason: Added code tags

          Comment

          • whodgson
            Contributor
            • Jan 2007
            • 542

            #6
            You are trying to use for loops when you should be using if statements.
            When num1 is typed from the keyboard you need something like:
            Code:
            if (num1>0)cout<<"the number is positive";
            else if (num1<0)cout<<"the number is negative";
            else cout<<"the number is zero";
            Also please enclose your code in code tags by selecting it and hitting #

            Comment

            • pavunkumar
              New Member
              • Feb 2010
              • 4

              #7
              Use if condition


              Actually I dont know , why you are using this much for loop unnecessarily

              Use if condition

              Refer following code.
              Code:
              <spoonfeeding code removed>
              Last edited by Banfa; Feb 26 '10, 09:45 AM. Reason: Removed spoonfeeding code

              Comment

              Working...