6th Year -Happy Anniversary


Thank you Universe…

Thank you WordPress…

Thank you Subscribers …

Thank you Visitors…

I am really thankful to everyone visited my blog and please comment your valuable feedback. WordPress, has given me a platform to share my programming topics to entire world. Surprisingly this is the 6th year. Realizing that I am not consistent in doing blog. Before a couple of days I could retrieve my logins after 1-2 years. In this 6th year anniversary, I like to resume my blogging of ISC, ICSE Computer topics.

Binary Search Program using methods – array- Class 12 Computer Science


Program to perform binary search, values input by the user using Scanner class. Binary Search can do only in sorted arrays. So a method for Sorting bubble Sort technique , then in the sorted array key is searched by BinarySearch method.

Usually we do by initializing the array value. Here each process defined in user defined methods.

import java.util.*;
public class Userdefined
{
    
     static void BubbleSort(int num[])  //Sorting
    {
        int len=num.length;
        int i,j, temp;
        for(i=0;i<len;i++)
        {
            for( j=0;j<len-i-1;j++)
            {
                if(num[j]>num[j+1])
                {
                    temp=num[j];
                    num[j]=num[j+1];
                    num[j+1]=temp;
                   
                }
            }
        }
   //   System.out.println("Sorted Elements:");
        for(int k=0;k<len;k++)
         System.out.print(num[k]+"\t");
          System.out.println(); 
    }
     static void BinSearch(int num[],int k)  //Binary Search
     {
         int l=0,u=num.length-1;
         int m=0, found=0;
         while(l<=u)
         {
         m= (l+u)/2;
         if( k > num[m])
            l =m+1;
            else if( k< num[m])
            u=m-1;
            else
            {
                found =1;
                break;
                
            }
       
        }
        if(found==1)
         System.out.println(" Element Present at"+" "+ (m+1) +" " +"after sorting");
        
         else
          System.out.println("Not Present");
        }
      public static void main(String [] args)
      {
          Scanner sc=new Scanner(System.in);
          System.out.println( "Enter the total number of elements in the array:");
          int n=sc.nextInt();
          int a[]=new int[n];
          System.out.println( "Enter the elements:");
          for(int i=0;i<n;i++)
          {
          a[i]=sc.nextInt();
          }
         System.out.println("Entered Elements");
          for(int i=0;i<n;i++)
          {
          
       System.out.print(a[i]+"\t");

           }
     System.out.println();
      System.out.println("Elements should be sorted before Binary Search .....\n                                                                                                                                         ..Sorted Elements....");
       BubbleSort(a);                         //invoking BubbleSort ()
      
       System.out.println("Binary Search \n");
       
       System.out.println("Enter element to search");
       int ky=sc.nextInt();
       BinSearch(a, ky); // invoking Binary search method 
    }
     
}

Output:

Enter the total number of elements in the array:
5
Enter the elements:
54
12
78
1
6
Entered Elements
54 12 78 1 6
Elements should be sorted before Binary Search …..
…Sorted Elements….
1 6 12 54 78
Binary Search

Enter element to search
12
Element Present at 3 after sorting

2D Array – ISC 12th Computer- SOLVED SPECIMEN QUESTION PAPER – 2021


harvestfarmfresh.com

Write a program to declare a square matrix M [ ] [ ] of order ‘N’ where ‘N’ must be greater than 3 and less than 10. Allow the user to accept three different characters from the keyboard and fill the array according to the instruction given below:

(i) Fill the four corners of the square matrix by character 1.
(ii) Fill the boundary elements of the matrix (except the four corners) by character 2.
(iii) Fill the non-boundary elements of the matrix by character 3.

Test your program with the following data and some random data:

Example 1:
INPUT: N = 4
FIRST CHARACTER: @
SECOND CHARACTER: ?
THIRD CHARACTER: #

OUTPUT:
@ ? ? @
? # # ?
? # # ?
@ ? ? @

Example 2: INPUT: N = 5
FIRST CHARACTER: A
SECOND CHARACTER: C
THIRD CHARACTER: X

OUTPUT:
A C C C A
C X X X C
C X X X C
C X X X C
A C C C A

Example 3: INPUT: N = 15
OUTPUT: SIZE OUT OF RANGE

import java.util.*;
class matRIX
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter no.of rows M of a square matrix:");
        int m=sc.nextInt();
         char mat[][] = new char[m][m];
       System.out.println("Enter corner elements - 1st character:");
       char ch1=sc.next().charAt(0);
        System.out.println("Enter boundary except corner elements - 2ndcharacter:");
       char ch2=sc.next().charAt(0);
         System.out.println("Enter  non boundary  - 3rdcharacter:");
       char ch3=sc.next().charAt(0);
      
          for(int i=0; i<m; i++)
            {
                for(int j=0; j<m;j++)
                {
                    //fill corners with char 1
                    if( (i==0||i==m-1) && (j==0 || j==m-1))
                      mat[i][j]=ch1;
                    //fill non-corner boundary elememts with char 2
                    else if(i==0 || j==0 || i==m-1 || j==m-1)
                         mat[i][j]=ch2;
                    //fill rest with char 3
                    else
                        mat[i][j]=ch3;
                }
            }
            
            //print the array
            System.out.println("");
            for(int i=0; i<m; i++)
            {
                for(int j=0; j<m; j++)
                {
                    System.out.print(mat[i][j] + "\t");
                }
                System.out.println();
            }
        
    }
}

OUTPUT:

Enter no.of rows M of a square matrix:
5
Enter corner elements – 1st character:
@
Enter boundary except corner elements – 2ndcharacter:
?
Enter non boundary – 3rdcharacter:
#

@ ? ? ? @
? # # # ?
? # # # ?
? # # # ?
@ ? ? ? @

Converting first character into UpperCase


Write a program to accept a sentence and check whether it is terminated by ‘.’ or ‘? ‘ or ‘!’ . Display the words using default delimiter (space) and the first character of each word should be converted into Upper Case.

Sample Input :

converting first character.

Sample Output:

converting Converting
first First
character Character

import java.util.*;
class strDec
{
public static String firstLetter(String wrd)
    {
        String res="";char ch2=' ';
        int l=wrd.length();
        for(int i=1;i<l;i++)
        {
        char ch1=wrd.charAt(i);
           ch2=wrd.charAt(0);
        if(Character.isLowerCase(ch2))
        {
           ch2 =Character.toUpperCase(ch2);
    
        }
      //System.out.println(ch1);
        res=res +ch1;
        
    }
        res=ch2+res;
        return res;
        //System.out.println(res);
    }
   public static void main(String args[])
  
   {   strDec strd=new strDec();
       String str;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a sentence:");
        str=sc.nextLine();
     int l=str.length();
        char c=str.charAt(l-1);
       
       if(c=='.'||c=='?'||c=='!')
       {
        StringTokenizer st=new StringTokenizer( str, " .?!");
        int n=st.countTokens();
         String wrd[]=new String [n];
       
         String ch[]=new String[n];
         
      
         
        for(int i=0;i<n;i++){
            wrd[i]=st.nextToken();
            ch[i]=firstLetter(wrd[i]);
        System.out.println(wrd[i] + "    "+ch[i]+" " );
        }
        
     }
        
     else
        {
         System.out.println("Enter a sentence terminated with . or ? or ! :");
   }
       
    }
}

Output:

Enter a sentence:
the sky is the limit.
the The
sky Sky
is Is
the The
limit Limit

ISC Computer Science Practical – Previous Year 2020 – Solved Paper – Matrix program


Write a program to declare a matrix A[][]of order ( M*N) where ‘M’ is the number of rows and ‘N’ is the number of columns such that the value of “M’ must be greater than 0 and less than 10 and the value of ‘N’ must be greater than 2 and less than 6. Allow the user to input digits (0-7) only at each location, such that each row represents an octal number.

Example: 

231(decimal equivalent of 1st row=153 i.e. 2*8² +3*8¹ +1*8⁰)    
405(decimal equivalent of 1st row=261 i.e. 4*8¹ +0*8¹ +5*8⁰) 
156(decimal equivalent of 1st row=110 i.e. 1*8² +5*8¹ +6*8⁰)

Perform the following tasks on the matrix:
  • Display the original matrix.
  • Calculate the decimal equivalent for each row and display as per the format given below.
Example 1:
Input:
M=1        
N=3            
ENTER ELEMENTS FOR ROW 1: 1   4  4
Output:
FILLED MATRIX   DECIMAL EQUIVALENT 
1  4  4                100



Example 2:
Input:
 M=3           
 N=4            
ENTER ELEMENTS FOR ROW 1: 1 1 3 7           
ENTER ELEMENTS FOR ROW 2: 2 1 0 6            
ENTER ELEMENTS FOR ROW 3: 0 2 4 5
Output:            
FILLED MATRIX   DECIMAL EQUIVALENT                          
1 1 3 7         607                             
2 1 0 6         1094                             
0 2 4 5         165



Example 3:
Input:
ENTER ROW SIZE  M=3           
ENTER COLUMN SIZE N=9
Output:OUT OF RANGE
import java.util.*;
class mat
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter no.of rows M ");
        int m=sc.nextInt();
        System.out.println("Enter no.of rows N ");
        int n= sc.nextInt();
        int flag=0,pow=0;  
        int mat[][]=new int[m][n];
        if((m>0 && m< 10 )&&(n>2 && n<6))          //M >0 AND <10  && N>2 AND <6
        {  
            for(int i=0;i<m;i++)
            {System.out.print("Enter elements for row "+(i+1)+":");
                for(int j=0;j<n;j++)
                {
                     mat[i][j]=sc.nextInt();
                     if(mat[i][j] < 0 ||   mat[i][j]>7)     //ALLOW THE USER TO INPUT ONLY FROM 0 TO 7 (OCTAL)
                    {
                        flag=1;
                        System.out.println("INPUT DIGITS (0 -7) ONLY AT EACH LOCATION");
                        System.exit(1);
                        
                    }
                 
                }
                
            }   
        }
        if(flag==0)
        {
            System.out.println("FILLED MATRIX \t DECIMAL EQUIVALENT");
                      for(int i=0;i<m;i++)
                      {
                         int r=0,sum=0;
                          for(int j=n-1;j>=0;j--)  
                          {   
                                  pow=((int)Math.pow(8,r++)); 
                                  sum=sum+(mat[i][j]*pow);
                    
                           }
                           
                           for(int k=0;k<n;k++)
                           {
                               System.out.print(mat[i][k]+ " " );
                           }
                            System.out.print("\t\t \t" +sum);
                            System.out.println();
                      }
         }       
           
                
else
        System.out.println("Enter valid range"); 
 }
}



        
        

OUTPUT:

Enter no.of rows M
3
Enter no.of rows N
3
Enter elements for row 1:2 3 1
Enter elements for row 2:4 0 5
Enter elements for row 3:1 5 6


FILLED MATRIX DECIMAL EQUIVALENT
2 3 1 153
4 0 5 261
1 5 6 110

to print possible combinations of numbers (234, 243,324,342,etc)


import java.util.*;
class permutationDigits
{
int count = 0;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a digit: ");
int a= sc.nextInt();
String s=String.valueOf(a); //converting int into string
System.out.println("The Anagrams are : ");
compute("",s);
System.out.println("Total Number of Anagrams = "+count);
}
void compute(String s1, String s2)
{
if(s2.length()<=1)
{
count++;
System.out.println(s1+s2);
}
else
{
for(int i=0; i<s2.length(); i++)
{
String a = s2.substring(i, i+1);
String b = s2.substring(0, i);
String c = s2.substring(i+1);
compute(s1+a, b+c);
}
}
}
public static void main(String args[])
{
permutationDigits ob=new permutationDigits();
ob.input();
}
}

Output:

Enter a digit: 234
The Anagrams are :
234
243
324
342
423
432
Total Number of Anagrams = 6

To print Anagrams (eg: TOP,TPO,POT, etc..)


import java.util.*;
class anagramsWord
{
int count = 0;
void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word : ");
String s = sc.next();
System.out.println("The Anagrams are : ");
compute("",s);
System.out.println("Total NO. of Anagrams = "+count);
}

void compute(String s1, String s2)
{
if(s2.length()<=1)
{
count++; // no of combination words
System.out.println(s1+s2);
}
else
{
for(int i=0; i<s2.length(); i++)
{
String a = s2.substring(i, i+1);
String b = s2.substring(0, i);
String c = s2.substring(i+1);
compute(s1+a, b+c); // recursive method
}
}
}
public static void main(String args[])throws Exception
{
anagramsWord ob=new anagramsWord();
ob.input();
}
}

Output:

Enter a word : TOP
The Anagrams are :
TOP
TPO
OTP
OPT
PTO
POT
Total NO. of Anagrams = 6

Sum of Series x2/1! + x4/3! + x6/5! + xn/(n-1)!


A class SeriesOne is desgined to calculate the sum of the following series:

Sum = x2/1! + x4/3! + x6/5! + xn/(n-1)!

Some of the members of the class are given below:

class name : SeriesOne

Data members/ instance variables :

x : to store an integer number
n : to store number of terms
sum : double variable to store the sum of the series

Member functions:

SeriesOne(int xx, int nn) : constructor to assign x=xx and n=nn

double findfact(int m) : to return the factorial of m .

double findpower(int x, int y) : to return x raised to the power of y

void calculate() : to calculate the sum of the series
void display() : to display the sum of the series.

Specify the class SeriesOne, giving the details of the above member data and methods only.

Define the main() function to create an object and call the member function accordingly to enable the task.

import java.util.*;
class SeriesOne
    {
         int x,n,sum;
           
            SeriesOne(int xx, int nn)
            {
                x=xx;
                n=nn;
            }
          
        double findfact(int m)
        {
            int fact=1;
            for(int i=1;i <=m; i++)
            fact=fact*i;
            return fact;
        }
        
        double findpower(int x, int y)
        {
          double res;
           res= Math.pow(x,y);
           return res;
        }
         void cal()
         {
          
             double term=0;
             for(int i=2; i <=n; i=i+2)
             {
             term= findpower(x,i) / findfact( i-1);
             sum+=term;
            }
         }
        void display()
        {
            System.out.println("Sum of the series:"+sum);
        }
        public static void main(String[] args){
            SeriesOne  so=new SeriesOne();
          
            so.cal();
            so.display();
          
        }
    }

Enter the value of x:
3
Enter the value of n:
4
Sum of the series:22

Prime Numbers Factorial


Write a program to check whether the given number is prime number or not, if it is prime number display the factorial.

import java.util.*;
public class primeFactorial
{    
    int i,m,flag,n;  
    primeFactorial()
    {
        n=0;
        m=0;
        flag=0;
    }
    public  void input()
    {    
        Scanner sc = new Scanner (System.in);
        System.out.println("Enter the number:");
        n=sc.nextInt();
    }
    public void calculate()
    {
       input();        // input()method invocation
        m=n/2;      
        if(n==0||n==1)
        {  
            System.out.println(n+" is not prime number");      
           
        }
        else
        {  
            for(i=2;i<=m;i++)
            {      
                if(n%i==0)
                {          
                    flag=1;      //status variable, if any divisor found, flag=1
                    break;      
                }      
            } // end of for loop
            if(flag==0) 
            { 
                System.out.println(n+" is prime number"); 
                fact(n);
                
            }
            else    
            {
                System.out.println(n+" is not prime number");
            }
        }//end of outer else  
    }
   public void fact(int num1)
   {
       int n2=num1,fact=1;
       for(int j=1;j<=n2;j++)
       fact=fact *j;
       System.out.println("Factorial " +   fact);
    }
    
 public static void main(String args[])
 {    
     primeFactorial pN=new primeFactorial();
     pN.calculate();   // calculate() method call
  
    }
}   

Output:

Enter the number:
5
5 is prime number
Factorial 120


Enter the number:
4
4 is not prime number

Emirp Number


An emirp number is a number which is prime backwards and forwards. Example : 13 and 31 are both prime numbers. Thus 13 is a emirp number.

import java.util.*;
class Emirp
{ 
    int n, rev,f;
    Emirp(int nn)
    {
        n=nn;
        rev=0;
        f=2;
    }
    int isprime(int x)
    {
        if(n==x)
        return 1;
        else if (n%x==0 || n==1)
        return 0;
        else
        return isprime(x+1);
    }
    void isEmirp()
    {
        int x=n;
        while(x!=0)
     {
         rev=rev*10 + x%10;
         x=x/10;
         
        }
        int ans1=isprime(f);
        n=rev;
        f=2;
        int ans2=isprime(f);
        if(ans1==1 && ans2==1)
        System.out.println(n+"is an emirp no");
        else
        System.out.println(n+"is not an emirp no.");
        
    }
    public static void main(String[] args) 
    { 
      int a; 
      Scanner sc= new Scanner(System.in);
      System.out.println("Enter the number:");
      a=sc.nextInt();
        Emirp emp=new Emirp(a);
        emp.isEmirp();

    } 
} 

Enter the number:
13
31is an emirp no
Enter the number:
51
15is not an emirp no.