Java Programming Practical 3

Question 1
Write a program to check multiple conditions using if statement along with logical operators.
LogicalDemo.java
Java
				import java.util.*;

public class LogicalDemo
{
    public static void main(String a[]) 
	{
        int i;
		
		i=1;
		do
		{
			if(i==5||i==10)
			System.out.println(i);
			i++;
		}
		while(i<=20);
    }
}
			
Output
Question 2
Write a program to check no is even or odd.
Evenodd.java
Java
				import java.util.*;

public class Evenodd 
{
    public static void main(String a[]) 
	{
        int num;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number");
        num = sc.nextInt();
        if (num % 2 == 0) 
		{
            System.out.println("Number is even");
        } 
		else 
		{
            System.out.println("Number is odd");
        }
    }
}
			
Output
Question 3
Write a program to check switch-case using character datatype.
SwitchDemo.java
Java
				import java.util.*;

public class SwitchDemo
{
    public static void main(String a[]) 
	{
        char ch;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a character between a to f");
        ch = sc.nextLine().charAt(0);
		switch(ch)
		{
			case 'a':
			System.out.println("You have entered a");
			break;
			
			case 'b':
			System.out.println("You have entered b");
			break;
			
			case 'c':
			System.out.println("You have entered c");
			break;
			
			case 'd':
			System.out.println("You have entered d");
			break;
			
			case 'e':
			System.out.println("You have entered e");
			break;
			
			case 'f':
			System.out.println("You have entered f");
			break;
			
			default:
			System.out.println("Invalid character");
		}
    }
}
			
Output
Question 4
Write a program to display 1 to 20 numbers using for, while and do-while loop.
LoopDemo.java
Java
				import java.util.*;

public class LoopDemo
{
    public static void main(String a[]) 
	{
        int i;
		System.out.println("1 to 20 numbers using for loop:");
		for(i=1;i<=20;i++)
			System.out.println(i);
		
		System.out.println("1 to 20 numbers using while loop:");
		i=1;
		while(i<=20)
		{
			System.out.println(i);
			i++;
		}
		
		System.out.println("1 to 20 numbers using do while loop:");
		i=1;
		do
		{
			System.out.println(i);
			i++;
		}
		while(i<=20);
    }
}
			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *