python basic coding QB
python basic coding QB
Program:
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
2. Write a program to find the given number is positive or negative.
Program:
# Input: 1.2
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Program:
# Input1 : 21
# Input2 : 11
# Output2 : 32
4. Write a program to find if the given number is prime or not.
Program:
# input: 23
flag = False
if num > 1:
if (num % i) == 0:
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE).
flag = True
break
if flag:
else:
# 23 is a prime number
5. Write a program to check if the given number is palindrome or not.
Program:
# Input: 12321
temp = num
reverse = 0
remainder = temp % 10
temp = temp // 10
if num == reverse:
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE).
print('Palindrome')
else:
print("Not Palindrome")
# Output: Palindrome
6. Write a program to check if the given number is Armstrong or not.
Program:
# Input: 407
sum = 0
temp = num
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
else:
Program:
if(sorted(s1)== sorted(s2)):
else:
# input1: "listen"
# input2: "silent"
check(s1, s2)
Program:
if a >= b:
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE).
return a
else:
return b
# input1: 2
# input2: 4
print(maximum(a, b))
# output: 4
9. Write a program to find a minimum of two numbers.
Program:
if a <= b:
return a
else:
return b
# input1: 2
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE).
# input2: 4
print(minimum(a, b))
# output: 2
10. Write a program to find a maximum of three numbers.
Program:
largest = a
largest = b
else:
largest = c
return largest
# Input1: 10
# Input2: 14
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE).
# Input3: 12
print(maximum(a, b, c))
# Output: 14
11. Write a program to find a minimum of three numbers.
Program:
# 12
# 14
# 11
smallest = 0
smallest = a
smallest = b
smallest = c
Program:
#7
factorial = 1
if num < 0:
elif num == 0:
else:
factorial = factorial*i
# 5040
13. Write a program to find a fibonacci of a number.
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE).
Program:
#7
n1, n2 = 0, 1
count = 0
if nterms <= 0:
elif nterms == 1:
print(n1)
else:
print("Fibonacci sequence:")
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE).
# Fibonacci sequence:
#0
#1
#1
#2
#3
#5
#8
14. Write a program to find GCD of two numbers.
Program:
if (a == 0):
return b
if (b == 0):
return a
if (a == b):
return a
if (a > b):
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE).
return gcd(a-b, b)
a = 98
b = 56
if(gcd(a, b)):
else:
print('not found')
15. Write a program to print the following pattern.
Program:
def myfunc(n):
for i in range(0, n):
for j in range(0, i+1):
print("* ",end="")
print("\r")
n=5
myfunc(n)
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE).
Program:
def myfunc(n):
k=n-1
for i in range(0, n):
for j in range(0, k):
print(end=" ")
k=k-1
for j in range(0, i+1):
print("* ", end="")
print("\r")
n=5
myfunc(n)
17. Write a program to print the following pattern.
Program:
def num(n):
num = 1
for i in range(0, n):
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE).
num = 1
for j in range(0, i+1):
print(num, end=" ")
num = num + 1
print("\r")
n=5
num(n)
18. Write a program to print the following pattern.
Program:
def num(n):
num = 1
for i in range(0, n):
for j in range(0, i+1):
print(num, end=" ")
num = num + 1
print("\r")
n=5
num(n)
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE).
Program:
def alphapat(n):
num = 65
for i in range(0, n):
for j in range(0, i+1):
ch = chr(num)
print(ch, end=" ")
num = num + 1
print("\r")
n=5
alphapat(n)
20. Write a program to print the following pattern.
Program:
def contalpha(n):
num = 65
JAI SHRIRAM ENGINEERING COLLEGE
TIRUPPUR – 638 660
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai
Recognized by UGC & Accredited by NAAC and NBA (CSE and ECE).