Tuesday 15 January 2019

Java Code to Print the Numbers as Pyramid - Examples

         This question is also one of the popular java interview question for freshers. The interviewer will check the logical thinking, how to solve the this kind of problems in real time.  I have written sample patterns in the below code, you can practice others also.


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 basic java coding interview questions. In interview, they will give an integer array from 1 to n and each number of array can not be duplicate. They will ask to write the java code to find the missing number of an array.

For Example:-

int[] numbers = {1,3,5,4,2,8,7}; 
Missing number is 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 previous post, I have written the code to check the given integer is a palindrome or not. Today's post, writing a java code to display the first n prime numbers. n can be any integer number and greater than 1.

The prime number is a number that is divisible only by itself and 1. The 1 is not a prime number but 2 is a first prime number and it's even number.

First few prime numbers are, 2,3,5,7,11,13,17,19,23..... 

Example:-

n=10, (first 10 prime numbers)
Expected output :--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 the given number is a palindrome or not

      Today's post, I have written java code to check the given number is a palindrome or not. The given number and reverse of that number should be same then it's a palindrome number. 

Example:-

int number = 12121,
reverse of above number is,12121 it should be same, so it's 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 the Floyd's triangle

      In the current post, I have written java code to print or display the Floyd's Triangle. The Floyd's triangle is a right angled triangular of natural array. The numbers are consecutive and natural numbers, start with 1 at the top of left corner.

See the below code,

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)