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: Why is it summing a long negative integer to zero????

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why is it summing a long negative integer to zero????

    import java.util.Scanner;
    public class Exercise2_6M
    {
        public static void main(String[] args)
        {
            // Create a Scanner
            Scanner input = new Scanner(System.in);
     
            // Enter amount
            System.out.print("Enter an integer:");
            int integer = input.nextInt();
     
            // Calculations
            int rinteger = Math. abs (integer);
            int sum = 0;
            int i=0;
     
            //loop through each digit (starting from the least significant) until the end of the number
            while(rinteger / Math.pow(10,i) > 0)
            {
                sum+=getDigit(rinteger,i);
                i++;
            }
     
     
            // Display results
            System.out.println("Sum all digits in " + integer + " is " + sum);
        }
     
        public static int getDigit(int num, int power)
        {
            return (num % (int)Math.pow(10,power+1)) / (int)Math.pow(10,power);
        }
    }
    Last edited by helloworld922; September 29th, 2011 at 11:34 PM.


  2. #2
    Member Staticity's Avatar
    Join Date
    Jul 2011
    Location
    Texas
    Posts
    105
    My Mood
    Inspired
    Thanks
    3
    Thanked 5 Times in 5 Posts

    Default Re: Why is it summing a long negative integer to zero????

    Is this program built to find the sum of the digits? Or to find the sum of the digits up to the number?

    Would 123 be... 1+2+3
    or
    Would 123 be... 1+2+3+4+5...+122+123
    Simplicity calls for Complexity. Think about it.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Why is it summing a long negative integer to zero????

    As far as i know, when you apply the power function, it crosses the limit of integer. And shows you the result after that.
    You must take care of memory sizes provided to different data types.

Similar Threads

  1. Summing a 4X4 Matrix in JOptionPane
    By Java Neil in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 15th, 2011, 09:15 AM
  2. Im so confused! Counting and Summing
    By captiancd89 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 28th, 2010, 06:49 PM
  3. Is there an integer-type variable bigger than "long"?
    By bardd in forum Java Theory & Questions
    Replies: 2
    Last Post: September 3rd, 2010, 02:43 PM
  4. [SOLVED] Please Review My Code (Long Integer Addition)
    By Saradus in forum Java Theory & Questions
    Replies: 10
    Last Post: July 4th, 2009, 12:55 PM
  5. [SOLVED] How to make a integer negative if it meets a certain criteria?
    By Lizard in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 14th, 2009, 02:27 PM

Tags for this Thread