CodeGym
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
NNAMDI HENRY
Level 6
ABUJA
  • 30.06.2021
  • 328views
  • 4comments

Confused here, assistance needed please

Question about the task Sum of the digits of a three-digit number
Java Syntax,  Level 3,  Lesson 12
Under discussion

Write the code for sumDigitsInNumber(int number). The method takes a three-digit whole number. You need to calculate the sum of the digits of this number, and then return the result.

Consider this example:
The sumDigitsInNumber method is called with the argument 546.

Example output:
15

Requirements:
  • The program must not read data from the keyboard.
  • The sumDigitsInNumber(int) method must be public and static.
  • The sumDigitsInNumber method must return an int.
  • The sumDigitsInNumber method should not display anything.
  • The sumDigitsInNumber method must return the sum of all digits in the parameter number.
package com.codegym.task.task01.task0132; /* Sum of the digits of a three-digit number */ public class Solution { public static void main(String[] args) { System.out.println(sumDigitsInNumber(546)); } public static int sumDigitsInNumber(int number) { //write your code here return number/3; } }
0
Comments (4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
ron_villela
Level 9 , Miami, United States
25 July 2021, 04:57useful
separate the numbers with modulus then add...
+1
DavidJugaad
Level 10
19 July 2021, 15:36useful
Dear Nnamdi, Divide and Conquer. Use divison (ratio and module) For a given number xyz, divide: xyz/100 and you will get 1st digit (x) Use the mod of the former operation to divide it again by 10 and you will get 2nd digit. (y) The mod of the former operation, will give you the third digit. (z) return x+y+z Tip: mod operator: % Stay safe, All the best Hope it helps...
+1
Jomar Tayactac
Level 2 , Winnipeg, Canada
Expert
19 July 2021, 14:57useful
There are different ways to solve this task. One way to solve it, which is the way I did it, is to convert the argument into a character array. Once that is done, you can then get the numerical value of each element in the character array and add them all together. That should give you the sum of all digits. Hope this helps.
+1
Thomas
Level 31 , Bayreuth, Germany
30 June 2021, 13:06useful
the task asks you to take the number (here eg. 546) and to split it up in single digits like 5 and 4 and 6 and finally you should add up these values (5 + 4 + 6) so you finally get the result 15 (cross sum).
+1
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.