0% found this document useful (0 votes)
73 views7 pages

Lab Ex 1-3

This document contains Python programs and examples for basic exercises including: addition, determining if a number is odd or even, calculating the area of a triangle, determining if a number is positive, negative, or zero, finding the largest of three numbers, calculating the greatest common divisor (GCD) of two and multiple numbers, calculating the square root of a number using Newton's method, and calculating exponentiation. The programs are presented with the aim, algorithm, program code, and example outputs or flowcharts for each exercise.

Uploaded by

Vigneshk
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)
73 views7 pages

Lab Ex 1-3

This document contains Python programs and examples for basic exercises including: addition, determining if a number is odd or even, calculating the area of a triangle, determining if a number is positive, negative, or zero, finding the largest of three numbers, calculating the greatest common divisor (GCD) of two and multiple numbers, calculating the square root of a number using Newton's method, and calculating exponentiation. The programs are presented with the aim, algorithm, program code, and example outputs or flowcharts for each exercise.

Uploaded by

Vigneshk
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/ 7

Exercise 1

1. Basic Python Programs


a) Addition of 2 Nos (Direct and Indirect Method)
b) Odd or Even Number
c) Area of triangle
d) Finding Whether the number is +ve, -ve or equal to zero
e) Largest among 3 nos
f) GCD
Note:
For exercise 1a to e write aim, algorithm, program and result on right side /
output on left side.
For exercise 1f, 1g, 2 and 3 write aim, algorithm, program and result on right
side / flowchart and output on left side.

a) Addition program
Direct input
a=6
b=10
c=a=b
print("The value of c is:", c)
Input from user
a=int(input("Enter the value of a:"))
b= int(input("Enter the value of b:"))
c=a=b
print("The value of c is:", c)
b) Odd or Even number
num=int(input("Enter a number"))
mod=num%2
if mod>0:
print("The number is odd")
else:
print("The number is even")

c) Area of triangle
b=int(input("The base is"))
h=int(input("The height is"))
area=b*h/2
print("The area of triangle is:", area)

d) Finding whether the number is positive, negative or zero


num=float(input("Enter a number")
if num>0:
print("Positive number")
elif num==0:
print("Zero")
else:
print("Negative number")

e) Largest among 3 nos


a=int(input("Enter the first num:"))
b= int(input("Enter the second num"))
c=int(input("Enter the third number"))
if(a>=b) and (a>=c):
largest=a
elif(b>=a) and (b>=c):
largest=b
else:
largest=c
print("The largest of 3 numbers is", largest)

f) GCD for 2 nos:


AIM:
To implement a python program to compute the GCD of two numbers.
ALGORITHM:
Step 1: Start the program
Step 2: Read data into variable i,j
Step 3: Assign highest value to i and smallest to j
Step 4: If j equal to zero then i value is the GCD. Otherwise divide the i until j
equal to zero
Step 5: Print the resulted value as GCD
Step 6: Stop the program
PROGRAM:
n1=int(input('Enter a number:'))
n2=int(input('Enter another number:'))
rem=n1%n2
while rem!=0:
n1=n2
n2=rem
rem=n1%n2
print'GCD of given numbers is:',n2

g) GCD for n numbers


numbers = []
count = int(input ("HOW MANY NUMBERS YOU WANT TO CALCULATE
GCD?\n"))
for i in range(0, count):
number = int(input("ENTER THE NUMBER : \n"))
numbers.append(number)
numbers_sorted = sorted(numbers)
print ("NUMBERS SORTED IN INCREASING ORDER\n",numbers_sorted)
gcd = numbers_sorted[0]
for i in range(1, count):
divisor = gcd
dividend = numbers_sorted[i]
remainder = dividend % divisor
if remainder == 0 :
gcd = divisor
else :
while not remainder == 0 :
dividend_one = divisor
divisor_one = remainder
remainder = dividend_one % divisor_one
gcd = divisor_one
print ("GCD OF " ,count,"NUMBERS IS \n", gcd)
2. Square Root of a Number
AIM:
To implement a python program to compute the square root of a number by
Newton’s method
ALGORITHM:
Step 1: Start the program
Step 2: Define value of n
Step 3: Read sgn and initialie its value as 0
Step 4: If n<0
Step 4.1: sgn = -1
Step 4.2: n = -n
Step 4.3 val = n
Step 5: WHILE True
Step 5.1: last = val
Step 5.2: val = (val+n/val)*0.5
Step 5.3: IF sgn < 0
Step 5.4: RETURN complex(0,val)
Step 6: RETURN val
Step 7: Read upper limit n
Step 8: FOR i = 1 to n
Step 8.1: Call function and PRINT ntsqrt(i)
Step 9: Stop the program
PROGRAM:
def ntsqrt(n):
sgn=0
if n<0:
sgn = -1
n = -n
val = n
while True:
last = val
val =(val+n/val)*0.5
if abs(val-last)<1e-9:
break
if sgn<0:
return complex(0,val)
return val
n = int(input("Enter upper limit:"))
for i in range(1,n):
print('Square root of',i,'is:',ntsqrt(i))

3. Exponentiation
AIM:
To implement a python program to find the exponentiation of a given
number
ALGORITHM:
Step 1: Start the program
Step 2: Enter the x value and n value
Step 3: Set a loop upto n
Step 4: Find the exponent value of x
temp=temp*x/i
sum=sum+temp
Step 5: After execution of the loop print the exponent value of x
Step 6: Stop the program
PROGRAM:
n=int(input('Enter number;'))
e=int(input('Enter exponent:'))
r=n
for i in range(1,e):
r=n*r
print'Exponentiation is:',r

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy