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

Share
Published by
codeflare

Recent Posts

C++ Queue

1. What Is a Queue? A Queue is a linear data structure that follows the principle: FIFO – First…

7 hours ago

Must-Know Angular Concepts

Angular is a full-featured frontend framework built by Google for creating large, maintainable, and high-performance web applications.…

2 days ago

Responsive Web Design (RWD)

What Is Responsive Web Design? Responsive Web Design (RWD) is an approach to building websites…

2 days ago

Geolocation API in JavaScript

The Geolocation API allows a web application to access a user’s geographical location (latitude, longitude, and more), with…

1 week ago

The Golden Ratio (φ)

1. What Is the Golden Ratio? The Golden Ratio, represented by the Greek letter φ (phi), is…

2 weeks ago

CSS Combinators

In CSS, combinators define relationships between selectors. Instead of selecting elements individually, combinators allow you to target elements based…

3 weeks ago