■ Java Flow Control Interview Programs (Complete
Solutions)
1. Check if a number is Even or Odd
public class EvenOdd {
public static void main(String[] args) {
int n = 10;
if (n % 2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
}
}
2. Find the Largest of Three Numbers
public class LargestOfThree {
public static void main(String[] args) {
int a = 10, b = 20, c = 15;
if (a >= b && a >= c)
System.out.println("Largest: " + a);
else if (b >= a && b >= c)
System.out.println("Largest: " + b);
else
System.out.println("Largest: " + c);
}
}
3. Simple Calculator using switch
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
char op = sc.next().charAt(0);
switch (op) {
case '+': System.out.println(a + b); break;
case '-': System.out.println(a - b); break;
case '*': System.out.println(a * b); break;
case '/': System.out.println(a / b); break;
default: System.out.println("Invalid operator");
}
}
}
4. Factorial of a Number
public class Factorial {
public static void main(String[] args) {
int n = 5, fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
System.out.println("Factorial: " + fact);
}
}
5. Fibonacci Series up to N terms
public class Fibonacci {
public static void main(String[] args) {
int n = 10, a = 0, b = 1;
System.out.print(a + " " + b);
for (int i = 2; i < n; i++) {
int c = a + b;
System.out.print(" " + c);
a = b;
b = c;
}
}
}
6. Reverse a Number
public class ReverseNumber {
public static void main(String[] args) {
int n = 12345, rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n = n / 10;
}
System.out.println("Reversed: " + rev);
}
}
7. Check if a Number is Palindrome
public class Palindrome {
public static void main(String[] args) {
int n = 121, temp = n, rev = 0;
while (n > 0) {
rev = rev * 10 + n % 10;
n /= 10;
}
if (temp == rev)
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
}
}
8. Sum of Digits of a Number
public class SumOfDigits {
public static void main(String[] args) {
int n = 1234, sum = 0;
do {
sum += n % 10;
n /= 10;
} while (n > 0);
System.out.println("Sum: " + sum);
}
}
9. Prime Number Check
public class PrimeCheck {
public static void main(String[] args) {
int n = 29;
boolean isPrime = true;
if (n <= 1) isPrime = false;
else {
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = false;
break;
}
}
}
System.out.println(isPrime ? "Prime" : "Not Prime");
}
}
10. Armstrong Number Check
public class Armstrong {
public static void main(String[] args) {
int n = 153, sum = 0, temp = n;
while (n > 0) {
int d = n % 10;
sum += d * d * d;
n /= 10;
}
if (temp == sum)
System.out.println("Armstrong Number");
else
System.out.println("Not Armstrong");
}
}
11. GCD of Two Numbers
public class GCD {
public static void main(String[] args) {
int a = 56, b = 98;
while (a != b) {
if (a > b) a -= b;
else b -= a;
}
System.out.println("GCD: " + a);
}
}
12. Count Digits in a Number
public class CountDigits {
public static void main(String[] args) {
int n = 987654, count = 0;
while (n > 0) {
n /= 10;
count++;
}
System.out.println("Digits: " + count);
}
}
13. Check Leap Year
public class LeapYear {
public static void main(String[] args) {
int year = 2024;
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
System.out.println("Leap Year");
else
System.out.println("Not Leap Year");
}
}
14. Days in a Month (switch)
public class DaysInMonth {
public static void main(String[] args) {
int month = 2;
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
System.out.println("31 days"); break;
case 4: case 6: case 9: case 11:
System.out.println("30 days"); break;
case 2:
System.out.println("28 or 29 days"); break;
default:
System.out.println("Invalid month");
}
}
}
15. Sum of First N Natural Numbers
public class SumNatural {
public static void main(String[] args) {
int n = 10, sum = 0, i = 1;
do {
sum += i;
i++;
} while (i <= n);
System.out.println("Sum = " + sum);
}
}
16. Swap Two Numbers Without Third Variable
public class SwapNumbers {
public static void main(String[] args) {
int a = 10, b = 20;
a = a + b;
b = a - b;
a = a - b;
System.out.println("a = " + a + ", b = " + b);
}
}
17. Missing Number in Array
public class MissingNumber {
public static void main(String[] args) {
int[] arr = {1, 2, 4, 5};
int n = 5;
int sum = n * (n + 1) / 2;
int arrSum = 0;
for (int num : arr) arrSum += num;
System.out.println("Missing Number: " + (sum - arrSum));
}
}
18. Find Duplicate Elements in Array
public class DuplicateElements {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 2, 5, 3};
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j])
System.out.println("Duplicate: " + arr[i]);
}
}
}
}
19. Find Largest and Smallest in Array
public class MinMax {
public static void main(String[] args) {
int[] arr = {10, 50, 20, 90, 5};
int min = arr[0], max = arr[0];
for (int n : arr) {
if (n < min) min = n;
if (n > max) max = n;
}
System.out.println("Min = " + min + ", Max = " + max);
}
}
20. Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str = "hello";
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev += str.charAt(i);
}
System.out.println("Reversed: " + rev);
}
}
21. Check if Two Strings are Anagrams
import java.util.Arrays;
public class AnagramCheck {
public static void main(String[] args) {
String s1 = "listen";
String s2 = "silent";
char[] a1 = s1.toCharArray();
char[] a2 = s2.toCharArray();
Arrays.sort(a1);
Arrays.sort(a2);
if (Arrays.equals(a1, a2))
System.out.println("Anagram");
else
System.out.println("Not Anagram");
}
}
22. Find First Non-Repeated Character
public class FirstNonRepeated {
public static void main(String[] args) {
String str = "aabbcdeff";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (str.indexOf(ch) == str.lastIndexOf(ch)) {
System.out.println("First Non-Repeated: " + ch);
break;
}
}
}
}
23. Count Vowels and Consonants
public class CountVowels {
public static void main(String[] args) {
String str = "Interview";
int v = 0, c = 0;
str = str.toLowerCase();
for (char ch : str.toCharArray()) {
if (ch >= 'a' && ch <= 'z') {
if ("aeiou".indexOf(ch) != -1)
v++;
else
c++;
}
}
System.out.println("Vowels: " + v + ", Consonants: " + c);
}
}
24. Find Second Largest Element in Array
public class SecondLargest {
public static void main(String[] args) {
int[] arr = {10, 20, 4, 45, 99, 99};
int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
for (int n : arr) {
if (n > first) {
second = first;
first = n;
} else if (n > second && n != first) {
second = n;
}
}
System.out.println("Second Largest: " + second);
}
}
25. Palindrome String
public class PalindromeString {
public static void main(String[] args) {
String str = "madam";
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev += str.charAt(i);
}
if (str.equals(rev))
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
}
}