Question:
Program to find the LCM of two numbers
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# Python Program to find the L.C.M. of two input number # defining a user defined function for finding LCM def lcm(x, y): # choose the greater number if x > y: large = x else: large = y while(True): if((large % x == 0) and (large % y == 0)): lcm = large break large += 1 return lcm nos1 = int(input("Enter first number: ")) nos2 = int(input("Enter second number: ")) print "The L.C.M. of", nos1,"and", nos2,"is", lcm(nos1, nos2) |
Explanation:
A common multiple is a number that is a multiple of two or more numbers. The common multiples of 3 and 4 are 0, 12, 24, …. The least common multiple(LCM) of two numbers is the smallest number (not zero) that is a multiple of both.
Output:
1 2 3 4 5 |
Enter first number: 6 Enter second number: 3 The L.C.M. of 6 and 3 is 6 |
Question
Write a python program to check whether given string is palindrome or not without using any inbuilt function to reverse the string
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Program to check if a string is palindrome or not take input from the user string = raw_input("Enter a string: ") #Use inbuilt function to convert all characters to lowercase string = string.lower() # check if the string is equal to its reverse if string == string[::-1]: print("It is palindrome") else: print("It is not palindrome") |
Explanation:
A palindrome is a string which is same read forward or backwards. Dad is a palindrome while Python is not.
In the program, we first accept the string to ‘string’. Since capital letters and small letters are perceived differently because of difference in ASCII codes, we convert the string to all lower for uniformity using inbuilt function ‘x.lower()’ where x is the string
In the above program, we have used the slice operator to reverese the string.The slice operator[n:m:p] returns the part of the string from nth to the (m-1)th character Setting p to -1 returns the reverse order of the slice of string you asked for. Since here we want the whole string, we use the shorthand [::-1]
Compare both the string and it’s reversal. If they are equal, then it’s a palindrome, otherwise not.
Output:
1.
1 2 3 |
Enter a string: Dad It is palindrome |
1 2 3 |
Enter a string: Python It is not palindrome |
Question 2
Write a Python program to check whether a given number is armstrong or not.
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# Python program to check if the number provided by the user is an Armstrong number or not # take input from the user nos = int(input("Enter a number: ")) # initialize sum sum = 0 # find the sum of the cube of each digit temp = nos while temp > 0: digit = temp % 10 sum += digit ** 3 temp //= 10 # display the result if nos == sum: print nos,"is an Armstrong number" else: print nos,"is not an Armstrong number" |
Explanation:
Armstrong number is the number whose sum of cube of individual digits is equal to the number itself.
Output:
1.
1 2 3 |
Enter a number: 371 371 is an Armstrong number |
1 2 3 |
Enter a number: 56 56 is not an Armstrong number |
Question:
Program to print the Fibonacci Number
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# Program to display the Fibonacci sequence up to n-th term n= int(input("Enter Limit: ")) # first two terms n1 = 0 n2 = 1 count = 0 # check if the number of terms is valid if n <= 0: print "Fibonacci exist only for positive numbers" elif n == 1: print "Fibonacci sequence upto",n,":" print n1 else: print "Fibonacci sequence upto",n,":" while count < n: print n1, n3 = n1 + n2 # update values n1 = n2 n2 = n3 count += 1 |
Explanation:
A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8….
The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms
Ex: 5th term is sum of 4th and 3rd term;3=2+1
Output:
1.
1 2 3 4 5 |
Enter Limit: 10 Fibonacci sequence upto 10 : 0 1 1 2 3 5 8 13 21 34 |
1 2 3 |
Enter Limit: -5 Fibonacci exist only for positive numbers |
1 2 3 4 5 |
Enter Limit: 1 Fibonacci sequence upto 1 : 0 |
Question;
Write a program to print the multiplication table of a number
Program:
1 2 3 |
num = int(input("Display multiplication table of: ")) for i in range(1,11): print(num,'x',i,'=',num*i) |
1 2 3 4 5 6 7 8 9 10 11 |
Display multiplication table of: 17 17 x 1 = 17 17 x 2 = 34 17 x 3 = 51 17 x 4 = 68 17 x 5 = 85 17 x 6 = 102 17 x 7 = 119 17 x 8 = 136 17 x 9 = 153 17 x 10 = 170 |
Question:
Write a program to print some Python built-in functions documents, such as abs(), int(), raw_input(). The built-in document method is __doc__.
Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
print abs.__doc__ print int.__doc__ print raw_input.__doc__ def square(num): '''Return the square value of the input number. The number must be integer. ''' return num ** 2 print square(2) print square.__doc__ |
Explanation:
Python documentation strings (or docstrings) provide a convenient way of associating documentation with Python modules, functions, classes, and methods. An object’s docsting is defined by including a string constant as the first statement in the object’s definition
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
abs(number) -> number Return the absolute value of the argument. int(x=0) -> int or long int(x, base=10) -> int or long Convert a number or string to an integer, or return 0 if no arguments are given. If x is floating point, the conversion truncates towards zero. If x is outside the integer range, the function returns a long instead. If x is not a number or if base is given, then x must be a string or Unicode object representing an integer literal in the given base. The literal can be preceded by '+' or '-' and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int('0b100', base=0) 4 raw_input([prompt]) -> string Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading. 4 Return the square value of the input number. The number must be integer. |