answer.
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]
2 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]2 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
10. There is repeated code in these onevent blocks. Choose the correct code for the updateScreen() function which would be calle
Lorico [155]

Answer:

front zise24

Explanation:

on event down botton

6 0
2 years ago
Without using any additional variables, and without changing the values of ndays or the elements of the parkingTickets array, wr
dusya [7]

Answer:

Explanation:

mostTickets=0;

for (k=0; k< ndays; k++){

if (parkingTickets[k]>mostTickets) mostTickets=parkingTickets[k];

}

7 0
2 years ago
Which of the following statements best describes the relative amount of content held by digital libraries vs. the amount held by
Lesechka [4]

Answer:

Digital libraries tend to have access to more information because they can share with other digital libraries.

Explanation:

A digital library can be defined as an electronic or cloud-based library where informations and books about various things, places, people, animals, subjects, etc, are kept for readers to access over the internet.

On the other hand, a traditional library is a physical library that people can walk into to read.

The statement which best describes the relative amount of content held by digital libraries vs. the amount held by traditional libraries is that, digital libraries tend to have access to more information because they can share with other digital libraries because they are usually interconnected through network.

5 0
2 years ago
9. Which of the following are true for all regular languages and all homomorphisms? (a) h (L1 ∪ L2)= h (L1) ∩ h (L2). (b) h (L1
mixer [17]

Answer:

h (L1 ∪ L2)= h (L1) ∩ h (L2).

This is true.

There will be w ∈  (L1 ∪ L2) for any s ∈ h (L1 ∪ L2) in such a way that s=h(w)

we can assume that w ∈ L1

So In this case h(w) ∈ L (S1). Hence s ∈ L(S1)

for any s ∈ h (L1) U h(L2)

We can assume that s ∈ L(S1)

There exists w ∈ L1 such that s= h(w)

In this case it is w ∈ L1 U L2 as well.

Hence , s ∈ h (L1 U L2)

Explanation

consider  = 0,1 and  = a,b and h(0) = a , h(1) = ab

(a) Consider L1 = 10,01 and L2 = 00,11

   Now L1 ∪ L2 = 00,01,10,11

   h (L1 ∪ L2) = h(00) , h(01) , h(10) , h(11) = h(0)h(0) , h(0)h(1) , h(1)h(0) , h(1)h(1)

  = aa, aab , aba , abab

Hence h (L1 ∪ L2) = aa, aab , aba , abab .

Here h (L1) = h(10) , h(01) = h(1)h(0) , h(0)h(1) = aba , aab

Hence h (L1) = aba , aab .

Here h (L2) = h(00) , h(11) = h(0)h(0), h(1)h(1) = aa, abab

Hence h(L2) = aa, abab.

Finally Hence , h (L1 ∪ L2)= h (L1) ∩ h (L2).

5 0
2 years ago
When considering server consolidation, plan on running ___________ vCPUs per core.a. 1 to 2b. 3 to 4c. 4 to 6d. 6 to 8
GREYUIT [131]

Answer:

c. 4 to 6

Explanation:

According to the VMware, a virtual machine software company widely known for producing the virtualization software, which is capable of making computer hardware components to be compartmentalized into various virtual layers, recommended the best practices of virtual CPU per core to be running 4 to 6.

Hence, in this case, the correct answer is option C

4 0
2 years ago
Other questions:
  • In what year were graphical user interfaces (GUIs) pioneered? 1969 1974 1991 2001
    12·2 answers
  • Mark’s friends told him about an automated program that sends unsolicited messages to multiple users. Which type of program were
    11·2 answers
  • Using the expected format, of putting key information where the reader can’t find it, is an example of?
    8·2 answers
  • Suppose a computer using direct mapped cache has 220 bytes of byte-addressable main memory, and a cache of 32 blocks, where each
    5·1 answer
  • Write a function that computes the average and standard deviation of four scores. The standard deviation is defined to be the sq
    15·1 answer
  • XYZ Corp.’s facilities in Nashua, New Hampshire, are two office buildings 400 feet apart, each with its own LAN. To connect the
    9·1 answer
  • C++
    8·1 answer
  • The elements of an integer-valued array can be initialized so that a[i] == i in a recursive fashion as follows: An array of size
    10·1 answer
  • The signature area in a cover letter includes the sender's first and last name, followed by his/her job title (if applicable). *
    13·1 answer
  • Write a MATLAB function named lin_spaced_vector with two inputs and one return value. The first input will be a single real numb
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!