Header Ads

04 - WAP to Print Positive & Negative, Odd or Even Numbers in C


This C - Programs gets the a Value and Checks if it's Less than or Greater than Zero, and Prints the Number if it's Odd or Even.

//to check positive or negative even and odd no
#include 
int main()
{
 int x, yes;
 while(1)
 {
  printf("Enter the number : ");
  scanf("%d",&x);
  //This part executes for Positive Number from below condition..
  if ( x > 0 )
  {
   if ( x % 2 == 0 )
   {
    printf("%d is a Positive Even Number\n",x);
   }
  else
   {
    printf("%d is a Positive Odd Number\n",x);
   }
  }
  //This part executes for Negative Number from below condition..
  else if ( x < 0)
  {
   if ( x % 2 == 0 )
   {
    printf("%d is a Negative Even Number\n",x);
   }
  else
   {
    printf("%d is a Negative odd Number\n",x);
   }
  }
  //If entered value is zero..  
  else
  {
   printf("You have entered '0'!!\n");
  }

  printf ("\nTo continue press ['Y'/'n'] : ");
  getchar();
  yes = getchar ();
  if (!(  yes == 'y' || yes == 'Y'))
  break;
 }
 return 0;
}

Program Output:

No comments