CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Chotan Barua
Level 7
New York City
  • 05.11.2019
  • 754views
  • 3comments

Not sure what the error means.. Please help ?

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 int n = 546 ; int sum = 0; while (n > 0) { sum =sum + n % 10; n = n /10; } return sum; } }
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
YO MA
Level 5 , Mumbai, India
8 November 2019, 06:55
you can rather try this int result = number/100 + ((number-99)/100) + ((number+99)/100); //5.46+4.47+6.45 return result;
0
poosa sudeshna
Level 5 , Hyderabad, India
6 November 2019, 03:34
In your code, you didn't used the value of the variable that passed as a parameter in the method. Instead you have used hard coded value. So remove the line10. And replace variable 'n' with 'number'. It should work.
0
Guadalupe Gagnon
Level 37 , Tampa, United States
5 November 2019, 18:31
The problem is that you hard coded "546" into your method. If you changed line 10 to pass in 657 to the method, it should output 18, but because it is hard coded it would still output 15. The method should accept any (three digit) number and return the sum of its digits.
0
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.