0

Im currently trying to create a Algorithm who is supposed to be able to line up numbers with a specific digit sum. For example numbers with a digit sum of 13 in between 1 and 500. Could someone give me some advice on how to proceed please?

My current progress:

 public class Digitsum{

static int Digitsum2(int number) {  //method to find out the digit sum from numbers between 1 and 500
    int sum = 0;
    if(number == 0){
        return sum;
    } else {
        sum += (number%10);
        Digitsum2(number/10);

    }
    return sum;
        }

static int Digitsum13(int x) { //method to line up the numbers with a digit sum of 13

    while (x <= 500) {
        x++;
        Digitsum2(x);
    }
    return x; 

}


public static void main(String[ ] args) {
   Digitsum13(13);
}

}

3 Answers3

0

You want to find all the numbers within a particular range (1-500 in this case), the individual digits of which sum up to a particular target sum (13 in this case) and print a list of them.

DigitSumClass:

public class Digitsum {

    static void digitSum(int target)
    {
    System.out.println("Target: " + target);
        for(int i = 1; i <= 500; i++)
        {
            int currsum = 0;
            int currnum = i;
            
            while(currnum > 0 )
            {
                currsum += currnum%10;
                currnum /= 10;
            }
            if( currsum == target)
            {
                //print each match
                System.out.println(i);
            }
        }
        
    }

    public static void main(String[] args) {
        System.out.println("Starting iteration from 1-500");
        digitSum(13);
    }

}

Test cases:

Starting iteration from 1-500 Target: 13 49 58 67 76 85 94 139 148 157 166 175 184 193 229 238 247 256 265 274 283 292 319 328 337 346 355 364 373 382 391 409 418 427 436 445 454 463 472 481 490

Starting iteration from 1-500 Target: 2 2 11 20 101 110 200

Starting iteration from 1-500 Target: 19 199 289 298 379 388 397 469 478 487 496

Community
  • 1
  • 1
0

There is three factors.

  • loop from 1 to 500
  • calculate digit sum
  • check if digit sum is acceptable value

If you think about these separately, code will be clean.


public class Digitsum {
    static int digitsum(int n) {
        int ret = 0;
        while (n != 0) {
            ret += n % 10;
            n /= 10;
        }
        return ret;
    }
    public static void main(String[ ] args) {
        // digitsum2
        System.out.println("------- digitsum2-------");
        for (int i = 1; i <= 500; i++)
            if (digitsum(i) == 2)
                System.out.println(i);

        // digitsum13
        System.out.println("------- digitsum13-------");
        for (int i = 1; i <= 500; i++)
            if (digitsum(i) == 13)
                System.out.println(i);
    }
}

The outputs of digitsum2 is followings

------- digitsum2-------
2
11
20
101
110
200
set0gut1
  • 1,652
  • 1
  • 8
  • 21
0

I'm posting a simple programme and think it will do your work.Because it was little bit difficult to understand your programme.

class Example{
public static void main(String args[]){
    digitsum(13);
}

public static void digitsum(int num){
    int value;
    int copyofvalue;

    for(int i=1;i<500;i++){
        int sum=0;
        value=i;
        copyofvalue=value;

        while(value!=0){
            sum+=value%10;
            value/=10;
        }

        if(sum==num){
            System.out.println(copyofvalue);
        }
    }
}

If you have any problem with above programme, please let me know by a comment. If it suits your work mark it as an accepted answer.

Thushara
  • 25
  • 4