Question

In: Computer Science

THIS PROGRAM HAS TO BE WRITTEN IN C Single function to perform the parity computation on...

THIS PROGRAM HAS TO BE WRITTEN IN C

Single function to perform the parity computation on the input integer passed to this function. The output of the function is 0 if the input has even parity (that is, the number of 1s in the binary representation of the input is even) and 1 if the input has odd parity (that is, the number of 1s in the binary representation of the input is odd).

Solutions

Expert Solution

CODE:

#include <stdio.h>

//Function to compute parity
int parity_compute(int a){
  
//To store the count of 1 in binary representation of a
int count=0;
  
//iterate until a becomes 0
while(a!=0){
  
//if last bit is 1
if(a&1==1){
//increment count by 1
count++;
}
  
//Right Shift to delete last bit from the binary representation of number
a = a>>1;
}
  
//If no of 1s is even, it will return 0
if(count%2==0){
return 0;
}

//If no of 1s is odd, it will return 1
return 1;
}

int main()
{
//To store the input
int a;
  
printf("Enter a number : ");
  
//To take the input from the user
scanf("%d",&a);
  
//calling the parity_compute() and printing returned value
printf("%d",parity_compute(a));

return 0;
}

OUTPUT:


Related Solutions

C Program: Write your own stringLength function (without calling a pre-written library function) that returns the...
C Program: Write your own stringLength function (without calling a pre-written library function) that returns the length of the string argument in characters, not including the terminating ‘\0’ character.   Write four versions of a void printString function (i.e., printString1, printString2, etc) each of which prints its string argument character-by-character but using the following techniques: printString1: array indexing printString2: pointer/offset with the array name as a pointer, printString3: pointer indexing, and printString4: pointer/offset with a pointer Write a void function called...
How to perform computation on inflation rates?
How to perform computation on inflation rates?
The equation for a simple consumption function is written as C = a + bY. The...
The equation for a simple consumption function is written as C = a + bY. The letter a represents the ________ part of consumption. The letters bY represent the ________ part of consumption. When graphing a consumption function, the vertical intercept is given by the letter ________ , and the slope of the function is given by the letter ________.
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
A program written in C that asks for the distance to be entered and then prints...
A program written in C that asks for the distance to be entered and then prints the fare A transportation company has the following rates For the first 100 miles                                                       20 cents a mile For the next 100 miles                                                       a) + 10 cents per mile over 100 miles For the next 100 miles                                                       b) + 8 cents per mile over 200 miles more than 300 miles                                                          c) + 5 cents per mile over 300 miles Write a program that asks...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
write a C++ program that : 1. Perform a rot13 substitution 2. Perform a caesar encryption...
write a C++ program that : 1. Perform a rot13 substitution 2. Perform a caesar encryption given a dictionary 3. Perform a caesar decryption given a dictionary 4. Create a random caesar cipher dictionary If user prints: -r : Perform rot13 substitution -g : generate a random caesar cipher dictionary. -e: Encrypt using the caesar cipher -d : Decrypt using the caesar cipher The format for the caesar cipher dictionary is a file with 26 pairs of letters, one per...
For C++ Function 1: Write a recursive function to perform a sequential search on a set...
For C++ Function 1: Write a recursive function to perform a sequential search on a set of integers The function will require an array parameter and the number to look for. These are the minimal parameter requirements The function should take an array of any size Function 2: Write a recursive function that will convert an integer (base 10) to binary The function should only have an integer parameter Have the function write the binary number to the console You...
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
In 2 to 3 paragraphs describe the C program written below (What the program is supposed...
In 2 to 3 paragraphs describe the C program written below (What the program is supposed to do). State the requirements for the program (How does the program meet the requirements) and discuss the logical process your program uses to meet those requirements (The process steps to meet the requirements). #include "stdio.h" int main(void) { //initialize array int arr[100];   //initialize variables   int i=0, j=0, n=0;    //infinite loop which will stop when user enters -1   while(n != -1) {   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT