Friday, June 28, 2019

Disarium Number | Programming | Eclipse JAVA | Follow on Youtube for more



import java.util.*;
public class disarium_number
{
static boolean check(int n1)
{
//Reversing the number
String s = Integer.toString(n1),s2 = "";
//Reverse loop
for(int i=0;i<s.length();i++)
{
char c = s.charAt(i);
s2 = c+s2;// reverse number in string format
}
int n = Integer.parseInt(s2);
double sum = 0;
int power = 1;
//checking the number
while(n!=0)//531
{
int rem = n%10;
sum = sum+Math.pow(rem,power);
n = n/10;
power++;
}
System.out.println("The sum is "+sum+" and Original Number is "+n1);
if(sum==n1)
return true;
else
return false;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//taking user input
System.out.println("Enter the number to be checked");
int n = sc.nextInt();
//sending the number to be checked
boolean b = check(n);
if(b==true)
System.out.println(n+" is a disarium number");
else
System.out.println(n+" is not a disarium number");
sc.close();
}
}

/*
Input   : n = 135
Output  : Yes
1^1 + 3^2 + 5^3 = 135
Therefore, 135 is a Disarium number

Input   : n = 89
Output  : Yes
8^1+9^2 = 89
Therefore, 89 is a Disarium number

Input   : n = 80
Output  : No
8^1 + 0^2 = 8
 */

No comments:

Post a Comment

ICSE COMPUTER SCIENCE SPECIMEN PAPER | QUESTION 7 | PLEASE DO WATCH THE VIDEO

  import  java.util.*; class icse_specimen_1 {     public static void main(String[] args)     {         Scanner sc1 = new Scanner(System.in)...