C programs to practice - To check the given number is odd or even by using conditional operator

 First, let us know the logic behind this problem. To check the given number is odd or even,just  use %(modulation) operator. It will give you the reminder.

 If the reminder is zero,it is even number. Otherwise, the given number is odd number.

Now, the coding snippet begins....

Program “oddoreven.c”

#include<stdio.h>

#include<conio.h>

void main()

{

int a;

printf(“Enter the number”);

scanf(“%d”,&a);

(a%2==0)? printf(“Given number is even”): printf(“Given number is odd”);

getch();

}

 

Output:

 Enter the number 5

Given number is odd


Now, try this....

 Write a c program to find which number is greater among the given two numbers using conditional operator.

Program: “great2.c”

#include <stdio.h>

#include<conio.h>

void main()

{

 int c,d;

 printf("enter the two numbers");

 scanf("%d %d",&c,&d);

 (c>d)? printf("c is greater than d"): printf("d is greater than c");

 getch();

 }

No comments:

Post a Comment