The steps to create a program to find whether a given number is odd or even using if…else statement:
stdio.h
.int num;
.printf
and scanf
functions.if
statement to check if the number is even. This can be done by checking if the remainder of the number divided by 2 is equal to 0, i.e., if (number % 2 == 0)
. If this condition is true, use printf
to display that the number is even.else
statement to display that the number is odd.Type the following code in your IDE (preferably Dev C++)
// ODD EEVEN using if...else statement
#include<stdio.h>
int main()
{
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if(num%2 == 0)
{
printf("%d is EVEN.", num);
}
else
{
printf("%d is ODD.", num);
}
return 0;
}
The steps to create a program to find whether a given number is odd or even using switch statement:
stdio.h
.int num, rem;
.printf
and scanf
functions.num % 2 and store the result on variable rem
.Type the following code in your IDE (preferably Dev C++)
// ODD EEVEN using switch statement
#include<stdio.h>
int main()
{
int num, rem;
printf("Enter positive integer: ");
scanf("%d", &num);
rem = num%2;
switch(rem)
{
case 1: printf("%d is ODD.", num);
break;
default: printf("%d is EVEN.", num);
break;
}
return 0;
}
The steps to create a program to find whether a given number is odd or even using conditional operator-based statement:
stdio.h
.int num, res;
printf
and scanf
functions.// ODD EEVEN using conditional operator
#include<stdio.h>
int main()
{
int num, res;
printf("Enter positive integer: ");
scanf("%d", &num);
res = (num%2)==0 ? printf("%d is EVEN.", num) : printf("%d is ODD.", num);
return 0;
}
The steps to create a program to find whether a given number is odd or even using if…else statement:
stdio.h
.int num;
.printf
and scanf
functions.if
statement to check if the number is even. This can be done by checking if the remainder of the number using bitwise and (i.e. &) by 1 is equal to 1, i.e., if (number & 1 == 1)
. If this condition is true, use printf
to display that the number is odd.else
statement to display that the number is even.// ODD EEVEN using bitwise operator
#include<stdio.h>
int main()
{
int num, res;
printf("Enter positive integer: ");
scanf("%d", &num);
if(num & 1 == 1)
{
printf("%d is ODD.", num);
}
else
{
printf("%d is EVEN.", num);
}
return 0;
}
The steps to create a program to find whether a given year is leap year or not using if…else statement:
stdio.h
.int year;
printf
and scanf
functions.#include <stdio.h>
int main() {
int year;
//year = 1904;
printf("\n Enter the year :");
scanf("%d", &year);
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
printf("A %d is a leap year", year);
else
printf("A %d is not a leap year", year);
return 0;
}
All about java servlets Part-2 What is a website? Website is a collection of related…
Let's train the machine. Machine learning is a growing technology which enables computers to learn…
“Unless you try to do something beyond what you have already mastered, you will never…
Whenever you decide to start coding and you want to become budding coder or programmer…
C components are essential elements of a C program, including comments, preprocessor directives, functions, statements,…
All about java servlets Part-2 What is a website? Website is a collection of related…