HomeInterviewinterview program javaTop 11 Interview program in java for checking conditions

Top 11 Interview program in java for checking conditions

This is set of program in java, You can also refer our previous post

Write a program in java to check whether the given number is an alphabet or not.

package com.onurdesk.check.programs;

//like us on facebook : https://facebook.com/onurdesk

import java.util.Scanner;

public class CheckAlphabetOrNot {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter any Number...");
		char ch = sc.next().charAt(0);
		if (ch > 'a' && ch < 'z' || ch > 'A' && ch < 'Z') {
			System.out.println("It is a alphabet");
		} else {
			System.out.println("It is a not alphabet");
		}

	}
}

Write a program in java to check whether a given string is an anagram or not.

package com.onurdesk.check.programs;

import java.util.Arrays;
import java.util.Scanner;

public class CheckAnagaramsOrNot {

	public static void main(String[] args) {

		String str1 = "dealer";
		String str2 = "leader";
		boolean b = areAnagrams(str1, str2);
		if (b == true) {
			System.out.println("it is a Anagran");
		} else {
			System.out.println("It is a not anagarms");
		}
	}

	private static boolean areAnagrams(String str1, String str2) {
		if (str1.length() != str2.length()) {
			return false;
		}
		str1 = str1.toUpperCase();
		str2 = str2.toUpperCase();

		char[] str11 = str1.toCharArray();
		Arrays.sort(str11);
		char[] str22 = str2.toCharArray();
		Arrays.sort(str22);
		return Arrays.equals(str11, str22);
	}

}

Write a program in java to check whether a given string is an anagram or not (Type 2).

package com.onurdesk.check.programs;

import java.util.Arrays;
import java.util.Scanner;

public class CheckAnagramOrNot {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter any String");
		String x = s.nextLine();

		System.out.println("Enter any String");
		String y = s.nextLine();

		char[] x1 = x.toCharArray();
		char[] y1 = y.toCharArray();
		Arrays.sort(x1);
		Arrays.sort(y1);
		Boolean result = Arrays.equals(x1, y1);
		if (result == true) {
			System.out.println("It is a Anagram");
		} else {
			System.out.println("It is not Anagram");
		}

	}

}

Write a program in java to check whether the no. is composite or not.

package com.onurdesk.check.programs;

public class CheckCompositeNumberOrNot {

	public static void main(String[] args) {
		int n = 11;
		compositeNumber(n);
	}

	public static void compositeNumber(int n) {
		int c = 0;
		for (int i = 2; i <= n; i++) {
			if (n % i == 0) {
				c++;
			}
		}
		if (c > 1) {
			System.out.println("It is composite number");
		} else {
			System.out.println("It is not composite number");
		}
	}

}

Write a program in java to check whether no. is even or odd.

package com.onurdesk.check.programs;

import java.util.Scanner;

public class CheckEvenOrOdd {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter any Number");
		int n = scan.nextInt();
		int i = 2;
		if (n % i == 0) {
			System.out.println("It is Even Number");
		} else {
			System.out.println("It is Odd Number");
		}

	}

}

Write a program in java to Check Whether An Array Contains Duplicate Or Not.

package com.onurdesk.check.programs;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CheckeWetheAmArrayContainsDuplicatsOrNot {

	public static void main(String[] args) {
		String arry[] = { "java", "sql", "java", "C langugae" };
		List<String> duplicateList = Arrays.asList(arry);
		Set<String> duplicateSet = new HashSet<>(duplicateList);
		if (duplicateList.size() != duplicateSet.size()) {
			System.out.println("It contains duplicates");
		} else {
			System.out.println("It does not contain duplicates");
		}

	}

}

Write a program in java to check whether it is a leap year or not.

package com.onurdesk.check.programs;

import java.util.Scanner;

public class CheckLeapYearOrNot {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter any Number");
		int yr = scan.nextInt();
		if (yr % 4 == 0 && yr % 100 != 0) {
			System.out.println("It is a leap year");
		} else if (yr % 100 == 0 && yr % 400 == 0) {
			System.out.println("It is leap year");
		} else if (yr % 400 == 0) {
			System.out.println("It is a leap year");
		} else {
			System.out.println("It is not leap Year");
		}
	}

}

Write a program in java to check whether a no. is prime or not.

package com.onurdesk.check.programs;

import java.util.Scanner;

public class CheckNumberPrimeOrNot {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		System.out.println("Enter any number");
		int x = s.nextInt();
		boolean prime = true;
		for (int i = 2; i < x; i++) {
			if (x % i == 0) {
				prime = false;
				break;
			}
		}
		if (prime == true) {
			System.out.println("Given number is prime");
		} else {
			System.out.println("Given number is not prime");
		}
	}

}

Write a program in java to check whether a no. and the string is palindrome or not.

package com.onurdesk.check.programs;

import java.util.Scanner;

public class CheckPalindromeOrNot {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter String");
		String str = sc.nextLine();
		if (isPalindrome(str)) {
			System.out.println("It is a palindrome");
		} else {
			System.out.println("It is not palindrome");

		}
		System.out.println("=============================");
		System.out.println("Enter a number");
		int num = sc.nextInt();
		if (palindrome(num) == num) {
			System.out.println("It is a palindrome");
		} else {
			System.out.println("it is not pakindrome");
		}
	}

	// in number Palindrome and Reverse a number
	private static int palindrome(int num) {
		int rev = 0;

		while (num > 0) {
			int r = num % 10;
			rev = rev * 10 + r;
			num = num / 10;
		}
		return rev;
	}

	// in String Palindrome
	private static boolean isPalindrome(String str) {
		char str1[] = str.toCharArray();
		int size = str1.length - 1;
		System.out.println(size);

		for (int i = 0; i <= size; i++) {
			if (str1[i] != str1[size]) {
				return false;
			}
			size--;
		}
		return true;
	}

}

Write a program in java to reverse the number and check whether it is reversal equal or not.

package com.onurdesk.check.programs;

import java.util.Scanner;

public class CheckReversalEqual {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Enter any Number");
		int n = scan.nextInt();
		int Orginal = n;
		int rem, rev = 0;
		while (n > 0) {
			rem = n % 10;
			rev = rev * 10 + rem;
			n = n / 10;
		}
		System.out.println("Reverse Number is " + rev);
		if (Orginal == rev) {
			System.out.println("It is Reverse is Equal");
		} else {
			System.out.println("It is Reverse is Not eqaul");
		}
	}

}

Write a program in java to check whether string is palindrome or not.

package com.onurdesk.check.programs;

public class CheckStringIsPalindromeOrNot {

	public static void main(String[] args) {
		String s1 = "saippuaki ikauppias";
		String s = s1.replace(" ", "");
		char[] c = s.toCharArray();
		int size = c.length;
		char[] rev = new char[size];
		for (int i = 0; i < size; i++) {
			rev[i] = c[size - 1 - i];
		}
		System.out.println(rev);
		for (int i = 0; i < size; i++) {
			if (c[i] == rev[i]) {

				System.out.println("It is  palindrome");
				System.exit(0);
			} else {
				i++;
				continue;
			}
		}
		System.out.println("It is a not palindrome");
	}
}

Admin
Onurdesk is a technical platform based primarily on Spring, Java, and Nodejs ecosystem app creation. We write tutorials and manuals concentrating extensively on realistic and real-life usage cases.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular

Recent Comments