Write a program that counts how many of the squares of the numbers from 1 to 100 end in a
1.
count = 0
# loop through the numbers from 1 to 100
for i in range(1, 101):
# square the number
squared = i ** 2
# convert the squared number to a string
squared_str = str(squared)
# check if the last character is a 1
if squared_str[-1] == '1':
# if it is, increment the count
count += 1
# print the count
print(count)
This program first sets a count variable to 0. It then loops through the numbers from 1 to 100, squares each number, converts the squared number to a string, and checks if the last character is a 1. If it is, it increments the count. Finally, it prints the count.
If you run the program, it will print the count of how many of the squares of the numbers from 1 to 100 end in a 1. For example, if you run the program, you might see something like:
10
This means that 10 of the squares of the numbers from 1 to 100 end in a 1.

0 Comments