Find Sum of Integer Until Becomes Single Digit

package org.wesome.dsalgo;

class SumNumber {
    public static int sumNumber(int number) {
        if (number > 0 && number < 10) {
            return number;
        }
        int sum = 0;
        while (number > 0 || sum > 9) {
            if (number == 0) {
                number = sum;
                sum = 0;
            }
            sum += number % 10;
            number = number / 10;
        }
        return sum;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class SumNumberTest {

    @Test
    void sumNumberTest1() {
        int number = 12345;
        Assertions.assertEquals(6, SumNumber.sumNumber(number));
    }

    @Test
    void sumNumberTest2() {
        int number = 54321;
        Assertions.assertEquals(6, SumNumber.sumNumber(number));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.4.1"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on