Question

Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Code for the question:

import java.util.Scanner;

public class Base1 {

      static int sum =0;

      public static void main(String[] args) {

            //Scanner function that gets the input from User

            Scanner scan = new Scanner(System.in);

            System.out.print("Enter an Integer: ");

            int number= scan.nextInt();

            //Calling the recursive method to sum the digits

            int total = sum(number);

            //Printing out the results

            System.out.println("Entered Integer: "+number);

            System.out.println("Digits Sum: "+total);

      }

      //Sum method that adds the digits of the given integer

      private static int sum(int x) {

            // TODO Auto-generated method stub

            sum = x%10;

            if(x==0){

                  return 0;

            }

            else{

                  return sum + sum(x/10);

            }

           

      }

}

Output:

<terminated» Base1 [Java Application] C Digits Sum: 6

Add a comment
Know the answer?
Add Answer to:
Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and re...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • java programe Write a program that prompts the user to enter in an integer number representing...

    java programe Write a program that prompts the user to enter in an integer number representing the number of elements in an integer array. Create the array and prompt the user to enter in values for each element using a for loop. When the array is full, display the following: The values in the array on a single line. The array with all of the elements reversed. The values from the array that have even numbered values. The values from...

  • Write a complete Java program, including comments in both the main program and in each method,...

    Write a complete Java program, including comments in both the main program and in each method, which will do the following: 0. The main program starts by calling a method named introduction which prints out a description of what the program will do. This method is called just once.      This method is not sent any parameters, and it does not return a value. The method should print your name. Then it prints several lines of output explaining what the...

  • in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices...

    in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices must of the same size. The program defines method Addition() that takes two two-dimensional arrays of integers and returns their addition as a two-dimensional array. The program main method defines two 3-by-3 arrays of type integer. The method prompts the user to initialize the arrays. Then it calls method Addition(). Finally, it prints out the array retuned by method Addition(). Document your code, and...

  • Write a Java application that prompts the user for pairs of inputs of a product number...

    Write a Java application that prompts the user for pairs of inputs of a product number (1-5), and then an integer quantity of units sold (this is two separate prompts for input values). You must use a switch statement and a sentinel-controlled loop (i.e. a loop that stops execution when an out of range value, such as -1, is input). All 15 items below are for a single purchase. There are five sets of inputs as follows: Product 1    1...

  • C++ please Problem 3: Write a C++ program that prompts the user to enter an integer...

    C++ please Problem 3: Write a C++ program that prompts the user to enter an integer of any length (but not more than 8- digits), the program then extracts and prints each digit in its respective column. For example, in the integer 436, 4 should be printed in the 4th column, 3 in the 3rd and 6 in the 6th column. Hint: you can use setw to solve this problem. Sample Input/Output Enter an integer of any length. 9836472 ...Printing...

  • Write a complete JAVA program to do the following program. The main program calls a method...

    Write a complete JAVA program to do the following program. The main program calls a method to read in (from an input file) a set of people's three-digit ID numbers and their donations to a charity (hint: use parallel arrays)Then the main program calls a method to sort the ID numbers into numerical order, being sure to carry along the corresponding donations. The main program then calls a method to print the sorted 1ists in tabular form, giving both ID...

  • Design a Java program that asks the user to enter an integer number n and then...

    Design a Java program that asks the user to enter an integer number n and then generates an array of that many random points (x, y) with x- and y-coordinates in the range between 0 and 100. After this the program must ask the user to enter two diagonal points (x_1, y_1) and (x_2, y_2) of a rectangle with sides parallel to the coordinate axis (see the image below, where possible pairs of diagonal points are shown in red color)....

  • Write a Java console application that prompts the user to enter the radius of a circle,...

    Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. Write a JavaFX GUI application to do the same calculation, and draw the circle. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Write and document your program per class coding conventions. Add an instance variable double radius. Generate...

  • in java the program must contain the main method and example( the number 50 should be...

    in java the program must contain the main method and example( the number 50 should be the input) must be included in the main method to check the correctness of your programs. Create a GUI program to let user enter a positive integer n through the first window, and then your program prints out all the prime integers within the range [1, n) through the second window. Your program should contain two windows in total. Below are two example pictures:...

  • Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the...

    Write a Java program to convert octal (integer) numbers into their decimal number equivalents (exactly the opposite of what you have done in Assignment 4). Make sure that you create a new Project and Java class file for this assignment. Your file should be named “Main.java”. You can read about octal-to-decimal number conversions on wikepedia Assignment 4 is at the bottom Objective Your program should prompt the user to enter a number of no greater than 8 digits. If the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT