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

write a program in C language Create a function to perform the insertion sort. Now the...
write a program in C language Create a function to perform the insertion sort. Now the program will perform the following steps: Prompt the user to enter the number of array elements (say, N). Read the number of elements (N). Use dynamic memory allocation to allocate an array of N single precision floating-point elements (C type float). Read the N single precision floating-points elements to the allocated array. Invoke a function to sort the array using insertion sort (the insertion...
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?
Please make a C program named: rotate.c This code will contain a single function (and the...
Please make a C program named: rotate.c This code will contain a single function (and the include directive to your header file) to perform the rotation operation using the two integer operands passed to this function. The first integer operand is the number to be rotated. The second integer operand is the number of bit positions that the first operand must be rotated by. EXAMPLE: Enter a 32-bit number (>= 1 and <= 4294967295, inclusively): 1 Enter the number of...
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...
C Programming Please write a single function and not the whole program Can you please provide...
C Programming Please write a single function and not the whole program Can you please provide comments and explanations for all questions. Thank you Write a single function for the following: void split_date(int day_of_year, int year, int *month, int *day);             day_of_year is an integer between 1 and 366, specifying a particular day within the year designated by the parameter year. Month and day point to variables in which the function will store the equivalent month (1 – 12) and day...
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...
The program should be written in C++ with comments Write a program that takes graduation rates...
The program should be written in C++ with comments Write a program that takes graduation rates (per 1000 of the population) for North, South, East, West and Central United States. Input the number from each of the regions in a function returning an int with the graduation rate to the main program. Also figure out which region has the highest graduation rate in another function and display the result from inside that particular function. So your function prototypes should be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT