1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
svetoff [14.1K]
3 years ago
15

Writing in Java, write a program that prompts the user to input an integer and then outputs both the individual digits of the nu

mber and the sum of the digits. For example, the program should: ouput the individual digits of 3456 as 3 4 5 6 and the sum as 18, output the individual digits of 8030 as 8 0 3 0 and the sum as 11, output the individual digits of 2345526 as 2 3 4 5 5 2 6 and the sum as 27, output the individual digits of 4000 as 4 0 0 0 and the sum as 4, and output the individual digits of -2345 as 2 3 4 5 and the sum as 14.
Computers and Technology
1 answer:
larisa [96]3 years ago
6 0

Answer:

//Here is code in java.

import java.util.*;

class Main

{

public static void main (String[] args) throws java.lang.Exception

{

   try{

       int in;

     //scanner class object to read the input

    Scanner scr=new Scanner(System.in);

     // variable to keep running total

    int total=0;

    System.out.println("please enter an Integer:");

     // read the input first time

    in=scr.nextInt();

    System.out.print("individual digits of "+in+" is ");

    if(in<0)

    {

        in=-(in);

    }

   

 

     //call the function to print the digits of number

   print_dig(in);

   System.out.println();

    //calculate the sum of all digits

   do{

       int r=in%10;

       total=total+r;

       in=in/10;

   }while(in>0);

    //print the sum

   System.out.println("total of digits is: "+total);

     

   }catch(Exception ex){

       return;}

}

 //recursive function to print the digits of number

public static void print_dig(int num)

{  

    if(num==0)

    return;

    else

    {

    print_dig(num/10);

    System.out.print(num%10+" ");

    }

}

}

Explanation:

Read a number with the help of object of scanner class.if the input is negative

then make it positive number before calling the function print_dig().call

the recursive method print_dig() with parameter "num".It will extract the digits

of the number and print then in the order. Then in the main method, calculate

sum of all the digits of number.

Output:

please enter an Integer:                                                                                                  

3456                                                                                                                      

individual digits of 3456 is 3 4 5 6                                                                                      

total of digits is: 18

please enter an Integer:                                                                                                  

-2345                                                                                                                      

individual digits of -2345 is 2 3 4 5                                                                                      

total of digits is: 14

You might be interested in
What type of computer would integrate the system unit with the display and keyboard?
kolbaska11 [484]
Pc
hope this helps have a good day

3 0
3 years ago
The amount of the lighted side of the moon you can see is the same during
Arte-miy333 [17]
D.full moon and third quarter phase.
7 0
4 years ago
Which of the following are good ways to protect your computer? check all that apply
Zanzabum
Everything but answer d I think
7 0
4 years ago
Read 2 more answers
What type of file is MyFile.exe?
a_sh-v [17]
The file type is excel
6 0
3 years ago
Create JUnit Tests Create JUnit Tests to test the budgetBalance methods in both the AllInclusive and ALaCarte classes. Test each
goblinko [34]

Answer:

Here is the code.

Explanation:

8 0
3 years ago
Other questions:
  • All ofthe following are correct EXCEPT one option when it comes towriting disappointing news letters. Identify theexception.
    6·1 answer
  • Consider the Google Trends graph of dogs and cats. Give a plausible explanation or hypothesis for the spike in dog searches that
    12·1 answer
  • A radiologist’s computer monitor has higher resolution than most computer screens. Resolution refers to the:______
    15·1 answer
  • A packet analyzer is a program that can enable a hacker to do all of the following EXCEPT ________. Select one: A. assume your i
    6·1 answer
  • Define a class named ComparableSquare that extends Square (defined above) and implements Comparable. Implement the compareTo met
    7·1 answer
  • Save As .csv .html Worksheet tab Format Cells SUM Function Destination cell Home tab AutoFit A. Applies a border or shading to s
    6·1 answer
  • Evie clicks through her presentation slides and realizes they all have transition effects coming from the same location, from th
    13·1 answer
  • What are interpersonal skills for non-technical user
    14·1 answer
  • Expalin the defference between the driver of a desktop and a laptop​
    8·1 answer
  • True or Fale A criminal defense attorney's main focus is to convict the accused of a crime, and a state prosecutor is to defend
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!