0% found this document useful (0 votes)
129 views

Program File 11

The document contains Python programs to find the larger/smaller number between two numbers, larger/smaller number between three numbers, generate different patterns using nested loops, calculate series sums, check if a number is perfect, Armstrong or palindrome, check if a number is prime or composite, display Fibonacci series terms, and find the greatest common divisor and least common multiple of two numbers.

Uploaded by

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

Program File 11

The document contains Python programs to find the larger/smaller number between two numbers, larger/smaller number between three numbers, generate different patterns using nested loops, calculate series sums, check if a number is perfect, Armstrong or palindrome, check if a number is prime or composite, display Fibonacci series terms, and find the greatest common divisor and least common multiple of two numbers.

Uploaded by

Gunkriti Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Q1.

Python program to input welcome message and display it


1.

Q2. Python program to input two numbers and display the larger/smaller number.
# python program to find smaller and larger number
#take two number from user
num1 = int(input("Enter first number :"))
num2 = int(input("Enter second number :"))
if (num1 > num2):
print(num1,"is larger than",num2)
elif (num1 == num2):
print(num1,"is equal to",num2)
else :
print(num1,"is smaller than",num2)

 Q3. Python program to input three numbers and display the larger/smaller number.

Q4. Generate the following patterns using nested loop

PATTERN-1 PATTERN-2 PATTERN-3


* 12345 A
** 1234 AB
*** 123 ABC
**** 12 ABCD
***** 1 ABCDE

1 # Left triangle star pattern


2 n = 5
3
4 for i in range(1, n+1):
5     # internal loop run for i times
6     for j in range(1, i+1):
        print("*", end="")
7     print()
8

1 n = 5
2 for i in range(n+1):
3     for j in range(1,n-i+1):
4         print(j, end=' ')
    print()
5
1 # Left triangle pattern
2 n = 5
3 for i in range(n):
4     for j in range(i+1):
        print(chr(j + 65), end="")
5     print()
6

 write a program to input the value of x and n and print the sum of the
following series:

1) 1 + x² + x³ + … + xⁿ
2) 1-x + x² – x³ + … + xⁿ
3 )x – x2/2 + x3/3 – x4/4 + x5/5 – x6/6
4) x +x2/2! – x3/3! + x4/4! – x5/5! + x6/6!

1 #Program to print 1) 1 + x² + x³ + … + xⁿ
2
x = int(input("Enter the value of x : "))
3 n = int(input("Enter the value of n : "))
4 sum = 0
5 for i in range(n+1):
6     if(i%2 == 0):
7         sum = sum + (x**i)
    else:
8         sum = sum + (x**i)
9
10
11 print("Sum of Series is :",sum)
12
13

1
x = int(input("Enter the value of x : "))
2 n = int(input("Enter the value of n : "))
3 sum = 0
4 for i in range(n+1):
5     if(i%2 == 0):
6         sum = sum+(x**i)
    else:
7         sum = sum-(x**i)
8 print("Sum of Series is :",sum)
9

1
x = int(input("Enter the value of x : "))
2 n = int(input("Enter the value of n : "))
3 sum = x
4 for i in range(2,n+1):
5     if(i%2 == 0):
6         sum = sum+((x**i)/i)
    else:
7         sum = sum-((x**i)/i)
8 print("Sum of Series is :",sum)
9

1
2
3 #Program to print x + x2/2! - x3/3! ...............
4 import math
5 x = int(input("Enter the value of x : "))
6 n = int(input("Enter the value of n : "))
sum = x
7 for i in range(2,n+1):
8     if(i%2 == 0):
9         sum = sum+((x**i)/math.factorial(i))
10     else:
11         sum = sum-((x**i)/math.factorial(i))
print("Sum of Series is :",sum)
12
13
14

Determine whether a number is a perfect number ,an armstrong number or a palindrome

1
2 n = int(input("Enter any number to check whether it is perfect number or not  : "))
sum = 0
3 # Check for perfect number
4 for i in range(1,n):
5     if n%i==0:
6         sum = sum + i
7
if sum == n :
8     print( n,"is perfect number")
9 else :
10     print( n, "is not perfect number")
11
1
2 #check for armstrong number
3 n = int(input("Enter any number to check whether it is  an armstrong   : "))
temp = n
4 total = 0
5 while temp > 0 :
6     digit = temp %10
7     total = total + (digit**3)
    temp = temp//10
8 if n == total:
9     print( n,"is an armstrong number")
10 else :
11     print( n, "is not armstrong number")
12

1
2 #check for palindrome number
3 n = int(input("Enter any number to check whether it is palindrome  : "))
temp = n
4 rev = 0
5 while n > 0:
6     d = n % 10
7     rev = rev *10 + d
    n = n//10
8
if temp == rev :
9     print( temp,"is palindrome number")
10 else :
11     print( temp, "is not palindrome number")
12

Input a number and check if the number is prime or composite number

1
2 #Input a number and check if the number is prime or composite number
3 n= int(input("Enter any number:"))
if(n ==0 or n == 1):
4     printf(n,"Number is neither prime nor composite")
5 elif n>1 :
6     for i in range(2,n):
7         if(n%i == 0):
8             print(n,"is not prime but composite number")
            break
9     else:
10         print(n,"number is prime but not composite number")
11 else :
12     print("Please enter positive number only ")
13

Display the terms of fibonacci series

1 #Display the terms of Fibonacci Series


2
# first two terms
3
n1 = 0
4 n2 = 1
5 term = int(input("Enter the number of terms : "))
6 # if number is negative or zero
7 if term <=0:
8
9     print("Please enter positive number only")
10 else:
11 # if there is only one term entered by user
12     if term ==1:
13         print(n1,end = " ")
    else :
14         print(n1,n2,end=" ")
15         for i in range(2,term):
16             n3 = n1+n2
17             print(n3,end=" ")
            n1 = n2
18             n2 = n3
19
20

Compute the greatest common divider and least common multiple of two integer

1
2 # python program to find LCM of two number using GCD
3
4 #input two numbers
5 n1 = int(input("Enter First number :"))
n2 = int(input("Enter Second number :"))
6 x = n1
7 y = n2
8 while(n2!=0):
9     t = n2
    n2 = n1 % n2
10
    n1 = t
11 gcd = n1
12 print("GCD of {0} and {1} = {2}".format(x,y,gcd))
13 lcm = (x*y)/gcd
14 print("LCM of {0} and {1} = {2}".format(x,y,lcm))
15

You might also like