Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

How to calculate generic root of a number in Java

2 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        long num = 12345, sum = 0, remainder;
 
        while(num > 10) {
            sum = 0;
            System.out.format("sum digits of %d = " , num);
            while(num != 0) {
                remainder = num % 10;
                num = num / 10;
                sum += remainder;
            }
            System.out.format("%d\n", sum);
            if(sum > 10)
                num = sum;
            else
                break;
        }
        System.out.format("generic root = %d", sum);
    }
}




/*
run:
    
sum digits of 12345 = 15
sum digits of 15 = 6
generic root = 6
    
*/

 



answered Oct 9, 2021 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) {
        long num = 12345;
 
        long generic_root = 1 + ((num - 1) % 9);
        
        System.out.format("generic root = %d", generic_root);
    }
}




/*
run:
    
generic root = 6
    
*/

 



answered Oct 9, 2021 by avibootz

Related questions

1 answer 165 views
2 answers 145 views
2 answers 166 views
2 answers 148 views
2 answers 334 views
3 answers 160 views
3 answers 194 views
...