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
For a typically large organization how many dns servers should you install
DiKsa [7]
<span>A large organization minimum at-least 2 DNS servers are needed. DNS servers are each internet domain in need. and Multiple server farms distribute the DNS Server. One of the DNS Server is must in separate location. install DNS server on all domain controller. DNS server on every domain controller is must.</span>
4 0
3 years ago
Is a protocol that allows users to log on to and access a remote computer?
kompoz [17]
The answer is yes I hope this help ya out

6 0
3 years ago
The Backstage view is where you can see information and options pertaining to the user account and settings. How is the Backstag
Georgia [21]

Answer:b) Mail settings can be changed under the options menu

Explanation: I fr just guessed so y’all can have an answer and I ended up getting it rights

6 0
2 years ago
The Early Days (Pre-Mechanical Period)​
Irina18 [472]

Answer:

The earliest age of technology. It can be defined as the time between 3000 B.C. and 1450 A.D. When humans first started communicating, they would try to use language to make simple pictures – petroglyphs to tell a story, map their terrain, or keep accounts such as how many animals one owned, etc.

<h2>Please mark me as brainliest</h2>

7 0
2 years ago
What theme could come from Piper's feelings and actions. Pixars short film piper
kaheart [24]

Answer:

Overcoming Fear

Explanation:

Given that a movie theme stimulates a universal human ordeal and at the same time, Piper is a short animated movie that was released in 2016. The fundamental idea of the movie is about a baby sandpiper who has to survive and conquer his anxiety and phobia of the water.

Hence, in this situation, the theme that could come from Piper's feelings and action is OVERCOMING FEAR

5 0
3 years ago
Other questions:
  • What is the sum of each pair of binary integers? (assuming 8-bit register is used)
    5·1 answer
  • Was the type writer the first part of the keyboard? ​
    8·1 answer
  • Describe the positive and negative effects of Internet​
    6·2 answers
  • The electric company gives a discount on electricity based upon usage. The normal rate is $.60 per Kilowatt Hour (KWH). If the n
    7·1 answer
  • To enter new code that performs as intended in the place of the old code that produces an error is the goal of
    9·1 answer
  • Two employees were very different in terms of​ age, gender, and other demographic data but they shared a common love of dogs and
    8·1 answer
  • According to the government, employees have a right to understand the risks associated with the materials they work with.
    15·1 answer
  • How do i make spaces in python<br> To draw hello world
    15·1 answer
  • What is the mest gun in pixel gun 3d imma give you brainliest
    14·2 answers
  • Which spreadsheet feature could the scholarship committee use to locate applicants who meet the criteria?
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!