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

The Programming Project: March 2019

Sunday, March 31, 2019

ISC COMPUTER SCIENCE PRACTICAL SOLVED 2006 QUESTION 1: CONSECUTIVE INTEGERS

A positive natural number, (for e.g. 27), can be represented as follows –
2+3+4+5+6+7
8+9+10
13+14
where every row represents a combination of consecutive natural numbers, which add up to 27.
Write a program which inputs a positive natural number N and prints the possible consecutive number combinations, which when added give N.
Test your program for the following data and some random data.
SAMPLE DATA
INPUT:
N = 9
OUTPUT:
4 + 5
2 + 3+ 4
INPUT:
 N = 15
OUTPUT:
7 +8
1 +2+ 3+ 4+ 5
4 +5+ 6
INPUT:
N = 21
 OUTPUT:
10+ 11
1+ 2+ 3+ 4+ 5+ 6
6+ 7+ 8
The main logic of the program is as follows:
For every integer i less than half of the input number (since once the half path is crossed the sum will always exceeds the input number), add consutive integers till the sum
is either equal to the input number or exceeds it. If the sum equals the input number print the
consecutive integers starting from i. See the code below
 while(sum !=natural_numb) {
                                  sum +=k;
                                  if(sum>=natural_numb)
                                         break;
                                  k++;
                                  counter++;
                                  }
k++ helps to add consecutive integers to the sum and counter counts the number of times
the addition operation takes place. Rest is reveled by the code itself.





import java.util.*;
public class ISC2006Q1 {
       public static void main(String[] args) {
       int N;
       Scanner in = new Scanner(System.in);
       System.out.println("Enter the natural number:");
       N = in.nextInt();
       IntegerSum obj = new IntegerSum(N);
       System.out.println("OUTPUT:");
       obj.consecutiveIntegerSum();
       in.close();

      }
}
class IntegerSum {
       public void consecutiveIntegerSum() {
              int counter,sum,k;
              flag=false;
              for(int i=1;i<=natural_numb/2;i++) {
                     sum=0;
                     counter=0;
                     k=i;
                     while(sum !=natural_numb) {
                                  sum +=k;
                                  if(sum>=natural_numb)
                                         break;
                                  k++;
                                  counter++;
                                  }
                     if(sum==natural_numb) {
                           flag=true;
                           for(int j=0;j<counter+1;j++) {
                                  if(j==counter)
                                         System.out.print((j+i)+"="+natural_numb);
                                  else
                                         System.out.print((j+i)+"+");
                                  }
                           System.out.println();
                           }
                    
                     }
              if(flag==false)
                     System.out.println(natural_numb+" cannot be written as sum of consecutive natural numbers:");
       }
       IntegerSum(int N){
              natural_numb=N;
       }
       private int natural_numb;
       private boolean flag;
}

Saturday, March 30, 2019

ISC COMPUTER SCIENCE PRACTICAL 2005 SOLVED QUESTION 2 : WONDROUS SQUARE

A wondrous square is an n by n grid which fulfils the following conditions:
    (i) It contains integers from 1 to n2, where each integer appears only once.
    (ii) The sum of integers in any row or column must add up to 0.5 * n * (n2 + 1).
For example the following grid is a wondrous square where the sum of each row or column is 65 when n = 5 :
                           17      24      2        8        15
 23      5        7       14      16
 4       6       13       20      22
                           10       12      19      21      3
 11      18      25      2        9
Write a program to read n (2 < = n < = 10) and the values stored in these n by n cells and output if the grid represents a wondrous square or not.
Also output all the prime numbers in the grid along with their row index and column index as shown in the output. A natural number is said to be prime if it has exactly two divisors. E.g. 2, 3, 5, 7, 11,.......
The first element of the given grid i.e. 17 is stored at row index 0 and column index 0 and the next element in the row i.e. 24 is stored at row index 0 and column index 1.
Test your program for the following data and some random data.
SAMPLE DATA:
      INPUT :                     N = 4
16       15      1        2
 6        4       10      14
                           9         8        12      5
                           3         7       11      13
OUTPUT:               YES IT REPRESENTS A WONDROUS SQUARE.



 PRIME                    ROW INDEX                              COLUMN INDEX
 2                                    0                                     3
 3                                    3                                    0
 5                                    2                                    3
 7                                    3                                    1
11                                   3                                    2
13                                   3                                    3

Input           N=4
                   1        2        4
                   3        7       5
                   8        9        6
Output                   not a wondrous square

PRIME                     ROW INDEX                              COLUMN INDEX
 2                                    0                                     0

 2                                    1                                     1

A simple program to code! The logic behind the program goes as follows:
i. Input the order and elements while checking its validity
   Remember elements must be from 1 to n^2
ii. Find the row sum and coloumn sum and equate it with the value determined
    by the formula 0.5 * n * (n^2 + 1).
iii. Check the frequeny of each element. None of the elements must appears more than once
iv. Lastly find all the primes along with thier postion and print them

Step iv doesn't require any explanation, just pass the matrix element along with its
row and coloumn index to a method which checks whether it is prime or not. If it is print else
contiune till the last element.

For step i and ii the following method has been used. The code is simple and revealing jsut go
through it.
row_sum +=mat[i][j];
col_sum +=mat[j][i];
respectively determines the row and coloumn sum inside the for loops.

  public boolean isWondrousSquare() {
       flag=true;
       double temp =  0.5*(float)order*((float)order*(float)order + 1);
       double row_sum,col_sum;
       int frequency=0;
       // checking that each element appears only once
       for(int k=1;k<=order*order;k++) {
              frequency=0;
              for(int p=0;p<order;p++) {
                     for(int q=0;q<order;q++) {
                           if(k==mat[p][q])
                                  frequency++;
                           }
                     }
              if(frequency>1)
                     break;
              }
       // checking the sum of each row and each coloumn
       for(int i=0;i<order;i++) {
              row_sum=0;
              col_sum=0;
            for(int j=0;j<order;j++) {
              row_sum +=mat[i][j];
              col_sum +=mat[j][i];
              }
            if(row_sum != temp || col_sum !=temp) {
              flag=false;
              break;
              }
              }
       if(flag==true && frequency==1)
              return true;
       else
              return false;
    }


Complete JAVA CODE


import java.util.*;
public class ISC2005Q2 { 
       public static void main(String[] args) {
       int N;
       Scanner in = new Scanner(System.in);
       do {
             System.out.println("Enter the order of the square matrix:");
             N = in.nextInt();
             if(N<2 || N>10)
                    System.out.println("INVALID INPUT,TRY AGAIN");
             }while(N<2 || N>10);
       WondrousSquare obj = new WondrousSquare(N);
       obj.inputElements();
       if(obj.isWondrousSquare())
          System.out.println("Yes it represents a wondrous square.");
       else
          System.out.println("Not a wondrous square.");
       System.out.println("Entered Matrix:");
       obj.display();
       in.close();

      }
}
class WondrousSquare {
    public boolean isWondrousSquare() {
       flag=true;
       double temp =  0.5*(float)order*((float)order*(float)order + 1);
       double row_sum,col_sum;
       int frequency=0;
       // checking that each element appears only once
       for(int k=1;k<=order*order;k++) {
              frequency=0;
              for(int p=0;p<order;p++) {
                     for(int q=0;q<order;q++) {
                           if(k==mat[p][q])
                                  frequency++;
                           }
                     }
              if(frequency>1)
                     break;
              }
       // checking the sum of each row and each coloumn
       for(int i=0;i<order;i++) {
              row_sum=0;
              col_sum=0;
            for(int j=0;j<order;j++) {
              row_sum +=mat[i][j];
              col_sum +=mat[j][i];
              }
            if(row_sum != temp || col_sum !=temp) {
              flag=false;
              break;
              }
              }
       if(flag==true && frequency==1)
              return true;
       else
              return false;
    }
       private void isPrime(int element,int row,int col) {
              flag = true;
              if (element==1)
             flag=false;
              else {
                     for(int i = 2; i<=element/2;i++)
                   if(element%i==0) {
                      flag=false;
                      break;
                   }
         }
        if(flag==true)
           System.out.println(element+"                 "+row+"                 "+col);
         }
      public void display() {
            for(int p=0;p<order;p++)
                   for(int q=0;q<order;q++)
                         if(MAX_MATRIX < mat[p][q])
                                MAX_MATRIX = mat[p][q];
            String s,element;
            s=Integer.toString(MAX_MATRIX);
            for(int i=0;i<order;i++) {
                   for(int j=0;j<order;j++) {
                          element=Integer.toString(mat[i][j]); // for formatted output
                         int tmp=element.length();
                         while(tmp !=s.length()) {
                                       element +=" ";
                                       tmp++;
                                       }
                         System.out.print(element+"  ");
                         }
                 System.out.println();
            }
            // printing all the primes
            System.out.println("Prime           "+"Row Index           "+"Coloumn Index");
            for(int i=0;i<order;i++)
                for(int j=0;j<order;j++)
                       isPrime(mat[i][j],i,j);
      }
      public void inputElements() {
             Scanner in = new Scanner(System.in);
            System.out.println("Enter the values of the matrix:");
            for(int i=0;i<order;i++) {
                   for(int j=0;j<order;j++) {
                         mat[i][j]=in.nextInt();
                         if(mat[i][j]<=0 || mat[i][j] > order*order) {
                                System.out.println("Invalid element,try again:");
                                j--;
                          }
                   }
            }
            in.close();
      }
      WondrousSquare(int N){
            order = N;
            mat = new int[order][order];
            MAX_MATRIX =0;
             }
       private int MAX_MATRIX;
       private boolean flag;
       private int[][] mat;
       private int order;
}