Program to find the lenght of digits in Java? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

Program to find the lenght of digits in Java?

Sometimes integers are parsed to String and length() function gets the numbers but again parsing back to the number for furthur logic say Automorphic numbers whose an automorphic number is an Automorphic number whose square "ends" in the same digits as the number itself. For example, 52 = 25, 62= 36, 762 = 5776, and 8906252 =793212890625. Write a Java program check whether a number is an Automorphic number or not. In mathematics, an automorphic number is a number whose square "ends" in the same digits as the number itself. For example, 52 = 25, 62 = 36, 762 = 5776, and 8906252 = 793212890625, so 5, 6, 76 and 890625 are all automorphic numbers. Now direct primitive library functions could be used but fun is about flexiblity in logic.Thank you https://code.sololearn.com/cFYnVumxsv0k/?re

21st Jan 2019, 10:56 PM
Prantik Sarkar
Prantik Sarkar - avatar
11 Respuestas
+ 3
Denise Roßberg Yes surely your code works fine for negative numbers, I just removed the dependency in math class abs() then explicitly type casting back. Let's keep things basic and logic independent. Thank you. Happy Coding my friend. https://code.sololearn.com/cFYnVumxsv0k/?ref=app
18th Mar 2019, 1:30 AM
Prantik Sarkar
Prantik Sarkar - avatar
+ 4
Denise Roßberg absolutely.. a missing semicolon ; it is alright, happens with all of us, only thing is to understand why it happened, and rectify it. As Sololearn is growing, and hand held devices took over laptops..even notebooks, book, paper, pencil, pen are being into paper save campaign, I feel simplifying a complex Object Oriented C++, made Oak, then JAVA, a type of bird sparrow, since Sir James Gosling founded Java, things got a bit more user friendly commands like System/out/print /new etcetera around 50 keywords which is easier to find in english dictionary, unlike cout/cin /setw idea was simple make a pure object oriented language, where as C++ is fantastic middle level language, like a semiconductor. Java is more of childish, yet it is strinct about input instructions, for a precise output. I would like my students and fellow programmers(coders), understand what is happening in each logical expressions and functions/methods.
17th Mar 2019, 9:36 PM
Prantik Sarkar
Prantik Sarkar - avatar
+ 3
Denise Roßberg Why do I need to convert a number to a string, again explicitly converting back to string again. The goal is to build a logic, which works free of any language constraint. That is exactly what I want, to think like the inventor was thing, human after all, researching modern technology which the creator didn't have back in 1990's. In a way, I am trying to find the why part, everyone knows how to find the 'x' variable, the interesting part is searching wh'y', curiosity.
17th Mar 2019, 7:32 PM
Prantik Sarkar
Prantik Sarkar - avatar
17th Mar 2019, 10:03 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Prantik Sarkar Your right. I changd it. Your program does not work for num = 0.
18th Mar 2019, 3:11 AM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Denise Roßberg Zero, as in prefixed like 012 or a single 0 or 000 or 120 or 102, please elaborate.
18th Mar 2019, 10:17 AM
Prantik Sarkar
Prantik Sarkar - avatar
+ 3
Denise Roßberg I thought so, you may ask the neutral factor Zero '0' . Like if some input is 000, output three digits is what we want. Let us do that.
18th Mar 2019, 6:28 PM
Prantik Sarkar
Prantik Sarkar - avatar
+ 2
Prantik Sarkar I think it is the easiest way to get the length of a number. You could also use a while loop: int n = 123 int counter = 0; while(n > 0){ n/=10; counter++; } System.out.println(counter); //length
17th Mar 2019, 7:43 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Prantik Sarkar As a single number Input = 0 Output = the number is 0 digits long
18th Mar 2019, 2:11 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
In mathematics, an automorphic number is a number whose square "ends" in the same digits as the number itself. For example, 52 = 25, 62 = 36, 762 = 5776, and 8906252 = 793212890625, so 5, 6, 76 and 890625 are all automorphic numbers. Test Data Input a number : 76  Sample Solution: Java Code: //faster approach import java.util.Scanner; public class Automorphic{ { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Input a number : "); int num = sc.nextInt(); int sq_num = num*num; String str_num = Integer.toString(num); String square = Integer.toString(sq_num); if(square.endsWith(str_num)) System.out.println("Automorphic Number.\n"); else System.out.println("\nNot an Automorphic Number."); } }}
21st Jan 2019, 11:00 PM
Prantik Sarkar
Prantik Sarkar - avatar
+ 1
I'm not really sure what you want. But for the length I use String. int number = 123; int length = Integer.toString (number).length ();
22nd Jan 2019, 12:32 AM
Denise Roßberg
Denise Roßberg - avatar