Shruthi Ravishankar:
Planteamiento del problema: la suma de tres dígitos - Encontrar todos los números entre 1 y 999, donde la suma de los dígitos primero y el segundo dígito es igual a la tercera dígitos.
Ejemplos:
123: 1 + 2 = 3
246: 2 + 4 = 6
Java:
public class AssignmentFive {
public static void main(String[] args) {
int i=1;
int valuetwo;
int n=1;
int sum = 0;
int valuethree;
int valueone = 0;
String Numbers = "";
for (i = 1; i <= 999; i++) {
n = i;
while (n > 1) {
valueone = n % 10;/*To get the ones place digit*/
n = n / 10;
valuetwo = n % 10;/*To get the tens place digit*/
n = n / 10;
valuethree = n;/*To get the hundreds place digit*/
sum = valuethree + valuetwo;/*adding the hundreds place and
tens place*/
}
/*Checking if the ones place digit is equal to the sum and then print
the values in a string format*/
if (sum == valueone) {
Numbers = Numbers + n + " ";
System.out.println(Numbers);
}
}
}
}
Tengo mi resultado:
1
10
100
1000
10000
100000
1000000
10000000
100000000
1000000000
10000000001
100000000011
1000000000111
10000000001111
100000000011111
1000000000111111
10000000001111111
100000000011111111
1000000000111111111
Process finished with exit code 0
El resultado no se muestra el resultado real como tiene que ser lo que debería mostrar valores como: (. Por favor refiérase a la declaración del problema anterior) 123, 246
Por favor, hágamelo saber lo que parece ser el problema con el código y cómo modificarlo.
Andreas:
No sabe lo que está tratando de hacer con ese while
bucle, o por qué usted está construyendo una cadena separada por espacios de los números.
El código debe ser algo como:
for (int n = 1; n <= 999; n++) {
int digit1 = // for you to write code here
int digit2 = // for you to write code here
int digit3 = // for you to write code here
if (digit1 + digit2 == digit3) {
// print n here
}
}