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
Drag the tiles to the correct boxes to complete the pairs.
Gala2k [10]

Answer:

So the first one i think is A. The second is 2, and that i do know

Explanation:

6 0
3 years ago
Carbon monoxide poisoning can result from breathing
Mamont248 [21]
The answer is the letter a
6 0
3 years ago
Who could vote in the first democracy?
zmey [24]
B citizens

in the 4th century there were about 100k citizens. only about 40k could participate in the democratic process ( this is only if the question is referring to ancient democracy )
4 0
3 years ago
Function of an actuator
RSB [31]

Answer:

an actuator is a device that uses a form of power to convert a control signal into mechanical motion

6 0
3 years ago
Read 2 more answers
You are asked to write an app to keep track of a relatively small music library. The app should load song information from a dat
gogolik [260]

Answer:

C++ PROGRAM

Explanation:

6 0
3 years ago
Other questions:
  • What is one course of action available in every problem solving process?
    9·2 answers
  • Programming assignment (100 pts): In the C++ programming language write a program capable of playing Tic-Tac-Toe against the use
    14·1 answer
  • Which browser should you choose if you want all viewing information except thumbnail previews of websites?
    15·2 answers
  • What is the 5 basic steps of computer programing?
    6·1 answer
  • Jenis jenis perangkat keras?
    14·1 answer
  • Can Someone help plz?
    11·1 answer
  • Does anyone know what i did wrong?
    13·1 answer
  • FREEE POINTS
    8·2 answers
  • Hello i need help whats -5 = 500
    13·2 answers
  • What is a cyber crime?<br><br>help
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!