3

In my javacode I am using while loop where a value is adding together and making it to a single digit .. for example if it is 2010 then 2+0+1+0 = 3 and if it is 2345 then 2+3+4+5 = 14 then 1 + 4 = 5 like that ... but at the same time this process is not valid for two numbers .. 11 & 22 if the coming value is 11 and 22 then no further addition like 1+1 =2 or 2+2 =4; they have to display it as both 11 and 22 ... I wrote code like below but it is not working.... anyone pls check the code and help me...what is the mistake...

private long getSum10(String text)
{
    long sum10 = 0;
    char[] name10 = new char[text.length()];
    name10 = text.toCharArray();
    for (int i = 0; i < text.length(); i++)
    {
        sum10 += value10(name10[i]);
    }
    while ((sum10 != 11) && (sum10 != 22) && (sum10 > 9))
    {
        sum10 = findDigitSum10(sum10);
    }
    return sum10;
}
private long findDigitSum10(long n)
{
    int sum10 = 0;
    while (n != 0)
    {
        sum10 += n % 10;
        n = n / 10;
    }
    return sum10;
}
private int value10(char a)
{
    switch (a)
    {
    case 'A':
        return 1;
    case 'B':
        return 2;
    case 'C':
        return 3;
    case 'D':
        return 4;
    case 'E':
        return 5;
    case 'F':
        return 6;
    case 'G':
        return 7;
    case 'H':
        return 8;
    case 'I':
        return 9;
    case 'J':
        return 1;
    case 'K':
        return 2;
    case 'L':
        return 3;
    case 'M':
        return 4;
    case 'N':
        return 5;
    case 'O':
        return 6;
    case 'P':
        return 7;
    case 'Q':
        return 8;
    case 'R':
        return 9;
    case 'S':
        return 1;
    case 'T':
        return 2;
    case 'U':
        return 3;
    case 'V':
        return 4;
    case 'W':
        return 5;
    case 'X':
        return 6;
    case 'Y':
        return 7;
    case 'Z':
        return 8;
    default:
        return 0;
    }
}
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
roshanpeter
  • 1,334
  • 3
  • 13
  • 32

3 Answers3

0

I would have implemented this in the following way

public static void main(String[] args) {
    System.out.println(getSum(12));
    System.out.println(getSum(11));
}
public static long getSum(int no){      
    if(no/10 == 0 || no%11 == 0){
        return no;
    }else{
        return getSum(no/10) + no%10;
    }
}
Pratik Shelar
  • 3,154
  • 7
  • 31
  • 51
0
 while ((sum10!=11) && (sum10!=22) && (sum10>9))
{
}

make it

if ((sum10 != 11) && (sum10 != 22) )
{
   sum10 = findDigitSum10(sum10);
}

change it to if condition.. I don see the reason why u have written it in while condition..the answer ur are goin to get from findDigitSum10(sum10) will alway be less than 10..

0

Put a condition to check the value of number. If n is greater than 99 add digits else show the number as it is.

Update your method like this:

private long findDigitSum10(long n) {

    // TODO Auto-generated method stub
    int sum10 = 0;
    long temp = n;

    if (temp > 99) {

        while (n != 0) {
            sum10 += n % 10;
            n = n / 10;
        }
    }

    else {
        return n;
    }

    return sum10;
}
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35