0% found this document useful (0 votes)
24 views6 pages

Day 5 Coding Solution

The document provides code examples in C, C++, Java, and Python to check if a given number is even or odd. It takes a number as input from the user and uses the modulo operator (%) to check if the remainder of dividing the number by 2 is 0, in which case it is even, or not 0, in which case it is odd. It then prints the corresponding output. Three different methods are demonstrated for each language using if/else conditional statements, ternary operators, and bitwise operators to determine and print whether the number is even or odd.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views6 pages

Day 5 Coding Solution

The document provides code examples in C, C++, Java, and Python to check if a given number is even or odd. It takes a number as input from the user and uses the modulo operator (%) to check if the remainder of dividing the number by 2 is 0, in which case it is even, or not 0, in which case it is odd. It then prints the corresponding output. Three different methods are demonstrated for each language using if/else conditional statements, ternary operators, and bitwise operators to determine and print whether the number is even or odd.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Talent Battle 100 Days Coding Series

Write a program to identify if the number is even or odd

Description

Get a number as input from the user and check whether the given number is odd or even

Input

10

Output

Even

Input

Output

Odd

C Program

Method 1

#include <stdio.h>

int main()

int num;

printf ("Enter a number: ");

scanf ("%d", &num);

if (num % 2 == 0)

printf ("Even number");

else

printf ("Odd number");

return 0;

Method 2
Talent Battle 100 Days Coding Series

#include <stdio.h>

int main()

int num;

printf ("Enter a number: ");

scanf ("%d", &num);

num%2 == 0? printf("Even number"):printf("Odd number");

return 0;

Method 3

#include <stdio.h>

int main()

int num,result;

printf ("Enter a number: ");

scanf ("%d", &num);

if(num & 1)

result=1;

else

result =0;

(result==1)? printf("Odd") : printf("Even");

return 0;

C++ Program
Talent Battle 100 Days Coding Series

Method 1

#include <iostream>

using namespace std;

int main()

int num;

cout<<"Enter a number: ";

cin>>num;

if (num % 2 == 0)

cout<<"Even number";

else

cout<<"Odd number";

return 0;

Method 2

#include <iostream>

using namespace std;

int main()

int num;

cout<<"Enter a number: ";

scanf ("%d", &num);

num%2 == 0? cout<<"Even number":cout<<"Odd number";

return 0;

Method 3

#include <iostream>
Talent Battle 100 Days Coding Series

using namespace std;

int main()

int num,result;

cout<<"Enter a number: ";

cin>>num;

if(num & 1)

result=1;

else

result =0;

(result==1)? cout<<"Odd" : cout<<"Even";

return 0;

Java

Method 1

import java.util.Scanner;

public class Main

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number : ");

int num = sc.nextInt();

if (num % 2 == 0)

System.out.println("Even number");

else

System.out.println("Odd number");

}
Talent Battle 100 Days Coding Series

Method 2

import java.util.Scanner;

public class Main

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number : ");

int num = sc.nextInt();

String result = num % 2 == 0 ? "Even number" : "Odd number";

System.out.println (result);

Python

Method 1

num = int(input("Enter a number:"))

if num % 2 == 0:

print("Even number ")

else:

print("Odd number ")

Method 2

num = int(input("Enter a number:"))

print("Even number") if num%2 == 0 else print("Odd number")

Method 3

num = int(input("Enter a number:"))

if num&1:

print("Odd")
Talent Battle 100 Days Coding Series

else:

print("Even")

You might also like