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
Do you agree to the song that ''magtanim ay di biro?'' why?
iren [92.7K]

Answer:

I agree in this song because being a farmer is not easy you need to be hardwork

5 0
2 years ago
What is the output of 1101×10==11000+10
aliya0001 [1]

Answer:

west ward cpt output

Explanation:

i know because i invented it

6 0
3 years ago
The computer that process data that are represented in the form of discrete values are called​
Vinvika [58]

Answer:

Digital computer

Explanation:

The computer that process data that are represented in the form of discrete values are called digital computer.

7 0
3 years ago
Read 2 more answers
The steps for creating a newsletter are to _____.
lys-0071 [83]
Puting your ideas together every write has to do that and also gather true information, and making a intering story for your readers.
7 0
3 years ago
Read 2 more answers
Brainly keeps deleting my questions and I don’t know why It says I violated the rules but I didn’t. pls help
labwork [276]

Well brainly could be deleting you question because of things like,

  • The question is for an exam
  • The question does not include proper question material
  • A singular swear word
  • Its not subject related to(math, science, history or etc).

<em>Hope this helps!</em>

3 0
2 years ago
Other questions:
  • In the INSERT statement that follows, assume that all of the table and column names are spelled correctly, that none of the colu
    9·1 answer
  • Multiple systems try to send data at the same time. The electrical impulses sent across the cable interfere with each other. Wha
    15·1 answer
  • Which service is enabled on a cisco router by default that can reveal significant information about the router and potentially m
    10·1 answer
  • The systems development life cycle (SDLC) is the overall process of developing, implementing, and retiring information systems t
    9·1 answer
  • Linda wants to apply for a job in a company of her choice. Which information would her potential employers likely review in her
    10·2 answers
  • Consider the code below. When you run this program, what is the output if the temperature is 77.3 degrees Fahrenheit?
    6·1 answer
  • What is the problem, if any, with the following code?The desired output is [3,19].
    13·1 answer
  • Who loves you tube , anyone?
    10·2 answers
  • Answer if you know Javascript, html, css, python, and Ruby.
    12·1 answer
  • What is the primary way that social networks work? Multiple Choice providing a security system for communication online connecti
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!