0% found this document useful (0 votes)
2 views19 pages

User Age and Number Analysis Program

The document contains a series of Python code snippets that demonstrate various programming concepts including user input handling, conditional statements, loops, functions, and data structures such as arrays, stacks, and queues. It covers operations like checking if a user is an adult or child based on age, calculating discounts, finding sums and averages, and sorting algorithms. Additionally, it includes examples of defining and using functions for specific tasks like calculating the area of a circle and performing linear searches.

Uploaded by

ghazalfitlife
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)
2 views19 pages

User Age and Number Analysis Program

The document contains a series of Python code snippets that demonstrate various programming concepts including user input handling, conditional statements, loops, functions, and data structures such as arrays, stacks, and queues. It covers operations like checking if a user is an adult or child based on age, calculating discounts, finding sums and averages, and sorting algorithms. Additionally, it includes examples of defining and using functions for specific tasks like calculating the area of a circle and performing linear searches.

Uploaded by

ghazalfitlife
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

#Take the age from user, check and print if user is adult or child

#Declare age as Integer

age=int(input("Enter your age "))

print("Your age is",age)

if age>18:

print("Adult")

else:

print("Child")

#Take the number from user check and print number is even or odd.

num=int(input("Enter a number "))

if num%2==0:

print("Number is even",num)

else:

print("Number is odd",num)

#find the largest number between two numbers.

num1=int(input("Enter the first number "))

num2=int(input("Enter the second number "))

if num1>num2:
print("Number 1 is greater",num1)

else:

print("Number 2 is greater",num2)

#A shop will give discount of 10% if the cost of purchased

#quantity is more than 1000.

#Ask user for quantity

#Suppose, one unit will cost 100.

#Judge and print total cost for user.

quantity=int(input("Enter the number of items ")) #taking input

price=quantity*100

if price>1000:

discount=price*0.10

NewPrice=price-discount

print("Discount is",discount)

print("Your new price with discount is",NewPrice)

else:

print("No discount",price)

#endif
#Write an algorithm using pseudocode. A company decided to give bonus of 5% to employee if

#his/her year of service is more than 5 years.

#Ask user for their salary and year of service and print the net bonus amount.

salary=int(input("Enter your salary "))

service=int(input("Enter your year of service "))

if service>5:

bonus=salary*0.05

NewSalary=salary+bonus

print("Bonus is",bonus)

print("Your new salary with bonus is",NewSalary)

else:

print("No bonus",salary)

#Take the 10 numbers from user and find the sum.

Sum=0

for i in range(10):

num=int(input("Enter a number "))

Sum=Sum+num

#endfor

print("Sum is",Sum)

avg=Sum/10

print("Average is",avg)
#Take the 10 numbers from user and find, which one is

#even and which one is odd.

for i in range(10):

num=int(input("Enter a number "))

if num%2==0:

print("Number is even")

else:

print("Number is odd")

#Take the 10 numbers from user and find,

#Sum of even numbers and product of odd numbers.

Sum=0

Prod=1

for i in range(10):

num=int(input("Enter a number "))

if num%2==0:

Sum=Sum+num

else:

Prod=Prod*num

print("Sum of even numbers is",Sum)

print("Product of odd numbers is",Prod)


#Declare an array

DataArray=[0 for i in range(10)]

print(DataArray)

#Filling an array.(always use loop)

for index in range(10):

DataArray[index]=int(input("Enter a number "))

print(DataArray)

#Declare an array

DataArray=[0 for i in range(10)]

print(DataArray)

Sum=0

#Filling an array.(always use loop)

for index in range(10):

DataArray[index]=int(input("Enter a number "))

Sum=Sum+DataArray[index]

print(DataArray)

print("Sum is",Sum)

Avg=Sum/10

print("Average is",Avg)
#Take the 10 numbers from user fill in an array and count how

#many number are positive

#Declare an array

DataArray=[0 for i in range(10)]

print(DataArray)

count=0

#Filling an array.(always use loop)

for index in range(10):

DataArray[index]=int(input("Enter a number "))

if DataArray[index]>=0:

count=count+1

print(DataArray)

print("Positive numbers occur",count,"times")

#Take the 10 numbers from user fill in an array and print the

#even and odd numbers from an array.

#Declare an array

DataArray=[0 for i in range(10)]

print(DataArray)

count=0

#Filling an array.(always use loop)

for index in range(10):

DataArray[index]=int(input("Enter a number "))


if DataArray[index]%2==0:

print(DataArray[index],"is even")

else:

print(DataArray[index],"is odd")

#Take the 10 numbers from user fill in an array and print the

# sum of even numbers and product of odd numbers from an array.

#Declare an array

DataArray=[0 for i in range(10)]

print(DataArray)

Sum=0

Prod=1

#Filling an array.(always use loop)

for index in range(10):

DataArray[index]=int(input("Enter a number "))

if DataArray[index]%2==0:

Sum=Sum+DataArray[index]

else:

Prod=Prod*DataArray[index]

print(DataArray)

print("Sum of all the even numbers is",Sum)

print("product of all the odd numbers is",Prod)


#Insertion Sort

List=[25,30,45,15,10]

print(List)

for pointer in range(1,5):

item=List[pointer]

current=pointer-1

while List[current]>item and current>-1:

List[current+1]=List[current]

current=current-1

#endwhile

List[current+1]=item

#endfor

print(List)

#Write a function which take the two numbers as parameter

# from user and retrun their Sum.

def Sum(n1,n2):

s=n1+n2

return s

## main ##

n1=int(input("Enter the first number "))

n2=int(input("Enter the second number "))

ans=Sum(n1,n2)

print("Sum is",ans)
#To find the area of a circle

#Function with Parameter

def Area(Radius):

A=3.14 *Radius*Radius

return A

#### main ###

r=int(input("Enter the radius "))

x=Area(r)

print("Area is",x)

#To find the area of a circle

#Function without Parameter

def Area():

Radius=int(input("Enter the radius "))

A=3.14 *Radius*Radius

return A

#### main ###

x=Area()

print("Area is",x)
#To find the area of a circle

#Procedure without Parameter

def Area():

Radius=int(input("Enter the radius "))

A=3.14 *Radius*Radius

print("Area is",A)

#### main ###

Area()

#To find the area of a circle

#Procedure with Parameter

def Area(Radius):

A=3.14 *Radius*Radius

print("Area is",A)

#### main ###

r=int(input("Enter the radius "))

Area(r)
#Write a procdure which take the number from user as parameter

# check and print number is even or odd

def EvenOdd(num):

if num%2==0:

print("Number is even")

else:

print("Number is odd")

## main ##

num=int(input("Enter the number "))

EvenOdd(num)

Past paper question

TheData=[20,3,4,8,12,99,4,26,4]

def InsertionSort(TheData):

for pointer in range(1,len(TheData)):

item=TheData[pointer]

currentItem=pointer-1

while TheData[currentItem]>item and currentItem>-1:

TheData[currentItem+1]=TheData[currentItem]

currentItem=currentItem-1

#endwhile
TheData[currentItem+1]=item

def Output(TheData):

for i in range(len(TheData)):

print(TheData[i])

def Search(num):

flag=False

for i in range(len(TheData)):

if num==TheData[i]:

print("Found")

flag=True

if flag==False:

print("Not found")

return False

else:

return True

## main code ##

print("Data before sorting")

Output(TheData)

InsertionSort(TheData)

print("Data After sorting")

Output(TheData)

num=int(input("Enter the number to search "))

x=Search(num)

print(x)
linear search and bubble sort

arrayData=[10,5,6,7,1,12,13,15,21,8]

def linearSearch(num):

flag=False

for i in range(len(arrayData)):

if num==arrayData[i]:

flag=True

return flag

def BubbleSort(theArray):

maxitem=len(theArray)

n=maxitem-1

for i in range(maxitem):

for j in range(n):

if theArray[j]<theArray[j+1]:

temp=theArray[j]

theArray[j]=theArray[j+1]

theArray[j + 1]=temp

n=n-1

print(theArray)

## Main ###

print(arrayData)

n=int(input("Enter a number to search from an array "))

check=linearSearch(n)

if check==False:

print("Number not found")


else:

print("Number found")

BubbleSort(arrayData)

Stack with -1

Stack=[0 for i in range(10)]

global StackPointer

StackPointer=-1

def Push(num):

global StackPointer

if StackPointer==9:

print("Stack is full")

else:

StackPointer=StackPointer+1

Stack[StackPointer]=num

print("Item Added")

## main code##

print(Stack)

for i in range(2):

n=int(input("Enter the number "))

Push(n)

print(Stack)
stack and push with -1

Stack=[0 for i in range(10)]

global StackPointer

StackPointer=-1

def Push(num):

global StackPointer

if StackPointer==9:

print("Stack is full")

else:

StackPointer=StackPointer+1

Stack[StackPointer]=num

print("Value Added")

def Pop():

global StackPointer

if StackPointer==-1:

print("Stack is empty")

return -1

else:

value=Stack[StackPointer]

Stack[StackPointer]=0

StackPointer=StackPointer-1

return value

## Main code ##

print(Stack)

for i in range(10):
n=int(input("Enter a number "))

Push(n)

print(Stack)

for i in range(3):

print(Pop())

print(Stack)

stack and push with 0

Stack=[0 for i in range(10)]

global StackPointer

StackPointer=0

def Push(num):

global StackPointer

if StackPointer>9:

print("Stack is full")

else:

Stack[StackPointer]=num

StackPointer = StackPointer + 1

print("Value Added")

def Pop():

global StackPointer

if StackPointer==0:

print("Stack is empty")
return -1

else:

StackPointer = StackPointer - 1

value=Stack[StackPointer]

Stack[StackPointer]=0

return value

## Main code ##

print(Stack)

for i in range(11):

n=int(input("Enter a number "))

Push(n)

print(Stack)

for i in range(11):

print(Pop())

print(Stack)

Queue = [0 for i in range(10)]

global Front

global End

Front = 0

End = -1

def Enqueue(num):
global End

if End == 9:

print("Queue is full")

else:

End = End + 1

Queue[End] = num

print("Number added")

def Dequeue():

global Front

global End

if End==-1:

print("Queue is empty")

return -1

else:

value=Queue[Front]

Queue[Front]=0

Front=Front+1

return value

### main Code ##

print(Queue)

for i in range(11):

n = int(input("Enter a number "))

Enqueue(n)

print(Queue)

for i in range(2):
x=Dequeue()

print(x)

print(Queue)

You might also like