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

python 2-3

Python coding for distance

Uploaded by

priyajenat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

python 2-3

Python coding for distance

Uploaded by

priyajenat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Ex. No: 2.

A) EXCHANGE THE VALUES OF TWO VARIABLES USING TEMPORARY


VARIABLE
Aim
To write a program to exchange the values of two variables using third variable
Algorithm
Step1: Start
Step2: Read x and y
Step3: Print x and y value before swap
Step4: Swap values using temporary
variable

temp=x
x=y
y=temp
Step4: Print x and y value after swap
Step5: Stop
Program
x = int(input("Enter x value:"))
y = int(input("Enter y value:"))
print("Value of x and y before
swap:",x,y) temp = x
x=y
y = temp
print("Value of x and y after swap:",x,y)

Result
Thus the Python program to exchange the values of two variables by using a third variable
was executed successfully.
Ex. No: 2.B) EXCHANGE THE VALUES OF TWO VARIABLES WITHOUT USING TEMPORARY
VARIABLE
Aim
To write a program to exchange the values of two variables without using third variable.
Algorithm
Step1: Start
Step 2: Read x and y
Step3: Print x and y value before swap
Step4: Swap values without temporary
variable

x=x+y
y=x-
y
x=x-
y
Step5: Print x and y value after swap
Step6: Stop
Program
x = int(input("Enter x
value:")) y = int(input("Enter
y value:"))
print("Value of x and y before
swap:",x,y) x=x+y
y=x-
y
x=x-
y
print("Value of x and y after swap:",x,y)

Result
Thus the Python program to exchange the values of two variables without using a third variable
was executed successfully.
Ex. No: 2.C) CIRCULATE THE VALUES OF N VARIABLES
Aim
To write a program to circulate the values of N variables.
Algorithm
Step1: Start
Step2: Read upper limit n
Step3: Read n element using loop
Step4: Store elements in list
Step5: POP out each element from list and append to list
Step6: Print list
Step7: Stop
Program
n = int(input("Enter number of values:"))
list1=[]
fori in range(0,n,1):
x=int(input("Enter integer:"))
list1.append(x)
print("Circulating the elements of list:",list1)
fori in range(0,n,1):
x=list1.pop(0)
list1.append(x)
print(list1)

Result
Thus the Python program to circulate the values of N variables was executed successfully.
Ex. No: 2.D) DISTANCE BETWEEN TWO VARIABLES

Aim
To write a program to find distance between two variables.
Algorithm
Step1: Start
Step2: Read four coordinates x1, y1, x2, y2

Step3:. Find distance using


Step4: Print distance
Step5: Stop
Program
x1=int(input("Enter x1 value: "))
y1=int(input("Enter y1 value: "))
x2=int(input("Enter x2 value: "))
y2=int(input("Enter y2 value: "))
distance=((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))**0.5
print("Distance = ",distance)

Result
Thus the Python program to find distance between two variables was executed successfully.
Ex. No: 3.A) NUMBERSERIES

Aim
To write a program to evaluate number series12+22+32+….+N2
Algorithm
Step1: Start
Step2: Read maximum limit n
Step3: Initialize sum=0 and i=1
Step4: Calculate sum of
series . Check while
i<=n, if true calculate
sum=sum+i*i and i=i+1
otherwise goto next
step.
Step5: print sum
Step6: stop
Program
n = int(input('Enter a number: '))
sum=0
i=1
whilei<=n:
sum=sum+i*i
i+=1
print('Sum = ',sum)

Result
Thus the Python program to evaluate number series was executed successfully.
Ex. No: 3.B) NUMBER PATTERN

Aim
To write a program to print number pattern.
Algorithm
Step1: Start
Step2: Read upper limit N
Step3: print pattern using nested for
loop fori in range(1,N+1)
for k in range (N,i-1)
print empty space
for j in range(1,i+1)
print j
for l in range(i-1,0,-1)
print l
Step4: Stop
Program
N = int(input("Enter the number:"))
fori in range(1,N+1):
for k in range(N,i,-1):
print " ", " "
for j in range(1,i+1):
print j, " "
for l in range(i-1,0,-1):
print l,
""

print ()

Result
Thus the Python program toprint number pattern was executed successfully.
Ex. No: 3.C) HALF PYRAMIDPATTERN

Aim
To write a python program to print half pyramid pattern
Algorithm
Step1: Start
Step2: Read upper limit rows
Step3: Print pyramid pattern using nested for
loop fori in range(rows):
for j in range(i+1)
print * in half pyramid
print newline
Step4: Stop
Program
rows = int(input("Enter number of rows: "))
fori in range(rows):
for j in range(i+1):
print "* ",
print("\n")
Output
Enter number of rows: 4
*
* *
* * *
* * * *

Result
Thus the Python program to print half pyramid pattern was executed successfully.
Ex. No: 3.D) FULL PYRAMID PATTERN

Aim
To write a program to print pyramid pattern.
Algorithm
Step1: Start
Step2: Read upper limit rows
Step3: Print pyramid using for and while
loop fori in range(1,rows+1):
print space in range 1,(rows-i)+1
while k!=(2*i-1):
print *
increment k
set k=0
Step4: Process continues upto range
Step5: Stop
Program
rows = int(input("Enter number of rows: "))
k=0
fori in range(1, rows+1):
for space in range(1, (rows-i)+1):
print " ",
while k!=(2*i-1):
print "*",
k += 1
k=0
print
Output
Enter the number of rows: 3
*
* *
* * *
Result
Thus the Python program to print full pyramid pattern was executed successfully.

You might also like