0% found this document useful (0 votes)
30 views4 pages

B54 Practical

The document contains 5 code examples that demonstrate different Java programming concepts: 1) A program that takes user input and determines if a number is even or odd. 2) A program that calculates the factorial of a user-input number. 3) A program that uses switch case to determine a student's grade based on test score. 4) A program that demonstrates logical operators like AND, OR, and NOT. 5) A program that demonstrates bitwise operators like AND, OR, XOR, and complement.
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)
30 views4 pages

B54 Practical

The document contains 5 code examples that demonstrate different Java programming concepts: 1) A program that takes user input and determines if a number is even or odd. 2) A program that calculates the factorial of a user-input number. 3) A program that uses switch case to determine a student's grade based on test score. 4) A program that demonstrates logical operators like AND, OR, and NOT. 5) A program that demonstrates bitwise operators like AND, OR, XOR, and complement.
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/ 4

1.Write a program to find the no. is even or odd.

import java.util.Scanner; public class EvenOdd {

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

System.out.print("Enter a number: "); int num = reader.nextInt();

if(num % 2 == 0)

System.out.println(num + " is even"); else

System.out.println(num + " is odd");

Output :

2.Write a program to calculate a factorial of a number.

import java.util.*; public class Example

public static void main(String []args)

//Take input from the user

//Create an instance of the Scanner Class Scanner sc=new Scanner(System.in);

//Declare and Initialize the variable System.out.println("Enter the number: "); int num=sc.nextInt();
int i=1,fact=1; while(i<=num)

fact=fact*i; i++;

System.out.println("Factorial of the number: "+fact);

Output :

3.Write a program to calculate the grade using switch case.

import java.util.Scanner; public class Student {

public static void main(String[] args) {

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

Scanner sc = new Scanner(System.in);

int score = sc.nextInt();

// for >= 90 case 10:

case 9:

grade = "A"; break;

// for >= 80 and <90

case 8:
grade = "B"; break;

// for >= 70 and <80

case 7:

grade = "C"; break;

// for >= 60 and <70

case 6:

grade = "D"; break;

// for >= 50 and <60

case 5:

grade = "E"; break;

// for < 50

default: grade = "F"; break;

System.out.println("Your Grade is = " + grade);

Output :

4.Write a program to demonstrate logical operator

class Example

public static void main (String []args)

System.out.println("Logical AND"); System.out.println((10>5)&&(2>1));


System.out.println((10>5)&&(2<1)); System.out.println((10<5)&&(2<1)); System.out.println("Logical
OR"); System.out.println((10>5)||(2<1)); System.out.println((10>5)||(2>1));
System.out.println((10<5)||(2<1)); System.out.println("Logical NOT"); System.out.println(!(10>5));
System.out.println(!(10<5));

Output:

5.Write a program to demonstrate a bitwise operator.

class Example

public static void main(String [] args)

int a=5 , b=7 ; System.out.println("AND" + (a&b)); System.out.println("OR"+ (a|b));


System.out.println("XOR"+ (a^b)); System.out.println("Complement"+ (~a))

Output:

You might also like