Page 565 - ComputerScience_Class_11
P. 565

Program 2     This class is designed to accept a positive integer in binary form and display whether it is a
                                 Pernicious number or not. A Pernicious number is a binary number that has a minimum of
                                 two digits and the number of 1's in it is a prime number.
                                 Examples:
                                 •  101 is a pernicious number because the number of 1's in 101 is 2 and 2 is a prime number.
                                 •   101010 is a pernicious number because the number of 1's in 101010 is 3 and 3 is a prime
                                   number.
                                 •   1111 is not a pernicious number because the number of 1's in 1111 is 4 and 4 is not a
                                   prime number.
                   1       import java.util.Scanner;

                   2       public class Perni
                   3       {

                   4           public static void main(String args[])
                   5           {

                   6               String num;
                   7               Scanner scanner = new Scanner(System.in);

                   8
                   9               System.out.print("Enter a binary number: ");

                   10              num = scanner.nextLine();
                   11

                   12              int count = 0;
                   13              int length = num.length();
                   14              for (int i = 0; i < length; i++)

                   15              {

                   16                  if (num.charAt(i) == '1')
                   17                  {
                   18                      count++;

                   19                  }
                   20              }

                   21
                   22              boolean isPrime = true;

                   23              if (count <= 1)
                   24              {

                   25                  isPrime = false;
                   26              }

                   27              else





                                                                                                Internal Assessment  563
   560   561   562   563   564   565   566   567   568   569   570