Answer to Question #257509 in Java | JSP | JSF for Nikhil Sahoo

Question #257509

Write a program in Java to store 25 numbers in an array then create another array to store sum of the digits. Then display only those numbers in ascending order where sum of the digits is a single digit. 


1
Expert's answer
2021-10-28T12:37:50-0400


package arrays;
import java.util.Arrays;


public class Start {


    static int getSum(int n)
    {   
        int sum = 0;
         
        while (n != 0)
        {
            sum = sum + n % 10;
            n = n/10;
        }
     
    return sum;
    }
    public static void main(String[] args) {
        int [] numbers = new int [25];
        int [] digits = new int[25];
        for(int i=0; i<25; i++){
            numbers[i] = i;
            digits[i]=getSum(numbers[i]);
        }
        int k=0;
        for(int i=0; i<25; i++ ){
            String x  = String.valueOf(digits[i]);
            if(x.length()==1){
                k++;
            }
             System.out.println();
        }
        System.out.println(k);
        int results [] = new int [k];
        int j = 0;
        for(int i=0; i<25; i++ ){
            String x  = String.valueOf(digits[i]);
            if(x.length()==1){
                results[j] = digits[i];
                j++;
            }
             
             
              
        }
        Arrays.sort(results);
        for(int i=0; i<24; i++ ){
              System.out.printf("%d ",results[i]);
              }
        
        
        
    }
    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS