Answer:
please mark as brainliest!!
Explanation:
public class SumOfDigits{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Please enter a number to calculate sum of digits"); int number = sc.nextInt(); // Remember number/10 reduces one digit from number // and number%10 gives you last digit int sum = 0; int input = number; while (input != 0) { int lastdigit = input % 10; sum += lastdigit; input /= 10; } System.out.printf("Sum of digits of number %d is %d", number, sum); // closing Scanner to prevent resource leak sc.close(); } }
Answer:
enhances the readability of a program.
Answer:
def sum_1k(M):
s = 0
for k in range(1, M+1):
s = s + 1.0/k
return s
def test_sum_1k():
expected_value = 1.0+1.0/2+1.0/3
computed_value = sum_1k(3)
if expected_value == computed_value:
print("Test is successful")
else:
print("Test is NOT successful")
test_sum_1k()
Explanation:
It seems the hidden part is a summation (sigma) notation that goes from 1 to M with 1/k.
- Inside the <em>sum_1k(M)</em>, iterate from 1 to M and calculate-return the sum of the expression.
- Inside the <em>test_sum_1k(),</em> calculate the <em>expected_value,</em> refers to the value that is calculated by hand and <em>computed_value,</em> refers to the value that is the result of the <em>sum_1k(3). </em>Then, compare the values and print the appropriate message
- Call the <em>test_sum_1k()</em> to see the result
Answer:
Explanation:
The following program is written in Java. Using the program code from Purchase class in 5.13 I created each one of the fruit objects. Then I set the price for each object using the setPrice method. Then I set the number of each fruit that I intended on buying with the setNumberBought method. Finally, I called each objects getTotalCost method to get the final price of each object which was all added to the totalCost instance variable. This instance variable was printed as the total cost of the bill at the end of the program. My code HIGHLIGHTED BELOW
//Entire code is in text file attached below.
//MY CODE HERE
DecimalFormat df = new DecimalFormat("0.00");
oranges.setPrice(10, 2.99);
oranges.setNumberBought(2*12);
eggs.setPrice(12, 1.69);
eggs.setNumberBought(2*12);
apples.setPrice(3, 1);
apples.setNumberBought(20);
watermelons.setPrice(1, 4.39);
watermelons.setNumberBought(2);
bagels.setPrice(6, 3.50);
bagels.setNumberBought(12);
totalCost = oranges.getTotalCost() + eggs.getTotalCost() + apples.getTotalCost() + watermelons.getTotalCost() + bagels.getTotalCost();
System.out.println("Total Cost: $" + df.format(totalCost));
}
}