You are on page 1of 3

1. Write a program that takes an integer and determines if it’s odd or even.

Use switch cases to


produce result.

package Lab2Demo;

import java.util.Scanner;

public class Q1 {
public static void main(String[] args) {
System.out.print("Enter a positive value :");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
if (n % 2 == 0)
n = 1;
else
n = 0;
switch (n) {
case 1:
System.out.println("Even");
break;
case 0:
System.out.println("Odd");
break;
}

}
}

Output :

Enter a positive value :6

Even

2. Write a program that takes an integer and determines if it’s prime or not. A number is prime
if it is divisible by 1 and itself only, i.e. 2, 3, 11, 37 etc.

package Lab2Demo;

import java.util.Scanner;

public class Q2 {
public static void main(String[] args) {
System.out.print("Enter a value :");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int count = 0;

for (int i = 1; i <= n; i++) {


if (n % i == 0) {
count++;
}
}
if (count == 2)
System.out.println("Prime Number");
else
System.out.println("Not Prime Number");

Output:

Enter a value :7
Prime Number

3. Write a program that prints the multiplication table from 1 to 5.


1X1=1
1X2=2
…………
5X10=50
package Lab2Demo;

import java.util.Scanner;

public class Q3 {
public static void main(String[] args) {
System.out.print("Enter a value for multiplication table: ");
Scanner ob = new Scanner(System.in);
int n = ob.nextInt();

for (int i = 1; i <= 10; i++) {


System.out.println(n + "*" + i + "=" + (n * i));
}
}
}
Output :

Enter a value for multiplication table: 5

5*1=5

5*2=10

5*3=15

5*4=20

5*5=25

5*6=30

5*7=35

5*8=40

5*9=45

5*10=50

4. Write a program that takes an integer and prints its divisors, i.e. divisors of 12 are 1, 2, 3, 4, 6.

package Lab2Demo;

import java.util.Scanner;

public class Q4 {
public static void main(String[] args) {
System.out.print("Enter a value :");
Scanner input = new Scanner(System.in);
int value = input.nextInt();
for (int i = 1; i <= value; i++) {
if (value % i == 0) {
System.out.printf("%d\t", i);
}
}
}
}

Output:

Enter a value :12

1 2 3 4 6 12

You might also like