You are on page 1of 6

Example of Algorithm of a program which contains only main() method:

Question:
Write a Program in Java to fill a square matrix of size n*n in a circular fashion (clockwise) with natural
numbers from 1 to n*n, taking n as input.
For example: if n = 4, then n*n = 16, hence the array will be filled as given below.

Note: This program is also known as Spiral Matrix


Algorithm:
Step 1 :

Start of algorithm

Step 2 :

Input the size of circular matrix and store it in integer variable n

Step 3 :

Create an integer square array of size n*n which will be the circular matrix

Step 4 :

Declare and initialize variables k = 0 (for filling the matrix), c1 = 0 (for storing index of first column),
c2 = n-1 (for storing index of last column), r1 = 0 (for storing index of first row),
r2 = n-1 (for storing index of last row)

Step 5 :

Start a while loop till k<=n*n and repeat steps 6 to 10

Step 6 :
(a)
(b)

Start a for loop from i = c1 to c2, where i increases by 1 every time and perform step (b)
Store the natural numbers in the first row using A[r1][i] = k++

(a)
(b)

Start a for loop from j = r1+1 to r2, where j increases by 1 every time and perform step (b)
Store the natural numbers in the last column using A[j][c2] = k++

(a)

Start a for loop from i = c2-1 to c1, where i decreases by 1 every time and perform step (b)

(b)

Store the natural numbers in the last row using A[r2][i] = k++

Step 7 :

Step 8 :

Step 9 :
(a)

Start a for loop from j = r2-1 to r1+1, where j decreases by 1 every time and perform step (b)

(b)

Store the natural numbers in the first column using A[j][c1] = k++

Step 10 :

Update the variables c1, c2, r1 and r2

Step 11 :

Display the circular matrix A[ ]

Step 12 :

End of Algorithm

import java.io.*;
class Circular_Matrix
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of elements : ");
int n=Integer.parseInt(br.readLine());
int A[][]=new int[n][n];
int k=1, c1=0, c2=n-1, r1=0, r2=n-1;
while(k<=n*n)
{
for(int i=c1;i<=c2;i++)
{
A[r1][i]=k++;
}
for(int j=r1+1;j<=r2;j++)
{
A[j][c2]=k++;
}
for(int i=c2-1;i>=c1;i--)
{
A[r2][i]=k++;
}
for(int j=r2-1;j>=r1+1;j--)
{
A[j][c1]=k++;
}
c1++;
c2--;
r1++;
r2--;
}
/* Printing the Circular matrix */
System.out.println("The Circular Matrix is:");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(A[i][j]+ "\t");
}
System.out.println();

Example 2:

Example of Algorithm of a program which contains other methods along with the main() method
Note: In such cases, start with first writing the algorithm for the main() method and then the algorithm for the
other methods.
Question:

A Palindrome is a word that may be read the same way in either direction.
Accept a sentence in UPPER CASE which is terminated by either . , ? or ! .
Each word of the sentence is separated by a single blank space.
Perform the following tasks:
(a) Display the count of palindromic words in the sentence.
(b) Display the Palindromic words in the sentence.

Example of palindromic words: MADAM, ARORA, NOON


Test your program with the sample data and some random data:
Example 1
INPUT : MOM AND DAD ARE COMING AT NOON.
OUTPUT
:
NUMBER OF PALINDROMIC WORDS : 3
Example 2
INPUT : NITIN ARORA USES LIRIL SOAP.
OUTPUT : NITIN ARORA LIRIL
NUMBER OF PALINDROMIC WORDS : 3
Example 3
INPUT : HOW ARE YOU?
OUTPUT : NO PALINDROMIC WORDS

MOM

DAD

NOON

Algorithm for main() method :

Step 1 :

Start of algotithm

Step 2 :

Input the sentence and store it in a String variable s

Step 3 :

Convert the sentence into upper case

Step 4 :
Create a StringTokenizer object str to extract tokens (words) from the sentence using space and other
the punctuation marks namely ., ?, !
Step 5 :
Count the number of tokens (words) and store it in an integer variable c. Also create a String array
word[ ] of size c
Step 6 :

Start a for loop from i = 0 to less than c and store the tokens of the sentence into the word [ ] array

Step 7 :

Declare an integer variable count and initialize it with 0

Step 8 :

Start a for loop from i = 0 to less than c and repeat step 9

Step 9 :
Call the function isPalin() as : ob.isPalin(word[i]). If the returned value is true then increase the count
variable and print the word.
Step 10 :

If count of palindromic words is not equal to zero, then print the value stored in the variable count

Step 11 :

End of algorithm for main() method

Algorithm for function boolean isPalin(String s) :

Step 1 :

Start of algorithm for function isPalin()

Step 2 :

Find the length of the String s and store it in an integer variable l

Step 3 :

Declare and initialize a String variable rev= for storing the reverse of the String s

Step 4 :

Start a reverse for loop from i = l-1 to 0 and repeat step 5

Step 5 :

Extract characters from the end of the original string and add them to the variable rev

Step 6 :
false.

If the reverse word obtained (rev) is equal to the original String (s), then return true, otherwise return

Step 7 :

End of algorithm for the function isPalin().

Similarly if you have more functions, then just write their algorithms in a similar fashion one after the
other.

import java.io.*;
import java.util.*;
class Palin_ISC2013
{
static BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
boolean isPalin(String s)
{
int l=s.length();
String rev="";
for(int i=l-1; i>=0; i--)
{
rev=rev+s.charAt(i);
}
if(rev.equals(s))
return true;
else
return
false;
}
public static void main(String args[])throws IOException
{
Palin_ISC2013 ob=new Palin_ISC2013();
System.out.print("Enter any sentence : ");
String s=br.readLine();
s=s.toUpperCase();
StringTokenizer str = new StringTokenizer(s,".?! ");
int w=str.countTokens();
String word[]=new String[w];
for(int i=0;i<w;i++)
{
word[i]=str.nextToken();
}
int count=0;
System.out.print("OUTPUT : ");
for(int i=0; i<w; i++)
{
if(ob.isPalin(word[i])==true)
{

count++;
System.out.print(word[i]+" ");
}

if(count==0)
System.out.println("No Palindrome Words");
else
System.out.println("nNumber of Palindromic Words : "+count);
}
}

This is how you can write algorithms in your ISC Practical Examination. So, as you see, this is nothing
but step wise solution to the given program in English language.

You might also like