Java while Loop — Interview
Problem-Solving Programs
1. Print numbers from 1 to N
public class PrintNumbers {
public static void
main(String[] args) {
int n = 10;
int i = 1;
while (i <=
n) {
System.out.print(i + " ");
i++;
}
}
}
Output:
1 2 3 4 5 6 7 8 9 10
2. Print even numbers from 1 to N
public class EvenNumbers {
public static void
main(String[] args) {
int n = 20;
int i = 2;
while (i <=
n) {
System.out.print(i + " ");
i += 2;
}
}
}
Output:
2 4 6 8 10 12 14 16 18 20
3. Find the sum of numbers from 1 to N
For n = 5:
1 + 2 + 3 + 4 + 5 = 15
public class SumNumbers {
public static void
main(String[] args) {
int n = 5;
int i = 1;
int sum = 0;
while (i <=
n) {
sum = sum
+ i;
i++;
}
System.out.println("Sum = " + sum);
}
}
4. Reverse a Number
This is a very common interview problem.
Input:
12345
Output:
54321
public class ReverseNumber {
public static void main(String[] args) {
int number =
12345;
int reverse =
0;
while (number
!= 0) {
int digit
= number % 10;
reverse =
reverse * 10 + digit;
number =
number / 10;
}
System.out.println("Reverse = " + reverse);
}
}
Logic
For 12345:
12345 % 10 = 5
12345 / 10 = 1234
1234 % 10 = 4
1234 / 10 = 123
123 % 10 = 3
...
Two operations are extremely important in number problems:
number % 10 //
extract last digit
number / 10 //
remove last digit
5. Sum of Digits
Input:
12345
Output:
15
Because:
1 + 2 + 3 + 4 + 5 = 15
Program:
public class SumOfDigits {
public static void
main(String[] args) {
int number =
12345;
int sum = 0;
while (number
!= 0) {
int digit
= number % 10;
sum = sum
+ digit;
number =
number / 10;
}
System.out.println("Sum of digits = " + sum);
}
}
6. Count Number of Digits
Input:
987654
Output:
Number of digits = 6
public class CountDigits {
public static void
main(String[] args) {
int number =
987654;
int count = 0;
while (number
!= 0) {
count++;
number =
number / 10;
}
System.out.println("Number of digits = " + count);
}
}
For production/interview discussion, remember 0 needs
special handling because it has one digit.
7. Check Palindrome Number
A palindrome remains the same after reversing.
Examples:
121
1331
1221
Program:
public class PalindromeNumber {
public static void
main(String[] args) {
int number =
121;
int original =
number;
int reverse =
0;
while (number
!= 0) {
int digit
= number % 10;
reverse =
reverse * 10 + digit;
number =
number / 10;
}
if (original
== reverse) {
System.out.println("Palindrome");
} else {
System.out.println("Not Palindrome");
}
}
}
8. Factorial of a Number
Factorial of 5:
5! = 5 × 4 × 3 × 2 × 1
= 120
Program:
public class Factorial {
public static void
main(String[] args) {
int number =
5;
long factorial
= 1;
while (number
> 0) {
factorial
= factorial * number;
number--;
}
System.out.println("Factorial = " + factorial);
}
}
Output:
Factorial = 120
9. Fibonacci Series
Example:
0 1 1 2 3 5 8 13 21 34
Program:
public class Fibonacci {
public static void
main(String[] args) {
int n = 10;
int first = 0;
int second =
1;
int count = 1;
while (count
<= n) {
System.out.print(first + " ");
int next =
first + second;
first =
second;
second =
next;
count++;
}
}
}
10. Check Prime Number ⭐⭐⭐
This is one of the most important interview problems.
public class PrimeNumber {
public static void
main(String[] args) {
int number =
17;
int i = 2;
boolean
isPrime = number >= 2;
while (i * i
<= number && isPrime) {
if (number
% i == 0) {
isPrime = false;
}
i++;
}
if (isPrime) {
System.out.println(number + " is Prime");
} else {
System.out.println(number + " is Not Prime");
}
}
}
Why:
17
17 % 2 != 0
17 % 3 != 0
17 has no divisor ≤ √17
Prime
11. Armstrong Number ⭐⭐
Example:
153
Because:
1³ + 5³ + 3³
= 1 + 125 + 27
= 153
For the classic three-digit version:
public class ArmstrongNumber {
public static void
main(String[] args) {
int number =
153;
int original =
number;
int sum = 0;
while (number
!= 0) {
int digit
= number % 10;
sum = sum
+ (digit * digit * digit);
number =
number / 10;
}
if (original
== sum) {
System.out.println("Armstrong Number");
} else {
System.out.println("Not Armstrong
Number");
}
}
}
12. Find Largest Digit in a Number ⭐⭐
Input:
58329
Output:
Largest digit = 9
public class LargestDigit {
public static void
main(String[] args) {
int number =
58329;
int largest =
0;
while (number
!= 0) {
int digit
= number % 10;
if (digit
> largest) {
largest = digit;
}
number =
number / 10;
}
System.out.println("Largest
digit = " + largest);
}
}
13. Find Smallest Digit
Input:
58329
Output:
Smallest digit = 2
public class SmallestDigit {
public static void
main(String[] args) {
int number =
58329;
int smallest =
9;
while (number
!= 0) {
int digit
= number % 10;
if (digit
< smallest) {
smallest = digit;
}
number =
number / 10;
}
System.out.println("Smallest
digit = " + smallest);
}
}
14. Product of Digits
Input:
234
Calculation:
2 × 3 × 4 = 24
public class ProductDigits {
public static void
main(String[] args) {
int number =
234;
int product =
1;
while (number
!= 0) {
int digit
= number % 10;
product =
product * digit;
number =
number / 10;
}
System.out.println("Product = " + product);
}
}
15. Check Strong Number ⭐⭐⭐
A Strong Number is a number where the sum of factorials of
its digits equals the original number.
Example:
145
1! + 4! + 5!
= 1 + 24 + 120
= 145
Program:
public class StrongNumber {
public static void
main(String[] args) {
int number =
145;
int original =
number;
int sum = 0;
while (number
!= 0) {
int digit
= number % 10;
int
factorial = 1;
int i = 1;
while (i
<= digit) {
factorial = factorial * i;
i++;
}
sum = sum
+ factorial;
number =
number / 10;
}
if (sum ==
original) {
System.out.println("Strong Number");
} else {
System.out.println("Not Strong Number");
}
}
}
This is particularly useful because it demonstrates a nested
while loop.
16. Find GCD of Two Numbers ⭐⭐⭐
Example:
48 and 18
GCD = 6
Use the Euclidean algorithm:
public class GCDExample {
public static void
main(String[] args) {
int a = 48;
int b = 18;
while (b != 0)
{
int
remainder = a % b;
a = b;
b =
remainder;
}
System.out.println("GCD = " + a);
}
}
The execution is:
48 % 18 = 12
18 % 12 = 6
12 % 6 = 0
GCD = 6
This is an excellent algorithmic interview problem.
No comments:
Post a Comment