if-else statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bitong
    New Member
    • Sep 2006
    • 40

    if-else statement

    Just cant figure out how to formulate the correct syntax..I can do it in looping statement but my professor said that use only the simple if-else statement. The problem goes like this..Write a program that will determine if the number that you entered is either odd or even number..

    I tried writing this code..

    #include<stdio. h>
    main()
    {
    int nos;
    clrscr();
    printf("Enter number:");
    scanf("%d",&nos );
    if(nos++){
    printf("Odd number");
    }
    else {
    printf("Even number");
    }
    getch();
    }
  • ssharish
    New Member
    • Oct 2006
    • 13

    #2
    this is the simple way of finding odd or even

    Code:
    if(num % 2 == 0)
        printf("Even\n");
    else
      printf("Odd\n");
    there are many different ways of finding odd and even. Depends upon the complexity of your program

    main should return value, Have a look at this main

    Code:
    int main()
    {
       ---
       ---
       return 0;
    }
    by the way use code tags

    ssharish

    Comment

    • bitong
      New Member
      • Sep 2006
      • 40

      #3
      Originally posted by ssharish
      this is the simple way of finding odd or even

      Code:
      if(num % 2 == 0)
          printf("Even\n");
      else
        printf("Odd\n");
      there are many different ways of finding odd and even. Depends upon the complexity of your program

      main should return value, Have a look at this main

      Code:
      int main()
      {
         ---
         ---
         return 0;
      }
      by the way use code tags

      ssharish
      [QUOTE=bitong]hello...i tried the code that you gave but still it didn't work..

      [code]

      #include<stdio. h>
      int main()
      {
      int nos;
      printf("Enter number:");
      scanf("&d",&nos );
      if(nos % 2 == 0)
      printf("Even number\n");
      else
      printf("Odd number\n");
      getch();
      return 0;
      }

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        In your scanf function you have an & instead of a %

        it should be: scanf("%d",&nos );

        Comment

        • bitong
          New Member
          • Sep 2006
          • 40

          #5
          Originally posted by DeMan
          In your scanf function you have an & instead of a %

          it should be: scanf("%d",&nos );
          sori...typo error... =)

          #include<stdio. h>
          int main()
          {
          int nos;
          printf("Enter number:");
          scanf("%d",&nos );
          if(nos % 2 == 0)
          printf("Even number\n");
          else
          printf("Odd number\n");
          getch();
          return 0;
          }

          even though the code goes like that, still any number that I input it brings back the "Even Number" output...even if i entered 3 or 2....

          =)

          Comment

          • sivadhas2006
            New Member
            • Nov 2006
            • 142

            #6
            Originally posted by bitong
            sori...typo error... =)

            #include<stdio. h>
            int main()
            {
            int nos;
            printf("Enter number:");
            scanf("%d",&nos );
            if(nos % 2 == 0)
            printf("Even number\n");
            else
            printf("Odd number\n");
            getch();
            return 0;
            }

            even though the code goes like that, still any number that I input it brings back the "Even Number" output...even if i entered 3 or 2....

            =)

            Hi,

            This code is working well.

            Code:
            #include<stdio.h>
            int main()
            {
               int 
                  nos = 0;
            
               printf("Enter number:");
               scanf("%d",&nos);
            
               if(nos % 2 == 0)
                  printf("Even number\n");
               else
                  printf("Odd number\n");
               getch();
               return 0;
            }
            Regards,
            M.Sivadhas.

            Comment

            • DeMan
              Top Contributor
              • Nov 2006
              • 1799

              #7
              In your print satement, you could add (temporarily of course) a printout of what the number is which might give a clue.
              Code:
              printf("Even Number %d", nos);
              Given that it is coming even I would suspect that it is coming through 0, however I must admit the code worked for me.

              Comment

              • bitong
                New Member
                • Sep 2006
                • 40

                #8
                Originally posted by sivadhas2006
                Hi,

                This code is working well.

                Code:
                #include<stdio.h>
                int main()
                {
                   int 
                      nos = 0;
                
                   printf("Enter number:");
                   scanf("%d",&nos);
                
                   if(nos % 2 == 0)
                      printf("Even number\n");
                   else
                      printf("Odd number\n");
                   getch();
                   return 0;
                }
                Regards,
                M.Sivadhas.
                Tnx M.Sivadhas...it worked!! =)

                Comment

                • bitong
                  New Member
                  • Sep 2006
                  • 40

                  #9
                  Originally posted by sivadhas2006
                  Hi,

                  This code is working well.

                  Code:
                  #include<stdio.h>
                  int main()
                  {
                     int 
                        nos = 0;
                  
                     printf("Enter number:");
                     scanf("%d",&nos);
                  
                     if(nos % 2 == 0)
                        printf("Even number\n");
                     else
                        printf("Odd number\n");
                     getch();
                     return 0;
                  }
                  Regards,
                  M.Sivadhas.
                  just want to ask whats the function or the purpose of placing % in the "if" statement ---> if(nos % 2 == 0)?

                  Comment

                  • MariyaGel
                    New Member
                    • Nov 2006
                    • 7

                    #10
                    Originally posted by bitong
                    just want to ask whats the function or the purpose of placing % in the "if" statement ---> if(nos % 2 == 0)?
                    The purpose of placing the % is that ur asking for the remainder after the number is divided by 2. If the remainder is 0 then the number is even. e.g if you do 10 % 6 than that would give you 4.
                    Hope it helped

                    Comment

                    • bitong
                      New Member
                      • Sep 2006
                      • 40

                      #11
                      Originally posted by MariyaGel
                      The purpose of placing the % is that ur asking for the remainder after the number is divided by 2. If the remainder is 0 then the number is even. e.g if you do 10 % 6 than that would give you 4.
                      Hope it helped
                      got it.. =) so it means that putting % in the conditional statement means that you're asking for the remainder of the problem? or its purpose varies depending on how you use it....

                      Comment

                      • MariyaGel
                        New Member
                        • Nov 2006
                        • 7

                        #12
                        Originally posted by bitong
                        got it.. =) so it means that putting % in the conditional statement means that you're asking for the remainder of the problem? or its purpose varies depending on how you use it....
                        From what I know its the mod operator and so far I only know that you can use it for finding the reminder. Im not sure if it has any other uses. Maybe someone more knowledgeable can answer that question better.
                        = )

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #13
                          Originally posted by MariyaGel
                          From what I know its the mod operator and so far I only know that you can use it for finding the reminder. Im not sure if it has any other uses. Maybe someone more knowledgeable can answer that question better.
                          = )
                          As an operator that is it's only use.

                          If I understand bitong correctly they are asking if % can only be used in an if

                          remembering that the part in the brackets of an if statement is just an expression % can be used anywhere you can validly put an expression.


                          Another often used method of finding if a number is even is to use the AND bitwise operator &

                          if (number & 1)
                          {
                          // It's odd
                          }
                          else
                          {
                          // It's even
                          }

                          This is not as portable as using the % operator but is usually more efficient as the % operator is one of the less efficient ones.

                          Comment

                          • thefarmer
                            New Member
                            • Nov 2006
                            • 55

                            #14
                            hi,

                            % in C++ means modulus, it means the remainder of the number you are asking for, so in your program he's asking for the remainder of zero to get an EVEN number so if your number divided by 2 is not zero then the answer should be ODD.

                            hope it might help??

                            regards

                            Comment

                            • bitong
                              New Member
                              • Sep 2006
                              • 40

                              #15
                              Originally posted by sivadhas2006
                              Hi,

                              This code is working well.

                              Code:
                              #include<stdio.h>
                              int main()
                              {
                                 int 
                                    nos = 0;
                              
                                 printf("Enter number:");
                                 scanf("%d",&nos);
                              
                                 if(nos % 2 == 0)
                                    printf("Even number\n");
                                 else
                                    printf("Odd number\n");
                                 getch();
                                 return 0;
                              }
                              Regards,
                              M.Sivadhas.
                              hello...just want to ask another question...what is the purpose of putting int in the main() ----> int main()...and putting 0 in variable declaration? ---> int nos=0;

                              Comment

                              Working...