Posted in

LOOPS CODING QUESTIONS IN JAVA

1. Write a program to print numbers from 1 to 10?

public class PrintNumbers
{
    public static void main(String[] args)
    {
        for(int i=1; i<=10; i++)
        {
            System.out.println(i);
        }
    }
}

Output :

1
2
3
4
5
6
7
8
9
10

2. Write a program to calculate the sum of first 10 natural number?

  public class SumNumbers
    {
    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

3. Write a program that prompts the user to input a positive integer. It should then print the multiplication table of that number?

     import java.util.Scanner;
      public class Table
      {
      public static void main(String[] args)
       {
        Scanner console = new Scanner(System.in);
        int num;
        System.out.print(“Enter any positive integer: “);
        num = console.nextInt();  
        System.out.println(“Multiplication Table of ” + num);
        for(int i=1; i<=10; i++)
        {
            System.out.println(num +” x ” + i + ” = ” + (num*i) );
        }
    }
}

Output :

(if the user enters 5):
Enter any positive integer: 5
Multiplication Table of 5
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

4. Write a program to find the factorial value of any number entered through the keyboard?

import java.util.Scanner;
     public class FactorialDemo1
     {
      public static void main(String[] args)
       {
        Scanner console = new Scanner(System.in);
        int num;
        int fact = 1;
        System.out.print(“Enter any positive integer: “);
        num = console.nextInt();
        for(int i=1; i<=num; i++)
        {
            fact *= i;
        }
        System.out.println(“Factorial: “+ fact);
    }
}

Output :

Enter any positive integer: 5
Factorial: 120

5. Write a program that takes two numbers as input from the user and calculates the result of raising the first number to the power of the second number?

import java.util.Scanner;
public class PowerDemo
{
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
        int base;
        int power;
        int result = 1;
        System.out.print(“Enter the base number “);
        base = console.nextInt();
        System.out.print(“Enter the power “);
        power = console.nextInt();
        for(int i = 1; i <= power; i++)
        {
            result *= base;
        }
        System.out.println(“Result: “+ result);
    }
}

Output :

     If the user enters:
     Enter the base number 2
     Enter the power 4
      Result: 16  

6. Write a program that prompts the user to input an integer and then outputs the number with the digits reversed?

import java.util.Scanner;
public class ReverseNumber
   {
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);    
        int number;
        int reverse = 0;       
        System.out.print(“Enter the number “);
        number = console.nextInt(); 
        int temp = number;
        int remainder = 0;  
        while(temp>0)
        {
            remainder = temp % 10;
            reverse = reverse * 10 + remainder;
            temp /= 10;
        }
        System.out.println(“Reverse of ” + number + ” is ” + reverse);
    }
}

Output :

If the user enters:
Enter the number 1234
Reverse of 1234 is 4321

7. Write a program that reads a set of integers, and then prints the sum of the even and odd integers?

import java.util.Scanner;
    public class ReadSetIntegers
    {
    public static void main(String[] args)
    {
        Scanner console = new Scanner(System.in);
        int number;
        char choice;
        int evenSum = 0;
        int oddSum = 0;
        do
        {
            System.out.print(“Enter the number “);
            number = console.nextInt();
            if( number % 2 == 0)
            {
                evenSum += number;
            }
            else
            {
                oddSum += number;
            }
            System.out.print(“Do you want to continue y/n? “);
            choice = console.next().charAt(0);   
        }while(choice==’y’ || choice == ‘Y’);
        System.out.println(“Sum of even numbers: ” + evenSum);
        System.out.println(“Sum of odd numbers: ” + oddSum);
  
        } 
}

Output :

Enter the number 10
Do you want to continue y/n? y
Enter the number 7
Do you want to continue y/n? y
Enter the number 2
Do you want to continue y/n? y
Enter the number 5
Do you want to continue y/n? n
Sum of even numbers: 12
Sum of odd numbers: 12

8. Write a program to check whether a given number is a palindrome or not using a loop?

     import java.util.Scanner;
     public class PalindromeNumber {
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter a number: “);
        int N = sc.nextInt();
        int original = N;
        int reverse = 0;
        while (N != 0) {
            int digit = N % 10;
            reverse = reverse * 10 + digit;
            N /= 10;
        }
        if (original == reverse) {
            System.out.println(original + ” is a Palindrome number.”);
        } else {
            System.out.println(original + ” is not a Palindrome number.”);
        }
    }
}

Output :

Enter a number: 121
121 is a Palindrome number.

9. Write a program to count the number of digits in a given number using a loop?

    import java.util.Scanner;
    public class CountDigits {
      public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter a number: “);
        int N = sc.nextInt();
        int count = 0;
        while (N != 0) {
            N /= 10;
            count++;
        }
        System.out.println(“Number of digits: ” + count);
    }
}

Output :

Enter a number: 12345
Number of digits: 5

10. Write a program to print all the prime numbers between 1 and N using a loop?

 import java.util.Scanner;
   public class PrimeNumbersBetween1toN {
      public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter a number: “);
        int N = sc.nextInt();
        for (int num = 2; num <= N; num++) {
            boolean isPrime = true;
            for (int i = 2; i <= num / 2; i++) {
                if (num % i == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                System.out.print(num + ” “);
            }
        }
    }
}

Output :

   Enter a number: 20
  2 3 5 7 11 13 17 19

11. Write a program to find the greatest common divisor (GCD) of two numbers using a loop?

  import java.util.Scanner;
    public class Demo {
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter two numbers: “);
        int a = sc.nextInt();
        int b = sc.nextInt();
        while (a != b) {
            if (a > b) {
                a -= b;
            } else {
                b -= a;
            }
        }
        System.out.println(“GCD is: ” + a);
    }
}

Output :

Enter two numbers: 36 60
GCD is: 12

12. Write a program to print a pyramid pattern using loops?

   import java.util.Scanner;
    public class PyramidPattern {
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter a number: “);
        int N = sc.nextInt();
        for (int i = 1; i <= N; i++) {
            for (int j = N; j > i; j–) {
                System.out.print(” “);
            }
            for (int j = 1; j <= (2 * i – 1); j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }
    }
}

Output :

   Enter a number: 5
       *
      ***
    *****
   *******
*********

13. Write a program to print a diamond pattern using loops?

 import java.util.Scanner;
   public class DiamondPattern {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter a number: “);
        int N = sc.nextInt();
        for (int i = 1; i <= N; i++) {
            for (int j = N; j > i; j–) {
                System.out.print(” “);
            }
            for (int j = 1; j <= (2 * i – 1); j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }
        for (int i = N – 1; i >= 1; i–) {
            for (int j = N; j > i; j–) {
                System.out.print(” “);
            }
            for (int j = 1; j <= (2 * i – 1); j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }
    }
}

Output :

Enter a number: 5
          *
         ***
      *****
     *******
   *********
     *******
       *****
         ***
           *

14. Write a program to print the Fibonacci series up to N terms using a loop?

import java.util.Scanner;
      public class Fibonacci {
         public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         System.out.print(“Enter the number of terms: “);
         int N = sc.nextInt();
        int first = 0, second = 1;
        System.out.print(“Fibonacci series: ” + first + ” ” + second + ” “);   
        for (int i = 3; i <= N; i++) {
            int next = first + second;
            System.out.print(next + ” “);
            first = second;
            second = next;
        }
    }
}

Output :

Enter the number of terms: 7
Fibonacci series: 0 1 1 2 3 5 8

15. Write a program to count the number of vowels and consonants in a given string?

    import java.util.Scanner;
      public class VowelConsonantCount {
       public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter a string: “);
        String str = sc.nextLine().toLowerCase();     
        int vowels = 0, consonants = 0;    
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i)    
            if (ch >= ‘a’ && ch <= ‘z’) {
                if (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’) {
                    vowels++;
                } else {
                    consonants++;
                }
            }
        }   
        System.out.println(“Vowels: ” + vowels);
        System.out.println(“Consonants: ” + consonants);
     }
}

Output :

 If the user enters:
 Enter a string: Hello World
 Vowels: 3
 Consonants: 7

16. Write a program to print a square star pattern of given size?

  import java.util.Scanner;
   public class SquareStarPattern {
      public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter the size of the square: “);
        int N = sc.nextInt();
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= N; j++) {
                System.out.print(“* “);
            }
            System.out.println();
        }
    }
}

Output :

Enter the size of the square: 4
         * * * *
         * * * *
         * * * *
         * * * *

17. Write a program to print a right-angled triangle pattern of numbers?

  import java.util.Scanner;
   public class RightAngledTrianglePattern {
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter a number: “);
        int N = sc.nextInt();  
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + ” “);
            }
            System.out.println();
        }
    }
}

Output :

   Enter a number: 5
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5

18. Write a program to print a pyramid pattern of numbers?

import java.util.Scanner;
   public class NumberPyramid {
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter the number of rows: “);
        int N = sc.nextInt();
       for (int i = 1; i <= N; i++) {
            for (int j = N; j > i; j–) {
                System.out.print(” “);
            }
            for (int j = 1; j <= (2 * i – 1); j++) {
                System.out.print(i);
            }
            System.out.println();
        }
    }
}

Output :

Enter the number of rows: 5
                1
              222
            33333
          4444444
        555555555

19. Write a program to print a hollow square pattern of stars?

import java.util.Scanner;
  public class HollowSquarePattern {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter the size of the square: “);
        int N = sc.nextInt();
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= N; j++) {
                if (i == 1 || i == N || j == 1 || j == N) {
                    System.out.print(“* “);
                } else {
                    System.out.print(”  “);
                }
            }
            System.out.println();
        }
    }
}

Output :

  Enter the size of the square: 5
     * * * * *
     *          *
    *          *
     *          *
     * * * * *

20. Write a program to print a right-aligned number pyramid pattern?

  import java.util.Scanner;
    public class RightAlignedNumberPyramid {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter the height of the pyramid: “);
        int N = sc.nextInt();
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= N – i; j++) {
                System.out.print(” “);
            }
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

Output :

Enter the height of the pyramid: 5
             1
           12
         123
       1234
     12345

21. Write a program to inverted Right-Angled Triangle?

  import java.util.Scanner;
  public class InvertedRightTriangle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter rows: “);
        int n = sc.nextInt();
        for (int i = n; i >= 1; i–) {
            for (int j = 1; j <= i; j++) {
                System.out.print(“* “);
            }
            System.out.println();
        }
    }
}

Output :

Enter rows: 4
      * * * *
      * * *
      * *
      *

22. Write a program to create a Floyd’s Triangle?

  import java.util.Scanner;
    public class FloydTriangle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter rows: “);
        int n = sc.nextInt();
        int num = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(num++ + ” “);
            }
            System.out.println();
        }
    }
}

Output :

Enter rows : 4
    1
    2 3
    4 5 6
    7 8 9 10

23. Write a program to create a binary triangle?

  import java.util.Scanner;
     public class BinaryTriangle {
      public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(“Enter rows: “);
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            int val = i % 2;
            for (int j = 1; j <= i; j++) {
                System.out.print(val + ” “);
                val = 1 – val;
            }
            System.out.println();
        }
    }
}

Output :

  Enter rows: 5
   1
   0 1
  1 0 1
   0 1 0 1
  1 0 1 0 1

Leave a Reply

Your email address will not be published. Required fields are marked *