Ticker

6/recent/ticker-posts

even/odd and positive/negative Program




1.Check whether number is even or odd - 

  • if-else

#include <stdio.h>

int main()
{ int a;
    printf("Enter the number ");
    scanf("%d",&a); //Input the number
 if (a%2==0)
{
    printf("The number is even"); 
}
else 
{
    printf("The number is odd"); 
}    
return 0;
}

  • Conditional (ternary)

#include <stdio.h>

int main()
{ int a;
    printf("Enter the number ");
    scanf("%d",&a); //Input the numbers
    a%2==0?printf("even"):printf("odd");

    return 0;
}



2.Check whether number is positive or negative - 

  • if-else 
#include <stdio.h>

int main()
{ int a;
    printf("Enter the number ");
    scanf("%d",&a); //Input the number
 if (a==0)
{
    printf("The number is 0"); //firstly check if it is zero
}
else if (a>0)
{
    printf("The number is positive"); //Then checks if it is positive 
}    
else 
{
    printf("The number is negative"); //If none of the above then simply prints negative
} 
return 0;
}
 
  • Conditional (ternary) - 
#include <stdio.h>

int main()
{ int a;
    printf("Enter the number ");
    scanf("%d",&a); //Input the numbers
    a==0?printf("0"):a>0?printf("positive"):printf("negative");
    return 0;
}

Post a Comment

0 Comments