Armstrong Number Aim:: IFETCE R-2019 ACADEMIC YEAR: 2021-2022
Armstrong Number Aim:: IFETCE R-2019 ACADEMIC YEAR: 2021-2022
Armstrong Number Aim:: IFETCE R-2019 ACADEMIC YEAR: 2021-2022
Ex No: 1a
ARMSTRONG NUMBER
Date :
AIM:
To write a python program to check if the number provided by the user is an Armstrong
number or not.
ALGORITHM:
1. Read a number
2. Initialize sum=0 and Times=0
3. Calculate number of individual digits
4. Divide the given number into individual digits using mod operator
5. Calculate the power of n for each individual and add those numbers
6. Compare the sum with the original number
7. If they exactly matched then it is armstrong number else it is not Armstrong
8. Finally, compare the sum with the original number and conclude that it is Armstrong
number if they are equal
PROGRAM:
#! /usr/bin/python
Number = int(input("\nPlease Enter the Number to Check for Armstrong: "))
# Initializing Sum and Number of Digits
Sum = 0
Times = 0
# Calculating Number of individual digits
Temp = Number
while Temp > 0:
Times = Times + 1
Temp //= 10
# Finding Armstrong Number
Temp = Number
while Temp > 0:
Reminder = Temp % 10
Sum = Sum + (Reminder ** Times)
Temp //= 10
if Number == Sum:
print("\n %d is Armstrong Number.\n" %Number)
else:
print("\n %d is Not a Armstrong Number.\n" %Number)
OUTPUT:
apl@apl-desktop ~/Desktop/pythonpractice $ ./Arm.py Please Enter the Number to Check for
Armstrong: 9474
9474 is Armstrong Number.
RESULT:
Thus the python program for checking of Armstrong number has been successfully
executed and output is verified.
Ex No: 1b
DESIGN OF SIMPLE CALCULATOR
Date :
AIM:
To write a program for making a simple calculator that can add, subtract, multiply and
divide using python.
ALGORITHM:
PROGRAM:
#!/usr/bin/python
4
5
('Result = ', -1)
apl@apl-desktop ~/Desktop/pythonpractice $ ./calc.py
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 3
Enter two numbers:
4
5
('Result = ', 20)
apl@apl-desktop ~/Desktop/pythonpractice $ ./calc.py
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 4
Enter two numbers:
4
5
('Result = ', 0)
apl@apl-desktop ~/Desktop/pythonpractice $
apl@apl-desktop ~/Desktop/pythonpractice $ ./calc.py
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 4
Enter two numbers:
10
2
('Result = ', 5)
RESULT:
Thus the program for making a simple calculator that can add, subtract, multiply and
divide using python has been executed successfully and the output is verified.
ALGORITHM:
PROGRAM:
#! /usr/bin/python
num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
OUTPUT:
apl@apl-desktop ~/Desktop/pythonpractice $ chmod a+x fact.py
apl@apl-desktop ~/Desktop/pythonpractice $ ./fact.py
Enter a number: 5
RESULT:
Thus the program to find the factorial of a given number using python has been executed
successfully and the output is verified.
To write a python program to generate random numbers from 1 to 20 and append them to
the list.
ALGORITHM:
PROGRAM:
import random
a=[]
n=int(input("Enter number of elements:"))
for j in range(n):
a.append(random.randint(1,20))
print('Randomised list is: ',a)
OUTPUT:
RESULT:
Thus the program to generate random numbers from 1 to 20 and append them to the list
using python has been executed successfully.
To write a python program to calculate the length of the string without using a library
function.
ALGORITHM:
PROGRAM:
#! /usr/bin/python
string=raw_input("Enter string:")
count=0
for i in string:
count=count+1
print("Length of the string is:")
print(count)of each node and
OUTPUT:
RESULT:
Thus the program to calculate the length of the string without using a library function using
python has been executed successfully.
Ex No: 2c
MAPPING OF TWO LISTS INTO DICTIONARY
Date :
AIM:
ALGORITHM:
PROGRAM:
keys=[]
values=[]
n=int(input("Enter number of elements for dictionary:"))
print("For keys:")
for x in range(0,n):
element=int(input("Enter element" + str(x+1) + ":"))
keys.append(element)
print("For values:")
for x in range(0,n):
element=int(input("Enter element" + str(x+1) + ":"))
values.append(element)
d=dict(zip(keys,values))
print("The dictionary is:")
print(d)
OUTPUT:
Enter number of elements for dictionary:3
For keys:
Enter element1:1
Enter element2:2
Enter element3:3
For values:
Enter element1:1
Enter element2:4
Enter element3:9
The dictionary is:{1: 1, 2: 4, 3: 9}
RESULT:
Thus the program to map two lists into a dictionary using python has been executed
successfully.
To write a python program to read a file and capitalize the first letter of every word in the
file.
ALGORITHM:
PROGRAM:
OUTPUT:
Contents of file:
hello world
hello
Enter file name: read.txt
Hello World
Hello
RESULT:
Thus the program to read a file and capitalize the first letter of every word in the file
using python has been executed successfully.
ALGORITHM:
1. import os library
2. Read the option from user (Yes or no)
3. If the option is no exit from the program
4. Else use os.system() function with the code "shutdown /s /t 1" and "shutdown /r /t 1"
to shut down and restart our computer in a second.
PROGRAM:
OUTPUT:
RESULT:
Thus the program to shut down and restart our computer using python has been executed
successfully.
Ex No: 3c
RESOLUTION OF THE IMAGE
Date :
AIM:
To write a python program to find resolution of a jpeg image without using external
libraries.
ALGORITHM:
PROGRAM:
def jpeg_res(filename): #This function prints the resolution of the jpeg image file passed
into it # open image for reading in binary mode
with open(filename,'rb') as img_file:
# height of image (in 2 bytes) is at 164th position
img_file.seek(163)
# read the 2 bytes
a = img_file.read(2)
# calculate height
height = (a[0] << 8) + a[1]
# next 2 bytes is width
a = img_file.read(2)
# calculate width
width = (a[0] << 8) + a[1]
print("The resolution of the image is",width,"x",height)
jpeg_res("img1.jpg")
OUTPUT:
The resolution of the image is 280 x 280
RESULT:
Thus the program to find resolution of a jpeg image without using external libraries using
python has been executed successfully.
ALGORITHM:
1. Import the necessary header files for the implementation of this pygame.
2. Set the display mode for the screen using screen=pygame.display.set_mode
((700,700))
3. Develop the balls with necessary colors.
white=(255,255,255)
blue=(0,0,255)
yellow=(255,255,0)
gray=(200,200,200)
black=(0,0,0)
4. Set the radius for sun, moon and their orbit.
5. Set the time for the pygame orbit clock=pygame.time.Clock()
6. Update the earth position.
7. Again update moon position based on earth position.
8. Update the moon and earth angles.
9. Reset the screen and draw the stars, sun, moon and the earth.
10. Set the clock tick as (60) and exit
PROGRAM:
import pygame
import random
import math
pygame.init()
screen=pygame.display.set_mode((700,700))
white=(255,255,255)
blue=(0,0,255)
yellow=(255,255,0)
gray=(200,200,200)
black=(0,0,0)
sun_radius=50
center=(350,350)
earth_x=50
earth_y=350 earth_orbit=0
moon_orbit=0
clock=pygame.time.Clock()
running=True
stars=[(random.randint(0,699),random.randint(0,699)) for x in range(140)]
while running:
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False
earth_x=math.cos(earth_orbit)*300+350
earth_y=-math.sin(earth_orbit)*300+350
moon_x=math.cos(moon_orbit)*50+earth_x
moon_y=-math.sin(moon_orbit)*50+earth_y
earth_orbit+=0.002
moon_orbit+=0.01
screen.fill(black)
for star in stars:
x,y=star[0],star[1]
pygame.draw.line(screen,white,(x,y),(x,y))
pygame.draw.circle(screen,yellow,center,sun_radius)
pygame.draw.circle(screen,blue,(int(earth_x),int(earth_y)),15)
pygame.draw.circle(screen,gray,(int(moon_x),int(moon_y)),5)
pygame.display.flip()
clock.tick(60)
pygame.quit()
OUTPUT:
RESULT:
Thus the simulation of Elliptical orbits using pygame has been successfully executed.
Ex No: 4b
SIMULATE BOUNCING BALL USING PYGAME
Date :
AIM:
To write a python program to simulate bouncing ball using Pygame
ALGORITHM:
PROGRAM:
import pygame,sys,time
import random frompygame.locals
import * from time
import * pygame.init()
windowSurface=pygame.display.set_mode((500,400),0,32)
pygame.display.set_caption("Bounce")
BLACK=(0,0,0)
WHITE=(255,255,255)
RED=(255,0,0)
GREEN=(0,255,0)
BLUE=(0,0,255)
info=pygame.display.Info()
sw=info.current_w
sh=info.current_h
y=0 direction=1
while True:
windowSurface.fill(BLACK)
pygame.draw.circle(windowSurface,GREEN,(250,y),13,0)
OUTPUT:
RESULT:
Thus the bouncing ball using pygame has been successfully executed.
Ex No: 4c
SIMULATE SIMPLE SNAKE PROGRAM
Date :
AIM:
ALGORITHM:
PROGRAM:
import pygame
# --- Globals ---
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Set the width and height of each snake
segment segment_width = 15
segment_height = 15
# Margin between each segment
segment_margin = 3
# Set initial speed
x_change = segment_width + segment_margin
y_change = 0
class Segment(pygame.sprite.Sprite):
# Class to represent one segment of the snake
def __init (self, x, y):
# Call the parent's constructor
super(). init ()
# Set height, width
self.image = pygame.Surface([segment_width, segment_height])
self.image.fill(WHITE)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.x = x self.rect.y =
# Call this function so the Pygame library can initialize itself
pygame.init()
# Create an 800x600 sized screen
screen = pygame.display.set_mode([800, 600])
# Set the title of the window pygame.display.set_caption('Snake Example')
allspriteslist = pygame.sprite.Group()
# Create an initial snake
snake_segments = []
for i in range(15):
x = 250 - (segment_width + segment_margin) * i
y = 30
segment = Segment(x, y)
snake_segments.append(segment)
allspriteslist.add(segment)
clock = pygame.time.Clock()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Set the speed based on the key pressed
# We want the speed to be enough that we move a full
# segment, plus the margin.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = (segment_width + segment_margin) * -1
y_change = 0
if event.key == pygame.K_RIGHT:
x_change = (segment_width + segment_margin)
y_change = 0
if event.key == pygame.K_UP:
x_change = 0
y_change = (segment_height + segment_margin) * -1
if event.key == pygame.K_DOWN:
x_change = 0
y_change = (segment_height + segment_margin)
# Get rid of last segment of the snake
OUTPUT:
RESULT:
Thus the simulation of simple snake program using pygame has been successfully
executed.