Comprobar si un número dado es par o impar

Dado un número, comprueba si es par o impar.

C++

// A simple C++ program to
// check for even or odd
#include <iostream>
using namespace std;
 
// Returns true if n is
// even, else odd
bool isEven(int n) { return (n % 2 == 0); }
 
// Driver code
int main()
{
    int n = 101;
    isEven(n) ? cout << "Even" : cout << "Odd";
 
    return 0;
}

Java

// Java program  to
// check for even or odd
 
class GFG
{
    // Returns true if n is even, else odd
    public static boolean isEven(int n)
    {
        return (n % 2 == 0);
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int n = 101;
        if(isEven(n) == true)
            System.out.print("Even");
        else
            System.out.print("Odd");
    }
}
 
// This code is contributed by rishabh_jain

Python3

# A simple Python3 code
# to check for even or odd
 
# Returns true if n is even, else odd
def isEven(n):
    return (n % 2 == 0)
     
# Driver code
n = 101
print("Even" if isEven(n) else "Odd")
 
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# program to
// check for even or odd
using System;
 
class GFG
{
    // Returns true if n is even, else odd
    public static bool isEven(int n)
    {
        return (n % 2 == 0);
    }
     
    // Driver code
    public static void Main()
    {
        int n = 101;
        if(isEven(n) == true)
            Console.WriteLine("Even");
        else
            Console.WriteLine("Odd");
    }
}
 
// This code is contributed by vt_m

PHP

<?php
// A simple PHP program to
// check for even or odd
 
// Returns true if n is
// even, else odd
function isEven($n)
{
    return ($n % 2 == 0);
}
 
// Driver code
$n = 101;
if(isEven != true)
    echo "Even";
    else
    echo "Odd";
 
// This code is contributed by Ajit
?>

Javascript

<script>
 
// A simple Javascript program to
// check for even or odd
 
 
// Returns true if n is
// even, else odd
function isEven(n) { return (n % 2 == 0); }
 
// Driver code
 
    let n = 101;
    isEven(n) ? document.write("Even") :document.write("Odd");
 
 
// This code is contributed by Mayank Tyagi
 
</script>

C++

// A simple C++ program to
// check for even or odd
#include <iostream>
using namespace std;
 
// Returns true if n is
// even, else odd
bool isEven(int n)
{
     
// n & 1 is 1, then
// odd, else even
return (!(n & 1));
}
 
// Driver code
int main()
{
int n = 101;
isEven(n)? cout << "Even" :
           cout << "Odd";
 
return 0;
}

C

#include <stdio.h>
#include <math.h>
 
int main(){
  int n = 101;
  if (n%2==0){
    printf("Even");
  }
  else{
    printf("Odd");
  }
 return 0;
}

Java

// Java program to
// check for even or odd
 
class GFG
{
    // Returns true if n
    // is even, else odd
    public static boolean isEven(int n)
    {
        if((n & 1) == 0)
            return true;
        else
            return false;
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int n = 101;
        if(isEven(n) == true)
            System.out.print("Even");
        else
            System.out.print("Odd");
    }
}
 
// This code is contributed by rishabh_jain

Python3

# A Python3 code program
# to check for even or odd
 
# Returns true if n is even, else odd
def isEven(n):
     
    # n&1 is 1, then odd, else even
    return (not(n & 1))
     
# Driver code
n = 101;
print("Even" if isEven(n) else "Odd")
 
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# program  to
// check for even or odd
using System;
 
class GFG
{
    // Returns true if n
    // is even, else odd
    public static bool isEven(int n)
    {
        if((n & 1) == 0)
            return true;
        else
            return false;
    }
     
    // Driver code
    public static void Main()
    {
        int n = 101;
        if(isEven(n) == true)
            Console.WriteLine("Even");
        else
            Console.WriteLine("Odd");
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// A simple PHP program to
// check for even or odd
  
// Returns true if n is
// even, else odd
function isEven($n)
{
    return (!($n & 1));
}
  
// Driver code
$n = 101;
if(isEven($n) == true)
    echo "Even";
else
    echo "Odd";
  
// This code is contributed by Smitha
?>

Javascript

<script>
// A simple JavaScript program to
// check for even or odd
 
// Returns true if n is
// even, else odd
function isEven(n)
{
     
    // n & 1 is 1, then
    // odd, else even
    return (!(n & 1));
}
 
// Driver code
let n = 101;
isEven(n)? document.write("Even") :
        document.write("Odd");
 
// This code is contributed by Manoj.
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *