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) | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
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."); } }

14th Sep 2021, 4:27 PM
Sparsh Srivastava
Sparsh Srivastava - avatar
6 Respuestas
+ 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
14th Sep 2021, 5:44 PM
Atul [Inactive]
+ 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 .
14th Sep 2021, 5:27 PM
Abhay
Abhay - avatar
+ 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
14th Sep 2021, 5:08 PM
Pariket Thakur
Pariket Thakur - avatar
+ 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?
14th Sep 2021, 6:35 PM
Sparsh Srivastava
Sparsh Srivastava - avatar
+ 1
HrCoder Abhay thank you 😊
14th Sep 2021, 6:36 PM
Sparsh Srivastava
Sparsh Srivastava - avatar
+ 1
Sparsh Srivastava Because Math.pow returns double and I typecasted it into int. It is not necessary always
14th Sep 2021, 6:46 PM
Atul [Inactive]