Skip to content

1.13 Increasing Sum

Farrukh Ahmad edited this page Dec 21, 2015 · 1 revision

Here program I will check whether a number is increasingly sum of itself or not Example:

  • 36 is true because 36 = 3 + 33

  • 984 is true because 984 = 8 + 88 + 888

  • 7404 is true because 7404 = 6 + 66 + 666 + 6666

      public class IncreasinSum {
    
      public static void main(String[] args) {
      int n = 7;
      System.out.println(isIncreasingSum(n));
      }
    
      static boolean isIncreasingSum(int n){
      int length = getLength(n);
      
      for(int i=1; i<=9 ; i++){
      	String digits = "";
      	int sum = 0;
      	for(int j=1; j<=length; j++){
      		digits = digits+""+i;
      		sum = sum + Integer.parseInt(digits);
      	}
      	if (sum == n)
      		return true;
      }
      return false;
      }
    
      static int getLength(int n){
      int length = 0;
      while(n!=0){
      	n = n / 10;
      	length++;
      }
      return length;
      }
      }
    

Clone this wiki locally