Blog

  • Java String Pattern: 2

    Java String Pattern: 2

    In this tutorial, we will go through the string pattern program in java. Before that, you may go through the following topic in java.

    String Pattern:

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        String str;
        int i, j;
        char[] ch;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter a Word: ");
        str = sc.nextLine();
    
        ch = str.toCharArray();
        int strLength = ch.length;
    
        for (i = 0; i < strLength; i++)
        {
          for (j = 1; j <= strLength; j++)
          {
            System.out.print(str.charAt(strLength - j) + " ");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    Enter a Word: SIMPLE
    E L P M I S
    E L P M I S
    E L P M I S
    E L P M I S
    E L P M I S
    E L P M I S


  • Java String Pattern: 1

    Java String Pattern: 1

    In this tutorial, we will go through the string pattern program in java. Before that, you may go through the following topic in java.

    String Pattern:

    public class Main
    {
      public static void main(String args[])
      {
        String str = "SIMPLE";
        int length = str.length();	//length of the string
    
        for (int i = 0; i < length; i++)
        {
          for (int j = 0; j < length; j++)
          {
            System.out.print(str.charAt(j) + " ");
          }
    
          System.out.println();
        }
      }
    }

    Output:

    S I M P L E
    S I M P L E
    S I M P L E
    S I M P L E
    S I M P L E
    S I M P L E


  • Algorithm and Flowchart to find Fibonacci series

    In this tutorial, we will write an algorithm to find the Fibonacci series, we will also learn to draw the flowchart to find the Fibonacci series for a number. You may go through the following topic first.

    Let us first understand what is fibonacci series.

    Fibonacci series is the series of numbers where the next number is achieved by the addition of the previous two numbers. The initial addition of two numbers is 0 and 1. For example: 0,1,1,2,3,5,8,13….etc.

    Let us start with a flowchart of a fibonacci series.

    Flowchart to calculate the fibonacci series

    flowchart of a Fibonacci series

    The above flowchart diagram is to find the Fibonacci series.


    Algorithm of Fibonacci Series

    Step 1: START
    Step 2: Declare variable n1, n2, sum, n, i
    Step 2: Initialize variables:
    n1 = 0, n2 = 1, i = 2
    Step 3:  Read n
    Step 4: Repeat this step until i <= n:
    sum = n1 + n2
    print sum
    n1 = n2
    n2 = sum
    i = i + 1
    Step 5: STOP

    This is the algorithm and flowchart of fibonacci series. If you want to understand through code, you may follow the below link:


  • Currency Conversion Program in C

    In this tutorial, we will write a C program for currency conversion. Before that, you may go through the following topic in C.

    Explanation: In this program, there is a use of switch statements in C. The program gives the user choice for the conversion. For this example, we have taken four currencies those are Rupee, Dollar, Pound and Euro.

    There are four cases for each currency conversion. You can modify the value of a currency with the actual current value.


    Currency Conversion Program in C

    #include <stdio.h>
    
    int main()
    {
      float amount;
      float rupee, dollar, pound, euro;
      int choice;
    
      printf("Following are the Choices:");
      printf("\nEnter 1: Ruppe");
      printf("\nEnter 2: Dollar");
      printf("\nEnter 3: Pound");
      printf("\nEnter 4: Euro");
    
      printf("\nEnter your choice: ");
      scanf("%d", &choice);
    
      printf("Enter the amount you want to convert?\n");
      scanf("%f", &amount);
    
      switch (choice)
      {
        case 1: // Ruppe Conversion
            dollar = amount / 70;
            printf("%.2f Rupee =  %.2f dollar", amount, dollar);
    
            pound = amount / 88;
            printf("\n%.2f Rupee =  %.2f pound", amount, pound);
    
            euro = amount / 80;
            printf("\n%.2f Rupee =  %.2f euro", amount, euro);
            break;
    
        case 2: // Dollar Conversion
            rupee = amount * 70;
            printf("\n%.2f Dollar =  %.2f rupee", amount, rupee);
    
            pound = amount *0.78;
            printf("\n%.2f Dollar =  %.2f pound", amount, pound);
    
            euro = amount *0.87;
            printf("\n%.2f Dollar =  %.2f euro", amount, euro);
            break;
    
        case 3: // Pound Conversion
            rupee = amount * 88;
            printf("\n%.2f Pound =  %.2f rupee", amount, rupee);
    
            dollar = amount *1.26;
            printf("\n%.2f Pound =  %.2f dollar", amount, dollar);
    
            euro = amount *1.10;
            printf("\n%.2f Pound =  %.2f euro", amount, euro);
            break;
    
        case 4: // Euro Conversion
            rupee = amount * 80;
            printf("\n%.2f Euro =  %.2f rupee", amount, rupee);
    
            dollar = amount *1.14;
            printf("\n%.2f Euro =  %.2f dollar", amount, dollar);
    
            pound = amount *0.90;
            printf("\n.2%f Euro =  %.2f pound", amount, pound);
            break;
    
         //Default case
        default:
            printf("\nInvalid Input");
      }
    
      return 0;
    }

    Output:

    Following are the Choices:
    Enter 1: Ruppe
    Enter 2: Dollar
    Enter 3: Pound
    Enter 4: Euro
    Enter your choice: 2
    Enter the amount you want to convert?
    7

    7.00 Dollar = 490.00 rupee
    7.00 Dollar = 5.46 pound
    7.00 Dollar = 6.09 euro

    There is a use of %.2f” which means only two digits after the decimal value will be displayed.


  • C Program to Find the Area of a Rectangle

    In this tutorial, we will write a C program to find the area of a rectangle. Before that, you may go through the following topic in C.

    Explanation: The program asks the user to enter the value of length and breadth as an input. Then simply using the formula (area = length * breadth), we calculate the area of a rectangle in C.


    C Program to find the area of a Rectangle

    #include <stdio.h>
    
    int main()
    {
      int len, br, area;
    
      printf("Enter the length: ");
      scanf("%d", &len);
      printf("Enter the breadth: ");
      scanf("%d", &br);
    
      area = len * br;
      printf("Area of a rectangle: %d", area);
    
      return 0;
    }

    Output:

    Enter the length: 2
    Enter the breadth: 5
    Area of a rectangle: 10

    You may change the data type int to float or any other as per your requirements.


  • Half Repeated Character Pattern in Java

    Half Repeated Character Pattern in Java

    In this tutorial, we will go through alphabet pattern programs in java programming. Before that, you may go through the following topic in java.


    Half Repeated Character Pattern in Java

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

    Output:

    Enter the no. of rows: 5
    ABCDEF
     BCDEF
      CDEF
       DEF
        EF
         F
         F
        EF
       DEF
      CDEF
     BCDEF
    ABCDEF

  • 2 Different Alphabet Patterns in Java (half hourglass)

    2 Different Alphabet Patterns in Java (half hourglass)

    In this tutorial, we will go through two different alphabet pattern programs in java programming. Before that, you may go through the following topic in java.


    2 different Alphabet pattern program in Java

    Both of the programs below take the number of rows as the user input and using for loop it prints the desired output.

    Pattern 1:

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int rows, i, j;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        rows = sc.nextInt();
    
        //upper part
        for (i = rows; i >= 0; i--)
        {
          int character = 65;
          for (j = 0; j <= i; j++)
            System.out.print((char)(character + j) + " ");
    
          System.out.println();
        }
        //lower part
        for (i = 0; i <= rows; i++)
        {
          int character = 65;
          for (j = 0; j <= i; j++)
            System.out.print((char)(character + j) + " ");
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the no. of rows: 5
    A B C D E F
    A B C D E
    A B C D
    A B C
    A B
    A
    A
    A B
    A B C
    A B C D
    A B C D E
    A B C D E F


    Pattern 2:

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int rows, i, j;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter the no. of rows: ");
        rows = sc.nextInt();
    
        //upper part
        for (i = rows; i >= 0; i--)
        {
          int character = 65;
          for (j = i; j >= 0; j--)
            System.out.print((char)(character + j) + " ");
    
          System.out.println();
        }
        //lower part
        for (i = 0; i <= rows; i++)
        {
          int character = 65;
          for (j = i; j >= 0; j--)
            System.out.print((char)(character + j) + " ");
    
          System.out.println();
        }
      }
    }

    Output:

    Enter the no. of rows: 5
    F E D C B A
    E D C B A
    D C B A
    C B A
    B A
    A
    A
    B A
    C B A
    D C B A
    E D C B A
    F E D C B A


  • Half Hourglass Star Pattern in Java

    Half Hourglass Star Pattern in Java

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

    The program below takes a user input for the number of rows needed and then in decreasing order of the first half is printed and then the increasing order of the second half of the half part of sandglass is printed.

    This is a half hourglass pattern program in java or you can say half sandglass pattern program using a star.


    Half Hourglass Star Pattern in Java

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

    Output:

    java half hourglass pattern

  • Menu Driven Program using Switch Case in C

    In this tutorial, we will write a menu-driven program in C. We will use the switch case statement present in C to create menus or options. Before that, you may go through the following topic in C programming.

    C Menu-driven Program using switch case:

    This is a menu program in C that displays a menu and takes the input from the user. The choice made by the user is among the option displayed in the menu. And accordingly, the output is displayed on the screen. It is a user-friendly program in C.


    Menu Driven Program using Switch Case in C

    #include <stdio.h>
    
    int main()
    {
      int choice;
    
      printf("To find the areas of following press:");
      printf("\n1 for the Area of a Circle");
      printf("\n2 for the Area of a Square");
      printf("\n3 for the Area of a Right Angled Triangle");
      printf("\n4 for the Area of a Rectangle");
      printf("\n5: Area of Rhombus");
      printf("\nEnter your Choice: ");
      scanf("%d", &choice);
    
      switch (choice)
      {
        case 1:
          printf("Enter radius for the circle:");
          float r, area;
          scanf("%f", &r);
          area = 3.14f *r * r;
          printf("Area: %f", area);
          break;
    
        case 2:
          printf("Enter side for square:");
          int s;
          scanf("%d", &s);
          int ae = s * s;
          printf("Area: %d", ae);
          break;
    
        case 3:
          printf("Enter height and base for traingle:\n");
          float h, bs;
          scanf("%f", &h);
          scanf("%f", &bs);
          float ar = 0.5f *h * bs;
          printf("Area: %f", ar);
          break;
    
        case 4:
          printf("Enter length and breadth for rectangle:\n");
          int l, b;
          scanf("%d", &l);
          scanf("%d", &b);
          int aa = l * b;
          printf("Area: %d", aa);
          break;
    
        case 5:
          printf("Enter the first diagonal of a Rhombus: ");
          float diagonal1, diagonal2;
          scanf("%f", &diagonal1);
          printf("Enter the second diagonal of the Rhombus: ");
          scanf("%f", &diagonal2);
          float aRhombus = (diagonal1 *diagonal2) / 2;
          printf("Area of the Rhombus is: %f", aRhombus);
          break;
    
        default:
          printf("Invalid Input");
          break;
      }
    
      return 0;
    }

    Output: 1

    To find the areas of following press:
    1 for the Area of a Circle
    2 for the Area of a Square
    3 for the Area of a Right Angled Triangle
    4 for the Area of a Rectangle
    5: Area of Rhombus
    Enter your Choice: 1
    Enter radius for the circle:2
    Area: 12.560000

    Output: 2

    To find the areas of following press:
    1 for the Area of a Circle
    2 for the Area of a Square
    3 for the Area of a Right Angled Triangle
    4 for the Area of a Rectangle
    5: Area of Rhombus
    Enter your Choice: 4
    Enter length and breadth for rectangle:
    5
    4
    Area: 20

    Similarly, you can create a calculator or any other program that serves a menu of options to choose from. You can use if..else statement instead of switch case.


  • C Program to check whether a Number is Binary or Not

    In this tutorial, we will write a program to check for the binary number in C.

    What is a Binary Number?

    A binary number is a number expressed in the base-2 numeral system that is the number that contains 2 symbols to represent all the numbers. The symbols are 0 and 1.

    For example: 101011, 110011110, 10001111 are binary numbers.


    C Program to check whether a Number is Binary or Not

    We have used the while loop to iterate through the entered number in the program below. The program takes the input of a number from the user.

    With each iteration, there is an if condition to check for 1 and 0 in the digit, If it is not 1 or 0 then it is not a binary number.

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      int i, number;
    
      printf("Enter the Number: ");
      scanf("%d", &number);
    
      int temp = number;
    
      while (number > 0)
      {
        i = number % 10;
    
        if (i != 0 && i != 1)
        {
          printf("%d is not a binary number", temp);
          break;
        }
    
        number /= 10;
    
        if (number == 0)
        {
          printf("%d is a binary number", temp);
        }
      }
    
      getch();
      return 0;
    }

    Output:

    Enter the Number: 1101
    1101 is a binary number