Blog

  • C Program to Convert Decimal to Binary

    In this tutorial, we will write a C program to convert decimal to binary. 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.

    Decimal Number

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

    Here is the chart where for the equivalent value of decimal and binary numbers.

    Decimal to binary

    Example:

    Input: 5
    Output: 101

    Input: 15
    Output: 1111

    Now let us go through a program for the conversion of decimal to binary in C using while loop.


    C Program to Convert Decimal to Binary using While loop

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      int deciNum, binaryNum[30], i = 0;
    
      printf("Enter a decimal number: ");
      scanf("%d", &deciNum);
    
      while (deciNum != 0)
      {
        binaryNum[i] = deciNum % 2;
        i++;
        deciNum = deciNum / 2;
      }
    
      printf("Equivalent Binary Value: ");
      for (i = (i - 1); i >= 0; i--)
        printf("%d", binaryNum[i]);
    
      getch();
      return 0;
    }

    Output:

    Enter a decimal number: 13
    Equivalent Binary Number: 1101


    C Program to Convert Decimal to Binary without using Array

    Source code: Decimal to Binary without using Array in C.

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      int deciNum, binaryNum = 0, mul = 1, rem;
    
      printf("Enter any decimal number: ");
      scanf("%d", &deciNum);
    
      while (deciNum > 0)
      {
        rem = deciNum % 2;
        binaryNum = binaryNum + (rem *mul);
        mul = mul * 10;
        deciNum = deciNum / 2;
      }
    
      printf("Equivalent Binary Value: %d", binaryNum);
    
      getch();
      return 0;
    }

    After the successful execution of the above program, it will produce the same output as the above program.


  • C Program to Convert Binary to Decimal

    In this tutorial, we will write a program to convert binary to decimal in C. 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.

    Decimal Number

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

    Here is the chart where for the equivalent value of decimal and binary numbers.

    Decimal to binary

    Example:

    Input: 101
    Output: 5

    Input: 111
    Output: 7

    Now let us go through a program for the conversion of binary to decimal in C using while loop.


    C Program to Convert Binary to Decimal using while loop

    In this program a math function pow() is used, it gives the power calculation and a header file math.h is required to add the beginning of the program.

    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    int main()
    {
      int binaryNum, deciNum = 0, i = 0, rem;
    
      printf("Enter a binary number: ");
      scanf("%d", &binaryNum);
    
      while (binaryNum != 0)
      {
        rem = binaryNum % 10;
        deciNum = deciNum + rem* pow(2, i);
        i++;
        binaryNum = binaryNum / 10;
      }
    
      printf("Equivalent Decimal Value: %d", deciNum);
    
      getch();
      return 0;
    }

    Output:

    Enter a binary number: 1111
    Equivalent Decimal Value: 15


    Convert Binary to Decimal in C without pow() Function

    Although if you do not want to use the pow() function then you simply replace the while loop of the above program with the following while loop code and others remain the same.

    while (binnum != 0)
    {
      rem = binnum % 10;
      decnum = decnum + (rem *i);
      i = i * 2;
      binnum = binnum / 10;
    }

    It will produce the same output as the above. Also, you do not need to include math.h header file.


  • C Program to Print ASCII Value of a Character

    In this tutorial, we will write a program to print ASCII value of a character in C programming. Before that, you should have knowledge on the following topic in C.

    ASCII stands for American Standard Code for Information Interchange. It is a 7-bit character set that contains 128 (0 to 127) characters. It represents the numerical value of a character.

    Example: ASCII value for character A is 65, B is 66 and a is 97, b is 98, and so on.


    C Program to Print ASCII Value of a Character

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      char ch;
      printf("Enter a Character: ");
      scanf("%c", &ch);
    
      printf("Its ASCII Value: %d", ch);
    
      getch();
      return 0;
    }

    Output:

    Enter a Character: A
    Its ASCII Value: 65


    C Program to print ASCII value of all Characters

    Source Code: Print the ASCII value of all the characters in C.

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      int i;
      printf("ASCII Value of all the Characters.\n");
    
      for (i = 0; i < 255; i++)
        printf("ASCII value of %c: %i\n", i, i);
    
      getch();
      return 0;
    }

    Output: You will get the list of all the characters starting from 0 to 255 on the output display screen.

    ASCII value of character in C

  • Java Program to Convert Binary to Hexadecimal

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

    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 java.


    Java Program to Convert Binary to Hexadecimal using While loop

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int[] hexaDecnum = new int[1000];
        int i = 1, j = 0, rem, decNum = 0, binary;
    
        Scanner in = new Scanner(System.in);
    
        System.out.print("Enter a Binary Number: ");
        binary = in .nextInt();
    
        while (binary > 0)
        {
          rem = binary % 2;
          decNum = decNum + rem * i;
          i = i * 2;
          binary /= 10;
        }
    
        i = 0;
    
        while (decNum != 0)
        {
          hexaDecnum[i] = decNum % 16;
          decNum = decNum / 16;
          i++;
        }
    
        System.out.print("Equivalent Hexadecimal value: ");
        for (j = i - 1; j >= 0; j--)
        {
          if (hexaDecnum[j] > 9)
            System.out.print((char)(hexaDecnum[j] + 55) + "\n");
          else
            System.out.print(hexaDecnum[j] + "\n");
        }
      }
    }

    Output:

    Enter a Binary Number: 1010
    Equivalent Hexadecimal value: A


  • Java Program to Convert Hexadecimal to Binary

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

    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.

    Now let us go through a program for hexadecimal to binary conversion in java.


    Java Program to Convert Hexadecimal to Binary using While loop

    import java.util.Scanner;
    
    public class Main
    {
      public static int conversionFunc(String hexa)
      {
        String digits = "0123456789ABCDEF";
        hexa = hexa.toUpperCase();
        int result = 0;
        for (int i = 0; i < hexa.length(); i++)
        {
          char c = hexa.charAt(i);
          int d = digits.indexOf(c);
          result = 16 *result + d;
        }
    
        return result;
      }
    
      public static void main(String args[])
      {
        String hexDecNum;
        int decnum, i = 1, j;
        int binnum[] = new int[100];
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter Hexadecimal Number: ");
        hexDecNum = scan.nextLine();
    
        //hexadecimal to decimal
        decnum = conversionFunc(hexDecNum);
    
        //decimal to binary
        while (decnum != 0)
        {
          binnum[i++] = decnum % 2;
          decnum = decnum / 2;
        }
    
        System.out.print("Equivalent Binary Value: ");
        for (j = i - 1; j > 0; j--)
        {
          System.out.print(binnum[j]);
        }
      }
    }

    Output:

    Enter Hexadecimal Number: AD
    Equivalent Binary Value: 10101101


  • Java Program to Convert Hexadecimal to Decimal

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

    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 java.


    Java Program to Convert Hexadecimal to Decimal using for loop

    import java.util.Scanner;
    
    public class Main
    {
      public static int conversionFunc(String hexa)
      {
        String digits = "0123456789ABCDEF";
        hexa = hexa.toUpperCase();
        int val = 0;
    
        for (int i = 0; i < hexa.length(); i++)
        {
          char c = hexa.charAt(i);
          int d = digits.indexOf(c);
          val = 16 *val + d;
        }
    
        return val;
      }
    
      public static void main(String args[])
      {
        String hexadeci;
        int decimal;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter a Hexadecimal Number: ");
        hexadeci = scan.nextLine();
    
        decimal = conversionFunc(hexadeci);
    
        System.out.print("Equivalent Decimal Value: " + decimal);
      }
    }

    Output:

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


  • Java Program to Convert Decimal to Hexadecimal

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

    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 java.


    Java Program to Convert Decimal to Hexadecimal using while loop

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int decimal, rem;
        String hexdeci = "";
    
        char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter a Decimal Number: ");
        decimal = scan.nextInt();
    
        while (decimal > 0)
        {
          rem = decimal % 16;
          hexdeci = hex[rem] + hexdeci;
          decimal /= 16;
        }
    
        System.out.print("Equivalent Hexadecimal Value: " + hexdeci);
      }
    }

    Output:

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


  • Java Program to Convert Decimal to Hexadecimal using Recursion

    In this tutorial, we will write a Java Program for the conversion of decimal to hexadecimal using recursion. Before that, you must have knowledge of the following topic in Java.

    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 java using recursion.


    Java Program to Convert Decimal to Hexadecimal using Recursion

    import java.util.Scanner;
    
    public class Main
    {
      char[] charHexa = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
      int num;
      String hexaDeci = "";
    
      public static void main(String[] args)
      {
        Main obj = new Main();
        int decimal;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter decimal number: ");
        decimal = sc.nextInt();
    
        String hex = obj.hexadecimal(decimal);
        System.out.println("Equivalent Hexadecimal Value: " + hex);
    
      }
    
      String hexadecimal(int deci)
      {
        if (deci != 0)
        {
          num = deci % 16;
          hexaDeci = charHexa[num] + hexaDeci;
          deci /= 16;
          hexadecimal(deci);   //recursion call
        }
    
        return hexaDeci;
      }
    }

    Output:

    decimal to hexadecimal in java using recursion

  • Java Program to Convert Octal Number to Decimal Number

    In this tutorial, we will write a program to convert octal to decimal in java. 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.

    Conversion example:

    Input: 23
    Output: 19

    Let us go through a program for the octal to decimal Conversion in java.


    Java Program to Convert Octal to Decimal using while loop

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int octalNum, decimalNumber = 0, i = 0;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter an Octal Number: ");
        octalNum = scan.nextInt();
    
        while (octalNum != 0)
        {
          decimalNumber += (octalNum % 10) *Math.pow(8, i);
          ++i;
          octalNum /= 10;
        }
    
        System.out.printf("Equivalent Decimal Number: %d", decimalNumber);
      }
    }

    Output:

    Enter an Octal Number: 23
    Equivalent Decimal Number: 19


  • Java Program to Convert Decimal Number to Octal Number

    In this tutorial, we will write a program to convert decimal to octal in java. 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.

    Conversion example:

    Input: 19
    Output: 23

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


    Java Program to Convert Decimal to Octal using while loop

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int decNum, octalNumber = 0, i = 1;
        Scanner scan = new Scanner(System.in);
    
        System.out.print("Enter a Decimal Number: ");
        decNum = scan.nextInt();
    
        while (decNum != 0)
        {
          octalNumber += (decNum % 8) *i;
          decNum /= 8;
          i *= 10;
        }
    
        System.out.printf("Equivalent Octal value: %d", octalNumber);
      }
    }

    Output:

    Enter a Decimal Number: 19
    Equivalent Octal value: 23