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
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]
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