0% found this document useful (0 votes)
2 views10 pages

CA Lab Programs

The document contains various C programs demonstrating binary and decimal number conversions, logic gate operations, and arithmetic operations such as addition and subtraction. It includes implementations for converting between binary, decimal, octal, and hexadecimal systems, as well as logic gates like NAND, NOR, AND, OR, and NOT. Additionally, it features half and full adders and subtractors for binary arithmetic.

Uploaded by

livic1967
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)
2 views10 pages

CA Lab Programs

The document contains various C programs demonstrating binary and decimal number conversions, logic gate operations, and arithmetic operations such as addition and subtraction. It includes implementations for converting between binary, decimal, octal, and hexadecimal systems, as well as logic gates like NAND, NOR, AND, OR, and NOT. Additionally, it features half and full adders and subtractors for binary arithmetic.

Uploaded by

livic1967
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

CA Lab Programs

Binary to decimal conversion

#include <stdio.h>
#include <math.h>

int convert(long long);

int main() {
long long binaryNum;
printf("Enter a binary number:");
scanf("%lld", &binaryNum);
printf("%lld in binary = %d in decimal", binaryNum, convert(binaryNum));
return 0;
}

int convert(long long binaryNum) {


int decimalNum = 0, i = 0, remainder;

for (i = 0; binaryNum != 0; ++i) {


remainder = binaryNum % 10;
binaryNum /= 10;
decimalNum += remainder * pow(2, i);
}

return decimalNum;
}

NAND Gate
#include <stdio.h>
#include <stdlib.h>

int main()
{

int a[5] = { 0, 0, 1, 1 };
int b[5] = { 0, 1, 0, 1 };
int i, ans;

for (i = 0; i < 4; i++) {


ans = !(a[i] * b[i]);
printf("\n %d NAND %d = %d",
a[i], b[i], ans);
}
}
NOT Gate
#include <stdio.h>
#include <stdlib.h>

int main()
{

int a[5] = { 1, 0 };
int i, ans;

for (i = 0; i < 2; i++) {


ans = !(a[i]);
printf("\n NOT %d = %d", a[i], ans);
}
}

NOR Gate
#include <stdio.h>
#include <stdlib.h>

int main()
{

int a[5] = { 0, 0, 1, 1 };
int b[5] = { 0, 1, 0, 1 };
int i, ans;

for (i = 0; i < 4; i++) {


ans = !(a[i] + b[i]);
printf("\n %d NOR %d = %d",
a[i], b[i], ans);
}
}

OR Gate
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a[5] = { 1, 0, 1, 0 };
int b[5] = { 0, 1, 1, 0 };
int i, or_ans;

for (i = 0; i <4; i++) {


// using the || operator
or_ans = a[i] || b[i];

printf("\n %d AND %d = %d",


a[i], b[i], or_ans);
}
}

AND Gate
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a[5] = { 1, 0, 1, 0 };
int b[5] = { 0, 1, 1, 0 };
int i, or_ans;

for (i = 0; i <4; i++) {

// using the + operator


if (a[i] * b[i] > 0)
or_ans = 1;
else
or_ans = 0;

printf("\n %d AND %d = %d",


a[i], b[i], or_ans);
}
}

Decimal to binary conversion


#include <stdio.h>

int main()
{
int decinum, binarynum[32], x;

printf("Please enter a decimal number: ");


scanf("%d", &decinum);

// Converts decimal to binary


for (x = 0; decinum > 0; x++) {
binarynum[x] = decinum % 2;
decinum = decinum / 2;
}

printf("Binary representation is: ");


for (x = x - 1; x >= 0; x--) {
printf("%d", binarynum[x]);
}

return 0;
}

Decimal to Octal
#include <stdio.h>

int main()
{
int decinum, octalnum[32], x;

printf("Please enter a decimal number: ");


scanf("%d", &decinum);

// Converts decimal to binary


for (x = 0; decinum > 0; x++) {
octalnum[x] = decinum % 8;
decinum = decinum / 8;
}

printf("Octal representation is: ");


for (x = x - 1; x >= 0; x--) {
printf("%d", octalnum[x]);
}

return 0;
}

Decimal to hexadecimal
#include <stdio.h>

int main()
{
int decinum, hexnum[32], x;

printf("Please enter a decimal number: ");


scanf("%d", &decinum);
// Converts decimal to binary
for (x = 0; decinum > 0; x++){
hexnum[x] = decinum % 16;
decinum = decinum / 16;
}

printf("Hexadecimal representation is: ");


for (x = x - 1; x >= 0; x--) {
printf("%d", hexnum[x]);
}

return 0;
}

Octal to Decimal

#include <stdio.h>
#include <math.h>

// function prototype
long long convertOctalToDecimal(int octalNumber);

int main() {

int octalNumber;

printf("Enter an octal number: ");


scanf("%d", &octalNumber);

printf("%d in octal = %lld in decimal", octalNumber,


convertOctalToDecimal(octalNumber));

return 0;
}

// function to convert octalNumber to decimal


long long convertOctalToDecimal(int octalNumber) {
int decimalNumber = 0, i = 0;

while(octalNumber != 0) {
decimalNumber += (octalNumber%10) * pow(8,i);
++i;
octalNumber/=10;
}
i = 1;

return decimalNumber;
}

Binary addition

#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;
}

Half Adder
#include <stdio.h>

typedef char bit;


bit carry=0;
bit halfadd(bit A,bit B){
carry=A&B;
return A^B;
}
int main()
{
int i,j,result;
printf("A B | S Carry\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
result=halfadd(i,j);
printf("%d %d | ",i,j);
printf("%d %d\n",result,carry);
}
}
return 0;
}

Binary Subtraction
#include <stdio.h>
#include<math.h>

void main()
{

int i,numa[4],numb[4],diff[4];
printf("\tSUBTRACTION USING TWO'S COMPLEMENT");
printf("\nEnter two 4-bit binary numbers\n");

printf("\nEnter first number: ");


for( i = 0; i<4; i++){
scanf("%d",&numa[i]);
}
printf("\nEnter second number: ");
for(i = 0; i<4; i++){
scanf("%d",&numb[i]);
}
for(i = 3; i >= 0; i--){
diff[i] = numa[i] - numb[i];
if(diff[i] < 0){
numa[i-1] = numa[i-1] - 1;
}
diff[i] = fabs(diff[i]%2);
}
printf("\nDifference is: ");
for(i = 0; i<4; i++){
printf("%d",diff[i]);
}

Half Subtraction

#include <stdio.h>

typedef char bit;


bit borrow=0;
bit halfsub(bit A,bit B){

borrow=!(A)&B;
return A^B;
}
int main()
{
int i,j,result;
printf("A B | Diff Borrow\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
result=halfsub(i,j);
printf("%d %d | ",i,j);
printf("%d %d\n",result,borrow);
}
}
return 0;
}
Full Adder

#include <stdio.h>

typedef char bit;


bit Cout=0;
bit fulladd(bit A ,bit B,bit Cin){
Cout=(A&B)||(Cin&(A^B));
return (A^B)^Cin;
}
int main()
{
int i,j,k;
int result;
printf("A B Cin | S Cout\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
result=fulladd(i,j,k);
printf("%d %d %d | ",i,j,k);
printf("%d %d\n",result,Cout);
}

}
}
return 0;
}

Full Subtraction

#include <stdio.h>

typedef char bit;


bit Bout=0;
bit fullsub(bit A ,bit B,bit Bin){
Bout=!(A) & Bin | !(A) & B | B & Bin;
return (A^B)^Bin;
}
int main()
{
int i,j,k;
int result;
printf("A B Cin | S Bout\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
result=fullsub(i,j,k);
printf("%d %d %d | ",i,j,k);
printf("%d %d\n",result,Bout);
}

}
}
return 0;
}

You might also like