Dear experts,

I am doing my assignment on finding how many numbers in N digit satisfy K requirement.
However, when i am testing the code, i could compile it but i couldn't execute it.
I am wondering if i made mistake in the

``x = result + y; // add them together and return it.``

as when i comment this out, my program is able to execute,though i didn't get the right output value
Please kindly enlighten me.

Logic of my codes (example):

N = 15, K = 3

When N = 1, result = 0 + (N%10)
= 0 + (1%10)
= 1

y = N/10
= 1/10
= 0

x = 1 + 0
= 1
return x = 1

if (sumOfDigits(1) == K)
noOfNoSatisfy ++;
----------------------------------------------------
In the case of above, noOfSatisfy still remains as 0
-----------------------------------------------------
When N = 12, result = 0 + (N%10)
= 0 + (12%10)
= 2

y = N/10
= 12/10
= 1

x = 1 + 2
= 3
return x = 3

if (sumOfDigits(3) == K)
noOfNoSatisfy ++;

----------------------------------------------------
In the case of above, noOfSatisfy increase and plus 1
-----------------------------------------------------

``````import java.util.*;

public class Digits {

public static int sumOfDigits(int x) {
int result =0;
int y =0;
//eg. Let x be 1
while(x>0){
// eg. 1 = 1 + (1%10);
result = result + (x%10); //take the last digit of x
// eg. 0 = 1/10
y = x/10; //take the first digit of x
// eg. 1 = 1 + 0
x = result + y; // add them together and return it.
}
//eg. return 1
return x;

}

public static void main(String[] args) {
int N, K, noOfNoSatisfy;

Scanner sc = new Scanner (System.in);

//eg. Let N be 1
N = sc.nextInt();//indicate Number
//eg. Let K be 1
K = sc.nextInt();//indicate requirement number

noOfNoSatisfy = 0; // to count how many time it matches with K (requirement number)

for (int i = 1; i <=N; i++){
if(sumOfDigits(i) == K){//eg. 1 == K
noOfNoSatisfy ++; // plus 1 to count
}
} System.out.println(noOfNoSatisfy); //print the number of count
}
}``````

## Recommended Answers

Hmm... I'm very confuse about what K requirement is? Can you give an example of N and K values that could satisfy your result? At the moment, it is very obscured to me. What you are doing would go into an infinite loop too.

``// Let's talk …``

## All 3 Replies

Hmm... I'm very confuse about what K requirement is? Can you give an example of N and K values that could satisfy your result? At the moment, it is very obscured to me. What you are doing would go into an infinite loop too.

``````// Let's talk about your sumOfDigits() method
public static int sumOfDigits(int x) {
int result=0;
int y=0;
//eg. Let x be 1
while(x>0) {
result = result + (x%10); // 0 + (1%10) => 1 | 1+ (1%10) => 2 | ...
y = x/10; // 1/10 => 0
// !!!! Infinite loop because x will never be equal to or lower than 0 !!!
x = result + y; // 1+0 => 1
}
// never reach this line...
return x;
}``````

Edit:
Nevermind, I got it. What you want is the sum of consecutive digit in a number N starting from 1 up to N is equal to a specified sum value K, right? My assumption is that the sum will always start from the first digit up to the end of N digit, correct?

If so, what you need to do is...
1)if the value is greater than 10
2)start loop and use the divide by 10 first to get the value of the first digit
3)divided the current number by 10, so you will get rid of the first digit from what you are calculating
4)if the result of the division is greater than 10, keep going
5)else end the loop
6)add the value from the loop result (<10) and return the sum result

you just need to change your sumOfDigits(int) function:

public static void sumOfDigits(int x)
{
int sum=0;
while(x>0)
{
sum+=x%10;
x/=10;
}
return sum;
}

@profyou

It is dangerous enough to just give them code because they would not learn how to solve the problem. You could easily get flamed on your post on this forum. Even worse, you give a piece of code without explanation or comment...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.