Function to sum the digits of a number


Monday, November 13, 2023 Difficulty: Intermediate

Objective

Develop a Java program that prompts the user for a number and stores it in a text string. Then, implement a function called sumDigits that receives the text as a parameter and returns the sum of its digits.

Instructions

  1. From the main function main of the program, prompt the user for a number as input and store it in a text string.
  2. Implement a function called sumDigits that takes the text containing the number as a parameter.
  3. The sumDigits function should sum the digits of the number and return the result.
  4. Print the result along with a message indicating that it is the sum of the digits of the number.

Input

2222222222222222222222

Output

The sum of the digits is: 44

Solution

Click here to view the exercise solution

Share it

Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.

Copied

The code has been successfully copied to the clipboard.

More Exercises

Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!

1

Function greeting and farewell

Develop your Java skills by creating a program with greeting and farewell functions. Learn to execute methods effectively from the main function (Main).

2

Greet and bid farewell with parameters

Create a Java program that implements two methods for greeting and saying goodbye. The greeting method should have a text parameter.

3

Check alphabetical string

Create a Java program that implements a function called 'isAlphabetic()' which takes a text parameter from the user and checks if the text contains alphabetic characters from 'a' to 'z', including both uppercase and lowercase.

4

Replace character in a string

Explore text manipulation in Java with this intermediate-level exercise. Implement the changeLetter() function to modify a character in a text. Learn practically and enhance your mastery in programming!

5

Function sum

Create a Java program that implements a function called "sum" that returns the sum of two integer numbers passed as parameters.

6

Check numeric string

Create a Java program that implements a function called IsNumber that receives a text parameter from the user and check if the text is a number or not.

7

Calculator with command-line parameters

Create a Java program that serves as a calculator with the ability to add, subtract, multiply, or divide.

8

Function count spaces of a text

Create a program in Java that implements a function called CountSpaces that receives as a parameter a text requested from the user.

9

Function to sum the digits of a number

Create a Java program that asks the user for a number and save it in a text string.

10

Return value from Main

Create a new version of the program Main parameter calculator where you can return the following error codes from Main.

11

Find the minimum and maximum in an array

Create a Java program that implements a function called FindMinMax that receives three parameters, an array of real numbers and two real numbers.

12

Function to write format text

Create a program in Java that implements a function called WriteCenteredUnderline that receives as a parameter a text requested from the user.

13

Function to add an array of integers

Create a Java program that asks the user for 5 integers to store them in an array of integers.

14

Function to show an inverted string

Create a Java program that implements a function called InvertString receive receive a text by parameter and display them in reverse order.

15

Calculate double a number

Create a Java program that implements a function called Double to calculate and return double an integer.

16

Function to check palindrome

Create a Java program that implements a function to check if a string is a palindrome or not.

17

Search the greatest value in an array

Create a program in Java that implements a function called SearchGreatestValue that receives as an parameter an array of 5 real numbers requested from the user.

18

Function to calculate the power of a number

Create a Java program that implements a function called Power and calculate the result of raising an integer to another number.

19

Function to write text with title format

Create a Java program that implements a function called WriteTitle that writes a text centered on the screen, in capital letters, with additional spaces and with one line above and another below.

Functions

Practice with exercises in Java that use functions of different types. Methods, functions with parameters or values by reference among others.

Practice exercises

Keep learning

C# Programming Course

Free programming course with practical exercises and solutions in C#. Start learning right now!

Exercises C# App

Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play