0% found this document useful (0 votes)
13 views4 pages

Source Code Coa Lab1

The document contains C source code for a program that performs binary arithmetic operations, including converting decimal numbers to binary, calculating the two's complement for negative numbers, and adding two binary numbers. It also includes functions for displaying binary numbers and converting binary results back to decimal. The program checks for overflow in signed addition and displays user information.

Uploaded by

abhayashrestha96
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Source Code Coa Lab1

The document contains C source code for a program that performs binary arithmetic operations, including converting decimal numbers to binary, calculating the two's complement for negative numbers, and adding two binary numbers. It also includes functions for displaying binary numbers and converting binary results back to decimal. The program checks for overflow in signed addition and displays user information.

Uploaded by

abhayashrestha96
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Source code

#include<stdio.h>
#include<conio.h>
#include<math.h>
void dectobinary(int nbit,int num, int arry[]){
for (int i = nbit - 1; i >= 0; i--) {
arry[i] = num % 2;
num /= 2;
}
}
void twoComplement(int arry[], int nbit){
int bitsum,i,carry=1;
for(i=0;i<nbit;i++){
arry[i] = !arry[i];
}
for(i=nbit-1;i>=0;i--){
bitsum = arry[i]+carry;
arry[i] = bitsum%2;
carry = bitsum/2;
}
}
void showbinary(int *p, int size) {
// Access elements of the array using the arr[] parameter
int i;
for (i = 0; i<size; i++) {
printf("%d ", *(p+i));
}
printf("\n");
}
int addbinary(int arry1[],int arry2[],int nbit,int sum[]){
int i, bitsum,carry =0;
for(i=nbit-1;i>=0;i--){
bitsum = arry1[i]+arry2[i]+carry;
sum[i] = bitsum%2;
carry = bitsum/2;
}
return carry;
}
void binarytodec(int arry[],int nbit){
int add=0,i;
int temp[10];
for (int i = 0; i < nbit; i++)
temp[i] = arry[i];
if(arry[0]==1){
twoComplement(temp,nbit);
}
for(i=0;i<=nbit-1;i++){
add = add + temp[nbit-1-i] * pow(2,i);
}
if(arry[0]==1){
add= (-add);
}
printf("The decimal equivalent is %d",add);
}
void displayinfo()
{
printf("\n\n\tAbhaya Shrestha\n");
printf("\tACE079BCT005\n\n");
}
int main(){
int num1,num2,arry1[10]={0},arry2[10]={0},sum[10]={0},nbit,status;
printf("Enter number of bits: ");
scanf("%d",&nbit);
printf("Enter the two number: ");
scanf("%d%d",&num1,&num2);
// negative
if(num1<0){
dectobinary(nbit,abs(num1),arry1);
twoComplement(arry1,nbit);
showbinary(&arry1[0],nbit);
}
else{
dectobinary(nbit,num1,arry1);
showbinary(&arry1[0],nbit);
}
//negative
if(num2<0){
dectobinary(nbit,abs(num2),arry2);
twoComplement(arry2,nbit);
showbinary(&arry2[0],nbit);
}
else{
dectobinary(nbit,num2,arry2);
showbinary(&arry2[0],nbit);
}
printf("The sum of binary is: \n");
status = addbinary(arry1,arry2,nbit,sum);
showbinary(&sum[0],nbit);
binarytodec(sum,nbit);

int sign1 = arry1[0], sign2 = arry2[0], signSum = sum[0];


if (sign1 == sign2 && signSum != sign1) {
printf("\nOverflow Detected (signed addition)\n");
}
displayinfo();
return 0;
}

You might also like