even – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sat, 13 Aug 2022 14:44:53 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg even – Programmerbay https://programmerbay.com 32 32 Java Program to Check Whether a Number is Even or Odd https://programmerbay.com/java-program-to-check-whether-a-number-is-even-or-odd/ https://programmerbay.com/java-program-to-check-whether-a-number-is-even-or-odd/#respond Tue, 18 Jan 2022 11:56:26 +0000 https://www.programmerbay.com/?p=2351 The program checks whether an entered input is even or not. A number which is divisible by 2 is said to be even number whereas remaining ones are said to be odd. For example, even numbers are 2,4,6 whereas odd numbers are 1,3,5.

There are various ways to achieve this functionality. But in this article, we’ll be using two ways :

1) Using if and else
2) Using ternary Operator

Java program to check whether a number is even or odd using if and else statement

Program:

import java.util.*;
public class Main
{
  public static void main(String[] args) {
  int number; 
  Scanner ed = new Scanner(System.in); 
  System.out.print("Please enter a number : "); 
  number = ed.nextInt(); 
  if(number%2==0)
  System.out.print(number+" is even");
  else
  System.out.print(number+" is odd"); 
  }
}

Ouput:

In the case of Even:

Please enter a number : 90                                                                                                    

90 is even

In case of Odd:

Please enter a number : 43                                                                                                    

43 is odd

Java program to check whether a number is even or odd using ternary operator

Program:

import java.util.*;
public class Main
{
  public static void main(String[] args) {
  int number; 
  String answer; 
  Scanner ed = new Scanner(System.in); 
  System.out.print("Please enter a number : "); 
  number = ed.nextInt(); 
  answer =(number%2==0)?(number+" is even"):(number+" is odd"); 
  System.out.println(answer);
  }
}

Output:

In case of even :

Please enter a number : 22                                                                                                    

22 is even

In case of Odd :

Please enter a number : 89                                                                                                    

89 is odd

Approach :

  1. Using Modulo Operator (%), we can find whether the input is divisible by 2 or not as it would give remainder as 1 otherwise 0
  2.  If the remainder is zero, then it would be an even number, otherwise odd

]]>
https://programmerbay.com/java-program-to-check-whether-a-number-is-even-or-odd/feed/ 0
C Program to Check Whether a Number is Even or Odd https://programmerbay.com/c-program-to-check-whether-the-given-number-is-even-or-odd/ https://programmerbay.com/c-program-to-check-whether-the-given-number-is-even-or-odd/#respond Tue, 18 Jan 2022 11:44:37 +0000 https://www.programmerbay.com/?p=2347 The program checks whether an entered number is even or odd. A number is considered as even number if it is divisible by 2, otherwise, it would be treated as an odd number.

There are various ways to achieve this functionality. In this article, we’ll be using two ways :

1) Using if and else
2) Using ternary Operator

C program to Check Whether a Number is Even or Odd using if and else

Program : 

#include <stdio.h>
int main()
{
    int number ; 
    printf("Please enter a number : ");
    scanf("%d", &number);
    if(number % 2 == 0)
    {
        printf("%d is an even number.", number);
    }
    else{
        printf("%d is an odd number.", number);
    }
    return 0;
}

Output:

In the case of Even number input :

Please enter a number : 28                                                                               
28 is an even number.

In case of Odd number input :

Please enter a number : 7                                                                    
7 is an odd number.

Explanation:

In the above example, we have used if and else statement to check a number is even or odd. If number % 2, produces result as 0, then if block would be executed to print “even number message” on output screen, otherwise, else condition would be executed.

C program to Check Whether a Number is Even or Odd using ternary operator

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int number;
clrscr();
printf("\nPlease enter a number =");
scanf("%d",&number);
(number%2)==0?printf("\n %d is even",number):printf("\n %d is odd",number);
getch();
}

Output:

In case of even:

Please enter a number =20     
20 is even

In case of odd: 

Please enter a number =101     
101 is odd

In above code, we used same approach, but this time with the help of ternary operator to find a number is even or odd.

Approach :

  1. Using Modulo Operator (%), we can find whether the input is divisible by 2 or not as it would give remainder as 1 otherwise 0
  2.  If the remainder is zero, then it would be an even number, otherwise odd number

]]>
https://programmerbay.com/c-program-to-check-whether-the-given-number-is-even-or-odd/feed/ 0