0% found this document useful (0 votes)
10 views3 pages

Check Digital Power & Digit Sequences

The document contains three programming tasks involving number manipulation. The first task checks if a number is a Digital Power Number, the second determines if the digits of a number are strictly increasing, decreasing, or neither, and the third assesses if a number is 'Happy' or 'Sad' based on the sum of squares of its digits. Each task includes code snippets demonstrating the logic and implementation using loops and conditionals.

Uploaded by

story forever
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Check Digital Power & Digit Sequences

The document contains three programming tasks involving number manipulation. The first task checks if a number is a Digital Power Number, the second determines if the digits of a number are strictly increasing, decreasing, or neither, and the third assesses if a number is 'Happy' or 'Sad' based on the sum of squares of its digits. Each task includes code snippets demonstrating the logic and implementation using loops and conditionals.

Uploaded by

story forever
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

"""

1. Given a number N, check whether it is a Digital Power Number. A number is a


Digital Power Number if the sum of its digits raised to the power of their positions
(from left to right) equals the number itself. Use loops and if-else logic no
string slicing allowed
"""

#Name and roll number:


print("Nabihah, PG/01/MSASDS/2025/014")

#Taking a number input:


N = int(input("Enter an integer to check whether it is a digital power number: "))

#Converting the integer into string:


N = str(N)

#Initializing a variable to hold the sum of digits:


sums = 0

#A boolean that shows whether the given number is a digital power or not:
digital_power = "is not"

#A loop to go across all the digits of the given number


for i in range(0, len(N)):
int_N = int(N[i])
sums = sums + pow(int_N, i+1)
#If the sum is the same as the given number, then "is a digital power number":
if sums == int(N):
digital_power = "is"
break
else:
digital_power = "is not"
i =+ 1

#Printing the solution:


print("The given number,", N, digital_power, "a digital power number.")

"""
2. Given a positive integer N, check if the sequence of its digits is strictly increasing,
strictly decreasing, or neither. If the sequence is increasing, print ”INCREASING”,
if decreasing, ”DECREASING”, otherwise ”NEITHER”. Use a for loop and
break for early termination.
"""

#Name and roll number:


print("Nabihah, PG/01/MSASDS/2025/014")

#Taking a number input:


N = int(input("Enter an integer to check whether it is strictly increasing,"
" strictly decreasing or neither:"))

#Converting the integer into string:


N = str(N)

#Keeping count of number of pairs that are either increasing or decreasing:


count_increase = 0
count_decrease = 0

1
#Comparisions of the digits of N:
for i in range(1, len(N)):
if N[i] > N[i-1]: #The adjacent digits are increasing
count_increase += 1
elif N[i] < N[i-1]: #The adjacent digits are decreasing
count_decrease += 1
else:
break

#Checking the number of increases and decreases against the number of digits in N:
if count_increase == (len(N) - 1):
print("The given number,", N, "has a series of digits that is strictly increasing")
elif count_decrease == (len(N) -1):
print("The given number,", N, "has a series of digits that is strictly decreasing")
else:
print("The given number,", N, "has a series of digits that is neither strictly"
" increasing nor strictly decreasing")

"""
3. Given an integer n, repeatedly replace the number with the sum of squares of its
digits until the number becomes a single digit. If during the process the number
becomes 1, return ”Happy”, otherwise return ”Sad”.
"""
#Defining a function to find the sum square of the digits in n:
def sum_square(x):
x = str(x)
sum_square = 0
for i in range(0, len(x)):
sum_square = sum_square + int(x[i])**2
return sum_square

#Function to check if the number is one digit number:


def unit_number(x):
if 0 <= x <= 9:
return True
else:
return False

#Name and roll number:


print("Nabihah, PG/01/MSASDS/2025/014")

#Taking a number input:


n = int(input("Enter a number to check its mood: "))

#A variable that will be required:


sum_squares = 0
one_digit = False

#A while loop to go until the number is a unit number:


while True:
#A loop that adds the digits of the number until it becomes one digit number
sum_squares = sum_square(n)
n = sum_squares
one_digit = unit_number(sum_squares)
#If the sum becomes a one digit number:
if one_digit:

2
#If the unit number is 1, happy:
if sum_squares == 1:
print("HAPPY")
break
#If the unit number is not 1, sad:
else:
print("SAD")
break

You might also like