Author: admin

  • Hollow Pyramid using Stars in Java

    Hollow Pyramid using Stars in Java

    In this tutorial, we will write a program to print a hollow pattern of stars in java. Before that, you may go through the following topic in java.

    Both of the programs below take the user input for the number of rows and display the result using the for loop in java. The program is basically the hollow triangle star pattern in java program.


    Hollow Pyramid using Stars in Java

    import java.util.Scanner;
    
    public class Main
    {
      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 = 1; j <= n - i; j++)
          {
            System.out.print(" ");
          }
    
          if (i == 1 || i == n)
          {
            for (int j = 1; j <= i *2 - 1; j++)
            {
              System.out.print("*");
            }
          }
          else
          {
            for (int j = 1; j <= i *2 - 1; j++)
            {
              if (j == 1 || j == i *2 - 1)
                System.out.print("*");
              else
                System.out.print(" ");
            }
          }
    
          System.out.println();
        }
      }
    }

    Output:

    Hollow Pyramid using Stars in Java

  • Java Program to print Half Pyramid using Numbers

    Java Program to print Half Pyramid using Numbers

    In this tutorial, we will write a number pattern program in java. The patterns are half pyramid patterns with different combinations of numbers such as in increasing order, decreasing order, or more.

    Before that, you may go through the following topic in java.


    Java Program to print Half Pyramid using Numbers

    The programs below take the user input for the number of rows to be printed for the half pyramid. Now let us go through the java program to print pyramid pattern of numbers.

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int rows;
    
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        rows = sc.nextInt();
    
        for (int i = 1; i <= rows; ++i)
        {
          for (int j = 1; j <= i; ++j)
          {
            System.out.print(j + " ");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the no. of rows: 5
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5

  • Java program to print Inverted Equilateral Star Triangle

    Java program to print Inverted Equilateral Star Triangle

    In this tutorial, we will write a program to print an inverted equilateral triangle in java. Before that, you may go through the following topic in java.

    It is a Java Program to Print Reverse Pyramid Star Pattern or you can say upside-down equilateral triangle pattern in java.

    The program takes the user input for the number of rows to be taken to print the reverse equilateral star pattern.

    Example:

    Input: number = 7   
    Output:
     *************  
     ***********   
      *********    
       *******     
        *****     
         ***       
          * 

    Java program to print Inverted Equilateral Star Triangle

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int num, i, j, k;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        num = sc.nextInt();
    
        for (i = num; i > 0; i--)
        {
          for (j = 0; j < num - i; j++)
          {
            System.out.print(" ");
          }
    
          for (k = 0; k < i; k++)
          {
            System.out.print("*" + " ");
          }
    
          System.out.print("\n");
        }
      }
    }

    Output:

    Inverted Equilateral Star Triangle

  • Java Program to Print Left Triangle Star Pattern

    Java Program to Print Left Triangle Star Pattern

    In this tutorial, we will write a program to print the left triangle star pattern in java. Before that, you may go through the following topic in java.


    Java Program to Print Left Triangle Star Pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int k = 5, num;
        int i, j;
    
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        num = sc.nextInt();
    
        for (i = 0; i < num; i++)
        {
          for (j = 2 *(num - i); j >= 0; j--)
            System.out.print(" ");
    
          for (j = 0; j <= i; j++)
            System.out.print("* ");
    
          System.out.println();
        }
      }
    }

    Output:

    left triangle star pattern

  • Java Program to Print Right Triangle Star Pattern

    Java Program to Print Right Triangle Star Pattern

    In this tutorial, we will write a program to print the right triangle star pattern in java. Before that, you may go through the following topic in java.

    It also can be called mirrored right triangle number pattern in java or a half pyramid pattern in java. The program takes a user input for the number of rows and displays it accordingly.


    Java Program to Print Right Triangle Star Pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int i, j, num;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of rows: ");
        num = sc.nextInt();
    
        for (i = 1; i <= num; i++)
        {
          for (j = 1; j <= i; j++)
          {
            System.out.print("*");
          }
    
          System.out.println("");
        }
      }
    }

    Output:


  • Java Program to Print Hollow Rectangle Pattern of Stars

    Java Program to Print Hollow Rectangle Pattern of Stars

    In this tutorial, we will write a program to print hollow rectangle pattern in java. Before that, you may go through the following topic in java.

    The program takes a user input for the number of rows and columns for the hollow rectangle. Then using two for loops it will print the star rectangular pattern in java.

    Example:

    Inputs:
          Rows: 6
          Columns:10
    Output:
    **********
    *        *
    *        *
    *        *
    *        *
    **********

    Java Program to Print Rectangle Star Pattern

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int rows, columns, i, j;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        rows = sc.nextInt();
        System.out.print("Enter the no. of columns: ");
        columns = sc.nextInt();
    
        System.out.print("Rectangular star Pattern: \n\n");
        for (i = 1; i <= rows; i++)
        {
          for (j = 1; j <= columns; j++)
          {
            if (i == 1 || i == rows || j == 1 || j == columns)
              System.out.print("*");
            else
              System.out.print(" ");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    Hollow rectangle star pattern in java

  • Java Program to Print Odd Number Pyramid

    Java Program to Print Odd Number Pyramid

    In this tutorial, we will learn how to print odd number patterns in java. Before that, you may go through the following topic in java.

    The program takes a user input for the number of rows and prints the number pattern of the pyramid for odd numbers in java.


    Java Program to Print of Odd Number Pyramid

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int i, j, k = 1, num;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        num = sc.nextInt();
    
        for (i = 1; i <= num; i++)
        {
          for (j = 1; j <= i; j++)
          {
            System.out.print(k + " ");
            k = k + 2;
          }
    
          System.out.print("\n");
        }
      }
    }

    Output:

    odd number pattern program in java

  • Java Program to print Even Odd number Pyramid

    Java Program to print Even Odd number Pyramid

    In this tutorial, we will write a program to print even and odd number pyramid in java. Before that, you may go through the following topic in java.

    Example:

    Input: 
           No. of rows:  7
    Output:
    * 
    1* 
    *2* 
    1*3* 
    *2*4* 
    1*3*5* 
    *2*4*6*

    Java Program to print Even Odd number Pyramid

    The program takes a user input for the number of rows with the help of a Scanner class in Java. Then iterating through for loop and inner for loop, it prints the even and odd number pattern in Java.

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int i, j, k, num;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        num = sc.nextInt();
    
        for (i = 1; i <= num; i++)
        {
          for (j = 1, k = i; j <= i; j++, k--)
          {
            if (k % 2 == 0)
              System.out.print(j);
            else
              System.out.print("*");
    
          }
    
          System.out.print("\n");
        }
      }
    }

    Output:

    odd even pattern in java

  • C Program to Add Two Numbers using Pointers

    In this tutorial, we will write a program to find the sum of numbers using pointers in C program. Before that, you should have knowledge on the following topic in C:


    C Program to Add Two Numbers using Pointers

    The program takes the two numbers from the user and finds the sum of those numbers in C.

    #include <stdio.h>
    
    int main()
    {
      int num1, num2, *ptr1, *ptr2, sum;
    
      printf("Enter the first number: ");
      scanf("%d", &num1);
    
      printf("Enter the Second number: ");
      scanf("%d", &num2);
    
      ptr1 = &num1;
      ptr2 = &num2;
    
      sum = *ptr1 + *ptr2;
    
      printf("\nSum of two numbers: %d", sum);
    
      return 0;
    }

    Output:

    Enter the first number: 10
    Enter the Second number: 20

    Sum of two numbers: 30


    C program to add two numbers using call by reference

    The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through the topic below.

    #include <stdio.h>
    
    //user-defined function
    float addFunc(float *a, float *b)
    {
      float sum;
    
      sum = *a + *b;
    
      return sum;
    }
    
    int main()
    {
      float num1, num2, result;
    
      printf("Enter the first number: ");
      scanf("%f", &num1);
    
      printf("Enter the Second number: ");
      scanf("%f", &num2);
    
      result = addFunc(&num1, &num2);
    
      printf("Sum of the result: %f", result);
    
      return 0;
    }

    Output:

    Enter the first number: 15.5
    Enter the Second number: 20.5
    Sum of the result: 36.000000


  • Java Program to Print Pascal Triangle

    Java Program to Print Pascal Triangle

    In this tutorial, we will write a program to print pascal triangle in java. Before that, you should have knowledge on the following topic in Java.

    Pascal’s triangle is one of the important examples that is taught to the student. The number outside Pascal’s triangle is zero (0), which is not visible. It is a pattern where the triangle starts from 1 at the top and below is the addition of the upper level as you can see in the figure below. The addition continues until the required level is reached.

    pascal's triangle

    Java Program to Print Pascal Triangle

    //Print Pascal Triangle in java
    
    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int n, i, j, k, num = 1;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        n = sc.nextInt();
    
        for (i = 0; i < n; i++)
        {
          for (k = n; k > i; k--)
          {
            System.out.print(" ");
          }
    
          num = 1;
          for (j = 0; j <= i; j++)
          {
            System.out.print(num + " ");
            num = num *(i - j) / (j + 1);
          }
    
          System.out.println();
        }
      }
    }

    Output:

    pascal triangle in java