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

python record ex 1

Uploaded by

emimavjangel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

python record ex 1

Uploaded by

emimavjangel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

23CS1008 Python Programming Lab

Ex.No.1
USE OF CONTROL STATEMENTS Reg.No: URK24CS1004
16.12.24

B) Write a python program to check whether a number is prime or not

Aim:
To check whether if a number is prime or not.

Algorithm:

Step 1: Input the number.


Step 2: Check if the num<2, if so, print “num is not a prime number” , else go to step 3.
Step 3: Use for loop with range, using if condition check if num%i==0, if so, print “num is not
a prime number”, else print “ num is a prime number”.
Step 4: End the program.

Program:
print("URK24CS1004")
print("Jeniliya J")
num = int(input(“Enter a number:”))
if num<2:
print(num,” is not a prime number”)
else:
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”)

Output: (Screenshots)

Result:
Hence, the code is executed successfully.

1
23CS1008 Python Programming Lab

2
23CS1008 Python Programming Lab

D) Write a python program that iterates the integers from1 to 15. For multiples of two print
“Karunya” instead of the number and for the multiples of three print “University”. For numbers
which are multiples of both two and three print “KarunyaUniversity”

Aim:
The aim of the program is to iterate the integers from1 to 15. For multiples of two print “Karunya”
instead of the number and for the multiples of three print “University”. For numbers which are
multiples of both two and three print “KarunyaUniversity”

Algorithm:
Step 1: Start the program .
Step 2: Start the loop from 1 to 15.
Step 3: Check whether the number is divisible by 2 and 3, if so, print “ KarunyaUniversity”, else step4.
Step 4: If not, check if the number is divisible 2. If so, print “Karunya”, else go to step 5.
Step 5: Check if the number is divisible by 3, if so print “University”.
Step 6: If none of the above, print the number.

Program:
print("URK24CS1004")
print("Jeniliya J")
for num in range (1, 16):
if num%2==0 and num%3==0:
print(“KarunyaUniversity”)
elif num%2==0:
print(“Karunya”)
elif num%3==0:
print(“University”)
else:
print(num)

Output: (Screenshots)

Result:
Hence, the code is executed successfully.

3
23CS1008 Python Programming Lab

Result:

4
23CS1008 Python Programming Lab

F) Write a python program to check whether the number is Armstrong number or not.

Aim:
To check whether the number is Armstrong number or not.

Algorithm:
Step 1: Start the program.
Step 2: Input the number num.
Step 3: Set sum=0 and temp=num.
Step 4: Use while loop with condition temp>0.
Step 5: Extract the last digit , digit=temp%10.
Step 6: Add the cube of the digit to sum, sum+=digit**3.
Step 7:Remove the last digit, temp//=10.

Program:
print("URK24CS1004")
print("Jeniliya J")
num=int(input(“Enter a number:”))
sum=0
temp=num
while temp>0:
digit=temp%10
sum+=digit**3
temp//=10
if sum==num:
print(num,” is an Armstrong number”)
else:
print(num,” is not an Armstrong number”)

Output: (Screenshots)

Result:
Hence, the code is executed successfully.

5
23CS1008 Python Programming Lab

Result:

6
23CS1008 Python Programming Lab

You might also like