Tuesday, January 25, 2011

BlueJ Program On Automorphic Number And Technical Analysis Of The Program

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.

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 PostBlueJ Programs on Number

73 comments:

  1. //sir I present much shorter version

    import 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");
    }
    }
    }

    ReplyDelete
  2. Sir Can you post the programm of all possible combinations of a given number?
    ex:
    n=15
    Output:
    1+1+1+1+1
    1+1+1+2
    1+2+2
    1+1+3
    1+4
    2+3

    ReplyDelete
  3. I think the number in your program would be 5.

    import 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();
    }
    }

    ReplyDelete
  4. the program of n=5
    output:
    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!

    ReplyDelete
  5. Please check the same page. I have posted 2 more programs.

    ReplyDelete
  6. Sir can you post the program of printing a pattern
    ex
    1
    2 3 4
    5 6 7 8

    ReplyDelete
  7. sir please help me how do i right programs?

    ReplyDelete
  8. Please come with specific problems, I am here to help you.

    ReplyDelete
  9. Please go through my previous posts on 'pattern using nested loop', you will such programs there.

    ReplyDelete
  10. sir post me a program on pattern:
    12345
    51234
    45123
    34512
    23451

    ReplyDelete
  11. sir 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
    String 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"

    ReplyDelete
  12. sir please help me in program"to accept a string and then print it after removing vowels"

    ReplyDelete
  13. sir 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

    ReplyDelete
  14. sir also prgram to accept a string and then print it after rewmoving vowels

    ReplyDelete
  15. sir post me a program to input number of any length and print repeated digits of number

    ReplyDelete
  16. sir 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

    ReplyDelete
  17. sir 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

    ReplyDelete
  18. Please check my post on March 17, 2011 in this blog.

    ReplyDelete
  19. Please check my post on March 17, 2011 in this blog.

    ReplyDelete
  20. Sorry for the late. Please check my post on March 17, 2011 in this blog.

    ReplyDelete
  21. Please check my post on March 17, 2011 in this blog.

    ReplyDelete
  22. Please check my post on March 17, 2011 in this blog.

    ReplyDelete
  23. thankyou sir very much

    ReplyDelete
  24. sir 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

    ReplyDelete
  25. Sorry Panchi, I totally forgot about the smith number program.

    Regarding 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();
    }
    }

    ReplyDelete
  26. sir im nt able to understnd from line
    for(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"?

    ReplyDelete
  27. Same problem again.
    Actually 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();
    }
    }

    ReplyDelete
  28. thankyou sir for taking trouble.

    ReplyDelete
  29. Is the program clear to you now?

    ReplyDelete
  30. sir plese post the program to accept a srting and print the total number of words also displat the word with maximum characters

    ReplyDelete
  31. Please check my latest post. The program is already posted.

    ReplyDelete
  32. sir post the progrm on special number on class basis

    ReplyDelete
  33. sir please post me the solved 1sc 2006 computer thteory paper

    ReplyDelete
  34. Please post the question paper

    ReplyDelete
  35. plese 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

    ReplyDelete
  36. Please go through my programs on array, you will find such programs.

    ReplyDelete
  37. plz 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

    ReplyDelete
  38. Please check the latest post.

    ReplyDelete
  39. sir pls respond to my query im in plight HELLLLLLLLLLLLLLLLLLP HELLLLLLLLLLLLLLLLLLLLLP.................

    ReplyDelete
  40. Sir can u plz help me to solve this question /
    "(Using String) accpet a number and check whether it is automorphic no. or not"


    waiting for ur reply

    ReplyDelete
  41. how do i wap for arranging words in a string in alphabetical order

    ReplyDelete
  42. Visit the page http://schooljava.blogspot.com/2010/08/isc-bluej-program-from-sample-paper.html' for Similar program.

    ReplyDelete
  43. sir 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

    ReplyDelete
  44. Please go through my string related programs, you will find similar programs.

    ReplyDelete
  45. sir 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

    ReplyDelete
  46. Please check my latest post in this site.

    ReplyDelete
  47. SIR PLZ TELL ME WHATS THE NEED OF INHERITANCE.
    BECAUSE 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?

    ReplyDelete
  48. 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.

    Difference 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.

    ReplyDelete
  49. Panchi, Please do not write in capital letters. Here is your program on prime number using recursive function.


    import 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:");
    }
    }

    ReplyDelete
  50. 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.

    ReplyDelete
  51. If 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?

    ReplyDelete
  52. SIR IM INCAPABLE TO UMDRSTND PROGRAM ON CALCULATING ELECTRICITY BILLS PLZ HELP ME SIR FOR SUCH PRGRMS :
    TO 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

    ReplyDelete
  53. 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.

    ReplyDelete
  54. SIR A VERY HAPPY AND PROSPEROUS DIWALI TO YOU
    sir 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.

    ReplyDelete
  55. 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.

    ReplyDelete
  56. sir help me in this prgrm:
    wap 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

    ReplyDelete
  57. Recently I have posted similar program in this blog.

    ReplyDelete
  58. Sir, Please tell me how to print the pattern in BLueJ:
    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5

    ReplyDelete
  59. class Ser
    {
    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();
    }
    }
    }

    ReplyDelete
  60. How to print:
    ____1
    ___12
    __123
    _1234
    12345

    (previous series in RIGHT ALLIGNED form)

    ReplyDelete
  61. public class Pat
    {
    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();
    }
    }

    ReplyDelete
  62. 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
    Sample Input: 112,220,141,121,129,233,161,201,514,154,120,132,181,98
    Output: 141,121,161,181

    ReplyDelete
    Replies
    1. import java.io.*;
      class 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]);
      }
      }

      Delete
  63. Sir, please help me to write a program to print all possible combinations of any three digit no.
    eg. for 123 it is 123,321,132,231,213,312

    ReplyDelete
  64. Sir, your posts have helped me greatly.
    Please help in-
    WAP to generate prime no.s from 1-100.
    Thank You

    ReplyDelete
    Replies
    1. class Pat
      {

      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;
      }
      }

      Delete

Subscribe via email

Enter your email address:

Delivered by FeedBurner