Previously I have posted a program on checking Automorphic number. In that program value of ‘int’ type was used through out the program to check whether the entered value is automorphic or not. Today I will show you the same program but the entire job, from taking value from user to checking will be done on string value.
Codes of the program
import java.io.*;
class Automorphic
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String w1,w2,w3=" ";
int i,len1,len;
public void takeNumber() throws Exception
{
System.out.println("Enter the number:");
w1=br.readLine();
w2=String.valueOf(Integer.parseInt(w1)*Integer.parseInt(w1));
len=w1.length();
len1=w2.length();
for( i=len1-1;i >=0;i--)
{
w3=w2.charAt(i)+w3;
len--;
if(len==0)
break;
}
w3=w3.trim();
if(w1.equals(w3))
System.out.println(w1+ " is an automorphic number.");
else
System.out.println(w1+ " is not an automorphic number.");
}
public static void main(String args[]) throws Exception
{
Automorphic ob=new Automorphic();
ob.takeNumber();
}
}
Technical analysis of the program
‘w1’ holds the entered number as string value and ‘w2’ holds the square of the number which is stored as string in ‘w1’. We have to extract the ‘len’ number of characters from ‘w2’ in reverse order where ‘len’ is the length of ‘w1’, the original number in string format. Using a for loop the job is performed and the value is stored in ‘w3’ another string object. Both the strings ‘w1’ and ‘w3’ are compared for equality to check whether entered value is automorphic or not.
Sir please help me for the following program:
ReplyDeleteCount the frequency of each word in a string.
Check the post with title 'ISC-2010 COMPUTER SCIENCE PRACTICAL EXAM'
ReplyDelete