Blog

  • 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


  • Hollow Inverted Right Triangle Star Pattern in Java

    Hollow Inverted Right Triangle Star Pattern in Java

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

    We will display the hollow inverted right triangle pattern of stars in java using different loops. We will use for loop and while and write two different codes.

    The programs however take the number of rows as an input from the user and print the hollow pattern. There is a use of if..else statement in the program to check the boolean condition. It is placed inside the for loop and while loop.


    Hollow Inverted Right Triangle Star Pattern in Java

    Using For Loop

    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 = rows; i > 0; i--)
        {
          if (i == 1 || i == rows)
          {
            for (j = 1; j <= i; j++)
              System.out.print("*");
          }
          else
          {
            for (k = 1; k <= i; k++)
            {
              if (k == 1 || k == i)
                System.out.print("*");
              else
                System.out.print(" ");
            }
          }
          System.out.println();
        }
      }
    }

    Output:

    Hollow Inverted Right Triangle Star Pattern in Java

    Using While Loop

    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();
        i = rows;
    
        while (i > 0)
        {
          j = 1;
          while (j <= i)
          {
            if (j == 1 || j == i || i == 1 || i == rows)
              System.out.print("*");
            else
              System.out.print(" ");
            j++;
          }
          System.out.println();
          i--;
        }
      }
    }

    Output:

    Hollow Inverted Right Triangle Star Pattern in Java

  • Reverse an Array in Java using Recursion

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

    Explanation: In this java program we will take user input for the size of an array and array elements. The elements are reversed using a recursive function in the program below.


    Reverse an Array in Java using Recursion

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int size, i;
        int arr[] = new int[50];
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter the size of a first array: ");
        size = scan.nextInt();
    
        System.out.println("Enter " + size + " elements in first Array:");
        for (i = 0; i < size; i++)
          arr[i] = scan.nextInt();
    
        System.out.print("Original array: ");
        for (i = 0; i < size; i++)
          System.out.print(arr[i] + " ");
    
        //calling a function
        reverseFunc(arr, 0, size - 1);
    
        System.out.print("\nReversed array: ");
        for (i = 0; i < size; i++)
          System.out.print(arr[i] + " ");
      }
    
      static void reverseFunc(int arr[], int start, int end)
      {
        int temp;
        if (start >= end)	//base condition
          return;
    
        temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        reverseFunc(arr, start + 1, end - 1);
      }
    }

    Enter the size of a first array: 5
    Enter 5 elements in first Array:
    1
    2
    3
    4
    5
    Original array: 1 2 3 4 5
    Reversed array: 5 4 3 2 1


  • Reverse an Array in Java

    In this java program tutorial, we will write a java program to reverse an array. Before that, you may go through the following topic in java.

    Explanation: In this java program we will take user input for the size of an array and array elements. The elements are reversed using for loop and while loop.

    We will write two programs:

    1. Using for loop
    2. Using while loop

    Java program to reverse an array without using another array

    Source Code: reverse the array using for loop.

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int size, i;
        int arr[] = new int[50];
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter the size of a first array: ");
        size = scan.nextInt();
    
        System.out.println("Enter " + size + " elements in first Array:");
        for (i = 0; i < size; i++)
          arr[i] = scan.nextInt();
    
        System.out.println("Original array: ");
        for (i = 0; i < size; i++)
        {
          System.out.print(arr[i] + " ");
        }
    
        System.out.println("\nArray in reverse order: ");
        for (i = size - 1; i >= 0; i--)
        {
          System.out.print(arr[i] + " ");
        }
      }
    }

    Output:

    Enter the size of a first array: 5
    Enter 5 elements in first Array:
    10
    20
    30
    40
    50
    Original array:
    10 20 30 40 50
    Array in reverse order:
    50 40 30 20 10


    Reverse the Array using while loop

    In this program, we will create a separate user-defined function to reverse the array, and then we will print the reversed array.

    Source Code: reverse an array using a while loop.

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int size, i;
        int arr[] = new int[50];
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter the size of a first array: ");
        size = scan.nextInt();
    
        System.out.println("Enter " + size + " elements in first Array:");
        for (i = 0; i < size; i++)
          arr[i] = scan.nextInt();
    
        System.out.print("Original array: ");
        for (i = 0; i < size; i++)
          System.out.print(arr[i] + " ");
    
        //calling a function
        reverseFunc(arr, 0, size - 1);
    
        System.out.print("\nReversed array: ");
        for (i = 0; i < size; i++)
          System.out.print(arr[i] + " ");
      }
    
      static void reverseFunc(int arr[], int start, int end)
      {
        int temp;
        while (start < end)
        {
          temp = arr[start];
          arr[start] = arr[end];
          arr[end] = temp;
          start++;
          end--;
        }
      }
    }

    Output:

    Enter the size of a first array: 5
    Enter 5 elements in first Array:
    1
    2
    3
    4
    5
    Original array: 1 2 3 4 5
    Reversed array: 5 4 3 2 1