NPTEL 2024 Introduction to programming in C Week 2

 


Week 2: Assignment 2 - Question 1

Due on 2024-02-08, 23:59 IST
Parity Checker

You are given a sequence of bits (1's and 0's).
The sequence is said to have even parity if and only if the number of 1's in the sequence if even.

Write a C program to that outputs 1 if the sequence has even parity and 0 otherwise.

Input

A sequence of bits (0's and 1's) ending with a -1.
(Note : -1 is not a part of input. It only signifies that input has ended)

Output

1 if the number of ones in the sequence is even.
0 if the number of ones in the sequence is odd.

Sample Inputs and Outputs

Sample input 1
--------------------
0 1 1 -1
Sample Output 1
----------------------

1  

Explanation

----------------
The input sequence is  011 . It has two ones. Since 2 is even, the sequence has even parity.


Sample input 2
--------------------
0 1 0 1 0 1 -1
Sample Output 1
----------------------

0  

Explanation

----------------
The input sequence is 010101 . The number of 1's is three. Since 3 is odd,  the sequence does not have even parity.
Your last recorded submission was on 2024-02-08, 20:48 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
int main()
3
{
4
    int b, count=0;
5
    scanf("%d", &b);
6
    while(b!=-1)
7
    {
8
        if(b==1)
9
            count++;
10
        scanf("%d",&b);
11
    }
12
    printf("%d",count%2==0);
13
    return 0;
14
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   

InputOutput
Test Case 1
0 1 1 -1
1
Test Case 2
0 1 0 1 0 1 -1
0

                                                                                                                                                                                                                                                

No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...