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

Toc Exp

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

Toc Exp

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

4) Design a C program for accepting decimal number divisible by 2.

#include <stdio.h>

int main() {
int number;

// Input the number


printf("Enter a decimal number: ");
scanf("%d", &number);

// Check if the number is divisible by 2


if (number % 2 == 0) {
printf("%d is divisible by 2.\n", number);
} else {
printf("%d is not divisible by 2.\n", number);
}

return 0;
}

5) Design a machine which accepts string having equal no. of 1’s and 0’s.

#include <stdio.h>
#include <string.h>

// Function to check if the string has equal number of 1's and 0's
int check_equal_ones_zeros(const char *str) {
int stack = 0; // We use an integer as a stack counter

// Traverse each character of the string


for (int i = 0; i < strlen(str); i++) {
if (str[i] == '1') {
// Simulate pushing by incrementing the counter
stack++;
} else if (str[i] == '0') {
// Simulate popping by decrementing the counter
if (stack == 0) {
// If there are more 0's than 1's, reject the string
return 0;
}
stack--;
} else {
// Reject the string if it contains characters other than '1' and '0'
return 0;
}
}

// If the stack is empty, accept the string


return stack == 0;
}

int main() {
char input[100];
// Input a string from the user
printf("Enter a binary string (containing only 1's and 0's): ");
scanf("%s", input);

// Check if the string has an equal number of 1's and 0's


if (check_equal_ones_zeros(input)) {
printf("The string is accepted (equal number of 1's and 0's).\n");
} else {
printf("The string is rejected (unequal number of 1's and 0's).\n");
}

return 0;
}

6) Design a C program for creating a machine which count number of 1’s and 0’s in a
given string.

#include <stdio.h>
#include <string.h>

int main() {
char input[100]; // To store the input string
int count_ones = 0, count_zeros = 0; // To store counts of '1's and '0's

// Input the binary string from the user


printf("Enter a binary string (containing only 1's and 0's): ");
scanf("%s", input);

// Traverse the input string and count '1's and '0's


for (int i = 0; i < strlen(input); i++) {
if (input[i] == '1') {
count_ones++; // Increment count for '1'
} else if (input[i] == '0') {
count_zeros++; // Increment count for '0'
} else {
// If the string contains characters other than '1' or '0', give an
error
printf("Invalid input! The string contains non-binary characters.\n");
return 1; // Exit the program with an error
}
}

// Display the counts of '1's and '0's


printf("Number of 1's: %d\n", count_ones);
printf("Number of 0's: %d\n", count_zeros);

return 0;
}

7) Design a C Program to find 2’s complement of a given binary number.


#include <stdio.h>
#include <string.h>

// Function to find the one's complement of the binary number


void findOnesComplement(char binary[], int length) {
for (int i = 0; i < length; i++) {
// Flip the bits: change '0' to '1' and '1' to '0'
if (binary[i] == '0')
binary[i] = '1';
else if (binary[i] == '1')
binary[i] = '0';
}
}

// Function to add 1 to the one's complement to get the 2's complement


void addOne(char binary[], int length) {
int carry = 1; // Start with adding 1 (carry)

for (int i = length - 1; i >= 0; i--) {


if (binary[i] == '1' && carry == 1) {
binary[i] = '0'; // If bit is '1' and carry is '1', set to '0' and
keep carry
} else if (binary[i] == '0' && carry == 1) {
binary[i] = '1'; // If bit is '0' and carry is '1', set to '1' and
clear carry
carry = 0;
}
}
}

int main() {
char binary[100];

// Input the binary number


printf("Enter a binary number: ");
scanf("%s", binary);

int length = strlen(binary);

// Step 1: Find the one's complement of the binary number


findOnesComplement(binary, length);

// Step 2: Add 1 to the one's complement to get the two's complement


addOne(binary, length);

// Output the two's complement


printf("The 2's complement of the given binary number is: %s\n", binary);

return 0;
}

8)#include <stdio.h>
#include <string.h>
// Function to increment a binary number by 1
void incrementBinary(char binary[], int length) {
int carry = 1; // Start with a carry of 1 to add

// Traverse the binary number from the least significant bit to the most
significant bit
for (int i = length - 1; i >= 0; i--) {
if (binary[i] == '1' && carry == 1) {
binary[i] = '0'; // If bit is '1' and carry is '1', set bit to '0' and
carry stays
} else if (binary[i] == '0' && carry == 1) {
binary[i] = '1'; // If bit is '0' and carry is '1', set bit to '1' and
clear carry
carry = 0;
}
}

// If carry is still 1 after looping through the entire string, prepend '1' to
the result
if (carry == 1) {
printf("1"); // Print an extra '1' if the carry overflowed
}
}

int main() {
char binary[100];

// Input the binary number


printf("Enter a binary number: ");
scanf("%s", binary);

int length = strlen(binary);

// Increment the binary number by 1


incrementBinary(binary, length);

// Output the result (incremented binary number)


printf("%s\n", binary);

return 0;
}

You might also like