Automorphic number
/* Write a program to check whether an entered number is an automorphic number or not.
* A number is said to be an automorphic number if it is present in the last digits of its square
*/
import java.io.*;
class automorphic
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int m,b,p;
double r=0;
System.out.println("Enter a number");
int n=Integer.parseInt(br.readLine());
m=n;
p=m*m;
int c=0;
do
{
c++;
m=m/10;
}while(m!=0);
int q= p%(int)Math.pow(10,c);
if(n==q)
System.out.println(n+" is an Automorphic number");
else
System.out.println(n+" is not an Automorphic number");
}}
i dont get why you have done c++ or taken c
ReplyDeleteThis comment has been removed by the author.
DeleteC is a counter variable
Delete'c' is to count the frequency of digits in the entered number
ReplyDeleteawesome but could you explain what is being done in q= p%(int)Math.pow(10,c);
ReplyDeletepublic class Automorphic
Delete{
public static void main(int num)
{
int a,b;
a=num;
b=a*a;
int c=0;
do
{
c++;
a=a/10;
}
while(a!=0);
int x=b%(int)Math.pow(10,c);
if(num==x)
System.out.println(num +" is an Automorphic number");
else
System.out.println(num +" is not an Automorphic number");
}
}
if num=5
b=25
a=a/10 \\ a= 2
x= 25%10
x=5
got it!!!!!!!!!!!!
a=5
Deletea/10=5??
No a/10 is 0 but c++ = 1
DeleteThis comment has been removed by the author.
Deleteit worked! but plz explain q= p%(int)Math.pow(10,c);
ReplyDeletethanks.
public class Automorphic
Delete{
public static void main(int num)
{
int a,b;
a=num;
b=a*a;
int c=0;
do
{
c++;
a=a/10;
}
while(a!=0);
int x=b%(int)Math.pow(10,c);
if(num==x)
System.out.println(num +" is an Automorphic number");
else
System.out.println(num +" is not an Automorphic number");
}
}
if num=5
b=25
a=a/10 \\ a= 2
x= 25%10
x=5
got it
v good
ReplyDeleteThora simple Mai prog batana Bhai
ReplyDelete