[Link]
com/
CST 362: PROGRAMMING IN PYTHON
TUTORIAL QUESTIONS
MODULE-I
1. Write a python program to input a time in seconds and print the time in
HH:MM:SS format.
2. Program to find Area and Circumference of a Circle.
3. Write a python program to check whether a number is even or odd.
m
4. Program to compare two numbers.
co
5. Program to find the roots of a quadratic equation.
6. Program that accepts the length of three sides of a triangle as input and
s.
determine whether or not the triangle is a right angled triangle.
te
7. Program to input a point and find the quadrant.
no
8. Program to find the Sum of the digits of a number.
9. Program to check whether the given number is prime or not
la
[Link] program to find the sum of even numbers from N given numbers.
ra
[Link] a Python program which takes a positive integer n as input and finds
the sum of cubes of all positive even numbers less than or equal to the
ke
number.
[Link] 4 integers (+ve and −ve). Write a Python code to find the sum of
negative numbers, positive numbers, and print them. Also, find the averages
of these two groups of numbers and print.
[Link] a Python program to reverse a number. Prompt the user for input.
[Link] first 10 Fibonacci numbers
[Link] Prime numbers less than 1000
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
[Link] a Nested loop to print the following pattern
54321
4321
321
21
1
[Link] to Print Multiplication table of 1-n numbers.
m
[Link] a python program to check Armstrong number of n digits.
co
Eg:1634= 1**4+6**4+3**4+4**4=1634
s.
te
no
la
ra
ke
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
TUTORIAL QUESTIONS
MODULE-I
1. Write a python program to input a time in seconds and print the time in
HH:MM:SS format.
Ans:
m
#program to convert time in sec to HH:MM:SS format
time=input("Enter time in seconds")
co
time=int(time)
timeinmin=time//60
s.
te
timeinsec=time%60
timeinhr=timeinmin//60
no
timeinmin=timeinmin%60
la
print("HH:MM::SS----{}:{}:{}".format(timeinhr,timeinmin,timeinsec))
ra
2. Program to find Area and Circumference of a Circle.
ke
Ans:
#Python Program to find Area and Circumference of a Circle
#Standard formula to calculate the Area of a circle is: a=π r².
#Circumference c=2 π r.
import math
r=input("Enter radius :")
r=int(r)
a=[Link] * r * r
c=2* [Link] * r
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
print("Area of the circle",a)
print ("Circumference of the circle",c)
3. Check whether a number is even or odd
Ans:
x=int(input("Enter a number...:"))
if x%2==0:
print("number is even")
else:
m
print("number is odd")
co
4. Compare two numbers s.
Ans:
te
no
x=int(input("Enter first number.."))
la
y=int(input("Enter second number.."))
if x>y:
ra
print("x is greater than y")
ke
elif x<y:
print ("x is smaller than y")
else:
print ("x and y are equal")
5. Program to find the roots of a quadratic equation
Ans:
import math
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
print("enter a b and c the coefficients line by line")
a=int(input())
b=int(input())
c=int(input())
if a==0:
print("Not a quadratic eqtn..root is ", -c/b)
else:
d=b*b-4*a*c
if d==0:
m
print("only one root",-b/(2*a))
co
elif d>0:
print("roots are real") s.
print("root1",-b+[Link](d)/(2*a))
te
print("root2",-[Link](d)/(2*a))
no
else:
la
print("roots are imaginary")
ra
6. Program that accepts the length of three sides of a triangle as input and
ke
determine whether or not the triangle is a right angled triangle
Ans:
a=int(input("enter side a..:"))
b=int(input("enter side b..:"))
c=int(input("enter side c..:"))
if a+b>c and a+c>b and b+c>a:
if a**2+b**2==c**2:
print("Right angled triangle")
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
else:
print("Not a right angled triangle")
else:
print("given sides does not form a triangle")
7. Input a point and find the quadrant
Ans:
x=int(input("Enter x:"))
y=int(input("enter y:"))
m
if x>0 and y >0:
co
print("first quadrant")
elif x<0 and y >0: s.
print("second quadrant")
te
elif x<0 and y <0:
no
print("third quadrant")
la
elif x>0 and y <0:
print("fourth quadrant")
ra
else:
ke
print("point at origin")
8. Program to find the Sum of the digits of a number
Ans:
sum=0
n=int(input("Enter a number.."))
while n!=0:
sum=sum+n%10;
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
n=n//10
print("Sum of the digits=",sum)
Output:
Enter a number..123
Sum of the digits=6
9. Program to check whether the given number is prime or not
Ans:
m
n=int(input("Enter a number.."))
co
i=2
prime=True s.
while i<=n//2:
te
if n%i==0:
no
prime=False
la
break
i=i+1
ra
if prime==True:
ke
print('Prime number')
else:
print('Not a prime number')
Output:
Enter a number..7
Prime number
Enter a number..4
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
Not a Prime number
[Link] program to find the sum of even numbers from N given
numbers
Ans:
sum=0
N=int(input("enter the number of numbers (N).."))
print("Enter the Numbers")
m
for i in range(N):
co
num=int(input())
if num%2==0: s.
sum=sum+num
te
print("Sum of even numbers..",sum)
no
la
Output:
Enter the number of numbers (N)..5
ra
Enter the 5 Numbers
ke
4
2
6
8
1
Sum of even numbers.. 20
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
[Link] a Python program which takes a positive integer n as input and
finds the sum of cubes of all positive even numbers less than or equal to
the number.
Ans:
sum=0
N=int(input("enter the number"))
for num in range(1,N+1):
if num%2==0:
sum=sum+num**3
m
print("Sum of cubes of even numbers..",sum)
co
output s.
enter the number 5
te
Sum of cubes of even numbers.. 72
no
[Link] 4 integers (+ve and −ve). Write a Python code to find the sum of
la
negative numbers, positive numbers, and print them. Also, find the
ra
averages of these two groups of numbers and print.
ke
Ans:
psum=0
nsum=0
pc=0
nc=0
print("Enter the 4 Numbers +ve and -ve")
for i in range(4):
num=int(input())
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
if num>0:
psum=psum+num
pc=pc+1
else:
nsum=nsum+num
nc=nc+1
print("Sum of +ve numbers..",psum)
if pc!=0:
print("Avg of +ve numbers..",psum/pc)
m
print("Sum of -ve numbers..",nsum)
co
if nc!=0:
s.
print("Avg of -ve numbers..",nsum/nc)
te
output
no
Enter the 4 Numbers +ve and -ve
la
2
3
ra
-4
ke
-1
Sum of +ve numbers.. 5
Avg of +ve numbers.. 2.5
Sum of -ve numbers.. -5
Avg of -ve numbers.. -2.5
[Link] a Python program to reverse a number. Prompt the user for
input.
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
Ans:
rev=0
print("Enter a number")
num=int(input())
while num!=0:
d=num%10
rev=rev*10+d
num=num//10
print("Reverse of the number=",rev)
m
co
output s.
Enter a number
te
123
no
Reverse of the number= 321
la
[Link] first 10 Fibonacci numbers
ra
Ans:
ke
a=0
b=1
for i in range(10):
c=a+b
a=b
b=c
print(c)
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
ouput
1 2 3 5 8 13 21 34 55 89
[Link] Prime numbers less than 1000
Ans:
print("Prime numbers less than 1000")
for n in range(2,1000):
m
i=2
co
while i<=n/2:
if n%i==0: s.
break
te
i=i+1
no
else:
la
print (n,end=' ')
ra
[Link] a Nested loop to print the following pattern
ke
54321
4321
321
21
1
Ans:
n=int(input("Enter a number::"))
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
for i in range(n,0,-1):
for j in range(i,0,-1):
print (j,end=' ')
print()
[Link] Multiplication table of 1-n numbers
n=int(input("Enter n::"))
for k in range(1,n+1):
for i in range(1,11):
m
print(k ,"X",i,"=",k*i)
co
print()
s.
[Link] numbers
te
A positive integer is called an Armstrong number of order n if
no
abcd... = a^n + b^n + c^n + d^n + ...
la
In case of an Armstrong number of 3 digits, the sum of cubes of each
digit is equal to the number itself. For example:153 = 1*1*1 + 5*5*5 +
ra
3*3*3 // 153 is an Armstrong number.
ke
Ans:
# Python program to check if the given 3 digit number is an Armstrong
number or not
# take input from the user
num = int(input("Enter a number: "))
# initialize sum sum = 0
# find the sum of the cube of each digit
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
m
print(num,"is not an Armstrong number")
co
Output s.
Enter a number: 663
te
663 is not an Armstrong number
no
Enter a number: 407
la
407 is an Armstrong number
ra
[Link] Armstrong number of n digits
ke
Eg:1634= 1**4+6**4+3**4+4**4=1634
Ans:
num =int(input("Enter a number..."))
# Changed num variable to string, and calculated the length (number of
digits)
order = len(str(num))
For More Study Materials : [Link]
[Link]
CST 362: PROGRAMMING IN PYTHON
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
m
co
# display the result
if num == sum: s.
print(num,"is an Armstrong number")
te
else:
no
print(num,"is not an Armstrong number")
la
output
ra
Enter a number...1634
ke
1634 is an Armstrong number
For More Study Materials : [Link]