I have to prepare program that summing the digits of two given by user numbers and then give back the random number which is
- natural number
- the digit sum of this number is bigger then the digit sum of given by user numbers
It's look like everything is ok but i dont know if it is just luck in random number generator or it's working well. Thank you for review.
import java.util.Random;
import java.util.Scanner;
public class Ex1 {
public static void main(String[] args) {
Random rnd = new Random();
Scanner scn = new Scanner(System.in);
int sum1 = 0;
int sum2 = 0;
int sum3 = 0;
int sum4 = 0;
System.out.println("1TH NUMBER : ");
int a1 = scn.nextInt();
System.out.println("2ND NUMBER : ");
int a2 = scn.nextInt();
System.out.println((0 > a1 || 0 > a2 ? "ERROR-NEGATIVE NUMBER" : "OK"));
while (a1 > 0) {
sum1 += a1 % 10;
a1 /= 10;
}
//System.out.println(sum1);
while (a2 > 0) {
sum2 += a2 % 10;
a2 /= 10;
}
//System.out.println(sum2);
int temp = sum1 + sum2; //temporary-for storage /=
while (temp > 0) {
sum3 += (temp) % 10;
(temp) /= 10;
}
// System.out.println(sum3);
while (true) {
int a3 = rnd.nextInt(Integer.MAX_VALUE);
sum4 += (a3) % 10;
(a3) /= 10;
// System.out.println(sum4);
if (sum4 > sum3) {
System.out.println(a3 + " this is my number");
break;
}
}
}
}
Input: a1 = 222
- so digit sum = 6;a2 = 333
- so digit sum = 9; sum of digits from a1 and a2= 15 = 1 + 5 = 6 so i expect the number with higher digit sum likeOutput 4545
because 4+5+4+5 = 18 Ex2 Input -a1 - 23234
(digit sum= 14)a2 - 454545
(digit sum=27), sum of digits a1+a2 = 14+27 = 41 = 4 + 1 = 5Output: a3=61= 6+1 = 7
(higher digit sum) \$\endgroup\$