Explanation
The following code is written in the C programming language and checks if a number entered by the
user is even or odd. The
code uses the ternary operator ? :
to achieve this. The code starts by including the
standard
input-output library stdio.h
which contains functions like printf
and
scanf
that are used in the code.
#include <stdio.h>
The main function is then defined using the int main()
syntax, which is the entry point
for all C
programs. Inside the main function, an integer variable d
is declared to store the
number entered by
the user.
int main()
{
int d;
The code then prints a message to the user asking them to enter a number. This is done using the
printf
function which takes a formatted string as an argument and displays it on the screen.
printf("\n\n Program to check if an entered number is even or odd. \n\n");
printf("Enter a number with a space - (Example: 23) - and press enter: ");
The scanf
function is then used to get the number entered by the user. The
%d
in the
format string of the scanf
function specifies that the input should be an integer, and
the
&d
symbol is the memory address of the d
variable, which stores the
entered number.
scanf("%d", &d);
The code then prints a message saying "The entered number is", followed by the result of the
even/odd check. This
check is performed using the ternary operator ? :
, which takes two expressions and
evaluates to the
expression on the left of the :
if the condition in the middle is true, and to the
expression on the
right if it is false.
printf("\n The entered number is");
(d % 2 == 0) ? printf(" an even number.\n") : printf(" an odd number.\n");
The condition being checked in the ternary operator is whether the entered number is divisible by 2.
If it is
divisible by 2, then the number is even and the code outputs "an even number." If it is not
divisible by 2, then the
code outputs "an odd number." The %
symbol is the modulo operator in C, which gives the
remainder of a
division operation.
Finally, the main function returns a value of 0 to indicate that the program has executed successfully.
return 0;
}
Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
int main()
{
int d;
printf("\n\n Program to check if an entered number is even or odd. \n\n");
printf("Enter a number with a space - (Example: 23) - and press enter: ");
scanf("%d", &d);
printf("\n The entered number is");
(d % 2 == 0) ? printf(" an even number.\n") : printf(" an odd number.\n");
return 0;
}
Output
Program to check if an entered number is even or odd
Enter a number with a space - (Example: 23) - and press enter: 5
The entered number is an odd number.