program in c or c++ for arithmetic operations


Arithmetic Operations


This program is made for Arithmetic operations of binary numbers in C but Subtraction can be done only for 8-bit binary numbers. The logic to compute these operation is given below:
This program seems long but it is very simple first read it at least once.

Arithmetic Operation:

The program focuses on four arithmetic activities.

(Ⅰ) Addition:

For addtion simply we use (A+B).

(Ⅱ) Subtraction:

A - B = A + (complement of B) + 1
A - B = A + (complement of B) + 00000001 (used in program for 8-bit binary number)

(Ⅲ) Increment

A + 1 = A + = A + 00000001 (used in program for 8-bit binary number)

(Ⅳ) Decrement

A - 1 = A + (complement of 1) + 1
A - 1 = A + 11111110 + 00000001
A - 1 = A + 11111111(used in program for 8-bit binary number)



 #include <stdio.h>
 #include <conio.h>
 #include <iostream>
 using namespace std;
  typedef char bit;

long u1=00000001,u2=11111111;
long bn1,bn2,ans;
int res[100], rem=0,ch;
int res1[8],res2[8];

int arth()
{

  int i=0,k=0;
 
printf("1. Addition\n2. Subtraction\n");
printf("3. Increment\n4. decrement\n");
 printf("Enter your choice: ");
  scanf("%d",&ch); 
switch(ch)
{
case 1:
 printf("Enter two binary numbers: ");
 scanf("%ld%ld",&bn1,&bn2);
  
while(bn1!=0||bn2!=0)
{
res[i]=(bn1 %10 + bn2 %10+rem)%2;
rem=(bn1 %10 + bn2 %10 + rem)/2;
bn1=bn1/10;
bn2=bn2/10;
  i++;
    }
 --i;
printf("Sum of two binary numbers: ");
while(i>=0)
 {
  
  printf("%d",res[i--]);  
      
    }
    printf("\ncarry= %d",rem);
     break;
    
    
case 3:
 printf("Enter a binary number");
  scanf("%ld",&bn1);
     
  rem=0,i=0;
 while(bn1!=0||u1!=0)
 {
 res[i]=(bn1 %10 + u1 %10+rem)%2;
 rem=(bn1 %10 + u1 %10 + rem)/2;
 bn1=bn1/10;
 u1=u1/10;
  i++;
 }
  --i;
  printf("binary1+1= ");
  while(i>=0)
  {
  
    printf("%d",res[i--]);  
      
      
     } 
     break;
    case 4:
 printf("Enter a binary number");
    scanf("%ld",&bn1);
     
  rem=0,i=0;
 while(bn1!=0||u2!=0)
 {
 res[i]=(bn1 %10 + u2 %10+rem)%2;
 rem=(bn1 %10 + u2 %10 + rem)/2;
 bn1=bn1/10;
 u2=u2/10;
 i++;
   }
    --i;
   printf("binary1-1= ");
  while(i>=0)
  {
  
      printf("%d",res[i--]);  
      
      
     } 
     break; 
case 2:
  k=7,i=7;
  printf("Enter two binary numbers: ");
  scanf("%ld%ld",&bn1,&bn2);
 
 while(k>=0)
 {
 res1[k]=not(bn2%10);
  bn2=bn2/10;
       
   k--;
   }
                                
  k=7; 
while(k>=0||i>=0)
{
res2[i]=(bn1 %10 + res1[k]+rem)%2;
rem=(bn1 %10 + res1[k] + rem)/2;
bn1=bn1/10;
 k--;
 i--;
      }
   
 int j=7;
 i=7,rem=0;
while(j>=0||i>=0)
{
res1[i]=(res2[j] + u1 %10+rem)%2;
rem=(res2[j]  + u1 %10 + rem)/2;
u1=u1/10;
 j--;
 i--;
 }
  i=0;
printf("binary1 - binary2= ");
while(i<=7)
 {
  printf("%d",res1[i++]);  
 }

     break; 
  
 } 
 
    
 return 0;     
}

int main()
{
 int ch;
 char b;
printf(" Arithmetic Operation\n");


  arth();
  
    printf("\n");
 
printf("Do you want to continue(Y/N):");
 cin>>b;
 if(b=='Y'||b=='y')
 {
  main();
 }
 else{
  return 0;
 }

}


Output:


 Arithmetic Operation
1. Addition
2. Subtraction
3. Increment
4. decrement
Enter your choice: 1
Enter two binary numbers: 11001010 10001001
Sum of two binary numbers: 01010011
carry= 1
Do you want to continue(Y/N): y
 Arithmetic Operation
1. Addition
2. Subtraction
3. Increment
4. decrement
Enter your choice: 3
Enter a binary number10111000
binary1+1= 10111001
Do you want to continue(Y/N): n

--------------------------------
Process exited after 153.1 seconds with return value 0
Press any key to continue . . .

Comments :

Post a Comment