Friday, 20 December 2019

Java Program to check whether a no. is Disarium or not

import java.util.*;
class Disarium
{
int num;
int size;
public Disarium(int nn)
{
num=nn;
size=0;
}
void countdigit()
{
int n=num;
while(n>0)
{
n=n/10;
size++;
}
}
int sumofDigits(int n,int p)
{
if(n==1)
return 1;
if(n==0)
return 0;
else
return (int)Math.pow(n%10,p)+sumofDigits(n/10,--p);
}
public void check()
{
if(num==sumofDigits(num,size))
System.out.println("Disarium no.");
else
System.out.println("Not a disarium no.");
}
public static void main()
{
System.out.println("Enter no");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
Disarium ob=new Disarium(n);
ob.countdigit();
ob.check();
}
}

No comments:

Post a Comment

Convey your thoughts to authors.