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
Design and document an IP addressing scheme to meet ElectroMyCycle’s needs. Specify which IP address blocks will be assigned to
noname [10]

Answer:

Given that: Design scenario - page 197 has a continuation of the chapter 1 design scenario for ElectroMyCycle

Explanation:

See attached image

7 0
1 year ago
python A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun
e-lub [12.9K]

Answer:

// program in Python.

#read year

i_year=int(input("Please Enter a year:"))

#check leap year

if((i_year % 4 == 0 and i_year % 100 != 0) or (i_year % 400 == 0)):

   print("{} is a leap year.".format(i_year))

else:

   print("{} is not a leap year.".format(i_year))

Explanation:

Read year from user and assign it to variable "year".If year is completely divisible by 4 and not divisible by 100 or year is completely divisible by 400 then year is leap year otherwise year is not a leap year.

Output:

Please Enter a year:2003                                                                                                          

2003 is not a leap year.

3 0
2 years ago
Read 2 more answers
Sam’s password is known to be formed of 3 decimal digits (0-9) in a given order. Karren and Larry are attempting to determine Sa
prohojiy [21]

Answer:

100

Explanation:

7 0
2 years ago
Read 2 more answers
To explain acceptable computer use to their employees, students, or other users, many organizations and educational institutions
docker41 [41]

Answer:

Codes of conduct

Explanation:

Codes of conduct are a set of rules or norms established by an organization for all employees, students or users, to ensure individual responsibilities and proper practices. The code of conduct can cover overall behaviour of individuals in an organization, but a specific code of conduct can be developed for proper computer use in order to establish what is appropriate and available to use in the organization´s computers, and also to restrict or avoid non related content.

7 0
1 year ago
Read 2 more answers
Which of the following are recommended techniques for protecting computer files and data? Check all of the boxes that apply.
lara [203]
Hello <span>Pouerietzach


Question: </span><span>Which of the following are recommended techniques for protecting computer files and data? Check all of the boxes that apply.
</span><span>
Answer: A, B, D, E, G

Hope That Helps
-Chris</span>
5 0
2 years ago
Read 2 more answers
Other questions:
  • The Spinning Jenny reduced the number of workers necessary to _______. a.remove cotton seeds from fibers b.pump water from the m
    7·1 answer
  • Graphical elements that precede each item in a list are known as​ __________.
    8·1 answer
  • The ________ program displays graphics and loading screens during the boot process.
    8·1 answer
  • Initialize the list short_names with strings 'Gus', 'Bob', and 'Zoe'. Sample output for the givenprogram:Gus Bob Zoeshort_names
    13·1 answer
  • __________ access control is a form of __________ access control in which users are assigned a matrix of authorizations for part
    8·1 answer
  • 12) Suppose you wanted a subroutine to return to an address that was 3 bytes higher in memory than the return address currently
    11·1 answer
  • Create two classes. The first, named Sale, holds data for a sales transaction. Its private data members include the day of the m
    8·1 answer
  • The terminal window wants to evaluate your current bash knowledge by using the ~/workspace/nested-directories folder:
    15·1 answer
  • In this exercise you will debug the code which has been provided in the starter file. The code is intended to take two strings,
    7·1 answer
  • 4.9 Code Practice: Question 4
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!