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 3 of 3

Thread: Sum of Digits in Java

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sum of Digits in Java

    import java.util.Scanner;
    public class CubesSum {
     
        public static void main (String [] args){
     
            int input;
     
            System.out.println("Enter a positive integer:");
            Scanner in = new Scanner(System.in);
            input = in.nextInt();
     
            int number = input; //number is a temp variable
            int sum = 0;
     
            while(number>0){
                int t= number%10;
                sum += t*t*t;
                number = number/10;
            }
            System.out.println("The sum of the cubes of the digits is:" +sum);
        }
    }

    As you can see I'm using a while loop. For part B which is to modify to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits. So for example, 371 = 3+7+1. Can someone tell me how to do it? I need to wrap a for loop around my while loop...


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Sum of Digits in Java

    So you are trying check a number is armstrong number or not.

    First you have to define a till which integer value you want to find out, and iterate you while loop inside a for/while loop.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,744
    My Mood
    Mellow
    Thanks
    233
    Thanked 711 Times in 697 Posts

    Default Re: Sum of Digits in Java

    Welcome to the forum! Thank you for taking the time to learn to post your code correctly.

Similar Threads

  1. java rounded sum
    By harvind in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 20th, 2013, 02:07 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. Rookie here, need help with sum of digits program
    By wkellogg10 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2013, 06:25 PM
  5. Replies: 2
    Last Post: February 19th, 2010, 07:10 AM

Tags for this Thread