0% found this document useful (0 votes)
15 views3 pages

Program 8

The document provides a Java program that utilizes lambda expressions to perform three operations: checking if a number is odd or even, determining if it is prime or composite, and verifying if it is a palindrome. The program prompts the user to select an operation and input a number, then applies the corresponding lambda expression to return the result. Interfaces are defined for each operation, and the main method contains a loop for continuous user interaction.

Uploaded by

PRIYANSHU KUMARI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Program 8

The document provides a Java program that utilizes lambda expressions to perform three operations: checking if a number is odd or even, determining if it is prime or composite, and verifying if it is a palindrome. The program prompts the user to select an operation and input a number, then applies the corresponding lambda expression to return the result. Interfaces are defined for each operation, and the main method contains a loop for continuous user interaction.

Uploaded by

PRIYANSHU KUMARI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lambda Expressions

8. Write the following methods that return a lambda expression performing a specified action:
(i) PerformOperation isOdd(): The lambda expression must return true if a number is odd
or false if it is even.
(ii) PerformOperation isPrime(): The lambda expression must return true if a number is
prime or false if it is composite.
(iii) PerformOperation isPalindrome(): The lambda expression must return true if a
number is a palindrome or false if it is not.
Write a JAVA program using above lambda expressions to take 2 integers as input where the
first integer specifies the condition to check for (case 1 for Odd/Even, case 2 for
Prime/Composite, or case 3 for Palindrome). The second integer denotes the number to be
checked.

import java.util.Scanner;

interface Odd_or_Even
{
Boolean IsOdd_or_Even(int n);
}

interface Prime_or_not
{
Boolean IsPrime(int n);
}

interface plaindrome{
Boolean Ispalindrome(int n);
}

public class Program8 {


public static void main(String[] args)
{
int a,b;
String[] array = new String[10];
Scanner sc = new Scanner(System.in);
int choice;
for(;;)
{
System.out.println("1:To check odd or even");
System.out.println("2:To check prime or not");
System.out.println("3:To check palindrome");
System.out.println("Enter your choice");
choice = sc.nextInt();

switch (choice)
{
case 1: System.out.println("Enter a number");
a=sc.nextInt();
Odd_or_Even odd_or_even = (n) -> {
if (n % 2 == 0) {
return false;
}
else
{
return true;
}
};
if(odd_or_even.IsOdd_or_Even(a))
System.out.println("True");
else
System.out.println("False");
break;

case 2: System.out.println("Enter a number");


a=sc.nextInt();
Prime_or_not is_prime = (n) -> {
int flag=0;

for (int i = 2; i <= n / 2; ++i) {


if (n % i == 0) {
flag = 1;
break;
}
}

if (flag == 0)
return true;
else
return false;

};
if(is_prime.IsPrime(a))
System.out.println("True");
else
System.out.println("False");
break;

case 3: System.out.println("Enter a number");


a=sc.nextInt();
plaindrome palindrome = (n) -> {
int temp,r,sum=0;
temp=n;
while(n>0){
r = n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
return true;
else
return false;
};
if(palindrome.Ispalindrome(a))
System.out.println("True");
else
System.out.println("False");
break;
}
}
}
}

Output

You might also like