Wednesday, May 27, 2020

Prime Number or Next Prime Number Checking


Write a program to take a number and show if it is prime. If not prime, then show the next prime.
 If user enters 14 output will be : next prime number is 17
 If user enters 13 output will be : 13 is a prime number


 
import java.util.*;
class A
{
  int i,n;
  Scanner sc=new Scanner(System.in);
  void show()
  {
      System.out.print("\nEnter the number:");
      n=sc.nextInt();
      for(i=2;i<n;i++)
      {
           if(n%i==0)
           break;
        }
       if(i==n)
       System.out.print(n+ " is a prime number");
       else
       {
       while(true)
       {
        if(nextPrime(n))
        {
          System.out.print("next prime number is:" + n);
          break;
        }
        n++;
       }
     }
    }
         private boolean nextPrime(int n)
        {
        int i;
        for(i=2;i<n;i++)
      {
           if(n%i==0)
           break;
        }
       if(i==n)
       return true;
       else
       return false;
    }
}
        

Sunday, May 3, 2020

ICSE and ISC Computer Application Online Class



Due to the present Covid Pandemic situation, schools are closed and students are facing lots of problems. Again ICSE 2020 Exam is postponed due to Covid and Geography, Computer and Biology exams are pending. 

I am basically software developer and web master of number of web sites but to help the students (including ICSE 2020 students), have decided to conduct online class on Computer Application for class IX and X standard students. 

I will conduct class on Skype and maximum 6 students will be in a batch. Interested students / guardians can contact me (From 7:00 AM to 8:00 PM).

Mobile Number: 9732009367

Tuesday, March 24, 2020

BlueJ Program On Converting Non Palindromic Words To Palindrome From A Sentence

For Details of The Program: CLICK HERE

import java.io.*;
class Prog3
{
     String str1,str2;
     int len;
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     public void takeSentence() throws Exception
     {
         System.out.print("\nEnter the sentence:");
         str1=br.readLine().trim();
         len=str1.length();
         char ch=str1.charAt(len-1);
         if(ch!='.' && ch!='?' && ch!='!')
         {
            System.out.print("\nINVALID INPUT:"); 
         return;
        }
         show();
      }
       private void show()
      {
           String s="",s1;
           s1=str1.substring(0,len-1);
           str2="";
           while(true)
           {
                int i=s1.indexOf(' ');
              if(i< 0)
              break;
                s=s1.substring(0,i);
                s1=s1.substring(i+1);
                str2=str2+" "+rev(s);
            }
            str2=str2+" "+ rev(s1);
            System.out.print("\n"+str1);
            System.out.print("\n"+str2);
        }
        private String rev(String s)
        {
           s=makePalin(s);
           return s;
        }
        private String makePalin(String s)
        {
             String s2,s3;
             int i;
             int len1=s.length();             
             s2="";
             for(i=len1-1;i>=0;i--)
             s2=s2+s.charAt(i);
             if(!s.equals(s2))
             {
             for(i=len1-1;i>=0;i--)
             {
                  if(s.charAt(i)!=s.charAt(i-1))
                  break;
                }
                
/* EXTRACTING THE PART OF THE WORD WHICH WILL BE REVERSED. IN THE ABOVE LOOP, THE WE HAVE SEARCHED FROM WHERE REPEATATION OF END CHARACTERS STARTS SO THAT THE WORD WHICH WILL BE REVERSED CAN ELIMINATES THE REPEATED CHARACTERS. E.G. XAZZZ WILL BECOME XAZZZAX AS WE HAVE TAKED XA TO REVERSE IT AND CONCATENATE WITH XAZZZ */

                s3=s.substring(0,i+1);
                len1=s3.length();
             s2="";
             for(i=len1-1;i>=0;i--)
             s2=s2+s3.charAt(i);
             //s=s+s3;
             s2=s2.substring(1);
             return s+s2;
            }
            else
            return s;
        }
        public static void main(String args[]) throws Exception
        {
             Prog3 ob=new Prog3();
             ob.takeSentence();
            }
        }

Back To 2019 Computer Practical Paper: CLICK HERE

Program On 1 D and 2 D Array In Combination

For Details of The Program: CLICK HERE

import java.io.*;
class Mat
{
int a[], N,b[][];
public void takeValues()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i;
System.out.print("\nENTER N=");
N=Integer.parseInt (br.readLine());
if (N<=1 || N>=12)
{
System.out.print("\nMATRIX SIZE OUT OF RANGE");
return;
}
a=new int[N];
b=new int[N][N];
for(i=0;i<=N-1;i++)
{
System.out.print("\nINPUT:");
a[i]=Integer.parseInt (br.readLine());
}
arrange();
}
private void arrange() 
{
int i,j,k,x,temp;
for(i=0;i<=N-2;i++)
{
for(j=i+1;j<=N-1;j++)
{
if(a[i]>a[j])
{
 temp=a[i];
 a[i]=a[j];
 a[j]=temp;
}
}
}
System.out.print("\nSORTED ARRAY\n");
for(i=0;i<=N-1;i++)
System.out.print(a[i]+" ");
for(i=0;i<=N-1;i++)
{
 for(j=0;j<=N-1-i;j++)
 {
      b[i][j]=a[j];
 }
 x=j;
 for(k=0;k<=i-1;k++)
 {
      b[i][x++]=a[k];
    }
}
 System.out.print("\nFILLED MATRIX\n");
 for(i=0;i<=N-1;i++)
 {
      for(j=0;j<=N-1;j++)
      {
         System.out.print(b[i][j]+" "); 
        }
   System.out.println();     
}
}
public static void main(String args[]) throws IOException
{
Mat ob=new Mat();
ob.takeValues();
}
}

Back To 2019 Computer Practical Paper: CLICK HERE

BlueJ Program On Day Number And Date

For Details of The Program: CLICK HERE

import java.io.*;
class A
{
   int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
   String mon[]={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
   int d,n,yr,leap=0;
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
   public void takeDatas() throws Exception
   {
        System.out.print("\nDAY NUMBER:");
        d=Integer.parseInt(br.readLine());
        System.out.print("\nYEAR NUMBER:");
        yr=Integer.parseInt(br.readLine());
        System.out.print("\nDAY AFTER (N):");
        n=Integer.parseInt(br.readLine());
   if (d<=1 || d>=366)
   {
   System.out.print("\nDAY NUMBER OUT OF RANGE");
   System.exit (0);
   }
   if (n<=0 || n>=101)
   {
   System.out.print("\nDATE AFTER (N DAYS) OUT OF RANGE");
   System.exit (0);
   }
 } 
   public void isLeap()
   {
  if(yr%100==0 && yr%400==0)
        leap++;
        else if(yr%100!=0 && yr%4==0)
        leap++;
    }
    public void show()
    {
         int i;
         n=n+d;
         for(i=0;i<=11;i++)
         {
           d=d-days[i];
           if(d< days[i])
           break;
        }                
        System.out.print("\nDATE: "+ d);
        if(d==1)
        System.out.print(" ST ");
         else if(d==2)
         System.out.print(" ND ");
         else if(d==3)
         System.out.print(" RD ");
         else
         System.out.print(" TH ");         
         System.out.print(mon[i+1] + "," + yr);
/* mon[i+1] as loop terminates when complete number of days of the previous month is taken */ 
         // AFTER 'N' NUMBER OF DAYS
         i=0;
         for(;;)
         {
           n=n-days[i++];
           if(i==12)
           i=0;
           if(n< days[i])
           break;
        }                
        System.out.print("\nDATE: "+ n);
        if(d==1)
        System.out.print(" ST ");
         else if(d==2)
         System.out.print(" ND ");
         else if(d==3)
         System.out.print(" RD ");
         else
         System.out.print(" TH ");         
         System.out.print(mon[i] + "," + yr);
/* Here 'i' was incremented before exit from loop */
        }
        public static void main(String args[]) throws Exception
        {
             A ob=new A();
             ob.takeDatas();
             ob.isLeap();
             ob.show();
            }
        }


Back To 2019 Computer Practical Paper: CLICK HERE

Monday, March 23, 2020

BlueJ Program On Competition and Banner


Details of The Program: CLICK HERE

import java.io.*; 
public class S 
{ 
public void show() throws Exception 
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
String str[]; 
int n, i, j;

System.out.println("Enter the number of names: "); 
n = Integer.parseInt(br.readLine());

str = new String[n]; 
for(i=0;i< n;i++)
{

System.out.print("\nEnter Name No "+(i+1) + ":"); 
str[i] = br.readLine();

} 
int max = 0; 
for(i=0;i< n;i++)
{

if(str[i].length() >max) 
max = str[i].length(); 
/* Length of the longest name will be stored in variable 'max' */ 
} 
System.out.println("OUTPUT:" ); 
for(i=0;i< max;i++)
{ 
for(j=0;j< n;j++)

{
/* characters from each name will be displayed with a tab at right to maintain the gap, if there is no character in any name at the location specified by 'charAt(i)' means 'i' has a value less than the length of the string, only tabular space will be printed */ 
if(i< str[j].length())

System.out.print(str[j].charAt(i)+"\t"); 
else 
System.out.print("\t"); 
}

System.out.println(); 
} 
}
public static void main(String args[]) throws Exception 
{ 
S obj=new S(); 
obj.show(); 
} 
}


Variable Description

Type
Name
Use

BufferedReader
br
Intakes limit from user


String
str
Names are stored

Int
i
Loop control variable

int
j
Loop control variable

int
n
Stores the number of students



Algorithm

Step 1: Create BufferedReader object ‘br’ and an int type variable ‘n’ to store the number of strings to be stored in the string type array.

Step 2: create the string array ‘str’ according to the value stored in ‘n’.

Step 3: Store the strings in ‘str’.

Step 4: store the length of the maximum length string in an int variable ‘max’




Step 5: To display, run the outer loop ‘max’ times and the inner loop will run ‘n’ number of times and from the inner loop display the characters of each string in separate columns. Whenever any stri


Back To 2018 Computer Practical Paper: CLICK HERE

BlueJ Program On 2 D Array and Sorting


Details of The Program: CLICK HERE

import java.io.*; 
public class P 
{ 
void sort(int arr[])          //Taking single dimension array as parameter 
{

int i, j, size = arr.length, temp,flag;

/*'size' holds the number of values in the array */ 
for(i=0;i 
{

flag=0; 
/*Sorting using bubble sort technique, so if there is no exchange of values in one pass there is no need to run the loop */ 
for(j=0;j 
{ 
if(arr[j]>arr[j+1])

{
flag=1; temp = arr[j]; 
arr[j] = arr[j+1]; 
arr[j+1] = temp; 
}

} 
if(flag==0)

break;
}

}

void show(int arr[][]) /*Taking 2D array as parameter*/ 
{

int i, j; for(i=0;i

{

for(j=0;j 
/* arr[i] gives one row at a time. arr[i].length gives the number of values in each row */ 
{ 
System.out.print(arr[i][j]+"\t"); 
}

System.out.println();

} 
}
 void sort2D(int arr[][]) 
{ 

/* In java, array is an object, so here the function calling technique is call be reference means any changes made to the formal parameter in this function will be reflected in the original parameter.*/


int i, j; 
for(i=0;i 
{ 
sort(arr[i]); 
} 
}
public static void main(String args[])throws Exception 
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

P obj = new P(); int arr[][], m, n, i, j; 
System.out.println("Enter the number of rows: "); 
m = 
Integer.parseInt(br.readLine()); 
System.out.println("Enter the number of columns: ");
n = Integer.parseInt(br.readLine()); 
if(m< 2 && m >9||n >9 && n< 2) 
{

System.out.println("Matrix out of range"); System.exit(0);

} 
arr = new int[m][n]; 
for(i=0;i
{
System.out.println("Enter values for row no "+(i+1) + ":"); for(j=0;j
{

System.out.print("\nColumn No " + (j+1) + ":"); arr[i][j] = Integer.parseInt(br.readLine()); 
} 
}
System.out.println("Original Matrix: ");
obj.show(arr); 
obj.sort2D(arr);

System.out.println("Matrix after sorting rows: "); obj.show(arr);
}
}

Variable Description

Type
Name
Use
BufferedReader
br
Intakes limit from user
int
arr
2 D array to hold the values
Int
m
Holds no of rows in the array
int
n
Holds no of columns in the


array

int
I,j
Loop Control Variable
int
size
Holds the no of elements in


each row the array
Algorithm




Step 1: Create BufferedReader object ‘br’ and int type variables ‘n’ and 'm' to store the column and row size of the 2 D array.

Step 2: Values of ‘n’ and 'm' are checked for validity and if it is invalid, terminate the program otherwise create the 2 D array 'arr'.

Step 3: Stores the values in the two dimensional array 'arr'.

Step 4: Display the original array


Step 5: Pass the two dimensional array to function 'sort2D()' which sends each rows of the 2 d array to 'sort()' function which sorts the elements bubble sort technique.


Back To 2018 Computer Practical Paper: CLICK HERE

Subscribe via email

Enter your email address:

Delivered by FeedBurner