Blog

  • 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

  • Java Program to Print Diamond Pattern

    Java Program to Print Diamond Pattern

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


    Java Program to Print Diamond Pattern

    Source Code: the program asks the user for the input of a number of rows for the diamond star pattern in java. Then using for loop, the diamond shape pattern is displayed on the screen.

    // Print Diamond star Pattern in java
    
    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int n, j, i, space = 1;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter the no. of Rows: ");
        n = scan.nextInt();
    
        space = n - 1;
    
        for (i = 1; i <= n; i++)
        {
          for (j = 1; j <= space; j++)
          {
            System.out.print(" ");
          }
    
          space--;
          for (j = 1; j <= (2 *i - 1); j++)
          {
            System.out.print("*");
          }
    
          System.out.println();
        }
    
        space = 1;
    
        for (i = 1; i <= (n - 1); i++)
        {
          for (j = 1; j <= space; j++)
          {
            System.out.print(" ");
          }
    
          space++;
          for (j = 1; j <= (2 *(n - i) - 1); j++)
          {
            System.out.print("*");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    java diamond pattern of star

  • Java Program to Print Floyd’s Triangle

    Java Program to Print Floyd’s Triangle

    In this tutorial, we will write a java program to display Floyd’s Triangle. Before that, you should have knowledge on the following topic in Java.


    Java Program to Print Floyd’s Triangle

    The program asks the user for the number of rows for the Floyd triangle. Using inner and outer for loop it prints the pattern in java.

    //Print Floyd's Triangle in java
    
    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int range, i, j, k = 1;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        range = scan.nextInt();
    
        System.out.print("Floyd's Triangle:\n");
        for (i = 1; i <= range; i++)
        {
          for (j = 1; j <= i; j++, k++)
          {
            System.out.print(k + " ");
          }
    
          System.out.println();
    
        }
      }
    }

    Output:

    print Floyd’s Triangle in java

  • Java Program to Calculate Grade of Students

    In this tutorial, we will write a java program to calculate the grade of the student. Before that, you should have knowledge on the following topic in java:

    Java Program to calculate and display Student Grades: The program simply takes a user input for the number of subjects and then for the marks obtained on each of those subjects.

    Then the sum of the marks is calculated and then the average of that sum is calculated. The average is calculated by dividing the sum of the marks by the number of subjects. Lastly, using the if-else ladder the grade is displayed on the screen.

    The grade is printed by following grade slab:

    • If marks > 80, Grade is A
    • If marks > 60 and <= 79, Grade is B
    • If marks > 40 and <= 59, Grade is C
    • Else Grade is D

    You can change the grades according to your need and your question. We will perform two different programs:

    1. Using if else ladder
    2. Using Switch Case

    Java program to find grade of a student using if else ladder

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int marks[] = new int[5];
        int i, subjects;
        float sum = 0, avg;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the number of subjects: ");
        subjects = sc.nextInt();
    
        System.out.println("Enter Marks for " + subjects + " Subjects: ");
        for (i = 0; i < subjects; i++)
        {
          marks[i] = sc.nextInt();
          sum = sum + marks[i];
        }
    
        avg = sum / subjects;
    
        System.out.print("Your Grade: ");
        if (avg > 80)
        {
          System.out.print("A");
        }
        else if (avg > 60 && avg <= 79)
        {
          System.out.print("B");
        }
        else if (avg > 40 && avg <= 59)
        {
          System.out.print("C");
        }
        else
        {
          System.out.print("D");
        }
      }
    }

    Output:

    Enter the number of subjects: 5
    Enter Marks for 5 Subjects:
    55
    84
    72
    67
    90
    Your Grade: B


    Java Program to find grade of a student using switch case

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int subjects, i;
        float sum = 0, percent;
        Scanner sc = new Scanner(System.in);
    
        System.out.println("Enter the Number of Subjects: ");
        subjects = sc.nextInt();
    
        System.out.println("Enter Marks for " + subjects + " Subjects:");
        for (i = 0; i < subjects; i++)
        {
          sum += sc.nextInt();
        }
    
        percent = (sum / (subjects *100)) *100;
        System.out.println("Percentage Obtained: " + percent);
    
        switch ((int) percent / 10)
        {
          case 9:
            System.out.println("Your Grade: A+");
            break;
          case 8:
            System.out.println("Your Grade: A");
            break;
          case 7:
            System.out.println("Your Grade: B");
            break;
          case 6:
            System.out.println("Your Grade: B+");
            break;
          case 5:
            System.out.println("Your Grade: C");
            break;
          default:
            System.out.println("Your Grade: D");
            break;
        }
      }
    }

    Enter the Number of Subjects:
    4
    Enter Marks for 4 Subjects:
    84
    79
    92
    85
    Percentage Obtained: 85.0
    Your Grade: A


  • C Program to Convert Octal to Decimal

    In this tutorial, we will write a program to convert octal to decimal in C using whille loop. Before that, you must have knowledge of the following topics in C.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Example:

    Input: 400
    Output: 256

    Let us go through a program for the Octal to Decimal Conversion in C. we will perform two different C programs.

    1. Basic program
    2. Using user-defined function

    C Program to Convert Octal to Decimal

    Source code:

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      int octalNum, decimalNum = 0, i = 0;
    
      printf("Enter an octal number: ");
      scanf("%d", &octalNum);
    
      while (octalNum != 0)
      {
        decimalNum = decimalNum + (octalNum % 10) *pow(8, i++);
        octalNum = octalNum / 10;
      }
    
      printf("Equivalent Decimal Value: %d", decimalNum);
    
      return 0;
    }

    Output:

    Enter an octal number: 400
    Equivalent Decimal Value: 256


    Using Function

    Source code: convert octal to decimal using function in C.

    #include <stdio.h>
    #include <math.h>
    
    int conversionFunc(int oct)
    {
      int i = 0, decimalNum = 0, rem;
    
      while (oct > 0)
      {
        rem = oct % 10;
        oct /= 10;
    
        decimalNum = decimalNum + rem* pow(8, i);
        ++i;
      }
    
      return decimalNum;
    }
    
    int main()
    {
      int octNum;
    
      printf("Enter an Octal number: ");
      scanf("%d", &octNum);
    
      printf("Equivalent Decimal Value: %d", conversionFunc(octNum));
    
      return 0;
    }

    Output: After the successful execution of the program, it will produce the same out as the above program.


  • C Program to Convert Decimal to Octal using Recursion and Loop

    In this tutorial, we will write a program to convert decimal to octal in C. Before that, you must have knowledge of the following topics in C.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Example:

    Input: 256
    Output: 400

    Let us go through a program for the Decimal to Octal Conversion in C.


    1. Using While loop

    Here, we will write a C program to convert decimal number to octal number using while loop. The program simply takes a decimal number as an input from the user and after calculation gives an octal output.

    #include <stdio.h>
    
    int main()
    {
      int deciNum, octalNum = 0, rem, i = 1;
    
      printf("Enter a Decimal number: ");
      scanf("%d", &deciNum);
    
      while (deciNum != 0)
      {
        rem = deciNum % 8;
        octalNum = (rem *i) + octalNum;
        deciNum /= 8;
    
        i *= 10;
      }
    
      printf("Equivalent Octal Value: %d", octalNum);
    
      return 0;
    }

    Output:

    Enter a Decimal number: 256
    Equivalent Octal Value: 400


    2. Using Recursion

    Here, we will convert a decimal number to octal number using recursion in C. Learn more on Recursion and its use.

    #include <stdio.h>
    
    void conversionFunc(int); //prototype
    
    int main()
    {
      int deciNum;
    
      printf("Enter a Decimal number: ");
      scanf("%d", &deciNum);
    
      printf("Equivalent Octal Value: ");
      conversionFunc(deciNum);
    
      return 0;
    }
    
    //recursive function
    void conversionFunc(int dec)
    {
      if (dec > 0)
      {
        conversionFunc(dec / 8);
        printf("%d", dec % 8);
      }
    }

    Output: After the successful execution of the program, it will produce the same out as the above program.