Tuesday, 25 February 2014

4.conversion(Number systems)

//Decimal to hexadecimal conversion in c
#include<stdio.h>
int main(){
    long int decimalNumber,remainder,quotient;
    int i=1,j,temp;
    char hexadecimalNumber[100];
    printf("Enter any decimal number: ");
    scanf("%ld",&decimalNumber);
    quotient = decimalNumber;
    while(quotient!=0){
         temp = quotient % 16;
      //To convert integer into character
      if( temp < 10)
           temp =temp + 48;
      else
         temp = temp + 55;
      hexadecimalNumber[i++]= temp;
      quotient = quotient / 16;
  }
    printf("Equivalent hexadecimal value of decimal number %d: ",decimalNumber);
    for(j = i -1 ;j> 0;j--)
      printf("%c",hexadecimalNumber[j]);
    return 0;
}
Sample output:
Enter any decimal number: 45
Equivalent hexadecimal value of decimal number 45: 2D
//Octal to binary conversion in c
#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
//Octal to decimal conversion in c
#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
//Hexadecimal to binary conversion in c
#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
//Binary to octal conversion in c
#include<stdio.h>
int main(){

    long int binaryNumber,octalNumber=0,j=1,remainder;
    printf("Enter any number any binary number: ");
    scanf("%ld",&binaryNumber);
    while(binaryNumber!=0){
         remainder=binaryNumber%10;
        octalNumber=octalNumber+remainder*j;
        j=j*2;
        binaryNumber=binaryNumber/10;
    }
    printf("Equivalent octal value: %lo",octalNumber);
    return 0;
}
Sample output:
Enter any number any binary number: 1101
Equivalent hexadecimal value: 15
//Write a c program to convert binary number to decimal number
#include<stdio.h>
int main(){

    long int binaryNumber,decimalNumber=0,j=1,remainder;
    printf("Enter any number any binary number: ");
    scanf("%ld",&binaryNumber);
    while(binaryNumber!=0){
         remainder=binaryNumber%10;
        decimalNumber=decimalNumber+remainder*j;
        j=j*2;
        binaryNumber=binaryNumber/10;
    }
    printf("Equivalent decimal value: %ld",decimalNumber);
    return 0;
}
Sample output:
Enter any number any binary number: 1101
Equivalent decimal value: 13
//Binary to hexadecimal conversion in c
#include<stdio.h>
int main(){

    long int binaryNumber,hexadecimalNumber=0,j=1,remainder;

    printf("Enter any number any binary number: ");
    scanf("%ld",&binaryNumber);

    while(binaryNumber!=0){
    remainder=binaryNumber%10;
    hexadecimalNumber=hexadecimalNumber+remainder*j;
        j=j*2;
        binaryNumber=binaryNumber/10;
      }

    printf("Equivalent hexadecimal value: %lX",hexadecimalNumber);
    return 0;
}
Sample output:
Enter any number any binary number: 1101
Equivalent hexadecimal value: D
//C program for addition of binary numbers
#include<stdio.h>
int main(){
    long int binary1,binary2;
    int i=0,remainder = 0,sum[20];
    printf("Enter any first binary number: ");
    scanf("%ld",&binary1);
    printf("Enter any second binary number: ");
    scanf("%ld",&binary2);
    while(binary1!=0||binary2!=0){
         sum[i++] =  (binary1 %10 + binary2 %10 + remainder ) % 2;
         remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
         binary1 = binary1/10;
         binary2 = binary2/10;
    }
    if(remainder!=0)
         sum[i++] = remainder;
    --i;
    printf("Sum of two binary numbers: ");
    while(i>=0)
         printf("%d",sum[i--]);
   return 0;
}
Sample output:
Enter any first binary number: 1100011
Enter any second binary number: 1101
Sum of two binary numbers: 1110000
//C program for multiplication of two binary numbers
#include<stdio.h>
int binaryAddition(int,int);
int main(){
    long int binary1,binary2,multiply=0;
    int digit,factor=1;
    printf("Enter any first binary number: ");
    scanf("%ld",&binary1);
    printf("Enter any second binary number: ");
    scanf("%ld",&binary2);
    while(binary2!=0){
         digit =  binary2 %10;
         if(digit ==1){
                 binary1=binary1*factor;
                 multiply = binaryAddition(binary1,multiply);
         }
         else
             binary1=binary1*factor;

         binary2 = binary2/10;
         factor = 10;
    }
    printf("Product of two binary numbers: %ld",multiply);

   return 0;
}
int binaryAddition(int binary1,int binary2){
    int i=0,remainder = 0,sum[20];
    int binarySum=0;
    while(binary1!=0||binary2!=0){
         sum[i++] =  (binary1 %10 + binary2 %10 + remainder ) % 2;
         remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
         binary1 = binary1/10;
         binary2 = binary2/10;
    }
    if(remainder!=0)
         sum[i++] = remainder;
    --i;
    while(i>=0)
         binarySum = binarySum*10 + sum[i--];
    return binarySum;
}
Sample output:
Enter any first binary number: 1101
Enter any second binary number: 11
Product of two binary numbers: 100111
//C program fractional binary conversion from decimal
#include<stdio.h>
#define MAX 1000
int main(){
    long double fraDecimal=0.0,dFractional=0.0 ,fraFactor=0.5;
    long int dIntegral = 0,bIntegral=0,bFractional[MAX];
    long int intFactor=1,remainder,i=0,k=0,flag=0;
    char fraBinary[MAX];
    printf("Enter any fractional binary number: ");
    scanf("%s",&fraBinary);

    while(fraBinary[i]){
     
         if(fraBinary[i] == '.')
             flag = 1;
         else if(flag==0)
             bIntegral = bIntegral * 10 + (fraBinary[i] -48);
         else
              bFractional[k++] = fraBinary[i] -48;
         i++;
    }

    while(bIntegral!=0){
        remainder=bIntegral%10;
        dIntegral= dIntegral+remainder*intFactor;
        intFactor=intFactor*2;
        bIntegral=bIntegral/10;
    }

    for(i=0;i<k;i++){
         dFractional  = dFractional  + bFractional[i] * fraFactor;
         fraFactor = fraFactor / 2;
    }
    fraDecimal = dIntegral + dFractional ;
    printf("Equivalent decimal value: %Lf",fraDecimal);

    return 0;
}
Sample output:
Enter any fractional binary number: 11.11
Equivalent decimal value: 3.750000
//C program for fractional decimal to binary fraction conversion
#include<stdio.h>
int main(){

    long double fraDecimal,fraBinary,bFractional = 0.0,dFractional,fraFactor=0.1;
    long int dIntegral,bIntegral=0;
    long int intFactor=1,remainder,temp,i;
    printf("Enter any fractional decimal number: ");
    scanf("%Lf",&fraDecimal);

    dIntegral = fraDecimal;
    dFractional =  fraDecimal - dIntegral;
    while(dIntegral!=0){
         remainder=dIntegral%2;
         bIntegral=bIntegral+remainder*intFactor;
         dIntegral=dIntegral/2;
         intFactor=intFactor*10;
    }
   for(i=1;i<=6;i++){
   
       dFractional = dFractional * 2;
       temp =  dFractional;
     
       bFractional = bFractional + fraFactor* temp;
       if(temp ==1)
             dFractional = dFractional - temp;
       fraFactor=fraFactor/10;
   }

   fraBinary =  bIntegral +  bFractional;
   printf("Equivalent binary value: %lf",fraBinary);

   return 0;
}
Sample output:
Enter any fractional decimal number: 5.7
Equivalent binary value: 101.101100
//C program to convert decimal to roman number
#include<stdio.h>
void predigits(char c1,char c2);
void postdigits(char c,int n);
char roman_Number[1000];
int i=0;
int main(){
    int j;
    long int number;

    printf("Enter any natural number: ");
    scanf("%d",&number);

    if(number <= 0){
         printf("Invalid number");
         return 0;
    }
    while(number != 0){
         if(number >= 1000){
             postdigits('M',number/1000);
             number = number - (number/1000) * 1000;
         }
         else if(number >=500){
             if(number < (500 + 4 * 100)){
                 postdigits('D',number/500);
                 number = number - (number/500) * 500;
             }
             else{
                 predigits('C','M');
                 number = number - (1000-100);
             }
         }
         else if(number >=100){
             if(number < (100 + 3 * 100)){
                 postdigits('C',number/100);
                 number = number - (number/100) * 100;
             }
             else{
                 predigits('L','D');
                 number = number - (500-100);
             }
         }
         else if(number >=50){
             if(number < (50 + 4 * 10)){
                 postdigits('L',number/50);
                 number = number - (number/50) * 50;
             }
             else{
                 predigits('X','C');
                 number = number - (100-10);
             }
         }
         else if(number >=10){
             if(number < (10 + 3 * 10)){
                 postdigits('X',number/10);
                 number = number - (number/10) * 10;
             }
             else{
                 predigits('X','L');
                 number = number - (50-10);
             }
         }
         else if(number >=5){
             if(number < (5 + 4 * 1)){
                 postdigits('V',number/5);
                 number = number - (number/5) * 5;
             }
             else{
                 predigits('I','X');
                 number = number - (10-1);
             }
         }
         else if(number >=1){
             if(number < 4){
                 postdigits('I',number/1);
                 number = number - (number/1) * 1;
             }
             else{
                 predigits('I','V');
                 number = number - (5-1);
             }
         }
    }
    printf("Roman number will be: ");
    for(j=0;j<i;j++)
         printf("%c",roman_Number[j]);
    return 0;
}
void predigits(char c1,char c2){
    roman_Number[i++] = c1;
    roman_Number[i++] = c2;
}
void postdigits(char c,int n){
    int j;
    for(j=0;j<n;j++)
         roman_Number[i++] = c;

}
Sample output:
Enter any natural number: 87
Roman number will be: LXXXVII

//C program to convert Roman number to decimal number
#include<stdio.h>
#include<string.h>
int digitValue(char);
int main(){
    char roman_Number[1000];
    int i=0;
    long int number =0;

    printf("Enter any roman number (Valid digits are I, V, X, L, C, D, M):  \n");
    scanf("%s",roman_Number);

    while(roman_Number[i]){
     
         if(digitValue(roman_Number[i]) < 0){
             printf("Invalid roman digit : %c",roman_Number[i]);
             return 0;
         }
         
         if((strlen(roman_Number) -i) > 2){
             if(digitValue(roman_Number[i]) < digitValue(roman_Number[i+2])){
                 printf("Invalid roman number");
                 return 0;
             }
         }
         if(digitValue(roman_Number[i]) >= digitValue(roman_Number[i+1]))
             number = number + digitValue(roman_Number[i]);
         else{
             number = number + (digitValue(roman_Number[i+1]) - digitValue(roman_Number[i]));
             i++;
         }
         i++;
    }
     
    printf("Its decimal value is : %ld",number);

    return 0;
}
int digitValue(char c){
    int value=0;
    switch(c){
         case 'I': value = 1; break;
         case 'V': value = 5; break;
         case 'X': value = 10; break;
         case 'L': value = 50; break;
         case 'C': value = 100; break;
         case 'D': value = 500; break;
         case 'M': value = 1000; break;
         case '\0': value = 0; break;
         default: value = -1;
    }
    return value;
}
Sample output:
Enter any roman number (Valid digits are I, V, X, L, C, D, M):
XVII
Its decimal value is: 17

No comments:

Post a Comment