Tuesday, 14 May 2019

check whether a given number is even or odd using conditional operator in c and c++

C PROGRAM:

#include <stdio.h>
int main()
{
    int num;

    printf("Enter an number: ");
    scanf("%d", &num);

    (num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.", num);

    return 0;
}

 OUTPUT:

C++ PROGRAM:

#include <iostream>
using namespace std;
int main()
{
    int num;

    cout<<"Enter an number: ";
    cin>>num;

    (num % 2 == 0) ? cout<<num<< "is even.":cout<<num<<" is odd.";

    return 0;
}

OUTPUT:


No comments:

Post a Comment