Tuesday, 14 May 2019

c and c++ program to check whether a given number is odd or even

EVEN NUMBER:

 Any number divided by 2 and gives the remainder 0

ex:4(4/2 remainder 0)

ODD NUMBER: 

Any number divided by 2 and gives the remainder 1

ex:3(3/2 remainder 1)

C PROGRAM:


#include <stdio.h>

int main()
{
    int num;

    printf("Enter an number");

    scanf("%d", &num);
    
    if(num % 2 == 0)
/*number is perfectly divided by 2 then if condition is trueo therwise false go to else statement*/
        printf("%d is even.", num);
    else
        printf("%d is odd.", num);

    return 0;
}

OUT PUT:

C++ PROGRAM:

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

    cout<<"Enter an number";
    cin>>num;
    
    if(num % 2 == 0)
  /*number is perfectly divided by 2 then if condition is true
    otherwise false go to else statement*/
        cout<<num<<" is even.";
    else
        cout<<num<<" is odd.";

    return 0;
}

OUTPUT:


No comments:

Post a Comment