When I answered this it was C
Answer:
def sum_digits(number):
total = 0
if 1 <= number <= 999:
while number > 0:
r = int (number % 10)
total +=r
number /= 10
else:
return -1
return total
print(sum_digits(658))
Explanation:
Write a function named sum_digits that takes one parameter, number
Check if the number is between 1 and 999. If it is, create a while loop that iterates until number is greater than 0. Get the last digit of the number using mudulo and add it to the total. Then, divide the number by 10 to move to the next digit. This process will continue until the number is equal to 0.
If the number is not in the given range, return -1, indicating invalid range.
Call the function and print the result
Answer:
Sorted array will be:-
3 5 5 9
Explanation:
In Selection Sort algorithm it sorts the array by repeatedly finds the minimum in the array form the array that is unsorted and swaps it with the value at the first position.
The unsorted array:- 5 3 9 5
In first iteration
minimum is 3.
3 will be swapped with 5.
Now the array is 3 5 9 5
sorted array 3
In second iteration
unsorted array is 5 9 5
sorted array 3 5
and the minimum from it is 5
No swapping will occur.
In third iteration
unsorted array is 9 5
minimum is 5
sorted array 3 5 5
swap it with 9.
Now the array is sorted.
3 5 5 9