``` * that prompts the user to enter an integer the */ public class Q2 { public static void main(String[] args) { java.util.Scanner input = new java.util.Sc System.out.print("Enter an Integer: "); long n = input.nextLong(); System.out.print("The sum of digits in "+ n } public static int sumDigits(long n) { int sum =0; while(n > 0) { sum += n%10; n /= 10; } return sum; } } ```
Added by Aaron M.
Close
Step 1
The `Scanner` object is not correctly initialized, and the `System.out.print` statement is incomplete. Show more…
Show all steps
Your feedback will help us improve your experience
Wei Yang and 70 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Calculate 1 + 3 + 5 +...+ (2n - 1) for several natural numbers n.
Victor S.
Write a program to find the sum of the first $n$ natural numbers, where the value of $n$ is provided by the user.
(Sum the digits in an integer) Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer is $932,$ the sum of all its digits is $14 .$ (Hint: Use the \% operator to extract digits, and use the // operator to remove the extracted digit. For instance, $932 \% 10=2$ and $932 / /$ $10=93 .)$ Here is a sample run: Enter a number between 0 and 1000: 999 The sum of the digits is 27
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD