/ Assignment / assignment_23.java
assignment_23.java
1 /* 2 * Write code to generate an expression that contanins all the 10 digits (0-9) and the 3 * sum of 2 3 digit no's should output a 4 digit no 4 */ 5 6 public class assignment_23 { 7 public static void main(String[] args) { 8 for (int first = 102; first < 1000; first++) { 9 for(int second = 103; second < 1000; second++) { 10 int third = first + second; 11 if(third >= 1000 && first < second) { //If third is 4 digit no 12 //To check for unique digits 13 // We will use bit masking 14 long combined_val = ((((first * 1000L) + second) * 10000) + third); // 1000L typecasts int as long 15 short ref = 0; 16 17 while (combined_val != 0) { 18 int digit = (int)(combined_val % 10); 19 if ((ref & (1 << digit)) == 0) { 20 ref = (short)(ref | ( 1 << digit)); //Converting int to short 21 } 22 else { 23 break; 24 } 25 combined_val /= 10; 26 } 27 if(combined_val == 0) { 28 System.out.println(first + " + " + second + " = " + third); 29 } 30 } 31 } 32 } 33 } 34 }