For & While loop Venkatesh
For & While Loop Challenges
1. Print numbers from 1 to 10.
Using for loop
Code :
class ForOneToTen {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.print(i + " ");
}
}
}
Output :
1 2 3 4 5 6 7 8 9 10
Using While loop
Code :
class WhileOneToTen {
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
System.out.print(i + " ");
i++;
}
}
}
Output :
1 2 3 4 5 6 7 8 9 10
2. Print even numbers between 1 and 20.
Using for loop
Code :
class ForEvenNumbers {
public static void main(String[] args) {
for (int i = 2; i <= 20; i += 2) {
System.out.print(i + " ");
}
}
}
Output :
2 4 6 8 10 12 14 16 18 20
Using While loop
Code :
class WhileEvenNumbers {
public static void main(String[] args) {
int i = 2;
while (i <= 20) {
System.out.print(i + " ");
i += 2;
}
}
}
Output :
2 4 6 8 10 12 14 16 18 20
3. Print the multiplication table of a given number.
Using for loop
Code :
class ForTable {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= 10; i++) {
System.out.println(n + " × " + i + " = " + (n * i));
}
}
}
Output :
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
Using While loop
Code :
class WhileTable {
public static void main(String[] args) {
int n = 5, i = 1;
while (i <= 10) {
System.out.println(n + " × " + i + " = " + (n * i));
i++;
}
}
}
Output :
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. Print the sum of first 10 natural numbers.
Using for loop
Code :
class ForSum10 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("Sum = " + sum);
}
}
Output :
Sum = 55
Using While loop
Code :
class WhileSum10 {
public static void main(String[] args) {
int sum = 0, i = 1;
while (i <= 10) {
sum += i;
i++;
}
System.out.println("Sum = " + sum);
}
}
Output :
Sum = 55
5. Print the sum of first N even numbers.
Using for loop
Code :
class ForSumEven {
public static void main(String[] args) {
int N = 5, sum = 0;
for (int i = 1; i <= N; i++) {
sum += 2 * i;
}
System.out.println("Sum = " + sum);
}
}
Output :
Sum = 30
Using While loop
Code :
class WhileSumEven {
public static void main(String[] args) {
int N = 5, sum = 0, i = 1;
while (i <= N) {
sum += 2 * i;
i++;
}
System.out.println("Sum = " + sum);
}
}
Output :
Sum = 30
6. Print the factorial of a number.
Using for loop
Code :
class ForFactorial {
public static void main(String[] args) {
int n = 5;
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
System.out.println("Factorial = " + fact);
}
}
Output :
Factorial = 120
Using While loop
Code :
class WhileFactorial {
public static void main(String[] args) {
int n = 5, fact = 1, i = 1;
while (i <= n) {
fact *= i;
i++;
}
System.out.println("Factorial = " + fact);
}
}
Output :
Factorial = 120
7. Print the reverse of a number (e.g., 123 → 321).
Using for loop
Code :
class ForReverse {
public static void main(String[] args) {
int n = 1234, rev = 0;
for (; n > 0; n /= 10) {
int digit = n % 10;
rev = rev * 10 + digit;
}
System.out.println("Reverse = " + rev);
}
}
Output :
Reverse = 4321
Using While loop
Code :
class WhileReverse {
public static void main(String[] args) {
int n = 1234, rev = 0;
while (n > 0) {
int digit = n % 10;
rev = rev * 10 + digit;
n /= 10;
}
System.out.println("Reverse = " + rev);
}
}
Output :
Reverse = 4321
8. Print the sum of digits of a number.
Using for loop
Code :
class ForSumDigits {
public static void main(String[] args) {
int n = 1234, sum = 0;
for (; n > 0; n /= 10) {
sum += n % 10;
}
System.out.println("Sum of digits = " + sum);
}
}
Output :
Sum of digits = 10
Using While loop
Code :
class WhileSumDigits {
public static void main(String[] args) {
int n = 1234, sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
System.out.println("Sum of digits = " + sum);
}
}
Output :
Sum of digits = 10
9. Print the product of digits of a number.
Using for loop
Code :
class ForProductDigits {
public static void main(String[] args) {
int n = 1234, product = 1;
for (; n > 0; n /= 10) {
product *= (n % 10);
}
System.out.println("Product of digits = " + product);
}
}
Output :
Product of digits = 24
Using While loop
Code :
class WhileProductDigits {
public static void main(String[] args) {
int n = 1234, product = 1;
while (n > 0) {
product *= (n % 10);
n /= 10;
}
System.out.println("Product of digits = " + product);
}
}
Output :
Product of digits = 24
10. Check if a number is a palindrome (e.g., 121 → yes).
Using for loop
Code :
class ForPalindrome {
public static void main(String[] args) {
int n = 121, temp = n, rev = 0;
for (; n > 0; n /= 10) {
rev = rev * 10 + (n % 10);
}
if (temp == rev)
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
}
}
Output :
Palindrome
Using While loop
Code :
class WhilePalindrome {
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");
}
}
Output :
Palindrome
11. Check if a number is an Armstrong number (e.g., 153).
Using for loop
Code :
class ForArmstrong {
public static void main(String[] args) {
int n = 153, temp = n, sum = 0;
for (; n > 0; n /= 10) {
int d = n % 10;
sum += d * d * d;
}
if (sum == temp)
System.out.println("Armstrong");
else
System.out.println("Not Armstrong");
}
}
Output :
Armstrong
Using While loop
Code :
class WhileArmstrong {
public static void main(String[] args) {
int n = 153, temp = n, sum = 0;
while (n > 0) {
int d = n % 10;
sum += d * d * d;
n /= 10;
}
if (sum == temp)
System.out.println("Armstrong");
else
System.out.println("Not Armstrong");
}
}
Output :
Armstrong
12. Print the Fibonacci series up to N terms.
Using for loop
Code :
class ForFibonacci {
public static void main(String[] args) {
int n = 7, a = 0, b = 1;
System.out.print(a + " " + b + " ");
for (int i = 3; i <= n; i++) {
int c = a + b;
System.out.print(c + " ");
a = b;
b = c;
}
}
}
Output :
0 1 1 2 3 5 8
Using While loop
Code :
class WhileFibonacci {
public static void main(String[] args) {
int n = 7, a = 0, b = 1, i = 3;
System.out.print(a + " " + b + " ");
while (i <= n) {
int c = a + b;
System.out.print(c + " ");
a = b;
b = c;
i++;
}
}
}
Output :
0 1 1 2 3 5 8
13. Count the number of digits in a number.
Using for loop
Code :
class ForCountDigits {
public static void main(String[] args) {
int n = 98765, count = 0;
for (; n > 0; n /= 10) {
count++;
}
System.out.println("Digits = " + count);
}
}
Output :
Digits = 5
Using While loop
Code :
class WhileCountDigits {
public static void main(String[] args) {
int n = 98765, count = 0;
while (n > 0) {
count++;
n /= 10;
}
System.out.println("Digits = " + count);
}
}
Output :
Digits = 5
14. Find the largest digit in a number.
Using for loop
Code :
class ForLargestDigit {
public static void main(String[] args) {
int n = 396, max = 0;
for (; n > 0; n /= 10) {
int d = n % 10;
if (d > max) max = d;
}
System.out.println("Largest digit = " + max);
}
}
Output :
Largest digit = 9
Using While loop
Code :
class WhileLargestDigit {
public static void main(String[] args) {
int n = 396, max = 0;
while (n > 0) {
int d = n % 10;
if (d > max) max = d;
n /= 10;
}
System.out.println("Largest digit = " + max);
}
}
Output :
Largest digit = 9
15. Find the smallest digit in a number.
Using for loop
Code :
class ForSmallestDigit {
public static void main(String[] args) {
int n = 396, min = 9;
for (; n > 0; n /= 10) {
int d = n % 10;
if (d < min) min = d;
}
System.out.println("Smallest digit = " + min);
}
}
Output :
Smallest digit = 3
Using While loop
Code :
class WhileSmallestDigit {
public static void main(String[] args) {
int n = 396, min = 9;
while (n > 0) {
int d = n % 10;
if (d < min) min = d;
n /= 10;
}
System.out.println("Smallest digit = " + min);
}
}
Output :
Smallest digit = 3
16. Print a square pattern of * (e.g., 5×5).
Using for loop
Code :
class ForSquarePattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output :
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Using While loop
Code :
class WhileSquarePattern {
public static void main(String[] args) {
int n = 5, i = 1;
while (i <= n) {
int j = 1;
while (j <= n) {
System.out.print("* ");
j++;
}
System.out.println();
i++;
}
}
}
Output :
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
17. Print a triangle pattern of *.
Using for loop
Code :
class ForTrianglePattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output :
*
* *
* * *
* * * *
* * * * *
Using While loop
Code :
class WhileTrianglePattern {
public static void main(String[] args) {
int n = 5, i = 1;
while (i <= n) {
int j = 1;
while (j <= i) {
System.out.print("* ");
j++;
}
System.out.println();
i++;
}
}
}
Output :
*
* *
* * *
* * * *
* * * * *
* * * * *
Comments
Post a Comment