Unique Number program by methods

How to write a program with methods?
Unique Number program by methods.

Each program has different parts
1. importing package
2. define class with instance variables
3. constructor to initialize variables
4. input method – to read an input
5. calculate method- processing
6. display method – to display the processed data
7. main method- invoke above mentioned methods by creating an object

import java.util.*;
public class UniqueNumber
{
    // instance variables - 
    int n;
    String s;
    int flag,i, j,l;
    /**
     * Constructor for objects of class UniqueNumber
     */
    public UniqueNumber()
    {
        // initialise instance variables
        n = 0;
        flag=0;
        s=" ";
    }
    public void input() // to input an integer
    {
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a number :");

        n=sc.nextInt();
    }
   public void calculate()
    {
       s= Integer.toString(n); // converting the given number into String datatype
// Int datatype is converting to String,
// String helps to extract the character by charAt() and check each character is matching with the rest.
        l=s.length();
//Loops to check the digits are repeated     
    for(i=0;i<l-1;i++)
    {
       for(j=i+1;j<l;j++)
        {
            if(s.charAt(i)==s.charAt(j)) // if any digits are repeated, then it is not a UniqueNumber
            { flag=1;
                break; 
            }
         }
    }
    }
    public void display()
    {
     calculate(); //invoke calculate() 
    if(flag ==0)
    System.out.println(" It is an UniqueNumber");
    else
    System.out.println(" It is not an UniqueNumber");
     }  
      public static void main(String args[]) throws Exception
      {   
    UniqueNumber un=new UniqueNumber(); //creating object 
    un.input();  //calling input() to main()
    un.display(); //calling display() to main()
       }
    }

Output:

Enter a number: 5673

It is an UniqueNumber

Enter a number: 5675

It is not an UniqueNumber

Tips – ISC Computer Science Board Exam

Keep up on the Exam pattern and Syllabus

Learn the concepts with examples

Dont memorize programs, think logically and write

Practice previous year question papers and specimen copies

Notice and follow the examiners comment from analysis.

Revise everyday.


Be Careful – Whenever practising the programs

Read the questions twice before starting your answer

Use the variables, method and class name what has given in the questions. Instead of that dont use your own variable, method and class. (especially Section B & Section C questions)

You have written the program with correct logic and not following the same variable, method and class name reduce your marks.

Dont forget to create main() and invoke specified methods to main(), if its asked in the question paper.

In Section C, Inheritance programs
DONT WRITE super class, main() and algorithm. (if its specified as NOT to be written)

Do more practice on recursion programs.

Back to blog

After a long days, I am back here..

Dear children, hope everyone is preparing well for the ICSE & ISC board examination 2020 …

Last year my students scored 99% in Computer Science ISC.. here I am taking the privilege to share .

I wish the current batch to score 100% in Computer Science, not only in Computer Science , other subjects also….

Wish you all the best my dear children…

Lucky Number

Consider the sequence of natural numbers.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 ………………………………….

Removing every second number produces the sequences
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23 ………………………….

Removing every third number produces the sequences
1, 3, 7, 9, 13, 15, 19, 21, 25 ………………………….

This process continues indefinitely by removing the fourth, fifth…and so on, till after a fixed number of steps, certain natural numbers remain indefinitely. These are known as Lucky Numbers.

Write a program to generate and print lucky numbers less than a given number N.

SAMPLE INPUT :  N = 10
OUTPUT :  The Lucky numbers less than 10 are:     1     3     7

SAMPLE INPUT :  N = 25
OUTPUT :  The Lucky numbers less than 25 are:     1     3     7     13     19

import java.io.*;
class luckyNumber
{
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];
int c=n;
 
for(int i=0;i<n;i++)
{ 
a[i]=i+1;
}
 
int del=1;
System.out.println("\nLucky Number Operation :\n");
 
while(del<n)
{
for(int i=del; i<n; i+=del)
{
for(int j=i; j<n-1; j++)
{
a[j]=a[j+1];
}
n--;
}
del++;
 
for(int i=0; i<n; i++)
{
System.out.print(a[i]+"   ");
}
System.out.println();
} //end of while
 
System.out.print("\nHence, the Lucky Numbers Less than "+c+" are : ");
for(int i=0; i<n; i++)
{
System.out.print(a[i]+"   ");
}
}
}

Output

Enter the Number of Elements : 30

Lucky Number Operation :

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
1 3 7 9 13 15 19 21 25 27
1 3 7 13 15 19 25 27
1 3 7 13 19 25 27
1 3 7 13 19 27

Hence, the Lucky Numbers Less than 30 are : 1 3 7 13 19 27

Magic square matrix

A magic square is an NxN matrix in which every row, column, and diagonal add up to the same number.

import java.io.*;
class magicmatrix
{
    public static void main (String args[]) throws IOException
    {
        InputStreamReader read =new InputStreamReader (System.in);
        BufferedReader x= new BufferedReader(read);
        System.out.println("Enter the size of the matrix");
        int n=Integer.parseInt(x.readLine());
        if(n>5)
        System.out.println("Enter the number from 1 to 5");
        else
        {
            int A[][]=new int[n][n];
            int i,j,k,t;
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    A[i][j]=0;
                }
            }
            if(n%2!=0)
            {
                i=0;
                j=n/2;
                k=1;
                while(k<=n*n)
                {
                    A[i][j]=k++;
                    i--;
                    j++;
                    if(i<0&&j>n-1)
                    {
                        i=i+2;
                        j--;
                    }
                    if(i<0)
                    i=n-1;
                    if(j>n-1)
                    j=0;
                    if(A[i][j]>0)
                    {
                        i=i+2;
                        j--;
                    }
                }
            }
            else
            {
                k=1;
                for(i=0;i<n;i++)
                {
                    for(j=0;j<n;j++)
                    {
                        A[i][j]=k++;
                    }
                }
                j=n-1;
                for(i=0;i<n/2;i++)
                {
                    t=A[i][j];
                    A[i][i]=A[j][j];
                    A[j][j]=t;
                    t=A[i][j];
                    A[i][j]=A[j][i];
                    A[j][i]=t;
                    j--;
                }
            }
            System.out.println("The magic matrix of size "+n+"x"+n+" is:");
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    System.out.print(A[i][j]+"\t");
                }
                System.out.println();
          
            }
        
        }
    }
}
        

Output:

Enter the size of the matrix
3
The magic matrix of size 3×3 is:
8 1 6
3 5 7
4 9 2

Kaprekar number

Write a Program in Java to input a number and check whether it is a Kaprekar number or not.

Note: A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two pieces, a right-hand piece that has ‘d’ digits and a left-hand piece that has remaining ‘d’ or ‘d-1’ digits.

If the sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar number. The first few Kaprekar numbers are: 9, 45, 297 ……..

Example  45

452 = 2025, right-hand piece of 2025 = 25 and left hand piece of 2025 = 20

Sum = 25 + 20 = 45, i.e. equal to the number. Hence, 45 is a Kaprekar number.

import java.io.*;
class KaprekarNum
{
   public static void main(String args[]) throws IOException
   {
     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.print("Enter a Number : ");
     int n = Integer.parseInt(br.readLine()); //Inputting the number
 
     int sq = n*n; //finding the square of the number  
     String s = Integer.toString(sq); //converting the square into a String
 
     if(sq<=9)
        s = "0"+s; //Adding a zero in the beginning if the square is of single digit
 
     int l = s.length(); //finding the length (i.e. no. of digits in the square).
     int mid = l/2; //finding the middle point
 
     String left=s.substring(0,mid); //extracting the left digits from the square
     String right=s.substring(mid); //extracting the right digits from the square
 
     int x = Integer.parseInt(left); //converting the left String into Integer
     int y = Integer.parseInt(right); //converting the right String into Integer
 
     //if sum of left and right numbers is equal to the original number then it is a Kaprekar number
     if(x+y == n) 
        System.out.println(n+" is a Kaprekar Number");
     else
        System.out.println(n+" is Not a Kaprekar Number");
   }
}

Enter a Number : 297
297 is a Kaprekar Number
Enter a Number : 9
9 is a Kaprekar Number
Enter a Number : 67
67 is Not a Kaprekar Number

IMEI number

An IMEI number/ International Mobile Station Equipment Identity is a 15 digit number and it is said to be IMEI number if and only if the sum of the number is exactly divisible by 10.

The IMEI (15 decimal digits: 14 digits plus a check digit) includes information on the origin, model, and serial number of the device.

The IMEI number is used by a GSM network to identify valid devices and therefore can be used for stopping a stolen phone from accessing that network

The IMEI is validated in three steps:

  1. Starting from the right, double every other digit (e.g., 7 becomes 14).
  2. Sum the digits (e.g., 14 → 1 + 4).
  3. Check if the sum is divisible by 10.
Input:  47415403237518
Output:
47415403237518 – Is an IMEI Number.
import java.io.*;
public class CheckIMEINumber{
// Function for finding and returning sum of digits of a number
	int sumDig(int n) 
	{
		// initialise here.
		int a = 0;
	    while(n>0)
	    {
	    	a = a + n%10;
	        n = n/10;
	    }
	    return a;}	     
	public static void main(String args[])throws IOException
	{
	// create object here.
		CheckIMEINumber ob = new CheckIMEINumber();
	    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            // The 'Long' data type is used to store 15 digits.
	    System.out.print("Enter a 15 digit IMEI code : ");
	    long n = Long.parseLong(br.readLine()); 
	// Converting the number into String for finding length
	    String s = Long.toString(n); 
	    int l = s.length();
	// If length is not 15 then IMEI is Invalid
	    if(l!=15) 
	    System.out.println("Output : Invalid Input");
	else
	    {  	int d = 0, sum = 0;
	        for(int i=15; i>=1; i--)
	        {
	        	d = (int)(n%10);
		if(i%2 == 0)
	            {
	        		// Doubling every alternate digit
	        		d = 2*d; 
	            }
	// Finding sum of the digits
	        	sum = sum + ob.sumDig(d); 
	        	n = n/10;
	        }
	             
	        System.out.println("Output : Sum = "+sum);
	          if(sum%10==0)    
	        	System.out.println("Valid IMEI Code");
	        else
	            System.out.println("Invalid IMEI Code");
	    }
	}}


Enter a 15 digit IMEI code : 47415403237518
Output : Invalid Input
Enter a 15 digit IMEI code : 490154203237518
Output : Sum = 60
Valid IMEI Code
Enter a 15 digit IMEI code : 123456789101112131
Output : Invalid Input

Pronic Number/oblong number/ rectangular number / heteromecic number

Pronic Number : A pronic number, oblong number, rectangular number or heteromecic number, is a number which is the product of two consecutive integers, that is, n (n + 1). The first few pronic numbers are: 0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 …

Input :56
Output :Pronic Number
Explanation: 56 = 7 * 8 i.e 56 is a product of two consecutive integers 7 and 8.
import java.util.*;
class PronicNumber
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
         
        System.out.print("Enter a number : ");
        int n = sc.nextInt();
        int flag = 0;
    
        for(int i=0; i<n; i++)
        {
            if(i*(i+1) == n)
            {
                flag = 1;
                break;
            }
        }
         
        if(flag == 1)
            System.out.println(n+" is a Pronic Number.");
        else
            System.out.println(n+" is not a Pronic Number.");      
    }

Enter a number : 123
123 is not a Pronic Number.
Enter a number : 110
110 is a Pronic Number.

Display as Lower triangular matrix program in java

A square matrix is called lower triangular if all the entries above the main diagonal are zero. Similarly, a square matrix is called upper triangular if all the entries below the main diagonal are zero. A triangular matrix is one that is either lower triangular or upper triangular.


import java.util.*;
class lowerTriangular
{
    void lowermat(int matrix[][],  
                      int row, int col) 
    { 
        int i, j; 
        for (i = 0; i < row; i++) 
        { 
            for (j = 0; j < col; j++) 
            { 
                if (i < j) 
                { 
                    System.out.print("0" + " "); 
                } 
                else
                System.out.print(matrix[i][j] + " "); 
            } 
            System.out.println(); 
        } 
    } 
      
 
   public static void main(String args[])
   { Scanner sc=new Scanner(System.in);
       lowerTriangular lt=new lowerTriangular();
        int i,j,count=0,no,size,flag=0;
        System.out.println("Enter size");
        size =sc.nextInt();
        int a[][]=new int[size][size];

        for(i=0;i<size;i++)
        {
            for (j=0;j<size;j++)
            {
                System.out.println("Enter the element("+i+","+j+")");
                a[i][j]=sc.nextInt();
            }
        }
        System.out.println("The given matrix:");
        for(i=0;i<size;i++)
        {
            for (j=0;j<size;j++)
            {
                System.out.print(a[i][j]);
              
            }
            System.out.println();
        }
        System.out.println("Lower triangular matrix");
        lt.lowermat(a,size,size);
    }
}

Output:

Enter size
3
Enter the element(0,0)
1
Enter the element(0,1)
2
Enter the element(0,2)
3
Enter the element(1,0)
4
Enter the element(1,1)
5
Enter the element(1,2)
6
Enter the element(2,0)
7
Enter the element(2,1)
8
Enter the element(2,2)
9
The given matrix:
123
456
789
Lower triangular matrix
1 0 0
4 5 0
7 8 9

Diagonal matrix program in java

A square matrix is said to be diagonal matrix if the elements of matrix except main diagonal are zero.

A square null matrix is also a diagonal matrix whose main diagonal elements are zero.

import java.util.*;
class DiagonalMatrix
{
	public static void main(String args[])throws Exception
	{
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter the size of the matrix : ");
		int m=sc.nextInt();
		int A[][]=new int[m][m];
		
		/* Inputting the matrix */
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<m;j++)
			{
				System.out.print("Enter an element : ");
				A[i][j]=sc.nextInt();
			}
		}

		
		System.out.println("The Matrix is : ");
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<m;j++)
			{
				System.out.print(A[i][j]+"\t");
			}
			System.out.println();
		}
		
		int p=0, q=0;
		
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(i!=j && A[i][j]!=0) // Checking non-diagonal elements
				{
					p=1;
					break;
				}
				if(i==j && A[i][j]==0) // Checking diagonal elements
				{
					q++;
				}
		    }
		}
		
		if(p==0 && q<m)
			System.out.println("The matrix is Diagonal");
		else
			System.out.println("The matrix is not Diagonal");
	}
}

Output:

Enter the size of the matrix : 3
Enter an element : (0,0)1
Enter an element : (0,1)0
Enter an element : (0,2)0
Enter an element : (1,0)0
Enter an element : (1,1)3
Enter an element : (1,2)0
Enter an element : (2,0)0
Enter an element : (2,1)0
Enter an element : (2,2)45
The Matrix is :
1 0 0
0 3 0
0 0 45
The matrix is Diagonal

Scalar matrix program in java

Write a Program in Java to input a 2-D square matrix and check whether it is a Scalar Matrix or not.

Scalar Matrix : A square matrix is said to be scalar matrix if all the main diagonal elements are equal and other elements except main diagonal are zero. Scalar matrix can also be written in form of n * I, where n is any real number and I is the identity matrix.

import java.util.*;
class ScalarMatrix
{
	public static void main(String args[])throws Exception
	{
		Scanner sc=new Scanner(System.in);
		System.out.print("Enter the size of the matrix : ");
		int m=sc.nextInt();
		int A[][]=new int[m][m];
		
		/* Inputting the matrix */
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<m;j++)
			{
				System.out.print("Enter an element :("+i+","+j+") ");
				A[i][j]=sc.nextInt();
			}
		}

		/* Printing the matrix */
		
		System.out.println("The Matrix is : ");
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<m;j++)
			{
				System.out.print(A[i][j]+"\t");
			}
			System.out.println();
		}
		
		
		int p = 0, q = 0, x = A[0][0]; // 'x' is storing the 1st main diagonal element
		
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<m;j++)
			{
			    /* Checking that the matrix is diagonal or not */
				if(i!=j && A[i][j]!=0) // All non-diagonal elements must be zero
				{
					p=1;
					break;
				}
				/* Checking the matrix for scalarity */
				// All main diagonal elements must be equal to 'x' and non-zero
				if(i==j && (A[i][j]==0 || A[i][j]!=x)) 
				{
					q=1;
					break;
				}
		    }
		}
		
		if(p==0 && q==0)
			System.out.println("The matrix is scalar");
		else
			System.out.println("The matrix is not scalar");
	}
}

Output:

Enter the size of the matrix : 3
Enter an element :(0,0) 1
Enter an element :(0,1) 2
Enter an element :(0,2) 1
Enter an element :(1,0) 3
Enter an element :(1,1) 1
Enter an element :(1,2) 3
Enter an element :(2,0) 1
Enter an element :(2,1) 1
Enter an element :(2,2) 3
The Matrix is :
1 2 1
3 1 3
1 1 3
The matrix is not scalar
Enter the size of the matrix : 3
Enter an element :(0,0) 1
Enter an element :(0,1) 0
Enter an element :(0,2) 0
Enter an element :(1,0) 0
Enter an element :(1,1) 1
Enter an element :(1,2) 0
Enter an element :(2,0) 0
Enter an element :(2,1) 0
Enter an element :(2,2) 1
The Matrix is :
1 0 0
0 1 0
0 0 1
The matrix is scalar

Frequency of alphabets

import java.util.Scanner;
public class JavaProgram
{
   public static void main(String args[])
   {
        int ci, i, j, k, l=0;
        String str, str1;
        char c, ch;
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter a String : ");
        str=scan.nextLine();
        
        i=str.length();
        for(c='A'; c<='z'; c++)
        {
            k=0;
            for(j=0; j<i; j++)
            {
                ch = str.charAt(j);
                if(ch == c)
                {
                    k++;
                }
            }
            if(k>0)
            {
                System.out.println("The character " + c + " has occurred for " + k + " times");
            }
        }
   }
}

Enter a String : hai how are you bye take care
The character a has occurred for 4 times
The character b has occurred for 1 times
The character c has occurred for 1 times
The character e has occurred for 4 times
The character h has occurred for 2 times
The character i has occurred for 1 times
The character k has occurred for 1 times
The character o has occurred for 2 times
The character r has occurred for 2 times
The character t has occurred for 1 times
The character u has occurred for 1 times
The character w has occurred for 1 times
The character y has occurred for 2 times

Decimal to roman number

import java.io.*;
class dec2rom
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter a number");
        int num=Integer.parseInt(br.readLine());
        if(num>0&&num<4000)
        {
            String thou[]={"","M","MM","MMM"};
            String hund[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
            String ten[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
            String unit[]={"","I","II","III","IV","V","VI","VII","VIII","IX"};
            int th=num/1000;
            int h=(num/100)%10;
            int t=(num/10)%10;
            int u=num%10;
            System.out.println("roman equivalent="+thou[th]+hund[h]+ten[t]+unit[u]);
        }
        else
         System.out.println("\nyou entered a number out of range \nplease enter a number in the range[1-3999]");
        }
    }

enter a number
1354
roman equivalent=MCCCLIV

Data Structure- Dequeue Program In Java

import java.io.*;
class dequeue
{
    int arr[]=new int[10];
    int f,r;
    int i,n;
    String str;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    dequeue()
    {
        f=-1;
        r=-1;
    }
    public void push()throws IOException
    {
        if(r==9)
        {
            System.out.println("Queue Overflow");
            return;
        }
        System.out.println("Specify the location(front or rear):");
        str=br.readLine().toLowerCase();
        System.out.println("Enter the value to insert:");
        n=Integer.parseInt(br.readLine());
        if(f==-1)
        {
            arr[++f]=n;
            r=0;
        }
        else if(str.charAt(0)=='f')
        {
            for(i=r+1;i>f;i--)
            arr[i]=arr[i-1];
            arr[i]=n;
            r++;
        }
        else
        {
            arr[++r]=n;
        }
    }
    public void display()
    {
        if(f==-1)
        return;
        for(i=f;i<=r;i++)
        System.out.println(""+arr[i]);
    }
    public void pop()throws IOException
    {
        if(f==-1)
        {
            System.out.println("Queue Overflow");
            return;
        }
        System.out.println("Specify the location(front  or rear):");
        str=br.readLine().toLowerCase();
        if(f==r)
        {
            f=-1;
            r=-1;
        }
        else if(str.charAt(0)=='f')
        {
            f++;
        }
        else
        {
            r--;
        }
    }
    public static void main(String args[])throws IOException
    {
        char op;
        BufferedReader br;
        dequeue ob=new dequeue();
        while(true)
        {
            br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("\nPress'P' for Push,'D' for Pop and 'Q' for Quit:");
            op=(char)br.read();
            
            if(op=='p' || op=='P')
            ob.push();
            else if(op=='d' || op=='D')
            ob.pop();
            ob.display();
            if(op=='q' || op=='Q')
            break;
            br=null;
        }
    }
}

press’P’ for push,’D’ for pop and ‘Q’ for quit:
p
Specify the location(f or r):
f
Enter the value to insert:
5

5

press’P’ for push,’D’ for pop and ‘Q’ for quit:
p
Specify the location(f or r):
r
Enter the value to insert:
6

5

6

press’P’ for push,’D’ for pop and ‘Q’ for quit:
p
Specify the location(f or r):
f
Enter the value to insert:
4

4

5

6

press’P’ for push,’D’ for pop and ‘Q’ for quit:
p
Specify the location(f or r):
r
Enter the value to insert:
7

4

5

6

7

press’P’ for push,’D’ for pop and ‘Q’ for quit:
p
Specify the location(f or r):
f
Enter the value to insert:
3

3

4

5

6

7

press’P’ for push,’D’ for pop and ‘Q’ for quit:
d
Specify the location(f or r):
f

4

5

6

7

press’P’ for push,’D’ for pop and ‘Q’ for quit:
d
Specify the location(f or r):
r

4

5

6

press’P’ for push,’D’ for pop and ‘Q’ for quit:
q

4

5

6

Remove repeated words

write a program in java to remove the repeated strings(words) from the input sentence.

import java.io.*;
class repeatwords
{
     public static void main(String args[]) throws IOException
    {
     InputStreamReader read = new InputStreamReader(System.in);
    BufferedReader x=new BufferedReader (read);
    System.out.println("enter any word");
    String s=x.readLine();
    s=s+" ";
    int l=s.length();
    String ans="";
    char ch1,ch2;
    for(int i=0;i<l-1;i++)
    {
        ch1=s.charAt(i);
        ch2=s.charAt(i+1);
        if(ch1!=ch2)
        {
            ans=ans+ch1;
        }
    }
    System.out.println("word after removing repeated characters="+ans);
}
}

Output:

enter any word
javaaaaaa
word after removing repeated characters=java

Time in words

write a program to convert time in numbers to time in words.

import java.io.*;
public class timeinwords
{
    
     public static void main(String args[]) throws IOException
    {
     InputStreamReader read = new InputStreamReader(System.in);
    BufferedReader x=new BufferedReader (read);
     System.out.println("enter hours");
     int h=Integer.parseInt(x.readLine());
      System.out.println("enter minutes");
     int m=Integer.parseInt(x.readLine());
     if((h>=1&&h<=12)&&(m>=0&&m<=59))
     {
         String word[]={"","one ","two ","three ","four ","five ","six ","seven ","eight ","nine ","ten "," eleven "," twelwe "," thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty","twenty one","twenty two","twenty three","twenty four","twenty five","twenty six","twenty seven","twenty eight","twenty nine"};
         String plu,a;
         if(m==1||m==59)
         plu="minute";
         else
         plu="minutes";
         if(h==12)
         a=word[1];
         else
         a=word[h+1];
          System.out.print("output: "\n+h+":"+m+"-----");
          if(m==0)
            System.out.println(word[h]+"o'clock");
            else  if(m==15)
            System.out.println("quarter past"+word[h]);
     else  if(m==30)
            System.out.println("half past"+word[h]);
            else  if(m==45)
            System.out.println("quarter to"+a);
            else  if(m<30)
            System.out.println(word[m]+" "+plu+" past+words[h] ");
            else
            System.out.println(word[60-m]+" "+plu+" to "+a);
        }
        else 
        System.out.println("invalid input!");
    }
}

output:

enter hours
10
enter minutes
55
output:
10:55—–five minutes to eleven

Reverse the string


import java.io.*;
import java.util.*;
class revstr
{
    public void main()throws IOException
    {
        Scanner sc=new Scanner(System.in);
        String str="",word;
        int l,l1,i;
        String a[]=new String[20];
        System.out.println("enter string");
        str=sc.nextLine();
        StringTokenizer data=new StringTokenizer(str);
        l=data.countTokens();
        for(i=0;i<l;i++)
        {
           word=data.nextToken();
           a[i]=word;
        }
        for(i=l-1;i>=0;i--)
        {
           System.out.print(a[i]+" ");
        }
    }
}

OUTPUT:

enter string
one important thing to know, seattle delivered his speech in his native language.
language. native his in speech his delivered seattle know, to thing important one