## BLOG PAGES

### Java Looping & Jumping Statements - Practice Programs with Solutions

////////////////////////////////////////////////////////////////////////////

// While loop

////////////////////////////////////////////////////////////////////////////

//Program1: Print Numbers from 1 to 10

public class PrintNumbers {

public static void main(String[] args) {

int i = 1;

while (i <= 10) {

System.out.println(i);

i++;

}

}

}

//Program2: Sum of First 10 Natural Numbers

public class SumNaturalNumbers {

public static void main(String[] args) {

int i = 1, sum = 0;

while (i <= 10) {

sum += i;

i++;

}

System.out.println("Sum: " + sum);

}

}

//Program 3: Factorial of a Number

public class Factorial {

public static void main(String[] args) {

int number = 5; // Example number

int factorial = 1;

int i = 1;

while (i <= number) {

factorial *= i;

i++;

}

System.out.println("Factorial of " + number + " is: " + factorial);

}

}

//Program1 4: Reverse a Number

public class ReverseNumber {

public static void main(String[] args) {

int number = 12345; // Example number

int reverse = 0;

while (number != 0) {

int digit = number % 10;

reverse = reverse * 10 + digit;

number /= 10;

}

System.out.println("Reversed Number: " + reverse);

}

}

//Program 5: Count the Digits in a Number

public class CountDigits {

public static void main(String[] args) {

int number = 12345; // Example number

int count = 0;

while (number != 0) {

number /= 10;

count++;

}

System.out.println("Number of digits: " + count);

}

}

//Program: 6 Check if a Number is Prime

public class PrimeCheck {

public static void main(String[] args) {

int number = 29; // Example number

boolean isPrime = true;

int i = 2;

while (i <= number / 2) {

if (number % i == 0) {

isPrime = false;

break;

}

i++;

}

if (isPrime) {

System.out.println(number + " is a prime number.");

} else {

System.out.println(number + " is not a prime number.");

}

}

}

////Program: 7 Print Even Numbers Between 1 and 20

public class EvenNumbers {

public static void main(String[] args) {

int i = 1;

while (i <= 20) {

if (i % 2 == 0) {

System.out.println(i);

}

i++;

}

}

}

//Program 8. Sum of Digits in a Number

public class SumOfDigits {

public static void main(String[] args) {

int number = 12345; // Example number

int sum = 0;

while (number != 0) {

int digit = number % 10;

sum += digit;

number /= 10;

}

System.out.println("Sum of digits: " + sum);

}

}

//Program: 9. Find the Largest Digit in a Number

public class LargestDigit {

public static void main(String[] args) {

int number = 52679; // Example number

int largest = 0;

while (number != 0) {

int digit = number % 10;

if (digit > largest) {

largest = digit;

}

number /= 10;

}

System.out.println("Largest digit: " + largest);

}

}

//Program 10. Generate Fibonacci Series

public class FibonacciSeries {

public static void main(String[] args) {

int n1 = 0, n2 = 1, n3, i = 2, count = 10;

System.out.print(n1 + " " + n2);

while (i < count) {

n3 = n1 + n2;

System.out.print(" " + n3);

n1 = n2;

n2 = n3;

i++;

}

}

}

//Program 11. Check if a Number is Palindrome

public class PalindromeCheck {

public static void main(String[] args) {

int number = 121; // Example number

int originalNumber = number;

int reverse = 0;

while (number != 0) {

int digit = number % 10;

reverse = reverse * 10 + digit;

number /= 10;

}

if (originalNumber == reverse) {

System.out.println(originalNumber + " is a palindrome.");

} else {

System.out.println(originalNumber + " is not a palindrome.");

}

}

}

//Program 12. Sum of Even Numbers from 1 to N

public class SumEvenNumbers {

public static void main(String[] args) {

int n = 10; // Example value of N

int sum = 0;

int i = 1;

while (i <= n) {

if (i % 2 == 0) {

sum += i;

}

i++;

}

System.out.println("Sum of even numbers from 1 to " + n + " is: " + sum);

}

}

//Program13. Sum of Odd Numbers from 1 to N

public class SumOddNumbers {

public static void main(String[] args) {

int n = 10; // Example value of N

int sum = 0;

int i = 1;

while (i <= n) {

if (i % 2 != 0) {

sum += i;

}

i++;

}

System.out.println("Sum of odd numbers from 1 to " + n + " is: " + sum);

}

}

////////////////////////////////////////////////////////////////////////////

// do-while loop

////////////////////////////////////////////////////////////////////////////

// Program 1. Print Numbers from 1 to 10

public class PrintNumbers {

public static void main(String[] args) {

int i = 1;

do {

System.out.println(i);

i++;

} while (i <= 10);

}

}

// Program 2. Sum of Digits in a Number

public class SumOfDigits {

public static void main(String[] args) {

int number = 12345; // Example number

int sum = 0;

do {

int digit = number % 10;

sum += digit;

number /= 10;

} while (number != 0);

System.out.println("Sum of digits: " + sum);

}

}

// Program 3. Reverse a Number

public class ReverseNumber {

public static void main(String[] args) {

int number = 12345; // Example number

int reverse = 0;

do {

int digit = number % 10;

reverse = reverse * 10 + digit;

number /= 10;

} while (number != 0);

System.out.println("Reversed Number: " + reverse);

}

}

// Program 4. Check if a Number is Palindrome

public class PalindromeCheck {

public static void main(String[] args) {

int number = 121; // Example number

int originalNumber = number;

int reverse = 0;

do {

int digit = number % 10;

reverse = reverse * 10 + digit;

number /= 10;

} while (number != 0);

if (originalNumber == reverse) {

System.out.println(originalNumber + " is a palindrome.");

} else {

System.out.println(originalNumber + " is not a palindrome.");

}

}

}

// Program 5. Menu-Driven Program for Basic Arithmetic Operations

import java.util.Scanner;

public class ArithmeticOperations {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int choice;

do {

System.out.println("2. Subtraction");

System.out.println("3. Multiplication");

System.out.println("4. Division");

System.out.println("5. Exit");

choice = scanner.nextInt();

if (choice >= 1 && choice <= 4) {

System.out.print("Enter first number: ");

double num1 = scanner.nextDouble();

System.out.print("Enter second number: ");

double num2 = scanner.nextDouble();

switch (choice) {

case 1:

System.out.println("Result: " + (num1 + num2));

break;

case 2:

System.out.println("Result: " + (num1 - num2));

break;

case 3:

System.out.println("Result: " + (num1 * num2));

break;

case 4:

if (num2 != 0) {

System.out.println("Result: " + (num1 / num2));

} else {

System.out.println("Division by zero is not allowed.");

}

break;

}

} else if (choice != 5) {

System.out.println("Invalid choice! Please select a valid option.");

}

} while (choice != 5);

scanner.close();

System.out.println("Program terminated.");

}

}

////////////////////////////////////////////////////////////////////////////

// for loop

////////////////////////////////////////////////////////////////////////////

// Program 1. Print Numbers 1 to 10

public class PrintNumbers {

public static void main(String[] args) {

for (int i = 1; i <= 10; i++) {

System.out.println(i);

}

}

}

// Program 2. Print Even Numbers from 1 to 20

public class PrintEvenNumbers {

public static void main(String[] args) {

for (int i = 2; i <= 20; i += 2) {

System.out.println(i);

}

}

}

// Program 3. Print Odd Numbers from 1 to 20

public class PrintOddNumbers {

public static void main(String[] args) {

for (int i = 1; i <= 20; i += 2) {

System.out.println(i);

}

}

}

// Program 4. Print Multiples of 5 from 5 to 50

public class MultiplesOfFive {

public static void main(String[] args) {

for (int i = 5; i <= 50; i += 5) {

System.out.println(i);

}

}

}

// Program 5. Calculate Sum of Numbers from 1 to 10

public class SumOfNumbers {

public static void main(String[] args) {

int sum = 0;

for (int i = 1; i <= 10; i++) {

sum += i;

}

System.out.println("Sum: " + sum);

}

}

// Program 6. Print Squares of Numbers from 1 to 5

public class SquaresOfNumbers {

public static void main(String[] args) {

for (int i = 1; i <= 5; i++) {

System.out.println("Square of " + i + " is " + (i * i));

}

}

}

// Program 7. Print Cube of Numbers from 1 to 5

public class CubesOfNumbers {

public static void main(String[] args) {

for (int i = 1; i <= 5; i++) {

System.out.println("Cube of " + i + " is " + (i * i * i));

}

}

}

// Program 8. Print Numbers in Reverse Order from 10 to 1

public class ReverseOrder {

public static void main(String[] args) {

for (int i = 10; i >= 1; i--) {

System.out.println(i);

}

}

}

// Program 9. Print Prime Numbers between 1 and 50

public static void main(String[] args) {

for (int num = 2; num <= 50; num++) {

boolean isPrime = true;

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

if (num % i == 0) {

isPrime = false;

break;

}

}

if (isPrime) {

System.out.println(num);

}

}

}

}

// Program 10. Print Sum of Even Numbers between 1 and 20

public class SumEvenNumbers {

public static void main(String[] args) {

int sum = 0;

for (int i = 2; i <= 20; i += 2) {

sum += i;

}

System.out.println("Sum of even numbers: " + sum);

}

}

// Program 11. Print Sum of Odd Numbers between 1 and 20

public class SumOddNumbers {

public static void main(String[] args) {

int sum = 0;

for (int i = 1; i <= 20; i += 2) {

sum += i;

}

System.out.println("Sum of odd numbers: " + sum);

}

}

// Program 12. Print Table of 7

public class MultiplicationTable {

public static void main(String[] args) {

int number = 7;

for (int i = 1; i <= 10; i++) {

System.out.println(number + " x " + i + " = " + (number * i));

}

}

}

// Program 13. Print Numbers Divisible by 3 and 5 from 1 to 100

public class DivisibleByThreeAndFive {

public static void main(String[] args) {

for (int i = 1; i <= 100; i++) {

if (i % 3 == 0 && i % 5 == 0) {

System.out.println(i);

}

}

}

}

// Program 14. Count Number of Digits in a Number

public class CountDigits {

public static void main(String[] args) {

int number = 12345;

int count = 0;

for (int temp = number; temp > 0; temp /= 10) {

count++;

}

System.out.println("Number of digits: " + count);

}

}

// Program 15. Print Multiples of 7 between 1 and 100

public class MultiplesOfSeven {

public static void main(String[] args) {

for (int i = 7; i <= 100; i += 7) {

System.out.println(i);

}

}

}

////////////////////////////////////////////////////////////////////////////

// continue

////////////////////////////////////////////////////////////////////////////

//Program 1. Print Odd Numbers

public class PrintOddNumbers {

public static void main(String[] args) {

for (int i = 1; i <= 20; i++) {

if (i % 2 == 0) {

continue; // Skip even numbers

}

System.out.println(i);

}

}

}

//Program 2. Skip Multiples of 5

public class SkipMultiplesOfFive {

public static void main(String[] args) {

int i = 1;

while (i <= 30) {

if (i % 5 == 0) {

i++; // Increment the counter before continue to avoid infinite loop

continue; // Skip multiples of 5

}

System.out.println(i);

i++;

}

}

}

//Program 3. Skip Numbers Divisible by 3 and 4

public class SkipDivisibleByThreeAndFour {

public static void main(String[] args) {

for (int i = 1; i <= 50; i++) {

if (i % 3 == 0 && i % 4 == 0) {

continue; // Skip numbers divisible by both 3 and 4

}

System.out.println(i);

}

}

}

////////////////////////////////////////////////////////////////////////////

// break

////////////////////////////////////////////////////////////////////////////

//Program 1. Find First Even Number

public class FindFirstEvenNumber {

public static void main(String[] args) {

for (int i = 1; i <= 20; i++) {

if (i % 2 == 0) {

System.out.println("First even number: " + i);

break; // Exit the loop after finding the first even number

}

}

}

}

//Program 2: Stop at First Multiple of 7

public class StopAtFirstMultipleOfSeven {

public static void main(String[] args) {

int i = 1;

while (i <= 50) {

if (i % 7 == 0) {

System.out.println("First multiple of 7: " + i);

break; // Exit the loop after finding the first multiple of 7

}

i++;

}

}

}

//Program 3. Find Number Greater Than 15

public class FindNumberGreaterThanFifteen {

public static void main(String[] args) {

for (int i = 1; i <= 30; i++) {

if (i > 15) {

System.out.println("Number greater than 15: " + i);

break; // Exit the loop after finding a number greater than 15

}

}

}

}