tbcse - 9 months ago 66

Java Question

I am trying to make a program where a user enters 2 numbers, and then the program gives the tau numbers in this gap.

- "Tau" number is the number which can divided by its total number of dividers.For example (1,2,3,4,6,8,12,24) all this numbers can divide 24. There is 8 numbers then 24 can divided by 8. So we can say 24 is a tau number.*

There is a mistake at second for loop I think but I cannot understand where is it.

`import java.util.Scanner;`

public class tauNumber {

public static void main(String [] args){

int start=0,stop=0,count=0;

Scanner input =new Scanner(System.in);

System.out.println("Please enter first number: ");

start=input.nextInt();

System.out.println("Please enter last number: ");

stop=input.nextInt();

for(int i=0+start;i<=stop;i++){

for(int j=1;j<=start;j++){

if(i%j==0){

count++;

}

}

if(start/count==0){

System.out.println(i+" is a tau number" );

}

}

}

}

Answer

Instead of saying "tau number" you should just refer to it as a refactorable number as @Tunaki pointed out.

I would suggest that you split your code up into functions to get a better understanding on whats going on, I think this is what you're looking for:

```
import java.util.Scanner;
class TauNumber {
public static void main(String[] args){
Scanner input =new Scanner(System.in);
System.out.println("Please enter first number: ");
int start=input.nextInt();
System.out.println("Please enter last number: ");
int stop=input.nextInt();
for(int i=start+1; i<stop; i++){
if(refractorable(i)){
System.out.println("Found tau number: "+ i);
break;
}
}
}
public static boolean refractorable(int number){
if(sumDivisors(number) == 0) return false;
if(number % sumDivisors(number) == 0){
return true;
} else {
return false;
}
}
public static int sumDivisors(int number){
int sum = 0;
for(int i=1; i<=number; i++){
if(number % i == 0){
sum++;
}
}
return sum;
}
}
```

Note: I would also like to point out that when you specify "gap" what do you mean by it? I took it as `start < x < stop`

, because that would be the gap but it could certainly be `start < x <= stop`

.

Source (Stackoverflow)