Tuesday, 15 January 2019

Java Program to Print Number Pyramid Patterns – Examples

         This is another popular Java interview question, especially for freshers. The interviewer asks this question to evaluate your logical thinking and problem-solving skills, as well as your ability to solve such problems in real-world scenarios.

I have included a few sample pattern programs below. Practice these examples and try creating additional patterns on your own to strengthen your programming and logical thinking skills.



1)                                                                                          2)
Pyramid program in Java


The above 1st pattern code is ,

PyramidWithNumbers.java,


package com.sample;

public class PyramidWithNumbers {

       public static void main(String[] args) {

                int rowCount = 1;
                System.out.println("Pyramid code output");
                int noOfRows = 5;            // number of rows for pyramid
         
                for (int i = noOfRows; i > 0; i--) {
          
                          // print the spaces at begining of each row.
                        for (int j = 1; j <= i; j++) {
                                 System.out.print(" ");
                        }

                        //print the value of rowcount for each row, for example 2, it will print 2 times 2 2.
                        for (int j = 1; j <= rowCount; j++) {
                               System.out.print(rowCount+" ");
                        }

                        System.out.println();
                        rowCount++;
               }
      }
}

Output :--
Pyramid code output
     1
    2 2
   3 3 3
  4 4 4 4
 5 5 5 5 5



The above second pattern of code is,

PyramindWithStar.java

package com.sample;

public class PyramidWithStar {

       public static void main(String[] args) {

               int rowCount = 1;
               System.out.println("Pyramid code output");
               int noOfRows = 5;            // number of rows for pyramid
         
               for (int i = noOfRows; i > 0; i--) {
          
                       // print the spaces at begining of each row.
                      for (int j = 1; j <= i; j++) {
                              System.out.print(" ");
                      }

                       //print the * value to each rowcount
                      for (int j = 1; j <= rowCount; j++) {
                             System.out.print("*"+" ");
                      }
                      System.out.println();
                      rowCount++;
              }
       }
}

Output:--

Pyramid code output
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 



Related Posts:--
1) Java Program to check the given number is a palindrome or not
2) Write a Java program to print the Floyd's triangle
3) Java Program for Bubble Sort in Ascending and Descending order
4) Java Program to Reverse a Linked List using Recursion and Loops
5) Java Program to Count Occurrence of Word in a Sentence
6) How to Remove duplicates from ArrayList in Java
7) Java program to print the first n prime numbers
8) Java Program to find the Missing Number in an Array

Sunday, 13 January 2019

Java Program to Find the Missing Number in an Array

         This is one of the most common Java coding interview questions. In an interview, you may be given an integer array containing numbers from 1 to n, where each number is unique (no duplicates are allowed). You will be asked to write a Java program to find the missing number in the array.

Example

int[] numbers = {1, 3, 5, 4, 2, 8, 7};

Output:

Missing number: 6

MissingNumber.java

package com.sample;

public class MissingNumber {
 
        public static void main(String[] args) {
  
                  int[] numbers = {1,4,2,5,3,6,7,9}; 
                  int totalNumbers = numbers.length + 1;
  
                  System.out.println("Total numbers - "+totalNumbers);
  
                    // first to calculate the sum of total numbers
  
                   int sumOfTotalNumbers = (totalNumbers * (totalNumbers + 1)) / 2;
  
                   // find the sum of given array. 
  
                   int sumOfArrayNumbers = 0;
  
                   for (int i=0; i<numbers.length; i++) {
                            sumOfArrayNumbers = sumOfArrayNumbers + numbers[i];
                   }
  
                   int missingNumber = sumOfTotalNumbers - sumOfArrayNumbers;
  
                   System.out.println("Missing number - "+missingNumber);
         }

}

Output:--
Total numbers - 9
Missing number - 8



Thank you for visiting blog.


Some other related posts:-
1) Java Program to check the given number is a palindrome or not
2) Write a Java program to print the Floyd's triangle
3) Java Program for Bubble Sort in Ascending and Descending order
4) Java Program to Reverse a Linked List using Recursion and Loops
5) Java Program to Count Occurrence of Word in a Sentence
6) How to Remove duplicates from ArrayList in Java
7) Java program to print the first n prime numbers

Friday, 11 January 2019

Java Program to Print the First n Prime Numbers

           In the previous post, I explained how to check whether a given integer is a palindrome. In today's post, we will write a Java program to display the first n prime numbers, where n can be any integer greater than 1.

A prime number is a number that is divisible only by 1 and itself. The number 1 is not a prime number, while 2 is the first prime number and the only even prime number.

The first few prime numbers are:

2, 3, 5, 7, 11, 13, 17, 19, 23, ...

Example

Input:

n = 10

Output (first 10 prime numbers):

2, 3, 5, 7, 11, 13, 17, 19, 23, 29

PrimeNumbers.java,


package com.example;

class PrimeNumbers {
 
    public static void main(String args[]) {
       
           int num = 3;
           boolean prime = true;
           System.out.println("The first 10 prime numbers are:--");
           System.out.println(2);          // first prime number
           for ( int i = 2 ; i <=10 ; ) {
                 for ( int j = 2 ; j <= Math.sqrt(num) ; j++ ) {
                        if ( num%j == 0 ) {
                               prime = false;
                               break;
                        }
                 }
                 if (prime) {
                        System.out.println(num);
                        i++;                          //if number is prime, increment i value
                 }
                 prime = true;
                 num++;
          }         
     }
}

Output:--
The first 10 prime numbers are:--
2
3
5
7
11
13
17
19
23
29


Related Posts:--
1) Java Program to check the given number is a palindrome or not
2) Write a Java program to print the Floyd's triangle
3) Java Program for Bubble Sort in Ascending and Descending order
4) Java Program to Reverse a Linked List using Recursion and Loops
5) Java Program to Count Occurrence of Word in a Sentence
6) How to Remove duplicates from ArrayList in Java

Thursday, 10 January 2019

Java Program to Check Whether a Number Is a Palindrome

       In today's post, I have written a Java program to check whether a given number is a palindrome.

A number is called a palindrome if it remains the same when its digits are reversed. In other words, if the original number and its reversed number are identical, then it is a palindrome.

Example

int number = 12121;

The reverse of the above number is 12121, which is the same as the original number. Therefore, it is a palindrome number.

PalindromeNumber.java

package com.example;

class PalindromeNumber{
 
       public static void main(String args[]) {
       
               int number = 12321; 
               int reverse = 0;
               int palindrome = number;
     
               while (number != 0) {
                       int reminder = number % 10;
                       reverse = reverse * 10 + reminder;
                       number = number/10;
               }
     
               if (palindrome == reverse) {
                       System.out.println("The given number "+ palindrome +" is a palindrome");
               } else {
                       System.out.println("The given number "+ palindrome +" is not a palindrome");
               }
        }
}

Output :-- The given number 12321 is a palindrome.



Related Posts:--
1) Write a Java program to print the Floyd's triangle
2) Java Program for Bubble Sort in Ascending and Descending order
3) Java Program to Reverse a Linked List using Recursion and Loops
4) Java Program to Count Occurrence of Word in a Sentence
5) How to Remove duplicates from ArrayList in Java

Wednesday, 9 January 2019

Write a Java Program to Print Floyd's Triangle

       In this post, I have written a Java program to print Floyd's Triangle. Floyd's Triangle is a right-angled triangular arrangement of consecutive natural numbers, starting with 1 at the top-left corner.

See the code below.

FloydsTriangle.java


package com.example;

class FloydsTriangle {

       public static void main(String args[]) {
       
              int noOfrows = 4;  // this is the number of rows.
              int number = 1;
       
              System.out.println("Floyd's triangle Example");
              System.out.println("---------------------------");
             
              for (int count = 1 ; count <= noOfrows ; count++ ) {
                     
                     for (int j = 1 ; j <= count ; j++ ) {
                            
                            System.out.print(number+" ");
                            
                            number++;
                     }

                   System.out.println();  // next line or new line

              }
       }
}

Output :--

Floyd's triangle Example
---------------------------

2 3 
4 5 6 
7 8 9 10



Related Posts:--
1) Java Program for Bubble Sort in Ascending and Descending order
2) Java Program to Reverse a Linked List using Recursion and Loops
3) How to Remove duplicates from ArrayList in Java
4) Java Program to Count Occurrence of Word in a Sentence
5) Java Object sorting example (Comparable and Comparable)