0
What's wrong here. Iwant to check whether the number is disarium no. or not. Disarium no is like 135. (1^1 + 3^2 + 5^3=135)
import java.util.*; public class Program { public static void main(String[] args) { Scanner sc= newScanner(System.in); int i,n,d,k,c=0,sum=0; n=sc.nextInt(); for(i=n;i>0;i++) {c++; } d=n; for(i=c;i>0;i--) { k=d%10; sum=sum + Math.pow(k,i) ; d=d/10; } if(sum==n) System.out.println(n +"is a disarium no."); } }
6 odpowiedzi
+ 2
//What's wrong here. Iwant to check whether the number is disarium no. or not. Disarium no is like 135. (1^1 + 3^2 + 5^3=135)
import java.util.*;
public class Program
{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int i,n,d,k,c=0,sum=0;
n=sc.nextInt();
for(i=n;i!=0;i/=10)
c++;
d=n;
for(i=c;i>0;i--)
{
k=d%10;
sum=sum + (int)Math.pow(k,i) ;
d=d/10;
}
if(sum==n)
System.out.println(n +" is a disarium no.");
}
}
//You can use while loop . Incase if you want to use for loop
+ 5
Correct code,
import java.util.*;
public class Program
{
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int i,n,d,k,c=0;
double sum=0;
n=sc.nextInt();
i=n;
while(i>0)
{
i=i/10;
c++;
}
d=n;
for(i=c;i>0;i--)
{
k=d%10;
sum=sum + Math.pow(k,i) ;
d=d/10;
}
if(sum==n)
System.out.println(n +"is a disarium no.");
}
}
Match it with your current code and feel free to ask any question regarding the changes i made .
+ 3
Looks like your code is big enough. So please post it in the code playground and attach the code with a description.If you don't know how to do it then you can have a look at it:
https://www.sololearn.com/post/75089/?ref=app
+ 1
Atul [Inactive] thank you! In First for loop there was a silly mistake in updation.
Can you tell me bro why we have to use (int) before Math.pow. Do we need to convert everytime?
+ 1
Sparsh Srivastava Because Math.pow returns double and I typecasted it into int. It is not necessary always