hi guys these are two answers to the disarmium question everyone has been bothered about- without using string functions
import java.util.Scanner;
public class disarmy1
{
void hello()
{ Scanner in = new Scanner(System.in);
System.out.println("enter the number ahole");
int s = in.nextInt();
int a = s;
int b=s;
int d=0;//to find the digits
int c=0;//to count the number of digits
int i,e=0,sum=0;
while(s>0)
{ d= s%10;
c++;
s=s/10;
}
for(i=c;i>=1;i--)
{
e= a%10;
sum = sum+ (int)(Math.pow(e,i));//since i is equal to number of digits
a=a/10;
}
if(sum==b)
System.out.println("disarmium number");
else
System.out.println("not");
}
}
//THIS IS THE SECOND ANSWER TO THE QUESTION WHICH IS A LITTLE HARDER TO COPREHEND
import java.util.Scanner;
public class disarmy2
{
void hello()
{
//this is using arrays
Scanner in = new Scanner(System.in);
System.out.println("enter the number ahole");
int s = in.nextInt();
int n =s;
int q=s;
int d=0;
int i;
int c=0;
int x=0;
int sum=0;
while(s>0)//to find the number of digits the number has
{d = s%10;
c++;
s=s/10;
}
int a[]= new int[c];
for(i=0;i<c;i++)//to put all these digits into an array
{
x=n%10;
a[i]= x;
n=n/10;
}
for(i=c-1;i>=1;i--)//taking array from the last element and adding the power of index+1 of that number to sum
{
sum = sum+ (int)Math.pow(a[i],i+1);
}
if(sum==q)
System.out.println("disarmium");
else
System.out.println(" not");
}
} |
|
| asked by Sankhya Gowda (taankkkkk) 7 years ago | |
2 | |
Other Questions on This Topic
Chhod aaye the kinara kabhi apni talash mein, Par dil ki gehrayi se ek aisi aawaz aayi, Ki lautna pada hamein isi mod ki talash mein. |
|
| asked by Nisarg Bhavsar (nisarg71) 26 days ago | |
2 | |
Bengali grammar icse question paper can I get? |
|
| asked by Debosmita Malik (debosmitamalik) 80 days ago | |
1 | |
I wish to download question papers for ICSE prelims and I cannot do that. Can you provide the details of the Customer support for Respaper to get in touch with regarding the issue mentioned above. |
|
| asked by S.Hemalata Rajaram (hema1877) 184 days ago | |
1 | |

