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
Logic errors are easily identified when a program is compiled true or false
lord [1]
<span>A compiler executes each program statement as soon as it is translated.
</span>The statement that logic errors are easily identified when a program is compiled is false, because the compiler locates only syntax errors, but logic errors <span>can be eliminated only through careful examination of your program. </span>
Logic errors are usually more difficult to find and resolve than syntax errors.


7 0
3 years ago
Read 2 more answers
Why is graphics important in multimedia application <br>​
erastova [34]
Graphics are important in multimedia application this is because humans are visually oriented etc.
5 0
3 years ago
GUI allows users to communicate with a device and see what they are doing onscreen.
zloy xaker [14]
The answer is true.
Gui basically is the thing that lets you see your desktop and what your doing. The G means graphics. Makes sense. If we didn't have GUI, then we would have to type in code to access documents, and do things on the computer. GUI changes that.
8 0
3 years ago
How do humans sense movement?
Butoxors [25]

Answer:

The answer to this question is "kinaesthesia".

Explanation:

Kinesthesis means the interpretation of body movements it also known as kinaesthesia. kinaesthesia requires the ability to track changes in the direction and movements of the body without depending on the information from both the five sense organs.

kinaesthesia means sensation, whenever we involved in physical activity such as walking, running, cycling, singing, swimming, etc. this is the reason the movement of humans is called  as kinaesthesia.

3 0
3 years ago
The _______ of the story describes the events that takes place as well as the conflict or problem that must be resolved.
Crank

Answer:

The answer is C the plot.

7 0
3 years ago
Other questions:
  • What will be the value of i after the C statements at the right have been executed
    6·1 answer
  • The technique of ________ uses three columns that allows the entrepreneur to weigh both the advantages and the disadvantages of
    13·1 answer
  • When using bits to represent fractions of a number, can you create all possible fractions? Why or why not?
    6·1 answer
  • A field with the ____ data type can store a unique sequential number that Access assigns to a record. Access will increment the
    7·1 answer
  • Identify a true statement to determine the number of columns in a web table.
    7·1 answer
  • Think of some ways you could use Excel at home. Describe one example of something in your life that could be improved or more ea
    14·1 answer
  • If you are going to refer to a few dozen classes in your application, how do<br> you do it?
    6·1 answer
  • Drag each label to the correct location on the image.
    7·1 answer
  • To break a text string into several lines, which means that the text string continues on the next line, the _____ character shou
    8·1 answer
  • status is a non-volatile memory chip used for store translation data between personal computer PC and digital device ​
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!