Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL SPECIMEN PAPER SOLVED 2020 QUESTION 1 : EVIL NUMBER

Tuesday, March 19, 2019

ISC COMPUTER SCIENCE PRACTICAL SPECIMEN PAPER SOLVED 2020 QUESTION 1 : EVIL NUMBER


NOTE: This question is from a SPECIMEN PAPER released by the council

An Evil number is a positive whole number which has even number of 1’s in its binary equivalent. Example: Binary equivalent of 9 is 1001, which contains even number of 1’s. Thus, 9 is an Evil Number. A few Evil numbers are 3, 5, 6, 9.... Design a program to accept a positive whole number ‘N’ where N>2 and N<100. > <100. Find the binary equivalent of the number and count the number of 1s in it and display whether it is an Evil number or not with an appropriate message. Test your program with the following data and some random data:


Example 1:
 INPUT: N = 15
BINARY EQUIVALENT: 1111
NUMBER OF 1’s: 4
OUTPUT: EVIL NUMBER
 Example 2:
 INPUT: N = 26
BINARY EQUIVALENT: 11010
NUMBER OF 1’s: 3
OUTPUT: NOT AN EVIL NUMBER
Example 3:
INPUT: N = 145
OUTPUT: NUMBER OUT OF RANGE



This particualr problem is quite simple. Going through the code will reveal the logic!



import java.util.*;
public class ISC2020Q1 {
       public static void main(String[] args) {
       int N;
       Scanner in = new Scanner(System.in);
       do {
             System.out.println("Enter the number:");
             N = in.nextInt();
             if(N <=2 || N>=100)
                    System.out.println("NUMBER OUT OF RANGE,TRY AGAIN");
             }while(N <=2 || N>=100);
       EvilNumber obj = new EvilNumber(N);
       obj.dsiplay();
       in.close();
       }
}
class EvilNumber {
       public void dsiplay() {
             testEvilNumber();
             System.out.println("INPUT NUMBER N:"+number);
             System.out.println("BINARY EQUIVALENT:");
             for(int i=1;i<=counter;i++)
                    System.out.print(binary[counter-i]+" ");
             System.out.println();
             System.out.println("NUMBER OF 1's:"+evil_counter);
             if (flag==true)
                    System.out.print("EVIL NUMBER");
             else
                    System.out.print("NOT AN EVIL NUMBER");
       }
       private void testEvilNumber() {
             int tmp=number;
             do {
                    binary[counter] = tmp%2;
                    tmp /=2;
                    counter++;
                    }while(tmp >=1);
             for(int i=0;i<counter;i++)
                    if(binary[i]==1)
                          evil_counter++;
            
             if(evil_counter%2==0)
                    flag=true;
       }
       EvilNumber(int N) {
             number = N;
             binary = new int[7]; // a binary sequence of length 7 ranges from 1 to 127
             counter = 0;
             evil_counter=0;
             flag = false;
       }
       private boolean flag;
       private int number;
       private int[] binary;
       private int counter;
       private int evil_counter;
}

No comments:

Post a Comment