Javascript required

Semester IV


Bsc.Physical Science Computer Science (CBCS) Programs


Computer System Architecture Programs



Question 1 : Write a program to convert a number in Radix ‘R’ to radix 10 and vice versa. Test the same by
             a. Converting an unsigned number from binary, octal, hex to decimal.
             b. Converting an unsigned number from decimal to binary, octal, hex.
                                
 C++ Code
                                         1(a)
                                    // C++ program to convert a number from any base to decimal 
#include <stdio.h> 
#include <string.h> 
int value(char c) 
{ 
    if (c >= '0' && c <= '9') 
        return (int)c - '0'; 
    else
        return (int)c - 'A' + 10; 
} 
int toDecimal(char *str, int base) 
{ 
    int len = strlen(str); 
    int power = 1; // Initialize power of base 
    int num = 0; // Initialize result 
    int i; 
    for (i = len - 1; i >= 0; i--) 
    { 
        if (value(str[i]) >= base) 
        { 
        printf("Invalid Number"); 
        return -1; 
        } 

        num += value(str[i]) * power; 
        power = power * base; 
    } 

    return num; 
} 
int main() 
{ 
    clrscr();
    char str[10]; 
    int base;
    cout<<"\nEnter Value : ";
    cin>>str;
    cout<<"\nEnter the base to which you want to convert : ";
    cin>>base;
    printf("Decimal equivalent of %s in base %d is "
        " %d\n", str, base, toDecimal(str, base)); 
    getch();
    return 0; 
}
                                
 
                                         1(b)
                                    // C++ Program to convert decimal to any given base 
#include <stdio.h> 
#include <string.h> 
char reVal(int num) 
{ 
    if (num >= 0 && num <= 9) 
        return (char)(num + '0'); 
    else
        return (char)(num - 10 + 'A'); 
} 
void strev(char *str) 
{ 
    int len = strlen(str); 
    int i; 
    for (i = 0; i < len/2; i++) 
    { 
        char temp = str[i]; 
        str[i] = str[len-i-1]; 
        str[len-i-1] = temp; 
    } 
}  
char* fromDeci(char res[], int base, int inputNum) 
{ 
    int index = 0;
    while (inputNum > 0) 
    { 
        res[index++] = reVal(inputNum % base); 
        inputNum /= base; 
    } 
    res[index] = '\0'; 
    strev(res); 
    return res; 
} 
int main() 
{ 
    clrscr();
    int inputNum, base; 
    char res[100];
    cout<<"\nEnter Value : ";
    cin>>inputnum;
    cout<<"\nEnter the base of the value you entered :  ";
    cin>>base; 
    printf("Equivalent of %d in base %d is "
        " %s\n", inputNum, base, fromDeci(res, base, inputNum)); 
    getch();
    return 0; 
}
                                

Question 2 : Write a program that will prompt for the input of two integer values. Then using the bitwise
             shift operators show the result of
               a. Left shifting the first number by the second
               b. Right shifting the first number by the second
               c. Exclusive OR of the first number by the second bitwise
               d. OR of the first number by the second bitwise
               e. AND of the first number by the second bitwise
                                
 C++ Code
                                         operations.cpp
                                    #include<iostream.h>
#include<math.h>
void and_operation();
void or_operation();
void exor_operation();
void left();
void right();
int main()
{
    clrscr();
    int ch;
    cout<<"\n\n";
    cout<<"Press 1 for Left Shift";
    Cout<<"\nPress 2 for Right Shift";
    Cout<<"\nPress 3 for Exclusive OR operation";
    Cout<<"\nPress 4 for OR operation";
    Cout<<"\nPress 5 for AND operation";
    cin>>ch;
    switch(ch)
    {
        case 1 :left();
                break;
        case 2 :right();
                break;
        case 3 :exor_operation();
                break;
        case 4 :or_operation();
                break;
        case 5 :and_operation();
                break;
        default :cout<<"\nWrong Input !!";
                 break;
    }
    getch();
    return 0;
}
void and_operation()
{
    int a[50],b[50],c[50],n,i;
    cout<<"\nEnter number of bits : ";
    cin>>n;
    cout<<"\nEnter first binary number : ";
    for(i=1;i<n;i++)
    {
        cin>>a[i];
    }
    cout<<"\nEnter second binary number : ";
    for(i=0;i<n;i++)
    {
        cin>>b[i];
    }
    for(i=0;i<n;i++)
    {
        c[i] = a[i] & b[i]; 
    }
    for(i=0;i<n;i++)
    {
        cout<<c[i];
    }
}
void or_operation()
{
    int a[50],b[50],c[50],n,i;
    cout<<"\nEnter number of bits : ";
    cin>>n;
    cout<<"\nEnter first binary number : ";
    for(i=1;i<n;i++)
    {
        cin>>a[i];
    }
    cout<<"\nEnter second binary number : ";
    for(i=0;i<n;i++)
    {
        cin>>b[i];
    }
    for(i=0;i<n;i++)
    {
        c[i] = a[i] | b[i]; 
    }
    for(i=0;i<n;i++)
    {
        cout<<c[i];
    }
}
void exor_operation()
{
    int a[50],b[50],c[50],n,i;
    cout<<"\nEnter number of bits : ";
    cin>>n;
    cout<<"\nEnter first binary number : ";
    for(i=1;i<n;i++)
    {
        cin>>a[i];
    }
    cout<<"\nEnter second binary number : ";
    for(i=0;i<n;i++)
    {
        cin>>b[i];
    }
    for(i=0;i<n;i++)
    {
        c[i] = a[i] ^ b[i]; 
    }
    for(i=0;i<n;i++)
    {
        cout<<c[i];
    }
}  
void left()
{
    int num,rem.sum=0,n=0,s,ans;
    cout<<"\nEnter binary number : ";
    cin>>num;
    while(num>0)
    {
        rem = num%10;
        n = n+1;
    }
    cout<<"\nEnter number of bits to be shifted : ";
    cin>>s;
    ans = sum<<s;
    cout<<"\nResult is : "<<ans;
}
void right()
{
    int num,rem.sum=0,n=0,s,ans;
    cout<<"\nEnter binary number : ";
    cin>>num;
    while(num>0)
    {
        rem = num%10;
        n = n+1;
    }
    cout<<"\nEnter number of bits to be shifted : ";
    cin>>s;
    ans = sum>>s;
    cout<<"\nResult is : "<<ans;
}
                                

Question 3 :  Write a program that will prompt for the input of a binary value. Find out following complements.
                a. One's complement
                b. Two's complement
                                
 C++ Code
                                         complement.cpp
                                    #include <iostream.h> 
#include <string.h>
#include <conio.h>
void oneandtwocomplement(char str[], int n)
{    char ones[10],twos[10];
     int carr=1;
     for(int k = 0;k<n;k++)
    {
    ones[k]=(str[k] == '0')? '1': '0';
    }
    cout<<"\n 1's Complement : ";
    for(int j = 0;j<n;j++)
    {
    cout<<ones[j];
    }
    cout<<"\n 2's Complement : ";
    for(int i = n-1;i>=0;i--)
    {
    if (ones[i] == '1' && carr == 1)
    {
        twos[i] = '0';
    }
    else if (ones[i] == '0' && carr == 1)
    {
        twos[i] = '1';
        carr = 0;
    }
    else
    {
        twos[i] = ones[i];
    }
    }
    for(int l = 0;l<n;l++)
    {
    cout<<twos[l];
    } 
}  
int main() 
{ 
    clrscr();
    char str[10];
    int n;
    cout<<"\nEnter number of bits : ";
    cin>>n;
    cout<<"\nEnter binay number : \n";
    for(int i = 0;i<n;i++)
    {
    cin>>str[i];
    }
    oneandtwocomplement(str,n);
    getch();
    return 0;
}
                                

Question 4 :  Write a program to print the values of a 5 bit binary up-down counter. User should be able to
              specify the up or down nature of the counter.
                                
 C++ Code
                                         binary_up_down.cpp
                                    // This is kind of fun i mean they already given 5-bit so I simply did it.If you want a user input binary up-down counter
//then message me or simply ask in our Q&A page
#include <iostream.h>
#include <conio.h>
int up(int a[])
{
int j,i;
for(i=0;i<31;i++)
 { cout<<"\n";
  for(j=0;j<5;j++)
 {
   if(a[1]==1 && a[2]==1 && a[3]==1 &&a[4]==1)
   {
    if(a[0]==1)
     a[0]=0;
    else
     a[0]=1;
   }else a[0]=a[0];
  }
  if(a[2]==1 && a[3]==1 && a[4]==1)
  {
   if(a[1]==1)
    a[1]=0;
   else
    a[1]=1;
  }else a[1]=a[1];
  if(a[3]==1 && a[4]==1)
  {
   if(a[2]==1)
    a[2]=0;
   else
    a[2]=1;
  }else a[2]=a[2];
  if(a[4]==1)
  {
   if(a[3]==1)
    a[3]=0;
   else
    a[3]=1;
  }else a[3]=a[3];
  if(a[4]==0)
   a[4]=1;   
  else
   a[4]=0;
for(j=0;j<5;j++)
{cout<<"\t"<<a[j];
}
 }
}
int down(int a[])
{
int j,i;
for(i=30;i>=0;i--)
 { cout<<"\n";
  for(j=0;j<5;j++)
 {
   if(a[1]==0 && a[2]==0 && a[3]==0 &&a[4]==0)
   {
    if(a[0]==0)
     a[0]=1;
    else
     a[0]=0;
   }
  else
   a[0]=a[0];
  }
  if(a[2]==0 && a[3]==0 && a[4]==0)
  {
   if(a[1]==0)
    a[1]=1;
   else
    a[1]=0;
  }
  else
   a[1]=a[1];
  if(a[3]==0 && a[4]==0)
  {
   if(a[2]==0)
    a[2]=1;
   else
    a[2]=0;
  }
  else
   a[2]=a[2];
  if(a[4]==0)
  {
   if(a[3]==0)
    a[3]=1;
   else
    a[3]=0;
  }
  else
   a[3]=a[3];
  if(a[4]==1)
   a[4]=0;   
  else
   a[4]=1;
for(j=0;j<5;j++)
{cout<<"\t"<<a[j];
}
 }
}
int main()
{ clrscr();
 int ch;
int a[5]={0,0,0,0,0};
int b[5]={1,1,1,1,1};
cout<<"\n Binary Up Down Counter : ";
cout<<"\n 1.Up";
cout<<"\n 2.Down";
cout<<"\n Enter Your Choice : ";
cin>>ch;
switch(ch)
 {
  case 1:up(a);
         break;
  case 2:down(b);
         break;
  default:cout<<"\n Wrong Choice ....";
 }
 getch();
return 0;
}
                                

Question 5 :  Write a program to implement the following binary operations:
                a. Addition 
                b. Subtraction using 2's complement
                                
 C++ Code
                                         binary_op.cpp
                                    #include <iostream.h>
#include <conio.h>
int Subtract(int a, int b) 
{ 
    int c; 
    c = a + (~b + 1); 
    return c; 
}
int main()
{
    clrscr();
    int b1, b2,ch;
    int i = 0, remainder = 0, sum[20];
    cout<<"\nPress 1 for Binary Addition";
    cout<<"\nPress 2 for Binary Subtraction using 2's complement";
    cout<<"\nEnter your choice : ";
    cin>>ch;
    switch(ch)
    {
        case 1 : cout<<"Enter the first binary number: ";
                 cin>>b1;
                 cout<<"Enter the second binary number: ";
                 cin>>b2;
                 while (b1 != 0 || b2 != 0)
                 {
                     sum[i++] =(b1 % 10 + b2 % 10 + remainder) % 2;
                     remainder =(b1 % 10 + b2 % 10 + remainder) / 2;
                     b1 = b1 / 10;
                     b2 = b2 / 10;
                 }
                 if (remainder != 0)
                     sum[i++] = remainder;
                 --i;
                 cout<<"Sum of two binary numbers: ";
                 while (i >= 0)
             cout<<sum[i--];
         break;
    case 2 : cout<<"Enter the first number: ";
         cin>>b1;
         cout<<"Enter the second number: ";
         cin>>b2;
         cout << Subtract(b1, b2);
         break;
    default : cout<<"\nWrong Input !!";
          break;
    }
    getch();
    return 0; 
}