Java programming write a program to input a number and display whether it is a disarium number or not .and a disarium number is a number in which the sum of the digits to the power of their respective position is equal to the number itself .example 135 is equal to 11 + 32 + 53
thebrainwork:
no one is able to do this
Answers
Answered by
14
/* Copy the Code into an IDE.
* Description and Explanation are given as comments
* Some Screenshots are attached
*/
import java.util.Scanner; //Importing Scanner
public class Disarium_Number //Creating Class
{
public static void main(String[] args) //Creating main() function
{
Scanner sc = new Scanner(System.in); //Creating Scanner Object
System.out.println("A Disarium Number is a number in which the sum of the digits to the power of their respective position is equal to the number itself\n");
System.out.print("Enter a number: "); //Asking for User Input
String num = sc.next(); //Scanning User Input as a String, stored in num
try //try-catch blocks are used for handling Exceptions
{
int sum=0; //sum will contain the Sum of digits raised to respective powers
for(int i=0;i<num.length();i++) //Loop variable i will go through each character of String num
{
char ch = num.charAt(i); //Extracting character from num into ch
int digit = Integer.parseInt(Character.toString(ch)); //Converting ch into integer
int newdigit = (int)Math.pow(digit,i+1); //Raising each digit to respective power
sum = sum + newdigit; //Adding newdigit to sum
}
String newnum = Integer.toString(sum); //Converting sum into a String
if(num.equals(newnum)) //Comparing num and newnum
{
System.out.println(num+" is a Disarium Number.");
}
else
{
System.out.println(num+" is not a Disarium Number.");
}
}
catch(Exception e) //Usually, NumberFormatException can arise if input is not a number. But general case used.
{
System.out.println("Invalid Input");
System.out.println(e);
}
}
}
* Description and Explanation are given as comments
* Some Screenshots are attached
*/
import java.util.Scanner; //Importing Scanner
public class Disarium_Number //Creating Class
{
public static void main(String[] args) //Creating main() function
{
Scanner sc = new Scanner(System.in); //Creating Scanner Object
System.out.println("A Disarium Number is a number in which the sum of the digits to the power of their respective position is equal to the number itself\n");
System.out.print("Enter a number: "); //Asking for User Input
String num = sc.next(); //Scanning User Input as a String, stored in num
try //try-catch blocks are used for handling Exceptions
{
int sum=0; //sum will contain the Sum of digits raised to respective powers
for(int i=0;i<num.length();i++) //Loop variable i will go through each character of String num
{
char ch = num.charAt(i); //Extracting character from num into ch
int digit = Integer.parseInt(Character.toString(ch)); //Converting ch into integer
int newdigit = (int)Math.pow(digit,i+1); //Raising each digit to respective power
sum = sum + newdigit; //Adding newdigit to sum
}
String newnum = Integer.toString(sum); //Converting sum into a String
if(num.equals(newnum)) //Comparing num and newnum
{
System.out.println(num+" is a Disarium Number.");
}
else
{
System.out.println(num+" is not a Disarium Number.");
}
}
catch(Exception e) //Usually, NumberFormatException can arise if input is not a number. But general case used.
{
System.out.println("Invalid Input");
System.out.println(e);
}
}
}
Attachments:
Answered by
14
The answers which I saw above looked a little complicated. The one which I am posting might seem a little easy for beginners as well as pros also....
import java.util.*;
class disarium
{public static void main()
{Scanner sn = new Scanner(System.in);
int n, n1, d=0, s=0, c=0;
System.out.println("Enter a number");
n = sn.nextInt();
n1 = n;
while(n>0)
{n = n/10;
c++;
}
while(n>0)
{d = n%10;
s = s+(int)(Math.pow(d,c));
n = n/10;
c--;
}
if(s==n1)
System.out.println(n1+" is a disarium number");
else
System.out.println(n1+" is not a disarium number");
}
}
Similar questions