1.

write a program in java to check for a special 2-digit number when the sum of the digits is added to the product of the digits the result is the number itself.​

Answer»

Program:

import java.io.*;

import java.util.*;

public class Main{

   public static void main(String args[]){

       int sum = 0;

       int TEMP = 0;

       int pro = 1;

       int j;

       for(int i = 10 ; i < 100 ; i++){

           j = i;

           while (j>0){

               temp = j % 10;

               sum = sum + temp;

               j = j/10;

           }

           temp = 0;

           j = i;

           while(j>0){

               temp = j % 10;

               pro = pro * temp;

               j = j/10;

           }

           if ((sum+pro) == i){

               System.out.println(i);

               temp = 0;

               sum = 0;

               pro = 1;

           }

           temp = 0;

           sum = 0;

           pro = 1;

       }

   }

}

Output:

19

29

39

49

59

69

79

89

99

Explanation:

  • i started a for loop from 10 to 99 (since you said 2 DIGIT numbers)
  • after in that loop i calculated sum of the DIGITS of the number taken from for loop
  • again i calculated product of the numbers taken from loop
  • in loop i checked number taken from loop by ADDING sum+pro
  • if that is true then i printed out the number
  • i did not write the else part because we want only if part

-----Hope This Program Helps You :)



Discussion

No Comment Found