Even or odd

 Check whether a number is Even or odd


C Program
1.Using if else Statement
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a;
    printf("Enter a number:");
    scanf("%d",&a);
    if(a%2==0)
    {
        printf("even");
    }
    else
    {
        printf("odd");
    }
}

2.Using Switch Statement
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a;
    printf("Enter a number:");
    scanf("%d",&a);
     switch(a%2)
    {
        case 0: printf("even");
                break;
        default: printf("odd");
    }
}

3.Using Ternary Operator
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a;
    printf("Enter a number:");
    scanf("%d",&a);
    (a%2==0) ? printf("even") : printf("odd");
}


Java Program
1. If Else Statement
import java.util.Scanner;

public class EvenOdd {

	public static void main(String[] args) 
	{
		Scanner s = new Scanner(System.in);
		System.out.print("Enter Any Number:");
		int a = s.nextInt();
		if(a%2==0)
			System.out.print("even");
		else
			System.out.print("odd");
	}

}

2.Switch Statement
import java.util.Scanner;

public class EvenOdd {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.print("Enter Any Number:");
		int a = s.nextInt();
		   switch(a%2)
		    {
		        case 0: System.out.print("even");
		                break;
		        default: System.out.print("odd");
		    }
	}

 }

3.Ternary Operator
import java.util.Scanner;

public class Evenodd{

	public static void main(String[] args)
	{
		Scanner s = new Scanner(System.in);
		System.out.print("Enter Any Number:");
		int a = s.nextInt();	
		String Check = a%2==0 ? "Even" : "Odd";

		System.out.println(Check);
	}

}

0 Comments:

Post a Comment