Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Digits sum

  1. #1
    Junior Member
    Join Date
    Jan 2024
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Digits sum

    Good evening, there is a task "to find the sum of digits of the number using string.length(). No other String methods are allowed"
    So I have the following code:

    public class DigitsSum {
        public static void printDigitsSum(int t) {
            int number;
             int sum = 0;
            for (number = t; number !=0; number /= 10)
                 sum += number % 10;
    System.out.println(sum);
            }
     
            public static void main (String[]args){
                int t = 12345
                printDigitsSum(t);
            }
    How can I use "string.length()" without "charAt()"?
    Thanks in advance for help
    Last edited by BadGoogler; January 23rd, 2024 at 10:31 AM.

Similar Threads

  1. Sum of Digits in Java
    By edward07 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 4th, 2014, 02:44 AM
  2. Replies: 2
    Last Post: October 25th, 2013, 06:31 AM
  3. sum of digits
    By snarayana.murthy86 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2013, 02:40 PM
  4. Calculating Pi to more digits?
    By sci4me in forum Java Theory & Questions
    Replies: 5
    Last Post: May 23rd, 2013, 01:41 PM
  5. All Digits only
    By kram in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 28th, 2011, 03:16 PM