Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6641655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:50:10+00:00 2026-05-25T23:50:10+00:00

the instructions are: Enter an integer (32 bit) and sum its digits, you may

  • 0

the instructions are: Enter an integer (32 bit) and sum its digits, you may be given a negative or positive integer. When reading in a negative integer, sum the digits but ignore the – sign.

this is what I have. Somehow I can’t get it to ignore the negative signs when the integer is negative.

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();
        if (integer < 0)
            integer = -integer;

        // Calculations
        int rinteger = Math. abs (integer);
        int lastinteger = integer % 10;
        int X = integer / 10;
        int secondinteger= X % 10;
        int firstinteger = X /10;
        int finalinteger = firstinteger + secondinteger + lastinteger;


        // Display results
        System.out.println("Sum all digits in " + rinteger + " is " + finalinteger);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:50:11+00:00Added an answer on May 25, 2026 at 11:50 pm

    To ignore the negative sign:

    int integer = Math.abs(input.nextInt());
    

    Edit:
    I didn’t notice that your code already did this. To make the code show the negative sign at the end, get rid of the following lines:

    if (integer < 0)
            integer = -integer;
    

    since you’re already doing

    int rinteger = Math. abs (integer);
    

    Then, replace all instances other than the fist of integer in the calculations sections with rinteger, like this

        // Calculations
        int rinteger = Math. abs (integer);
        int lastinteger = rinteger % 10;
        int X = rinteger / 10;
        int secondinteger= X % 10;
        int firstinteger = X /10;
        int finalinteger = firstinteger + secondinteger + lastinteger;
    

    Finally, when displaying the results, use integer instead of rinteger to restore the minus sign, or just put in a minus sign manually if integer<0.

    Your code is also flawed if integer is more than 3 digits, as others have pointed out, but that’s a separate issue.

    To add more than 3 digits, you’ll need a loop. Try this:

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SQLite version 3.7.3 Enter .help for instructions Enter SQL statements terminated with a ;
I'm following the instructions on Github for installing the SSH Keys, but after I
I'm using the vogella tutorial, and I'm almost positive I followed the instructions exactly,
$ sqlite3 test.db SQLite version 3.6.21 Enter .help for instructions Enter SQL statements terminated
I'm trying to implement a critical section in CUDA using atomic instructions, but I
saturating instructions saturate unsigned to unsigned or signed to signed int. What's the best
I've followed the instructions in this MSDN article: http://msdn.microsoft.com/en-us/library/dd206945.aspx Is it possible to call
Tried following the instructions here: How to use Google app engine with my own
Does anyone have instructions on building the opensource version of Qt? Now that the
There are instructions here to create a C# assembly using the SimMetrics library. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.