Java Program to Print Reverse Floyd’s Triangle Number Pattern

Print Reverse Floyd’s Triangle Number Pattern

In the previous article, we have discussed Java Program to Print Floyd’s Triangle Number Pattern

In this article we are going to see how to print reverse Floyd’s triangle number pattern.

Example-1

When rows value = 5

15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
Example-2:

When rows value=7

28 27 26 25 24 23 22
21 20 19 18 17 16
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1

Now, let’s see the actual program to print it.

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Approach:

  • Enter total number of rows and store it in an integer variable rows
  • Take one outer for loop to iterate the rows.
  • Take one inner for loop to iterate the columns.
  • After each iteration print a new line.

Java Code to Print Reverse Floyd’s Triangle Number Pattern

import java.util.Scanner;
class pattern
{
//Function to set the counter
static int calculateCounter(int rows)
{
    int sum = 0;
    for (int i = 1; i <= rows; i++)
    {
        sum += i;
    }
    return sum;
}

//Main Function
public static void main(String[] args)
{
    //Create a new Scanner object
    Scanner scan = new Scanner(System.in);

    //Taking total number of rows as input from user
    System.out.print("Rows : ");
    int rows= scan.nextInt();

   //Row and column are the iterators and counter to print
    int numberOfRows, numberOfColumns, counter = calculateCounter(rows);

    //Outer loop to iterate the rows
    //Iterates from 1 to the number of rows entered by the user(backwards)
    for (numberOfRows = rows; numberOfRows >= 1; --numberOfRows)
    {
        //Inner loop to print number
        //Iterator iterates from 1 to the numberOfRows 
        for (numberOfColumns = 1; numberOfColumns <= numberOfRows; ++numberOfColumns)
        {
            System.out.print(counter-- + " ");
        }
    //Prints a newline
    System.out.println();
    }
}
}

Output:

Rows : 5

15 14 13 12 11
10 9 8 7
6 5 4
3 2
1

C Code to Print Reverse Floyd’s Triangle Number Pattern

#include <stdio.h>
//Function to set the counter
int calculateCounter(int rows)
{
   int sum = 0;
   for (int i = 1; i <= rows; i++)
   {
      sum += i;
   }
   return sum;
}

//Main Function
int main()
{
   //Taking total number of rows as input from user
   printf("Rows : ");
   int rows;
   scanf("%d", &rows);

   //Row and column are the iterators and counter to print
   int numberOfRows, numberOfColumns, counter = calculateCounter(rows);

   //Outer loop to iterate the rows
   //Iterates from 1 to the number of rows entered by the user(backwards)
   for (numberOfRows = rows; numberOfRows >= 1; --numberOfRows)
   {
      //Inner loop to print number
      //Iterator iterates from 1 to the numberOfRows
      for (numberOfColumns = 1; numberOfColumns <= numberOfRows; ++numberOfColumns)
      {
         printf("%d ", counter--);
      }
      //Prints a newline
      printf("\n");
   }
   return 0;
}
Output:

Rows : 5

15 14 13 12 11
10 9 8 7
6 5 4
3 2
1

C++ Code to Print Reverse Floyd’s Triangle Number Pattern

#include <iostream>
using namespace std;

//Function to set the counter
int calculateCounter(int rows)
{
    int sum = 0;
    for (int i = 1; i <= rows; i++)
    {
        sum += i;
    }
    return sum;
}

//Main Function
int main(int argc, char const *argv[])
{
    //Taking total number of rows as input from user
    cout << "Rows : ";
    int rows;
    cin >> rows;

    //Row and column are the iterators and counter to print
    int numberOfRows, numberOfColumns, counter = calculateCounter(rows);

    //Outer loop to iterate the rows
    //Iterates from 1 to the number of rows entered by the user(backwards)
    for (numberOfRows = rows; numberOfRows >= 1; --numberOfRows)
    {
        //Inner loop to print number
        //Iterator iterates from 1 to the numberOfRows
        for (numberOfColumns = 1; numberOfColumns <= numberOfRows; ++numberOfColumns)
        {
            cout << counter-- << " ";
        }
        //Prints a newline
        cout << endl;
    }
    return 0;
}

Output:

Rows : 5

15 14 13 12 11
10 9 8 7
6 5 4
3 2
1

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Number Pattern Programs:

Java Program to Print Square with Repeated Number Decreasing Order Pattern

Printing Square with Repeated Number Decreasing Order Pattern

In the previous article, we have discussed Java Program to Print Square with Repeated Number Increasing Order Pattern

In this program we are going to see how to print the square with repeated number decreasing number pattern.

Example-1

When size value=5 and 
starting number = 9

9 9 9 9 9
8 8 8 8 8
7 7 7 7 7
6 6 6 6 6
5 5 5 5 5
Example-2:

When size value=3 and 
starting number = 5

5 5 5
4 4 4
3 3 3

Now, let’s see the actual program to print it.

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Approach:

  • Enter total size and number store them in integer variables size & num .
  • Take one outer for loop to iterate the rows.
  • Take one inner for loop to iterate the columns and print the column values.
  • After each iteration print a new line.

Java Code to Print Square with Repeated Number Decreasing Order Pattern

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
     // Create a new Scanner object
    Scanner scan = new Scanner(System.in);

    //Taking size as input from user
    System.out.print("Size of square : ");
    int size = scan.nextInt();

    //Taking number as input from user
    System.out.print("Number to print from : ");
    int num = scan.nextInt();

    //Row and column are the iterators
    int numberOfRows, numberOfColumns;

    //Outer loop to iterate the rows
    //Iterates from 1 to the size entered by the user
    for (numberOfRows = 1; numberOfRows <= size; numberOfRows++)
    {
        //Inner loop to iterate the columns
        //Iterates from 0 to one less than the size entered by the user
        for (numberOfColumns = 0; numberOfColumns < size; numberOfColumns++)
        {
            //Prints the num value
            System.out.print(num+" ");
        }
        //Incrementing the num variable after each row
        num--;
        //Prints a newline
        System.out.println();
    }
}
}

Output:

Size of square : 5
Number to print from : 9

9 9 9 9 9 
8 8 8 8 8 
7 7 7 7 7 
6 6 6 6 6 
5 5 5 5 5

C Code to Print Square with Repeated Number Decreasing Order Pattern

#include <stdio.h>

int main()
{
    //Taking size as input from user
    printf("Size of square : ");
    int size;
    scanf("%d", &size);

    //Taking number as input from user
    printf("Number to print from : ");
    int num;
    scanf("%d", &num);

    //Row and column are the iterators
    int numberOfRows, numberOfColumns;

    //Outer loop to iterate the rows
    //Iterates from 1 to the size entered by the user
    for (numberOfRows = 1; numberOfRows <= size; numberOfRows++)
    {
        //Inner loop to iterate the columns
        //Iterates from 0 to one less than the size entered by the user
        for (numberOfColumns = 0; numberOfColumns < size; numberOfColumns++)
        {
            //Prints the num value
            printf("%d ", num);
        }
        //Incrementing the num variable after each row
        num--;
        //Prints a newline
        printf("\n");
    }
    return 0;
}

Output:

Size of square : 5
Number to print from : 9

9 9 9 9 9 
8 8 8 8 8 
7 7 7 7 7 
6 6 6 6 6 
5 5 5 5 5

C++ Code to Print Square with Repeated Number Decreasing Order Pattern

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    //Taking size as input from user
    printf("Size of square : ");
    int size;
    cin >> size;

    //Taking number as input from user
    printf("Number to print from : ");
    int num;
    cin >> num;

    //Row and column are the iterators
    int numberOfRows, numberOfColumns;

    //Outer loop to iterate the rows
    //Iterates from 1 to the size entered by the user
    for (numberOfRows = 1; numberOfRows <= size; numberOfRows++)
    {
        //Inner loop to iterate the columns
        //Iterates from 0 to one less than the size entered by the user
        for (numberOfColumns = 0; numberOfColumns < size; numberOfColumns++)
        {
            //Prints the num value
            cout << num << " ";
        }
        //Incrementing the num variable after each row
        num--;
        //Prints a newline
        cout << endl;
    }
    return 0;
}

Output:

Size of square : 5
Number to print from : 9

9 9 9 9 9 
8 8 8 8 8 
7 7 7 7 7 
6 6 6 6 6 
5 5 5 5 5

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Number Pattern Programs:

Java Program to Print the Series 2 1 1/2 1/4 1/8 …N

Java Program to Print the Series 2 1 1/2 1/4 1/8 … N

In the previous article we have discussed about Java Program to Print the Series 2 3 12 37 86 166 … N

In this article we are going to see how to print the series 6 11 21 36 56 …N by using Java programming language.

Java Program to Print the Series 2 1 1/2 1/4 1/8 …N

On observing the pattern carefully, we can see first number starts from 2

Then the next number follows a logic i.e previous element/2

2
2/2 = 1
1/2
1/2 / 2 = 1/4
1/4 / 2 = 1/8 and so on.

Example:

2 1 1/2 1/4 1/8 1/16 …… N

Let’s see different ways to print the series 6 11 21 36 56 …N

Method-1: Java Program to Print the Series 2 1 1/2 1/4 1/8 …N By Using For Loop

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the number of terms in the series.
  • Prompt the user to enter a number as value of n.
  • Let declare an double variable say ‘result’ and initialize it to 2
  • Use a for loop from i=1 to i<=n-1 where the loop is incremented by 1
  • Inside the for loop we will find the value of result=result/2
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the number of terms ");
        int n = s.nextInt();
        double result = 2;
        System.out.print(result);
        //for loop to print the series
        for (int i = 1; i <= n-1; i++) 
        {
            result /=2; 
            System.out.print(" "+result);
        } 
    }
}
Output:

Enter the number of terms
5
2.0 1.0 0.5 0.25 0.125

Method-2: Java Program to Print the Series 2 1 1/2 1/4 1/8 …N By Using While Loop

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the number of terms in the series.
  • Prompt the user to enter a number as value of n.
  • Let declare an double variable say ‘result’ and initialize it to 2
  • Declare and initialize an integer variable i=1
  • Continue a while loop till i<=n-1, where i is incremented by 1.
  • Inside the while loop we will find the value of result=result/2
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the number of terms ");
        int n = s.nextInt();
        double result=2;
        System.out.print(result);
        int i=1;
        while(i<=n-1)
        {
            result /=2; 
            System.out.print("  "+result);
            i++;
        }      
    }
}
Output:

Enter the number of terms
7
2.0 1.0 0.5 0.25 0.125 0.0625 0.03125

Method-3: Java Program to Print the Series 2 1 1/2 1/4 1/8 …N By Using User Defined Method

Approach:

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the number of terms in the series.
  • Prompt the user to enter a number as value of n.
  • Then call a user defined method printSeries() by passing n as parameter.
  • Inside method declare an double variable say ‘result’ and initialize it to 2
  • Use a for loop from i=1 to i<=n-1 where the loop is incremented by 1
  • Inside the for loop we will find the value of result=result/2
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // creating object of scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the number of terms ");
        int n = s.nextInt();
        // calling printSeries method to print the series
        printSeries(n);
    }
    
    //printSeries metthod to print the series
    public static void printSeries(int n)
    {
        double result = 2;
        System.out.print(result);
        //for loop to print the series
        for (int i = 1; i <=n-1; i++) 
        {
            result /=2; 
            System.out.print(" "+result);
        } 
    }
}
Output:

Enter the number of terms
9
2.0 1.0 0.5 0.25 0.125 0.0625 0.03125 0.015625 0.0078125

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Programs:

Java Program to Print Square with Repeated Number Increasing Order Pattern

Printing Square with Repeated Number Increasing Order Pattern

In the previous article, we have discussed Java Program to Print Square with Alternate row Binary Number Pattern

In this program we are going to see how to print the square with repeated number increasing number pattern.

Example-1

When size value=5 and 
starting number = 1

1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
Example-2:

When size value=3 and 
starting number = 5

5 5 5
6 6 6
7 7 7

Now, let’s see the actual program to print it.

Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.

Approach:

  • Enter total size and starting number and store them in integer variables size & num  respectively..
  • Take one outer for loop to iterate the rows.
  • Take one inner for loop to iterate the columns and print the column values i.e the numbers.
  • After each iteration print a new line.

Java Code to Print Square with Repeated Number Increasing Order Pattern

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
     // Create a new Scanner object
    Scanner scan = new Scanner(System.in);

    //Taking size as input from user
    System.out.print("Size of square : ");
    int size = scan.nextInt();

    //Taking number as input from user
    System.out.print("Number to print from : ");
    int num = scan.nextInt();

    //Row and column are the iterators
    int numberOfRows, numberOfColumns;

    //Outer loop to iterate the rows
    //Iterates from 1 to the size entered by the user
    for (numberOfRows = 1; numberOfRows <= size; numberOfRows++)
    {
        //Inner loop to iterate the columns
        //Iterates from 0 to one less than the size entered by the user
        for (numberOfColumns = 0; numberOfColumns < size; numberOfColumns++)
        {
            //Prints the num value
            System.out.print(num+" ");
        }
        //Incrementing the num variable after each row
        num++;
        //Prints a newline
        System.out.println();
    }
}
}
Output

Size of square : 3
Number to print from : 5

5 5 5
6 6 6
7 7 7

C Code to Print Square with Repeated Number Increasing Order Pattern

#include <stdio.h>

int main()
{
    //Taking size as input from user
    printf("Size of square : ");
    int size;
    scanf("%d", &size);

    //Taking number as input from user
    printf("Number to print from : ");
    int num;
    scanf("%d", &num);

    //Row and column are the iterators
    int numberOfRows, numberOfColumns;

    //Outer loop to iterate the rows
    //Iterates from 1 to the size entered by the user
    for (numberOfRows = 1; numberOfRows <= size; numberOfRows++)
    {
        //Inner loop to iterate the columns
        //Iterates from 0 to one less than the size entered by the user
        for (numberOfColumns = 0; numberOfColumns < size; numberOfColumns++)
        {
            //Prints the num value
            printf("%d ", num);
        }
        //Incrementing the num variable after each row
        num++;
        //Prints a newline
        printf("\n");
    }
    return 0;
}

Output:

Size of square : 3
Number to print from : 5

5 5 5
6 6 6
7 7 7

C++ Code to Print Square with Repeated Number Increasing Order Pattern

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    //Taking size as input from user
    printf("Size of square : ");
    int size;
    cin >> size;

    //Taking number as input from user
    printf("Number to print from : ");
    int num;
    cin >> num;

    //Row and column are the iterators
    int numberOfRows, numberOfColumns;

    //Outer loop to iterate the rows
    //Iterates from 1 to the size entered by the user
    for (numberOfRows = 1; numberOfRows <= size; numberOfRows++)
    {
        //Inner loop to iterate the columns
        //Iterates from 0 to one less than the size entered by the user
        for (numberOfColumns = 0; numberOfColumns < size; numberOfColumns++)
        {
            //Prints the num value
            cout << num << " ";
        }
        //Incrementing the num variable after each row
        num++;
        //Prints a newline
        cout << endl;
    }
    return 0;
}

Output:

Size of square : 3
Number to print from : 5

5 5 5
6 6 6
7 7 7

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Number Pattern Programs:

Java Program to Print Rectangular with User Input Centre Number Pattern

Print Rectangular with User Input Centre Number Pattern

In the previous article, we have discussed Java Program to Print Heart Number Pattern. In this article, we will see how to print rectangular with User Input Centre number pattern.

When size is 5

2 2 2 2 2
2 1 1 1 2
2 1 0 1 2
2 1 1 1 2
2 2 2 2 2

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Approach :

  • Take a variable “size” for dimension for the matrix.
  • Declare the center coordinate of the matrix .
  • Iterate an outer loop by using a variable numberOfRows  and do the following steps:
    • Iterate an inner loop using a variable numberOfColumns  and do the following steps:
    • Print maximum of abs(c1 – i) and abs(c2 – j).

Java Code to Print Rectangular with User Input Centre Number Pattern

import java.io.*;
class Main
{
    public static void main(String[] args)
    {
        //taking the coordinate of the  center of matrix as c1 and c2 
        // taking the dimension of the square of the matrix as size 
        int c1 = 2 , c2 = 2 , size = 5, numberOfRows , numberOfColumns ;
        // // Iterate in the range[0, n-1] for outer loop ( rows)
        for(numberOfRows = 0; numberOfRows < size ; numberOfRows++)
             {
                // Iterate in the range[0, n-1] for inner loop (column)
                for( numberOfColumns  = 0; numberOfColumns  < size ; numberOfColumns ++)
                        //  maximum of abs(c1 – i) and abs(c2 – j).
                        System.out.print((Math.max(Math.abs(c1 - numberOfRows), Math.abs(c2 - numberOfColumns ))) + " ");
                System.out.println();
            }
    }
}
Output:

2 2 2 2 2
2 1 1 1 2
2 1 0 1 2
2 1 1 1 2
2 2 2 2 2

C Code to Print Rectangular with User Input Centre Number Pattern

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int c1 = 2 , c2 = 2 , size = 5, numberOfRows , numberOfColumns , result1 , result2 ;
        for(numberOfRows = 0; numberOfRows < size ; numberOfRows++)
             {
                for( numberOfColumns  = 0; numberOfColumns  < size ; numberOfColumns ++)
                    {
                        result1= abs(c1 - numberOfRows);
                        result2=abs(c2 - numberOfColumns);
                        if (result1 > result2)
                            printf( "%d ",result1);
                        else
                           printf( "%d ",result2); 
                    }
                printf("\n");
            }
    return 0;
}

Output:

2 2 2 2 2
2 1 1 1 2
2 1 0 1 2
2 1 1 1 2
2 2 2 2 2

C++ Code to Print Rectangular with User Input Centre Number Pattern

#include <bits/stdc++.h>
using namespace std;
int main()
{
     int c1 = 2 , c2 = 2 , size = 5, numberOfRows , numberOfColumns ;
        // // Iterate in the range[0, n-1] for outer loop ( rows)
        for(numberOfRows = 0; numberOfRows < size ; numberOfRows++)
             {
                for( numberOfColumns  = 0; numberOfColumns  < size ; numberOfColumns ++)
                       cout << max(abs(c1 - numberOfRows), abs(c2 - numberOfColumns)) << " ";
               cout << endl;
            }
    return 0;
}
Output:

2 2 2 2 2
2 1 1 1 2
2 1 0 1 2
2 1 1 1 2
2 2 2 2 2

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Number Pattern Programs:

Java Program to Print the Series 1 9 17 33 49 73 97 … N

Java Program to Print the Series 1 9 17 33 49 73 97 … N

In the previous article we have discussed about Java Program to Print the Sum of Series 3 + 7+ 13 + 21 + … + N

In this article we are going to see how to print the series 1 9 17 33 49 73 97 … N by using Java programming language.

Java Program to Print the Series 1 9 17 33 49 73 97 … N

On observing the pattern carefully, we can see the series follows a logic

If i is odd then 2*(i*i)-1

If i is even then 2*(i*i)+1

Example:

2*(i*i)-1 = 2*(1*1)-1 = 1

2*(i*i)-1 = 2*(2*2)+1 = 9

2*(i*i)-1 = 2*(3*3)-1 = 17

2*(i*i)-1 = 2*(4*4)+1 = 33 … and so on.

1 9 17 33 49 73 …… N

Let’s see different ways to

Method-1: Java Program to Print the Series 1 9 17 33 49 73 97 … N By Using For Loop

Approach: 

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the number of terms in the series.
  • Prompt the user to enter a number as value of n.
  • Let declare an integer variable say ‘result
  • Use a for loop from i=1 to i<=n where the loop is incremented by 1
  • Inside the for loop we will find the value of result according to the position of i. If i is odd then result=2*(i*i)-1, If i is even then result=2*(i*i)+1
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the number of terms  ");
        int n = s.nextInt();
        int result;
        //for loop to print the series
        for (int i = 1; i <= n; i++) 
        {
            if(i%2==0)
            {
                result=(int) (2*Math.pow(i,2)+1);
                System.out.print(result+" ");
            }
            else
            {
                result=(int) (2*Math.pow(i,2)-1);
                System.out.printf(result+" ");
            }
        } 
    }
}
Output:

Enter the number of terms 
5
1 9 17 33 49

Method-2: Java Program to Print the Series 1 9 17 33 49 73 97 … N By Using While Loop

Approach: 

  • Create Scanner class object.
  • Declare an integer variable say ‘n’ which holds the number of terms in the series.
  • Prompt the user to enter a number as value of n.
  • Let declare integer variable say ‘result
  • Declare and initialize an integer variable i=1
  • Continue a while loop till i<=n, where i is incremented by 1.
  • Inside while loop we will find the value of result according to the position of i. If i is odd then result=2*(i*i)-1, If i is even then result=2*(i*i)+1
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        //creating object of Scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the Nth term “N” ");
        int n = s.nextInt();
        int result;
        int i=1;
        while(i<=n)
        {
            if(i%2==0)
            {
                result=(int) (2*Math.pow(i,2)+1);
                System.out.print(result+" ");
            }
            else
            {
                result=(int) (2*Math.pow(i,2)-1);
                System.out.printf(result+" ");
            }
            i++;
        } 
    }
}
Output:

Enter the number of terms 
7
1 9 17 33 49 73 97

Method-3: Java Program to Print the Series 1 9 17 33 49 73 97 … N By Using User Defined Method

Approach:

  • Create a Scanner class object.
  • Prompt the user to enter a value of n which holds the number of terms in the series.
  • Call a user defined method printSeries() by passing n as parameter.
  • Inside the method declare integer variable say ‘result’.
  • Inside the for loop we will find the value of result according to the position of i. If i is odd then result=2*(i*i)-1, If i is even then result=2*(i*i)+1
  • Print the result in the series.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // creating object of scanner class 
        Scanner s = new Scanner(System.in);
        //Taking input of number of elements in the series
        System.out.println("Enter the value of Nth term 'N' ");
        int n = s.nextInt();
        // calling printSeries method to print the series
        printSeries(n);
    }
    
    //printSeries() metthod to print the series
    public static void printSeries(int n)
    {
        int result ;
        for(int i = 1; i<=n; i++)
        {
            if(i%2==0)
            {
                result=(int) (2*Math.pow(i,2)+1);
                System.out.print(result+" ");
            }
            else
            {
                result=(int) (2*Math.pow(i,2)-1);
                System.out.printf(result+" ");
            }
        }
    }
}
Output:

Enter the number of terms 
9
1 9 17 33 49 73 97 129 161

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Programs:

Java Program to Print Sand Glass Number Pattern

Print Sand Glass Number Pattern

In the previous article, we have discussed Java Program to Print Reverse Floyd’s Triangle Number Pattern

In this article we are going to see how to print sand glass number pattern.

Example-1

When rows value = 5

1 2 3 4 5
 2 3 4 5
  3 4 5
   4 5
    5
    5
   4 5
  3 4 5
 2 3 4 5
1 2 3 4 5


Example-2:

When rows value=7

1 2 3 4 5 6 7
 2 3 4 5 6 7
  3 4 5 6 7
   4 5 6 7
    5 6 7
     6 7
      7
      7
     6 7
    5 6 7
   4 5 6 7
  3 4 5 6 7
 2 3 4 5 6 7
1 2 3 4 5 6 7

Now, let’s see the actual program to print it.

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Approach:

  • Enter total number of rows and store it in an integer variable rows.
  • Take two outer for loops(for both halves) to iterate the rows.
  • Take two inner for loop one to print space and the other one to print the number.
  • After each iteration print a new line.

Java Code to Print Sand Glass Number Pattern

import java.util.Scanner;
class Main
{

public static void main(String[] args)
{
    //Create a new Scanner object
    Scanner scan = new Scanner(System.in);

    //Taking total number of rows as input from user
    System.out.print("Rows : ");
    int rows= scan.nextInt();

    //Row and column are the iterators and counter to print
    int numberOfRows, numberOfColumns;

    //Outer loop to print the lower half
    //Iterates from 1 to the number of rows entered by the user
    for (numberOfRows = 1; numberOfRows <= rows; numberOfRows++)
    {
        //Inner loop to print the space
        for (numberOfColumns = 1; numberOfColumns < numberOfRows; numberOfColumns++)
        {
            System.out.print(" ");
        }
        //inner loop to print the number
        for (numberOfColumns = numberOfRows; numberOfColumns <= rows; numberOfColumns++)
        {
           System.out.print(numberOfColumns+" ");
        }
        //Prints a newline
        System.out.println();
    }
    //Outer loop to print the lower half
    //Iterates from number of rows entered by user to 1
    for (numberOfRows = rows; numberOfRows >= 1; numberOfRows--)
    {
        //Inner loop to print the space
        for (numberOfColumns = 1; numberOfColumns < numberOfRows; numberOfColumns++)
        {
            System.out.print(" ");
        }
        //inner loop to print the number
        for (numberOfColumns = numberOfRows; numberOfColumns <= rows; numberOfColumns++)
        {
           System.out.print(numberOfColumns+" ");
        }
        //Prints a newline
        System.out.println();
    }
}
}
Output:

Rows : 5

1 2 3 4 5
 2 3 4 5
  3 4 5
   4 5
    5
    5
   4 5
  3 4 5
 2 3 4 5
1 2 3 4 5

C Code to Print Sand Glass Number Pattern

#include <stdio.h>

int main()
{
   //Taking total number of rows as input from user
   printf("Rows : ");
   int rows;
   scanf("%d", &rows);

   //Row and column are the iterators and counter to print
   int numberOfRows, numberOfColumns;

   //Outer loop to print the lower half
   //Iterates from 1 to the number of rows entered by the user
   for (numberOfRows = 1; numberOfRows <= rows; numberOfRows++)
   {
      //Inner loop to print the space
      for (numberOfColumns = 1; numberOfColumns < numberOfRows; numberOfColumns++)
      {
         printf(" ");
      }
      //inner loop to print the number
      for (numberOfColumns = numberOfRows; numberOfColumns <= rows; numberOfColumns++)
      {
         printf("%d ", numberOfColumns);
      }
      //Prints a newline
      printf("\n");
   }
   //Outer loop to print the lower half
   //Iterates from number of rows entered by user to 1
   for (numberOfRows = rows; numberOfRows >= 1; numberOfRows--)
   {
      //Inner loop to print the space
      for (numberOfColumns = 1; numberOfColumns < numberOfRows; numberOfColumns++)
      {
         printf(" ");
      }
      //inner loop to print the number
      for (numberOfColumns = numberOfRows; numberOfColumns <= rows; numberOfColumns++)
      {
         printf("%d ", numberOfColumns);
      }
      //Prints a newline
      printf("\n");
   }
   return 0;
}
Output:

Rows : 5

1 2 3 4 5
 2 3 4 5
  3 4 5
   4 5
    5
    5
   4 5
  3 4 5
 2 3 4 5
1 2 3 4 5

C++ Code to Print Sand Glass Number Pattern

#include <iostream>
using namespace std;

int main(int argc, char const *argv[])
{
    //Taking total number of rows as input from user
    cout << "Rows : ";
    int rows;
    cin >> rows;

    //Row and column are the iterators and counter to print
    int numberOfRows, numberOfColumns;

    //Outer loop to print the lower half
    //Iterates from 1 to the number of rows entered by the user
    for (numberOfRows = 1; numberOfRows <= rows; numberOfRows++)
    {
        //Inner loop to print the space
        for (numberOfColumns = 1; numberOfColumns < numberOfRows; numberOfColumns++)
        {
            cout << " ";
        }
        //inner loop to print the number
        for (numberOfColumns = numberOfRows; numberOfColumns <= rows; numberOfColumns++)
        {
            cout << numberOfColumns << " ";
        }
        //Prints a newline
        cout << endl;
    }
    //Outer loop to print the lower half
    //Iterates from number of rows entered by user to 1
    for (numberOfRows = rows; numberOfRows >= 1; numberOfRows--)
    {
        //Inner loop to print the space
        for (numberOfColumns = 1; numberOfColumns < numberOfRows; numberOfColumns++)
        {
            cout << " ";
        }
        //inner loop to print the number
        for (numberOfColumns = numberOfRows; numberOfColumns <= rows; numberOfColumns++)
        {
            cout << numberOfColumns << " ";
        }
        //Prints a newline
        cout << endl;
    }
    return 0;
}

Output:

Rows : 5

1 2 3 4 5
 2 3 4 5
  3 4 5
   4 5
    5
    5
   4 5
  3 4 5
 2 3 4 5
1 2 3 4 5

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Number Pattern Programs:

Java Program to Sort the elements of a Matrix

In the previous article, we have discussed Java Program to Rotate the Matrix 180 degree

In this article we are going to see how we can write a program to sort elements in a matrix in JAVA language.

Java Program to Sort the elements of a Matrix

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
                   | A20   A21   A22 | 3*3
  • Matrix A represents a 3*3 matrix.
  • A‘ represents the matrix element
  • Aij‘ represents the matrix element at it’s matrix position/index.
  • i‘ represents the row index
  • j‘ represents the column index
  • Means A00=Aij  where i=0 and j=0A01=aij where i=0 and j=1 and like this.
  • Here we have started row value from 0 and column value from 0.

Let’s see different ways to Sort the elements of a Matrix.

Method-1: Java Program to Sort the elements of a Matrix By Static Initialization of Array Elements

Approach:

  • Initialize and declare two arrays of size 3×3, one with elements.
  • Copy the elements of the matrix into a 1D array.
  • Sort the 1D array then insert the elements into the matrix.
  • Print the resultant array.

Program:

import java.io.*;
import java.util.*;
public class matrix{
    public static void main(String args[])
    {
        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = {{19,25,32},{40,54,62},{70,20,60}}, res[][] = new int[3][3];
        int row, col ;
        
        System.out.print("The matrix elements are : ");
        printMatrix(arr);

        System.out.print("\nThe sorted matrix:");
        printMatrix(sortMatrix(arr));
    }

    // Method to sort the matrix elements
    static int[][] sortMatrix(int arr[][])
    {
        int temp[] = new int [3*3];
        int k = 0,row,col;

        // Copying the array elements into a 1D array
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {
                temp[k++]=arr[row][col];
            }
        
        // Sorting the 1D array
        Arrays.sort(temp);
        k=0;

        // Copying the elements from the sorted array into the 2D array
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {
                arr[row][col]=temp[k++];
            }
        return arr;
    }

    // Method to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
        System.out.print("\n");
    }

}
Output:

The matrix elements are : 
19 25 32 
40 54 62 
70 20 60

The sorted matrix:
19 20 25 
32 40 54 
60 62 70

Method-2: Java Program to Sort the elements of a Matrix By Dynamic Initialization of Array Elements

Approach:

  • Declare two arrays of size 3×3.
  • Ask the user for input of array elements and store them in the one array using two for loops.
  • Copy the elements of the matrix into a 1D array.
  • Sort the 1D array then insert the elements into the matrix.
  • Print the resultant array.

Program:

import java.io.*;
import java.util.*;
public class matrix{
    public static void main(String args[])
    {
        //Scanner class to take input
        Scanner scan = new Scanner(System.in);

        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = new int[3][3], res[][] = new int[3][3];
        int row, col ;
        
        // Taking matrix input
        System.out.println("\nEnter matrix elements : ");
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                arr[row][col] = scan.nextInt();
        
        System.out.print("The matrix elements are : ");
        printMatrix(arr);

        System.out.print("\nThe sorted matrix : ");
        printMatrix(sortMatrix(arr));
    }

    // Metrhod to sort the matrix elements
    static int[][] sortMatrix(int arr[][])
    {
        int temp[] = new int [3*3];
        int k = 0,row,col;

        // Copying the array elements into a 1D array
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {
                temp[k++]=arr[row][col];
            }
        
        // Sorting the 1D array
        Arrays.sort(temp);
        k=0;

        // Copying the elements from the sorted array into the 2D array
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {
                arr[row][col]=temp[k++];
            }
        return arr;
    }

    // Method to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
        System.out.print("\n");
    }

}
Output:

Enter matrix elements : 9 8 3 4 5 6 7 2 1
The matrix elements are : 
9 8 3 
4 5 6 
7 2 1

The sorted matrix : 
1 2 3 
4 5 6 
7 8 9

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs:

Java Program to Print Upward Arrow Mark Symbol Number Pattern

Print Upward Arrow Mark Symbol Number Pattern

In the previous article, we have discussed Java Program to Print Downward Arrow Mark Symbol Star Pattern

In this article we are going to see how to print the upward arrow mark symbol number pattern.

Example-1 

When size value= 5

   3  
 234 
1 3 5
   3  
   3
 Example-2

When size value= 7

    4   
  345  
 2 4 6 
1  4  7
   4   
   4   
   4

Now, let’s see the actual program to print it.

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Approach:

  • Enter size of the pattern and store it in an integer variable size.
  • Take one outer for loop to iterate the rows.
  • Take one inner for loops, to iterate the columns.
  • After each iteration print a newline.

Java Code to Print Upward Arrow Mark Symbol

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
    int size, r, c;
    //Taking size as input from user
    System.out.print("Size : ");
    Scanner scan = new Scanner(System.in);
    size = scan.nextInt();
    //Taking middle row of the pattern
    int mid = size / 2 + 1;
    //Outer Loop
    for (r = 1; r <= size; r++)
    {
        //Inner loop
        for (c = 1; c <= size; c++)
        {
            if (c == size / 2 + 1 || c == mid || c == size - mid + 1)
                System.out.print(c);
            else
                System.out.print(" ");
        }
        //Prints a newline
        System.out.println();
        //Incrementing the mid value
        mid++;
    }
}
}
Output:

Size :   7

    4   
  345  
 2 4 6 
1  4  7
   4   
   4   
   4

C Code to Print Upward Arrow Mark Symbol

#include <stdio.h>
int main()
{
    int size, r, c;
    //Taking size as input from user
    printf("Size : ");
    scanf("%d", &size);
    //Taking middle of the pattern
    int mid = size / 2 + 1;
    //Outer Loop
    for (r = 1; r <= size; r++)
    {
        //Inner loop
        for (c = 1; c <= size; c++)
        {
            if (c == size / 2 + 1 || c == mid || c == size - mid + 1)
                printf("%d",c);
            else
                printf(" ");
        }
        //Prints a newline
        printf("\n");
        //incrementing the mid value
        mid++;
    }
    return 0;
}
Output:

Size :  7
 
    4   
  345  
 2 4 6 
1  4  7
   4   
   4   
   4

C++ Code to Print Upward Arrow Mark Symbol

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    int size, r, c;
    //Taking size as input from user
    cout << "Size : ";
    cin >> size;
    //Taking middle of the pattern
    int mid = size / 2 + 1;
    //Outer Loop
    for (r = 1; r <= size; r++)
    {
        //Inner loop
        for (c = 1; c <= size; c++)
        {
            if (c == size / 2 + 1 || c == mid || c == size - mid + 1)
                cout << c;
            else
                cout << " ";
        }
        //Prints a newline
        cout << endl;
        //Incrementing the mid value
        mid++;
    }
    return 0;
}
Output:

Size : 7

    4   
  345  
 2 4 6 
1  4  7
    4   
    4   
    4

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Number Pattern Programs:

Java Program to Print Window Number Pattern

Program to Print Window Number Pattern

In the previous article, we have discussed Java Program to Print Crown Number Pattern

In this article we are going to see how to print window number program.

Example-1

Enter rows : 5

1 1 1 1 1 1 
2    2 2    2 
3 3 3 3 3 3 
4 4 4 4 4 4 
5    5 5    5 
6 6 6 6 6 6
Example-2: 

Enter rows : 5

1 1 1 1 1 
2    2    2 
3 3 3 3 3 
4    4    4 
5 5 5 5 5

Now, let’s see the actual program to print it.

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Approach to Print Window Number Pattern

  • Enter total row and store it in an integer variable row.
  • Calculate middle element.
    •  if n is odd we get 1 element .
    •  in case of n is even we get 2 values.
  • Take first for loop to print the row value and number for each row .
    • Take first inner for loop to print column value i.e., number  according to condition
      if (r == 1 || c == 1 || r == row || c == row) and if (r == a || c == a) and if (r == b || c == b)  else it will print space .
  • Then go on printing the numbers according to loop.

Java Code to Print Window Number Pattern

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,a,b;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    // If n is odd then we will have only one middle element
    if (row % 2 != 0)
    {
      a = (row / 2) + 1;
      b = 0;
    }
    // If n is even then we will have two values
    else
    {
      a = (row / 2) + 1;
      b = row / 2 ;
    } 
    for(  r = 1; r <= row; r++)
    {
      for( c = 1; c <= row ; c++)
      {
 
     
        if (r == 1 || c == 1 || r == row || c == row)
          System.out.print(r+" ");          
        else
        {
 
          
          if (r == a || c == a)
            System.out.print(r+" ");
          else if (r == b || c == b)
            System.out.print(r+" ");
          else
            System.out.print("  ");
        }
      }
      System.out.println();
    }
  }
}
Output:

CASE-1:
Enter rows : 6

1 1 1 1 1 1 
2    2 2    2 
3 3 3 3 3 3 
4 4 4 4 4 4 
5    5 5    5 
6 6 6 6 6 6
CASE-2:
Enter rows : 5

1 1 1 1 1 
2    2    2 
3 3 3 3 3 
4    4    4 
5 5 5 5 5

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Number Pattern Programs: