Today I will show you how to check a number whether it is automorphic number or not in BlueJ program. Before I proceed further, few clarifications are needed.
For any number we have to find the number of digits in it. From the square value of the number take digits equal to the number of digits of the number from the right side. If the original number and the part of the number are found equal on checking then the entered number is an automorphic number.
For example, 25 is an automorphic number. Number of digits in 25 is 2 and square value of 25 is 625. We have to take 2 extreme right digits from 625 as there are 2 digits in the original entered number. Two extreme right digits of 625 is 25 which matches with original number 25. So 25 is an automorphic number. Similarly 5 is also an automorphic number.
import java.io.*;
class Automorphic
{
int i,n,no,sqnum,rev=0,digit=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void getNumber() throws Exception
{
System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
no=n;
do
{
digit++;
no=no/10;
} while(no!=0);
sqnum=n*n;
}
public void showResult()
{
do
{
rev=rev*10+sqnum%10;
sqnum=sqnum/10;
digit--;
if(digit==0)
break;
} while(true);
rev=reverse(rev);
if(n==rev)
System.out.println(n+" is an Automorphic number");
else
System.out.println(n+" is not an Automorphic number");
}
private int reverse(int n)
{
int r=0;
while(n!=0)
{
r=r*10+n%10;
n=n/10;
}
return r;
}
public static void main(String args[])throws Exception
{
Automorphic obj=new Automorphic();
obj.getNumber();
obj.showResult();
}
}
Three functions are defined in this program. The function ‘public void getNumber()’ takes the number from user, counts the digits in it using a do while loop and calculate the square value of the number.
Within the body of another function ‘public void showResult()’ in this program, the digits from the square value is retrieved with proper checking so that the digits retrieved is equal to the number of digits in the original number. This retrieval of digits is performed using do while loop in this program.
The third defined function in this program ‘private int reverse(int n)’ defined in this class is to reverse a number, as from 625 on extracting two digits we will get 52 but we need it as 25.
Related Post: BlueJ Programs on Number
What is automorphic number
For any number we have to find the number of digits in it. From the square value of the number take digits equal to the number of digits of the number from the right side. If the original number and the part of the number are found equal on checking then the entered number is an automorphic number.
For example, 25 is an automorphic number. Number of digits in 25 is 2 and square value of 25 is 625. We have to take 2 extreme right digits from 625 as there are 2 digits in the original entered number. Two extreme right digits of 625 is 25 which matches with original number 25. So 25 is an automorphic number. Similarly 5 is also an automorphic number.
Codes of the automorphic number program
import java.io.*;
class Automorphic
{
int i,n,no,sqnum,rev=0,digit=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void getNumber() throws Exception
{
System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
no=n;
do
{
digit++;
no=no/10;
} while(no!=0);
sqnum=n*n;
}
public void showResult()
{
do
{
rev=rev*10+sqnum%10;
sqnum=sqnum/10;
digit--;
if(digit==0)
break;
} while(true);
rev=reverse(rev);
if(n==rev)
System.out.println(n+" is an Automorphic number");
else
System.out.println(n+" is not an Automorphic number");
}
private int reverse(int n)
{
int r=0;
while(n!=0)
{
r=r*10+n%10;
n=n/10;
}
return r;
}
public static void main(String args[])throws Exception
{
Automorphic obj=new Automorphic();
obj.getNumber();
obj.showResult();
}
}
Technical analysis of the automorphic number program
Three functions are defined in this program. The function ‘public void getNumber()’ takes the number from user, counts the digits in it using a do while loop and calculate the square value of the number.
Within the body of another function ‘public void showResult()’ in this program, the digits from the square value is retrieved with proper checking so that the digits retrieved is equal to the number of digits in the original number. This retrieval of digits is performed using do while loop in this program.
The third defined function in this program ‘private int reverse(int n)’ defined in this class is to reverse a number, as from 625 on extracting two digits we will get 52 but we need it as 25.
Related Post: BlueJ Programs on Number
//sir I present much shorter version
ReplyDeleteimport java.io.*;
class auto
{
public static void main(String args[])throws IOException
{
BufferedReader d=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number");
int n=Integer.parseInt(d.readLine());
String n1="",n2="";
n1=Integer.toString(n);
int t=n1.length();
n2=Integer.toString(n*n);
int t1=n2.length();
if(n1.equals(n2.substring((t1-t),t1)))
{System.out.println("Automorphous");}
else
{
System.out.println("non Automorphous");
}
}
}
Thanks.
ReplyDeleteSir Can you post the programm of all possible combinations of a given number?
ReplyDeleteex:
n=15
Output:
1+1+1+1+1
1+1+1+2
1+2+2
1+1+3
1+4
2+3
I think the number in your program would be 5.
ReplyDeleteimport java.io.*;
class Combination
{
int sum,n,i,j;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void take() throws Exception
{
System.out.println("\nEnter the Number:");
n=Integer.parseInt(br.readLine());
System.out.println("\nThe combinations are\n");
for (i=n-1;i>0;i--)
{
sum=i;
System.out.print(i+" ");
for(j=1;jn)
{
j=j-(sum-n);
System.out.print(j+ " ");
break;
}
else
System.out.print(j+ " ");
}
System.out.println();
}
}
public static void main(String args[])throws Exception
{
Combination ob=new Combination();
ob.take();
}
}
the program of n=5
ReplyDeleteoutput:
1+1+1+1+1
1+1+1+2
1+1+3
1+4
2+3
is not giving the correct result sir. please can you do it again!
Please check the same page. I have posted 2 more programs.
ReplyDeleteSir can you post the program of printing a pattern
ReplyDeleteex
1
2 3 4
5 6 7 8
sir please help me how do i right programs?
ReplyDeletePlease come with specific problems, I am here to help you.
ReplyDeletePlease go through my previous posts on 'pattern using nested loop', you will such programs there.
ReplyDeletesir post me a program on pattern:
ReplyDelete12345
51234
45123
34512
23451
Posted in comment section
ReplyDeletesir post me program "to accept a string then display new string after removing vowels i'v written a code:sir I'V ACCPTD STRING IN UPPERCASE
ReplyDeleteString word="";
for(int i=0;i<len;i++)
{
char ch=str.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
//i thnk below line is wrong//
word=word+str.charAt(i+1)
else
{
word=word+ch;
}
word="";
SIR IM NOT ABLE TO DO THIS PROGRAM SIR HELP ME
QUES:"WAP TO ACCEPT A STRING AND DSPLAY NEW STRING AFTER REMOVING VOWELS"
sir please help me in program"to accept a string and then print it after removing vowels"
ReplyDeletesir plz help me in program "to accept anumber of any length and print all digits that arew repeated"sir im not able to do this program i'v tried but failed
ReplyDeletesir also prgram to accept a string and then print it after rewmoving vowels
ReplyDeletesir post me a program to input number of any length and print repeated digits of number
ReplyDeletesir please help me in program to accept a number and print repeated digits in number sir iv posted this question earlier bt u ddnt reply plz help me sir
ReplyDeletesir plz plz aap kyun nahi vo program "to accept a number and display the repeated digits of that number" kyun ni post kar rhe sir mene use try kiya par vo ni aarha plz sir jaldi se jald post kar diye
ReplyDeletePlease check my post on March 17, 2011 in this blog.
ReplyDeletePlease check my post on March 17, 2011 in this blog.
ReplyDeleteSorry for the late. Please check my post on March 17, 2011 in this blog.
ReplyDeletePlease check my post on March 17, 2011 in this blog.
ReplyDeletePlease check my post on March 17, 2011 in this blog.
ReplyDeletethankyou sir very much
ReplyDeletesir help me out in program to print all the elements above the left diagonal and all elements below right diagonal of a 2d array sir wat abt ur MODIFIED CODES OF SMITH NUMBER PROGRAM U TOLD ME BY THE NEXT WEEK U'LL POST MODIFIED CODE OF SMITH NUMBER PROGRAM
ReplyDeleteSorry Panchi, I totally forgot about the smith number program.
ReplyDeleteRegarding the first program, check my today's post
Here is the modified codes of Smith Number. Feel free to comment if you feel it tough.
import java.io.*;
class Smith1
{
int m,no,n,j,sum=0,sumfactors=0,i;
boolean bool;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeNumber() throws Exception
{
System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
m=n;
for(i=n;i>0;i=i/10)
sum=sum+i%10;
/* 'sum' holds the sum of the digits */
/* now find the prime factors and there sum*/
for(i=1;i<=n;i++)
{
if(n%i==0)
{
/* If the control enters here, means 'i' is a factor 'n', next check whether it is prime or not */
for(j=2;j0)
{
sumfactors=sumfactors+no%10;
no=no/10;
}
n=n/i;
i--;
}
}
}
System.out.println(sum+ " "+sumfactors);
if(sum==sumfactors)
System.out.println(m+ " is a smith number.");
else
System.out.println(m+ " is not a smith number.");
}
public static void main(String args[])throws Exception
{
Smith1 obj=new Smith1();
obj.takeNumber();
}
}
sir im nt able to understnd from line
ReplyDeletefor(j=2;j0)
{
sumfactors+=no%10;
no=no/10;
}
n=n/i;/*why u hv divided by "i"why nt by "j"/
i--;"why u hv decreamented i"
WY U HV STORED THE NUMBER FROM "n" to "m" NO USE OF "m" IS IN THE PROGRAM ? IN ABV LINE WHY U HV TAKEN "no" INSTEAD OF "n"?
Same problem again.
ReplyDeleteActually comments box never checks html tags. The '<' operator in loop creates the problem and you are getting wrong codes. These never happens while posting in normal post as html tags are checked there.
import java.io.*;
class Smith1
{
int m,no,n,j,sum=0,sumfactors=0,i;
boolean bool;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeNumber() throws Exception
{
System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
m=n;
for(i=n;i >0;i=i/10)
sum=sum+i%10;
/* 'sum' holds the sum of the digits */
/* now find the prime factors and there sum*/
for(i=1;i< =n;i++)
{
if(n%i==0)
{
/* If the control enters here, means 'i' is a factor 'n', next check whether it is prime or not */
for(j=2;j< i;j++)
{
if(i%j==0)
break;
}
if(j==i)
{
/* control enters here if 'i' is prime */
no=i;
/* the factor may be greater that '9', means double digit number. In such case we have to
add the digits. e,g if you have entered 22, the prime factors are 2 and 11. Sum of the
digits is 2+2=4. Sum of the factors will be 2+1+1=4. For the factor 11, the number should be broken into digits*/
while(no>0)
{
sumfactors=sumfactors+no%10;
no=no/10;
}
n=n/i;
i--;
}
}
}
if(sum==sumfactors)
System.out.println(m+ " is a smith number.");
else
System.out.println(m+ " is not a smith number.");
}
public static void main(String args[])throws Exception
{
Smith1 obj=new Smith1();
obj.takeNumber();
}
}
thankyou sir for taking trouble.
ReplyDeleteIs the program clear to you now?
ReplyDeletesir plese post the program to accept a srting and print the total number of words also displat the word with maximum characters
ReplyDeletePlease check my latest post. The program is already posted.
ReplyDeletesir post the progrm on special number on class basis
ReplyDeleteCheck my today's post
ReplyDeletesir please post me the solved 1sc 2006 computer thteory paper
ReplyDeletePlease post the question paper
ReplyDeleteplese post me program to accept an array of integers and then insert a number in that array also delete a no from the array using functions
ReplyDeletePlease go through my programs on array, you will find such programs.
ReplyDeleteplz sir post me the progrm on the question gven to you also post tech anlysis of such progrms im nt able to do such prgrms HELP HELP
ReplyDeletePlease check the latest post.
ReplyDeletesir pls respond to my query im in plight HELLLLLLLLLLLLLLLLLLP HELLLLLLLLLLLLLLLLLLLLLP.................
ReplyDeleteCheck today's post
ReplyDeleteSir can u plz help me to solve this question /
ReplyDelete"(Using String) accpet a number and check whether it is automorphic no. or not"
waiting for ur reply
Check my today's post
ReplyDeletehow do i wap for arranging words in a string in alphabetical order
ReplyDeleteVisit the page http://schooljava.blogspot.com/2010/08/isc-bluej-program-from-sample-paper.html' for Similar program.
ReplyDeletesir help me out to wap to enter a SENTENCE and count total words in the string NOTE THAT SENTENCE MAY CONTAIN MORE THAN ONE BLANK SPACES BETWEEN WORDS
ReplyDeletePlease go through my string related programs, you will find similar programs.
ReplyDeletesir im not able to find such programs sir plz post me the solution of the programto enter a SENTENCE and count total words in the string NOTE THAT SENTENCE MAY CONTAIN MORE THAN ONE BLANK SPACES BETWEEN WORDS
ReplyDeletePlease check my latest post in this site.
ReplyDeleteSIR PLZ TELL ME WHATS THE NEED OF INHERITANCE.
ReplyDeleteBECAUSE ACC TO JAVA INHERITANCE IS USEDTO INHERIT THE PROPERTIES OF ONE CLASS BY SOME OTHER CLASS SINCE WE CAN DO THE SAME THING IF WE DEFINE MEMBERS OF ONE CLASS AS PUBLIC ,PROTECTED TO USE THEM IN ANOTHER CLASS SO WHY INHERITANCE IS USED?
What you have written here is called containership. Containership means where a class contains objects of another class as a data member. Inheritance is something different from containership.
ReplyDeleteDifference between inheritance and containership can be explained as:
A child inherites his parents, means acquires their properties. Again suppose in a room four friends are living and they are helping each other. This is containership, all the members are doing any job jointly but no one has acquired properties of the other.
Panchi, Please do not write in capital letters. Here is your program on prime number using recursive function.
ReplyDeleteimport java.io.*;
class Prime
{
int i=2;
boolean bool=true;
public void prime(int n)
{
if(n%i==0)
bool=false;
if(i==n-1)
return;
i++;
prime(n);
}
public static void main(String args[]) throws Exception
{
Prime ob=new Prime();
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number:");
n=Integer.parseInt(br.readLine());
ob.prime(n);
if(ob.bool)
System.out.println(n+ " is a prime number:");
else
System.out.println(n+ " is not a prime number:");
}
}
sorry sir! sir plz tell me that in isc question paper are we allowed to use our own declared instance variables in addition to those given in question paper as in program of prime no posted by u,u used variable 'i' and the question of this program contains the only instance variable to be used is "num" so if i take some instance variable by myself so will my marks be deducted.sir plz tll me im always confsed regarding this.
ReplyDeleteIf you are trying to find whether 'num' is a prime number or not, you have to carry on the process of modulo division of 'num' by 2,3,4.....num-1. To generate the numbers 2,3,4..., you must have another variable otherwise how the numbers will be generated?
ReplyDeleteSIR IM INCAPABLE TO UMDRSTND PROGRAM ON CALCULATING ELECTRICITY BILLS PLZ HELP ME SIR FOR SUCH PRGRMS :
ReplyDeleteTO CALCULATE BILL WHWN USER INPUTS PARTICULAR UNITS:
//SIR PLZ AAP KHUD SE VARIABLES FUNC LE LIYE GA HM SIR CALLS AND RATE DE RHE HE//
NO Of calls: rate
1-50 free
for next 100units 4paise/unit
for next 200units 6paise/unit
ab0ve 300 1Rs/unit
sir im not able to understnd how to do such prgrms .also this type given below:
NO Of calls: rate
1-100 free
101-200 1RS/UNIIT
201-300 1.20RS/unit
above 300 1.50RS/unit
sir wht is difference in the codes of 1and 2 questions plz send technical analysis of programs
Search 'Guess BlueJ program in ICSE 2011 using else if ladder' in this blog and you will get the solution. If any problem, post here.
ReplyDeleteSIR A VERY HAPPY AND PROSPEROUS DIWALI TO YOU
ReplyDeletesir plz can you post some expected questions in isc class 12 THEORY paper. sir plz post questions and solutions on computer hardware(logic gates,adder,multiplexer etc which are in syllabus)plz plz sir i shall be very thankful to you sir.
Same to you Panchi. Very soon I will post expected theory paper on ISC computer 2012. Sorry I can't help you in hardware section.
ReplyDeletesir help me in this prgrm:
ReplyDeletewap to input two valid dates each comprising of day(2 digits),month(2 digits).year(4 digits)
and calculate no of days elapsed b/w two dates
input: 1st date
day: 24
month: 09
year:1960
2nd date:
dayday: 08
month: 01 year:1852
Recently I have posted similar program in this blog.
ReplyDeleteSir, Please tell me how to print the pattern in BLueJ:
ReplyDelete1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
class Ser
ReplyDelete{
int i,j;
public void show()
{
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
How to print:
ReplyDelete____1
___12
__123
_1234
12345
(previous series in RIGHT ALLIGNED form)
public class Pat
ReplyDelete{
int i,j,k;
public void show()
{
for(i=1;i<=5;i++)
{
for(k=i;k<=5;k++)
System.out.print(" ");
for(j=1;j<=i;j++)
System.out.print(j);
System.out.println();
}
}
public static void main(String args[])
{
Pat ob=new Pat();
ob.show();
}
}
Hello sir,can you plz help me out in solving the program to store n integers(n>0) in a SDA and store only palindrome numbers in another array
ReplyDeleteSample Input: 112,220,141,121,129,233,161,201,514,154,120,132,181,98
Output: 141,121,161,181
import java.io.*;
Deleteclass Arr
{
int arr[]=new int[15];
int palin[]=new int[15];
int x=0;
int i,j;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void show() throws IOException
{
int rev;
for(i=0;i<15;i++)
{
System.out.print("Enter 1st Number: ");
arr[i]=Integer.parseInt(br.readLine());
}
for(i=0;i<15;i++)
{
rev=0;
for(j=arr[i];j>0;j=j/10)
{
rev=rev*10+j%10;
}
if(rev==arr[i])
palin[x++]=arr[i];
}
System.out.print("\nPalindrome numbers are=");
for(i=0;i<x;i++)
System.out.print(" "+palin[i]);
}
}
Sir, please help me to write a program to print all possible combinations of any three digit no.
ReplyDeleteeg. for 123 it is 123,321,132,231,213,312
Please check this page
DeleteThank you, Sir.
ReplyDeleteSir, your posts have helped me greatly.
ReplyDeletePlease help in-
WAP to generate prime no.s from 1-100.
Thank You
class Pat
Delete{
public void show()
{
int i;
System.out.println(" The prime numbers between 1 and 100 are: ");
for(i=1;i<=100;i++)
{
if(prime(i))
System.out.print(" "+i);
}
}
private boolean prime(int i)
{
int j;
for(j=2;j<i;j++)
{
if(i%j==0)
break;
}
if(j==i)
return true;
else
return false;
}
}