******************************************************************************************
Q.)
Given ‘n’ integers, write an algorithm and the subsequent Python code to print all numbers that are sum-equivalent to the first number. Two numbers ‘m’ and ‘n’ are said to be sum-equivalent if ‘m’ and ‘n’ have the same number of digits and the sum of the digits in ‘m’ and ‘n’ are equal. 12381 is sum-equivalent to 10545. Here both the numbers are five digit numbers. Sum of the digits in 12381 is 1+2+3+8+1=15. Similarly the sum of the digits in 10545 is 15.
Write a function to check whether two numbers are sum-equivalent or not. If none of the numbers are sum-equivalent then print ‘No sum-equivalent’.
Input Format
First line contains the number of elements, n
Next ‘n’ line contains the numbers
Output Format
Print first number in the first line
Next few lines contain the numbers that are sum-equivalent to first number.
If the none of the numbers are sum-equivalent , then Print “No sum-equivalent”
******************************************
1) Define a function to find the sum of digits of a number
2) Define a function to check whether two numbers are sum equivalent or not
3) Read the numbers
4) For all numbers check if they are sum equivalent to the first number.
5) Print the sum equivalent numbers
6) If the none of the numbers are sum-equivalent , then Print “No sum-equivalent”
Pseudo-Code:
def sum_digits(n):
s=0
while(n>0):
s = remainder(n,10)
n//=10
return s
def sum_eq(m,n):
temp=0
if(sum of digits of m = sum of digits of n):
temp=1
return temp
for i in range(1,n):
if(sum_eq(numbers[0],numbers[i])==1):
temp=1
print(numbers[i])
if(temp==0):
print('No sum-equivalent')
****************************************************************************
not running bro
ReplyDeleteplease tell me what to do
I think you should try (for I in range(1,n-1))bcoz list numbering will only be from 0 to n-1
Deletethe last limit is excluded so the range is correct duh
DeleteThis doesn't check for no of digits in the input so some test cases may not work
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete