You are on page 1of 22

CS Practical File

TOPIC- PYTHON PROGRAMS

BY
ARMAN JAISWAL
XI-C
ROLLNO.6
NO. OF PRACTICAL PROGRAMS
INDEX
s.no programs remarks sign.
1. #input a welcome message and
Display it.
2. #input two numbers and display
The larger number
3. #input three numbers and display
The largest number
4. #generate the patern from nested
Loop
5. #write a program to input the value
Of x and n and print the sum of the
Following series
6. #determine the number is a perfect
Number,an armstrong number or
A palindrome
7. #input a number and check whether
The number is prime or composite
Number
8. #display the term of a fibocci series
9. # compute the greatest common
devisor and least common multiple
of two integers
10. # count and display the number of
Vowels,consonents,uppercase and
Lowerecase character In a string
11. #input a string and determine
Whether it is a palindrome or
Not; convert the case of the
Character in a string

PROGRAMS-:
#INPUT A WELCOME MESSAGE AND DISPLAY
IT

PROGRAM
message=input(“Enter welcome message :” ) # message is a input variable that is taking value by
“showing Enter welcome message”
print(“Hello,” ,message) # The variable message is printed with a hello in the beginning.

OUTPUT

#INPUT TWO NUMBER AND DISPLAY THE


LARGER NUMBER
PROGRAM
a = int(input(“Enter the first number: “)) #input first number
b = int(input(“Enter the second number: “)) #input second number
if(a >= b): #comparing
print(a, “is greater”)
else:
print(b, “is greater”)

OUTPUT

#INPUT 3 NUMBER AND DISPLAY THE LARGEST


NUMBER
PROGRAM
#num1 = float(input("Enter first number: "))
#num2 = float(input("Enter second number: "))
#num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):


largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3

print("The largest number is", largest)

OUTPUT

#GENERATE FOLLOWWING PATERN WITH


NESTED LOOP
1.

PROGRAM
def star_pattern1(n):

for i in range(1, n+1):

for j in range(1, i + 1):


print("* ", end="")
print()

n = int(input("Enter number of rows:"))


star_pattern1(n)

OUTPUT

2.
PROGRAM
for i in range(6, 1, -1):

for j in range(1, i):

print(j, end="")

print ()

OUTPUT

3.
PROGRAM
# outer loop
for i in range (65,70):
# inner loop
for j in range(65,i+1):
print(chr(j),end="")
print()

OUTPUT

#WRITE THE PROGRAM TO INPUT THE


VALUE OF X AND N AND PRINTTHE SUM OF
THE FOLLOWING SERIES:
1. 1+x+x+x+x …….x^n

PROGRAM

x = float (input ("Enter value of x :"))


n = int(input ("Enter value of n :"))
s = 0
for a in range (n + 1) :
s += x**a
print ("Sum of series", s)

OUTPUT

2. 1-x+x^2+x^3+x^4……..x^n

PROGRAM
x = float (input ("Enter value of x :"))
n = int (input ("Enter value of n :"))
s=0

for a in range (n + 1) :
    if a%2==0:
        s += x**a
    else:
        s -= x**a
print ("Sum of series", s)

OUTPUT

3. x-x^2/2+x^3/3+x^4/4+…….x^n/n

PROGRAM
x = float (input (“Enter value of x :”))
n = int (input (“Enter value of n :”))
s=0

for a in range (n + 1) :
if a%2==0:
s += (x**a)/n

else:
s -= (x**a)/n
print (“Sum of series”, s)

OUTPUT

4. 1+x^2/2!+x^3/3!+x^4/4! ……..x^n/n!

PROGRAM
x = float (input ("Enter value of x :"))
n = int (input ("Enter value of n :"))
s=0
a=1
fact=1
for a in range (1,n + 1) :
    fact=fact*a
    if a%2==0:
        s += (x**a)/fact
    else:
        s -= (x**a)/fact
print ("Sum of series", s)

OUTPUT

#DETERMINE WHETHER THE NUMBER IS


PERFECT NUMBER , ARMSTRONG NUMBER
OR PALINDROME
PROGRAM
def palindrome(n): # palindrome a function is made and given the input n
temp = n #temp is taken as input
rev = 0 #reverse is 0
while n > 0: #if n is greater than 0 this loop runs
dig = n % 10
rev = rev * 10 + dig
n = n // 10
if temp == rev:
print(temp, “The number is a palindrome!”)
else:
print(temp, “The number isn’t a palindrome!”)

def armstrong(n):
count = 0
temp = n
while temp > 0:
digit = temp % 10
count += digit ** 3
temp //= 10
if n == count:
print(n, “is an Armstrong number”)
else:
print(n, “is not an Armstrong number”)

def perfect(n):
count = 0
for i in range(1, n):
if n % i == 0:
count = count + i
if count == n:
print(n, “The number is a Perfect number!”)
else:
print(n, “The number is not a Perfect number!”)

if name == ‘main‘:
n = int(input(“Enter number:”))
palindrome(n)
armstrong(n)
perfect(n)

OUTPUT
#INPUT A NUMBER AND CHECK IF THE
NUMBER IS A COMPOSITE OR PRIME
NUMBER

PROGRAM
num = int(input("Enter any number : "))

if num > 1:
for i in range(2, num):
    if (num % i) == 0:

print(num, "is NOT a prime number")
            break
  else:
        print(num, "is a PRIME number")
elif num == 0 or 1:
    print(num, "is a neither prime NOR c
omposite number")
else:
    print(num, "is NOT a prime number it 
is a COMPOSITE number")
OUTPUT

#DISPLAY THE TERM OF THE FIBONACCI


SERIES

PROGRAM
nterms = int(input("How many terms? "))
n1, n2 = 0, 1
count = 0

if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci series upto",nterms,":")
print(n1)
else:
print("Fibonacci series:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

OUTPUT

#COMPUTE THE GREATEST COMMON


DEVISOR OF TWO INTEGERS

PROGRAM
a = int(input("Enter integer 1 : "))
b = int(input("Enter integer 2 : "))
for i in range(1,a+b):
⠀⠀⠀if a%i==0 and b%i==0:
⠀⠀⠀⠀⠀⠀⠀s=i
print("Greatest common integer = ",s)

OUTPUT

#COUNT AND DISPLAY THE NUMBER OF


VOWELS,CONSONANTS,UPPERCASE,
LOWERCASE CHARACTER IN A STRING

PROGRAM
print("Count and display the number of vowels, consonants, uppercase, lowercase")

print('characters in string.')

str = input("Enter the string : ")

vowels = 0

consonants = 0

uppercase = 0

lowercase = 0

for i in range(0, len(str)):

if(str[i].isupper()):

uppercase = uppercase + 1

elif((str[i].islower())):

lowercase = lowercase + 1

str = str.lower()

for i in range(0, len(str)):

if(str[i] == 'a' or str[i] == 'e' or str[i] == 'i' or str[i] == 'o' or str[i] == 'u'):

vowels = vowels + 1

elif((str[i]>= 'a' and str[i]<= 'z')):

consonants = consonants + 1

print("Vowels: ", vowels);

print("Consonants: ", consonants);

print("uppercase: ", uppercase);

print("lowercase: ", lowercase);

OUTPUT
#INPUT A STRING AND DETERMINE
WHETHER IT IS A PALINDROME OR
NOT;CONVERT CASE OF CHARACTER IN A
STRING
PROGRAM
def isPalindrome(str):

for i in range(0, int(len(str)/2)):

if str[i] != str[len(str)-i-1]:

return False

return True

s = "malayalam"

ans = isPalindrome(s)

if (ans):

print("Yes")

else:

print("No")

OUTPUT

You might also like