Ugly Number – Python

Ugly Number is one the first programming problem I resolve. My first Ugly number was in C programming. Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5,6, 8, 9, 10, 12, 15, show the first 11 ugly numbers. By convention, 1 is included. I wrote a program that asks the user for a number and determines if the number entered is an ugly number. I compile using Python 2.7.

In this program you can see a good example for while loop in Python. Also in this program you will see combination of equations and if statement. This code and program is free to use and share, any questions free to ask.

 

Download Source Code

#XD Creations Ugly Number

print("***** Ugly Number *****")
print("========================")
x = input ("Enter a number: ")
print("========================")

answer = x
#Using While Loops we can check if number is ugly or not.
while (x%2 == 0):#Checking if remainder 2 is equal to 0.
   x = x / 2 #Dividing by 2
while (x%3 == 0):#Checking if remainder 3 is equal to 0.
   x = x / 3 #Dividing by 3
while (x%5 == 0):#Checking if remainder 5 is equal to 0.
   x = x / 5  #Dividing by 5
#Dividing every number if the result is 1 is a ugly number
if x == 1:
 print `answer` + " is a Ugly Number"
 print("========================")
 print("      By: XD Creations")
#If the number is not 1 isn't a  ugly number
elif x != 1:
 print `answer` + " Is not a Ugly Number"
 print("========================")
 print("      By: XD Creations")
#Checking if the input is less or equal than 100000
elif x >= 100000:
 print " Wrong Number"
 print("========================")
 print("      By: XD Creations")
 raw_input ("Press  Enter to finish")

#XD Creations Ugly Number

Leave a comment