Computer Science, asked by sapkotashreejana960, 3 months ago

6.1 Write a program to find and print all four-digit numbers of the type ABCD, where: A+B = C+D.

Answers

Answered by allysia
3

Language:

Python

Program:

___________________________________________

for i in range(1000,9999):

   num=str(i)

   if int(num[3])+int(num[2]) == int(num[1])+int(num[0]):

       print(i)

___________________________________________

Output:

-- too long to paste or write here--

Explanation:

Using // was a bit tricky here so I converted the 4 digit number (I accessed through the loop) to text and then accessed the number though indices.

Converted it back to integer and checked the given condition with and returned the number.

If you wanna check the sum per number use this insted:

___________________________________________

for i in range(1000,9999):

   num=str(i)

   if int(num[3])+int(num[2]) == int(num[1])+int(num[0]):

       print(int(num[3])+int(num[2]), int(num[1])+int(num[0]),i)

___________________________________________

Similar questions