Categories: softare development

Write a static method that takes int number as input argument and returns the sum of the digits of that number.


public CheckSum {


public static void main (String []args){

System.out.println(digitSum(45));

public static int digitSum(int number) {
  
 int sum = 0;
         
        while (number != 0)
        {
            sum = sum + number % 10;
            number = number/10;
        }
     
    return sum;
 }
}
}

Check if a given number is perfect square in Java

Recent Posts

ReferenceError vs. TypeError: What’s the Difference?

When debugging JavaScript, you’ll often encounter ReferenceError and TypeError. While both indicate something went wrong,…

2 days ago

document.querySelector() vs. getElementById(): Which is Faster?

When selecting DOM elements in JavaScript, two common methods are document.querySelector() and document.getElementById(). But which…

2 days ago

npm vs. Yarn: Which Package Manager Should You Use in 2025?

When starting a JavaScript project, one of the first decisions you’ll face is: Should I…

4 days ago

Why Learn Software Development? (And Where to Start)

Software development is one of the most valuable skills you can learn. From building websites…

1 week ago

JavaScript Multidimensional Arrays

In JavaScript, arrays are used to store multiple values in a single variable. While JavaScript…

2 weeks ago

What is Containerization

Containerization is a lightweight form of virtualization that packages an application and its dependencies into…

2 weeks ago