eclipse - java query not passing test case -
this question:
a dosod number divisor of sum of digits. example, if take number 12, sum of digits 1 + 2 = 3. 12 divisible 3 , hence dosod number. same reasoning, 11 not dosod number, closest dosod number 11 12. write program take integer n input , print dosod number closest not less n. sample input-1 16 expected output 18 sample input-2 20 expected output 20
this code:
class dosod{ public static void main(string args[]){ scanner s=new scanner(system.in); int n=s.nextint(); int temp=0,x; x=n; int y=n; while(n>0){ x=n%10; temp+=x; n=n/10; } if(y%temp==0){ system.out.print(y); }else if(y%temp!=0){ y=y+2; system.out.println(y); } } }
but not passing test case .can ?
you need update else branch. closest number forward or backward current number. since problem statement explicitly says closest value should greater input value. can write simple while loop increments input value till value mod sumofdigits
0.
public static void main(string args[]) { scanner s = new scanner(system.in); int n = s.nextint(); int y = n; int temp = 0; temp = sumofdigits(n); if (y % temp == 0) { system.out.print(y); } else { int yforward = y+1; while(yforward % sumofdigits(yforward) !=0) yforward++; system.out.println(yforward); } } static int sumofdigits(int n) { int temp = 0; while (n > 0) { int x = n % 10; temp += x; n = n / 10; } return temp; }
Comments
Post a Comment