Answer:
The solution in Python is as follows:
num = int(input("Number: "))
if num>50:
sum = 0
count = 0
for i in range(1,num):
count = count + 1
sum = sum + i**2
if sum > num:
sum = sum - i**2
count = count - 1
break;
print("Sum: "+str(sum))
print("Numbers: "+str(count))
else:
print("Number must be greater than 50")
Step-by-step explanation:
The condition stated in the question do not conform with the example. The question says, the loop should stop when sum > x.
But:
When x = 100 and sum = 91, the program loop should not stop because 91 is not greater than 100.
However, I'll answer based on the example given in the question.
This prompts user for number
num = int(input("Number: "))
The following if condition is executed if number is greater than 50
if num>50:
This initializes sum to 0
sum = 0
This initializes count to 0
count = 0
The iterates through the inputted number (e.g. 100)
for i in range(1,num):
This increases the count
count = count + 1
This calculates the sum of square of the positive integer
sum = sum + i**2
The following removes excess number from the sum
if sum > num:
sum = sum - i**2
count = count - 1
break;
This prints the calculated sum
print("Sum: "+str(sum))
This prints the count of number used
print("Numbers: "+str(count))
The following is executed if user input is less than 50
else:
print("Number must be greater than 50")