#Post Title #Post Title #Post Title
Saturday, 29 August 2015

C program for hexadecimal to binary conversion

C program for hexadecimal to binary conversion

#include<stdio.h>
#define MAX 1000
int main(){
    char binaryNumber[MAX],hexaDecimal[MAX];
    long int i=0;
    printf("Enter any hexadecimal number: ");
    scanf("%s",hexaDecimal);
    printf("\nEquivalent binary value: ");
    while(hexaDecimal[i]){
         switch(hexaDecimal[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("\nInvalid hexadecimal digit %c ",hexaDecimal[i]); return 0;
         }
         i++;
    }
    return 0;
}
Sample output:
Enter any hexadecimal number: 2AD5
Equivalent binary value: 0010101011010101


Algorithm:

Hexadecimal to binary conversion method:
To convert or change the hexadecimal number to binary number replace the each octal digits by a binary number using hexadecimal to binary chart.
Hexadecimal
Binary
0
0000
1
0001
2
0010
3
0011
4
0100
5
0101
6
0110
7
0111
8
1000
9
1001
A
1010
B
1011
C
1100
D
1101
E
1110
F
1111
Hexadecimal to binary table
Hexadecimal to binary conversion examples:
For example we want to convert or change hexadecimal number 65B2 to binary. For this we will replace each hexadecimal digit to binary values using the above table:
Hexadecimal number:  6    5    B    2
Binary values:     0110 0101 1011 0010
So (65B2)16 = (0110010110110010)2
[ Read More ]

C code to convert octal number to decimal number

C code to convert octal number to decimal number

#include<stdio.h>
#include<math.h>
int main(){
   
    long int octal,decimal =0;
    int i=0;
    printf("Enter any octal number: ");
    scanf("%ld",&octal);
    while(octal!=0){
         decimal = decimal + (octal % 10) * pow(8,i++);
         octal = octal/10;
    }
    printf("Equivalent decimal value: %ld",decimal);
   return 0;
}

Sample output:
Enter any octal number: 346
Equivalent decimal value: 230
C program to change octal to decimal

#include<stdio.h>
int main(){
   
    long int octalNumber;
    printf("Enter any octal number: ");
    scanf("%o",&octalNumber);
    printf("Equivalent decimal number is: %d",octalNumber);
    return 0;
}
Sample output:

Enter any octal number: 17
Equivalent decimal number is: 15


Algorithm:

Octal to decimal conversion method:
To convert an octal number to decimal number multiply each digits separately of octal number from right side by  8^0,8^1,8^2,8^3 … respectively and then find out the sum of them.
Octal to decimal conversion examples:
For example we want to convert octal number 3401 to decimal number.
Step 1: 1 * 8 ^0 = 1*1 =1
Step 2: 0 * 8 ^1 = 0*8 =0
Step 3: 4 * 8 ^2 = 4*64 =256
Step 4: 3 * 8 ^3 = 3*512 =1536
Sum= 1 + 0 + 256 + 1536 = 1793
So, (3401)8 = (1793)10
[ Read More ]

C program to convert binary to octal



C program to convert binary to octal

#include<stdio.h>
#define MAX 1000
int main(){
   
    char octalNumber[MAX];
    long int i=0;
    printf("Enter any octal number: ");
    scanf("%s",octalNumber);
    printf("Equivalent binary value: ");
    while(octalNumber[i]){
        switch(octalNumber[i]){
             case '0': printf("000"); break;
             case '1': printf("001"); break;
             case '2': printf("010"); break;
             case '3': printf("011"); break;
             case '4': printf("100"); break;
             case '5': printf("101"); break;
             case '6': printf("110"); break;
             case '7': printf("111"); break;
             default:  printf("\nInvalid octal digit %c ",octalNumber[i]); return 0;
    }
    i++;
 }
    return 0;
}
Sample output:
Enter any octal number: 123
Equivalent binary value: 001010011


Algorithm:

Octal to binary conversion method:
To convert or change the octal number to binary number replace the each octal digits by a binary number using octal to binary chart.
Octal
Binary
0
000
1
001
2
010
3
011
4
100
5
101
6
110
7
111
Octal to binary table
Octal to binary conversion examples:
For example we want to convert or change octal number 65201 to decimal. For this we will replace each octal digit to binary values using the above table:
Octal number:   6   5   2   0   1
Binary values: 110 101 010 000 001
So (65201)8 = (110101010000001)2
[ Read More ]

decimal to octal converter




C code for decimal to octal converter



#include<stdio.h>
int main(){
  long int decimalNumber,remainder,quotient;
  int octalNumber[100],i=1,j;
  printf("Enter any decimal number: ");
  scanf("%ld",&decimalNumber);
  quotient = decimalNumber;
  while(quotient!=0){
      octalNumber[i++]= quotient % 8;
      quotient = quotient / 8;
  }
  printf("Equivalent octal value of decimal number %d: ",decimalNumber);
  for(j = i -1 ;j> 0;j--)
      printf("%d",octalNumber[j]);
  return 0;
}
Sample output:
Enter any decimal number: 50
Equivalent octal value of decimal number 50: 62
2. Easy way to convert decimal number to octal number in c

#include<stdio.h>
int main(){
  long int decimalNumber;
  printf("Enter any decimal number : ");
  scanf("%d",&decimalNumber);
  printf("Equivalent octal number is: %o",decimalNumber);
  return 0;
}
Sample output:
Enter any decimal number: 25
Equivalent octal number is: 31

[ Read More ]

Type Casting

Type Casting

Assigning a value of one type to a variable of another type is known as Type Casting.
Example :
int x = 10;
byte y = (byte)x;
In Java, type casting is classified into two types,
  • Widening Casting(Implicit)
  • widening-type-conversion
  • Narrowing Casting(Explicitly done)
  • narrowing-type-conversion

Widening or Automatic type converion

Automatic Type casting take place when,
  • the two types are compatible
  • the target type is larger than the source type
Example :
public class Test
{
    public static void main(String[] args)
    {
      int i = 100; 
      long l = i; //no explicit type casting required  
      float f = l; //no explicit type casting required  
      System.out.println("Int value "+i);
      System.out.println("Long value "+l);
      System.out.println("Float value "+f);
    }
    
}
Output :
Int value 100
Long value 100
Float value 100.0

Narrowing or Explicit type conversion

When you are assigning a larger type value to a variable of smaller type, then you need to perform explicit type casting.
Example :
public class Test
{
    public static void main(String[] args)
    {
      double d = 100.04;  
      long l = (long)d;  //explicit type casting required  
      int i = (int)l; //explicit type casting required  
      
      System.out.println("Double value "+d);
      System.out.println("Long value "+l);
      System.out.println("Int value "+i);
     
    }
    
}
Output :
Double value 100.04
Long value 100
Int value 100
[ Read More ]
 
 

Labels