USING JAVA write the function sumOfDigits(in). This function takes a non-negative integer paramenter n and returns the sum of its digits. No credit will be given for a solution using a loop
Examples:
sumOfDitigts(1234) --> 10
sumOfDitigts(4123) --> 10
sumOfDitigts(999) --> 27

Answers

Answer 1

The `sumOfDigits` function recursively adds the last digit of a number to the sum of its remaining digits. This process continues until the number becomes less than 10. The main method demonstrates the function's usage.

public class Main {

   public static int sumOfDigits(int n) {

       if (n < 10) {

           return n;

       }

               return n % 10 + sumOfDigits(n / 10);

   }

       public static void main(String[] args) {

       System.out.println(sumOfDigits(1234)); // Output: 10

       System.out.println(sumOfDigits(4123)); // Output: 10

       System.out.println(sumOfDigits(999));  // Output: 27

   }

}

The sumOfDigits function takes a non-negative integer n as input and recursively calculates the sum of its digits.

In the function, the base case is defined by checking if the number n is less than 10. If n is less than 10, it means that n is a single-digit number, so the function simply returns n.

If n is greater than or equal to 10, the function uses the modulus operator % to obtain the last digit of n (by calculating n % 10) and adds it to the recursive call of sumOfDigits with the remaining digits (by calculating sumOfDigits(n / 10)).

The function continues this process until the number becomes less than 10, at which point the sum of all the digits is returned.

In the main method, the sumOfDigits function is called with different input values to demonstrate its usage. The expected output is shown as comments next to each function call.

learn more about System.out.println here:

https://brainly.com/question/14428472

#SPJ11


Related Questions

A new radio station must register its frequency at Malaysian Communications and Multimedia Commission (MCMC). The free slot only available at 93.0 MHz and it can broadcast its channel within a bandwidth of 200 kHz. If the frequency deviation for modulation is 18 kHz: (i) (ii) what is the maximum modulating frequency to achieve modulation index of 3? [C3, SP4] how much is the bandwidth used if the maximum modulating frequency is 18 kHz? [C3, SP4] (iii) sketch the FM signal spectra if the modulating index is 0.5 and assuming that the amplitude of the carrier signal is 10 V. [C3, SP4]

Answers

To achieve a modulation index of 3, the maximum modulating frequency would be 54 kHz. The bandwidth used in this case would be 108 kHz. When the modulating index is 0.5 and the carrier signal amplitude is 10 V, the FM signal spectra can be sketched with the help of Bessel functions, resulting in a series of sidebands around the carrier frequency.

The modulation index (m) is defined as the ratio of the frequency deviation (Δf) to the maximum modulating frequency (fm). In this case, the frequency deviation is given as 18 kHz, and we need to find the maximum modulating frequency to achieve a modulation index of 3. Using the formula for modulation index, we can rearrange it to find the maximum modulating frequency: fm = Δf / m. Substituting the given values, we get fm = 18 kHz / 3 = 6 kHz. However, the maximum modulating frequency should be within the bandwidth of 200 kHz. Since 6 kHz is within this range, it becomes the maximum modulating frequency.

To determine the bandwidth used, we use the formula BW = 2 × (Δf + fm). Substituting the given values, we get BW = 2 × (18 kHz + 6 kHz) = 48 kHz. However, we need to note that the bandwidth is double the deviation, as it includes both the upper and lower sidebands.

When the modulating index is 0.5 and the carrier signal amplitude is 10 V, we can sketch the FM signal spectra using Bessel functions. The sidebands will be located around the carrier frequency, and their amplitudes will be determined by the Bessel function coefficients. The amplitude of the carrier signal remains unchanged, while the sidebands vary in amplitude. The resulting spectrum will show a central carrier peak and multiple sideband peaks, with the amplitudes decreasing as we move away from the carrier frequency.

Learn more about modulation index

brainly.com/question/13265507

#SPJ11

Other Questions
Write Verilog code for Register File/Memory that has following specifications: 128 x 32 bits memory 3-write addresses 3-read addresses 2-Independent clocks (clk_read, clk_write) Writing code for quadcopter state-space model in MATLAB, How to plug parameters of the quadcopter in A B C and D matrix, provide an example in matlabWhereA is the System MatrixB is the Input MatrixC is the Output MatrixD is the Feed forward Matrix a) (b) Streams are objects that represent sources and destinations of data. Streams that are sources of data can be read from, and streams that are destinations of data can be written to. Streams are ordered and in sequence so that the Java virtual machine can understand and work upon the stream. They exist as a communication medium, just like electromagnetic waves in wireless communication. (i) Based on your understanding, is Java programming able to read input and write output file in pdf (.pdf) format? Write down your opinion and why you decide it. C2 [SP1] (ii) Write a Java program that read your name and matrix number in .txt file and write as output string at command prompt. C5 [SP4] (i) (iii) Modify the Java program in Q4(a)(ii) so the output will be in .txt file with addition of "I love Java Programming" String into it. C4 [SP3] Describe what is User Support and how it can help the user? When Java programmer want to design User Interface (UI), some of the important principles need to understand for it to be user-friendly. The users must be put in control of the interface so the user will feel comfortable when using the product. C2 [SP1] Using MATLAB, Write a program that plotsp(x,t) = 32.32cos(4* 10^3 * t -12.12x + 36) Which of the following is NOT an advantage of ADRs to U.S. shareholders?A) Settlement for trading is generally faster in the United States.B) Transfer of ownership is done in the U.S. in accordance with U.S. laws.C) In the event of the death of the shareholder, the estate does not go through a foreign court.D) All of the above are advantages of ADRs. 1. Discuss the contributions of microfinance to the Ghanaianeconomy. how do software characteristics differ from hardware characteristics? the first step in analyzing a negligence claim is to determine whether the tortfeasor owed a legal (one word) to the injured party. If you have a deposit plan for 5 years to deposit $461 at theend of each year, how much money would be there after 5 years ifthe interest rate is 6% compounded quarterly?(Do not use the negative si the definition of states: a global strategy in which a firm allows a foreign company to produce its product in exchange for a fee. Calculate the inrush current on a 12470-277/480V 150kVA delta-wye transformer assuming the inrush is 12X full load amps for 6 cycles. a. 144A b. 83Ac. 6498A d. 2165A Examine the picture below. Answer the True or False stament.The purpose of the double-headed arrow (white) as pointed to by the red arrow is to select all fields from the table in the design of Quer A simplified model of hydrogen bonds of water is depicted in the figure as linear arrangement of point ...12 charges. The intra molecular distance between q and 92, as well as qs and qs is 0.10 nm (represented as thick line). And the shortest distance between the two molecules is 0.17 nm (q2 and qs, inter-molecular bond as dashed line). The elementary charge e = 1.602 x 10-1C. Midway OH -0.35 H +0.356 OH-0.35e H +0.35 91 42 93 7 Fig. 2 (a) Calculate the energy that must be supplied to break the hydrogen bond (midway point), the elec- trostatic interaction among the four charges. (b) Calculate the electric potential midway between the two H2O molecules. Question 40 ( 2 points) An example of a discretionary access control would be using the chmod or chosm commands to change file ownership or permissions. True False [5 points] The impulse response function is obtained from the transfer function of a system when the input signal is equated to a unit step function. Select one: O True O False [5 points] If two blocks A and B respectively are in cascade connection, then the resultant using block diagram reduction technique is: O A+B O A/B O 2* (A+B) O A*B [5 points] Differentiate between a linear and non-linear multimediaapplication using3 appropriate examples All of the following are examples of good practices of water management in hotels and resorts except Select one: a. increasing water flush in toilets for better efficlency. b. considering ozone laundry technologies. c. using grey water for fountains. d. introducing linen reuse programs. a. The following text describe problem situations, Read the text and note down th problem signaling words and expressions. (10 Marks) Zinn's Burgers and Pima Zinn's Burgers and Pizza is a medium-sized A 0.25-kg block oscillates linearly on the end of the spring with a spring constant of 160 N/m. If the system has an energy of 5 J, then the magnitude of the amplitude of the oscillation is: .........m, round to two decimal places. 14. Which of the following is not an argument that can be used to support the need for using ethical reasoning in business a) Inadequacy of laws b) Inadequacy of moral rules c) complexity of moral problems d) all of the above