Author: admin

  • 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


  • C Program to Convert Hexadecimal to Octal

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

    • C operator
    • C while loop
    • C for loop

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    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.

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


    C Program to Convert Hexadecimal to Octal

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      int deci = 0, oct[30], rem, i = 0, length = 0;
      char hexDec[10];
    
      printf("Enter Hexadecimal number: ");
      scanf("%s", hexDec);
    
      //find the length of the number entered
      while (hexDec[i] != '\0')
      {
        length++;
        i++;
      }
    
      length--;
    
      i = 0;
      while (length >= 0)
      {
        rem = hexDec[length];
    
        if (rem >= 48 && rem <= 57)
          rem -= 48;
        else if (rem >= 65 && rem <= 70)
          rem -= 55;
        else if (rem >= 97 && rem <= 102)
          rem -= 87;
        else
        {
          printf("The hexadecimal number entered is invalid.");
          return 0;	//exit the program
        }
    
        deci += (rem* pow(16, i));
        length--;
        i++;
      }
    
      i = 0;
      while (deci != 0)
      {
        oct[i] = deci % 8;
        i++;
        deci /= 8;
      }
    
      //display
      printf("Equivalent Octal Value: ");
      for (i = i - 1; i >= 0; i--)
        printf("%d", oct[i]);
    
      return 0;
    }

    Output:

    Enter Hexadecimal number: FF
    Equivalent Octal Value: 377


  • C Program to Convert Octal to Hexadecimal

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

    • C operator
    • C while loop
    • C for loop

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    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.

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


    C Program to Convert Octal to Hexadecimal

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      int octalNum, rem, i = 0;
      char HexaDecimal[100];
      int decimalNum = 0, sem = 0;
    
      printf("Enter an Octal number: ");
      scanf("%d", &octalNum);
    
      //Octal to decimal covert
      while (octalNum != 0)
      {
        decimalNum = decimalNum + (octalNum % 10) *pow(8, sem);
        sem++;
        octalNum /= 10;
      }
    
      //Decimal to Hexadecimal
      while (decimalNum != 0)
      {
        rem = decimalNum % 16;
    
        if (rem < 10)
          HexaDecimal[i++] = rem + 48; // 48 Ascii=0
        else
          HexaDecimal[i++] = rem + 55; //55 Ascii=7
    
        decimalNum /= 16;
      }
    
      printf("Equivalent Hexadecimal Value: ");
      int j;
    
      for (j = i - 1; j >= 0; j--)
        printf("%c", HexaDecimal[j]);
    
      return 0;
    }

    Output:

    Enter an Octal number: 377
    Equivalent Hexadecimal Value: FF


  • C Program to Convert Binary to Hexadecimal

    In this tutorial, we will write a C Program to Convert a Binary Number to Hexadecimal Number. Before that, you must have knowledge of the following topics in C.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Input: 11011111
    Output: DF

    Input: 10001101
    Output: 8D

    Now let us go through a program for Binary to hexadecimal conversion in C.


    C Program to Convert Binary to Hexadecimal

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      long int binarynum, hexadecimalnum = 0, i = 1, rem;
    
      printf("Enter a Binary number: ");
      scanf("%ld", &binarynum);
    
      while (binarynum != 0)
      {
        rem = binarynum % 10;
        hexadecimalnum = hexadecimalnum + rem * i;
        i = i * 2;
        binarynum = binarynum / 10;
      }
    
      printf("Equivalent Hexadecimal number: %lX", hexadecimalnum);
    
      getch();
      return 0;
    }

    Output:

    Enter a Binary number: 10001101
    Equivalent Hexadecimal number: 8D


  • C Program to Convert Hexadecimal to Binary

    In this tutorial, we will write a C Program to Convert a Hexadecimal Number to Binary Number. Before that, you must have knowledge of the following topics in C.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Input: DF
    Output: 11011111

    Input: 8D
    Output: 10001101

    Now let us go through a program for hexadecimal to decimal conversion in C.


    C Program to Convert Hexadecimal to Binary

    #include <stdio.h>
    #define MAX 1000
    
    int main()
    {
      char binarynum[MAX], hexadecimalnum[MAX];
      long int i = 0;
    
      printf("Enter a hexadecimal number: ");
      scanf("%s", hexadecimalnum);
    
      printf("Equivalent binary value: ");
      while (hexadecimalnum[i])
      {
        switch (hexadecimalnum[i])
        {
          case '0':
            printf("0000");
            break;
          case '1':
            printf("0001");
            break;
          case '2':
            printf("0010");
            break;
          case '3':
            printf("0011");
            break;
          case '4':
            printf("0100");
            break;
          case '5':
            printf("0101");
            break;
          case '6':
            printf("0110");
            break;
          case '7':
            printf("0111");
            break;
          case '8':
            printf("1000");
            break;
          case '9':
            printf("1001");
            break;
          case 'A':
            printf("1010");
            break;
          case 'B':
            printf("1011");
            break;
          case 'C':
            printf("1100");
            break;
          case 'D':
            printf("1101");
            break;
          case 'E':
            printf("1110");
            break;
          case 'F':
            printf("1111");
            break;
          case 'a':
            printf("1010");
            break;
          case 'b':
            printf("1011");
            break;
          case 'c':
            printf("1100");
            break;
          case 'd':
            printf("1101");
            break;
          case 'e':
            printf("1110");
            break;
          case 'f':
            printf("1111");
            break;
          default:
            printf("Invalid hexadecimal Number.");
            return 0;
        }
    
        i++;
      }
    
      return 0;
    }

    Output:

    Enter a hexadecimal number: 8D
    Equivalent binary value: 10001101


  • C Program to Convert Decimal to Hexadecimal

    This is a C Program to Convert a Decimal Number to its Hexadecimal Equivalent. Before that, you must have knowledge of the following topics in C.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    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.

    Now let us go through a program for decimal to hexadecimal conversion in C.


    C Program to Convert Decimal to Hexadecimal

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      int decimalnum, rem, i = 0;
      char hexanum[50];
    
      printf("Enter a Decimal number: ");
      scanf("%d", &decimalnum);
    
      while (decimalnum != 0)
      {
        rem = decimalnum % 16;
        if (rem < 10)
          rem = rem + 48;
        else
          rem = rem + 55;
        hexanum[i] = rem;
        i++;
        decimalnum /= 16;
      }
    
      printf("Equivalent Hexadecimal Value: ");
      for (i = i - 1; i >= 0; i--)
        printf("%c", hexanum[i]);
    
      getch();
      return 0;
    }

    Output:

    Enter a Decimal number: 125
    Equivalent Hexadecimal Value: 7D


  • C Program to convert Hexadecimal to Decimal

    This is a C Program to Convert a Hexadecimal Number to its decimal Equivalent. Before that, you must have knowledge of the following topics in C.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    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.

    Now let us go through a program for Hexadecimal to decimal conversion in C.


    C Program to convert Hexadecimal to Decimal

    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    int main()
    {
      int decimal = 0, rem, i = 0, len = 0;
      char hexadecimal[20];
    
      printf("Enter a Hexadecimal number: ");
      scanf("%s", hexadecimal);
    
      while (hexadecimal[i] != '\0')
      {
        len++;
        i++;
      }
    
      len--;
      i = 0;
      while (len >= 0)
      {
        rem = hexadecimal[len];
        if (rem >= 48 && rem <= 57)
          rem = rem - 48;
        else if (rem >= 65 && rem <= 70)
          rem = rem - 55;
        else if (rem >= 97 && rem <= 102)
          rem = rem - 87;
    
        decimal = decimal + (rem* pow(16, i));
        len--;
        i++;
      }
    
      printf("Equivalent Decimal Value: %d", decimal);
    
      getch();
      return 0;
    }

    Output:

    Enter a Hexadecimal number: 7D
    Equivalent Decimal Value: 125


  • C Program to Reverse a given Number using Recursive Function

    In this tutorial, we will write a program to reverse an input number using recursion in C. Before that you may go through the following topic in C programming.


    C Program to Reverse a given Number using Recursive Function

    #include <stdio.h>
    
    int recursive_func(int, int);
    
    int main()
    {
      int num, result;
    
      printf("Enter the number: ");
      scanf("%d", &num);
    
      //calling recursive function
      result = recursive_func(num, 0);
    
      printf("Reverse of %d is %d", num, result);
    
      return 0;
    }
    
    int recursive_func(int num, int rev)
    {
      if (num == 0)
        return rev;
      else
        return recursive_func(num/10, rev*10 + num%10);
    }

    Output:

    Enter the number: 789456
    Reverse of 789456 is 654987