0% found this document useful (0 votes)
44 views7 pages

C Asg - PDF

The C program takes in two 8-bit binary numbers as input with each bit separated by a space, adds the two numbers, and outputs the sum in both binary and decimal formats. It defines the number of bits, takes in the two binary inputs by looping through the bit arrays, prints the inputs, adds the numbers by looping through the bits and tracking a carry, prints the sum in binary, and finally converts and prints the decimal equivalent of the binary sum.

Uploaded by

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

C Asg - PDF

The C program takes in two 8-bit binary numbers as input with each bit separated by a space, adds the two numbers, and outputs the sum in both binary and decimal formats. It defines the number of bits, takes in the two binary inputs by looping through the bit arrays, prints the inputs, adds the numbers by looping through the bits and tracking a carry, prints the sum in binary, and finally converts and prints the decimal equivalent of the binary sum.

Uploaded by

Abhishem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Q1) Write a C program to add 8-bit binary numbers (Input is a sequence of "0" and "1") ,

and output the result in both binary and decimal formats .

Code :-

#include<stdio.h>

#include<math.h>

#define COUNT 8 //change the value of COUNT to increase the no. of bits

int sum[COUNT];

int take_bin(int a[]);

int print_bin(int a[]);

int add_bin(int a[],int b[]);

int convertBinaryToDecimal(int a[]);

int main()

int p[COUNT],q[COUNT];

printf("Enter first Binary number(space between each bit): ");

take_bin(p);

printf("Enter second Binary number (space between each bit): ");

take_bin(q);

print_bin(p);

print_bin(q);

add_bin(p,q);

return 0;

int take_bin(int a[])


{

int i;

for(i=0;i<COUNT;i++)

scanf("%d",&a[i]);

int print_bin(int a[])

int i;

for(i=0;i<COUNT;i++)

printf("%d",a[i]);

printf("\n");

int add_bin(int a[],int b[])

int i,carry=0;

for(i = COUNT-1;i>=0;i--)

if(a[i]==0 && b[i]==0 && carry==0)


{

sum[i] = 0;

carry = 0;

else if(a[i]==0 && b[i]==0 && carry==1)

sum[i] = 1;

carry = 0;

else if(((a[i]==1 && b[i]==0)||(a[i]==0 && b[i]==1)) && carry==0)

sum[i] = 1;

carry = 0;

else if(((a[i]==1 && b[i]==0)||(a[i]==0 && b[i]==1)) && carry==1)

sum[i] = 0;

carry = 1;

else if(a[i]==1 && b[i]==1 && carry==0)

sum[i] = 0;

carry = 1;

else if(a[i]==1 && b[i]==1 && carry==1)


{

sum[i] = 1;

carry = 1;

printf("+\n");

printf("--------\n");

print_bin(sum);

convertBinaryToDecimal(sum);

int convertBinaryToDecimal(int a[])

int c=0,i;

for(i=0;i<8;i++)

c*=2;

c=c+a[i];

}
printf("the result in decimal is %d",c);
}
Output :-
Q5) Write a C program to arrange integer in the array so that all negative integers
precede all positive integers .

Code:-

#include <stdio.h>

void main()

int i, j, a, n, number[30];

printf("Please enter the value of Numbers => ");

scanf("%d", &n);

printf("Please enter the numbers here => ");

for (i = 0; i < n; ++i)

scanf("%d", &number[i]);

for (i = 0; i < n; ++i)

for (j = i + 1; j < n; ++j)

if (number[i] > number[j])

a = number[i];

number[i] = number[j];
number[j] = a;

printf("After arrange all the integers (All -ve integers preced all +ve integers) => ");

for (i = 0; i < n; ++i)

printf("%d ", number[i]);

Output :-

You might also like