Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
85 views

Shivam Python Programs

The document contains Python code snippets for calculating areas of different shapes, finding the largest/smallest number among inputs, checking if a number is prime, printing Fibonacci series, and other numerical/conditional logic programs. It aims to provide a set of examples for solving common mathematical and programming problems using basic Python constructs like loops, conditional statements, functions etc.

Uploaded by

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

Shivam Python Programs

The document contains Python code snippets for calculating areas of different shapes, finding the largest/smallest number among inputs, checking if a number is prime, printing Fibonacci series, and other numerical/conditional logic programs. It aims to provide a set of examples for solving common mathematical and programming problems using basic Python constructs like loops, conditional statements, functions etc.

Uploaded by

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

#find area of square

a=int(input("Enter for side a :"))


b=int(input("Enter for side b:"))
area = a*b
print("area of square=",area)

#find area of circle


r=int(input("Enter for radius of circle:"))
area=3.14*r*r
print("area of circle =",area)

#find area of rectangle


l=int(input("Enter for side of rectangle:"))
b=int(input("Enter for side of rectangle:"))
area=l*b
print("area of rectangle =",area)

#find area of triangle


b=int(input("Enter for base of triangle:"))
h=int(input("Enter for height of triangle:"))
area=0.5*b*h
print("area of triangle =",area)

#find area of sqaure


S=int(input("Enter for side of square:"))
s=int(input("Enter for side of square:"))
area=S*s
print("area of sqaure =",area)

#find area of triangle by heron's formula


a=int(input("Enter first side :"))
b=int(input("Enter second side :"))
c=int(input("Enter third side :"))
s=(a+b+c)/2
Area=(s*(s-a)*(s-b)*(s-c))**0.5
print("semi perimiter of triangle =",s)
print("area of triangle =",Area)

#find squareroot of any number


number=int(input("enter any number :"))
squareroot =number**0.5
print("Required squareroot is =",squareroot)
#find larger number among two number by if else statement
a =int(input("Enter for a : "))
b =int(input("Enter for b : "))
if a > b :
print("larger no = ", a)
else :
print("larger no = ", b)

#WAP to check whether a person is eligible for voting or not ?


age=int(input("Enter for age :"))
if age>=18:
print("You are Eligible for voting",age)
else:
print("You are not Eligible for voting",age)

alternative method

age=int(input("Enter Your Age :"))


if age>=18:
Eligibility="Eligible"
else:
Eligibility="Not Eligible"
print("You are ",Eligibility," for voting,")

#WAP to check whether a year is leap or not ?


Year=int(input("Enter Year:"))
if Year %4 ==0 :
print("this is leap year")
else :
print("this is not a leap year")

#WAP to print month name by month number


month=int(input("Enter for month :"))
if month == 1:
print("January")
elif month == 2:
print("February")
elif month == 3:
print("March")
elif month == 4:
print("April")
elif month == 5:
print("May")
elif month == 6:
print("June")
elif month == 7:
print("July")
elif month == 8:
print("August")
elif month == 9:
print("September")
elif month == 10:
print("October")
elif month == 11:
print("November")
elif month == 12:
print("December")
else :
print("Out of Range")

#Q- wap to find the sum of following series.


s=1+2+3……… n where is n is input

n=int(input("Enter for n:"))


s=0
for i in range(1,n+1):
s=s+i
print("Sum of series=",s)

#Q- write a python programme to find sum of series


s=2+4+6+……… +upto n terms

n=int(input("Enter for n:"))


s=0
for i in range(1,n+1):
s=s+i*2
print("Sum of series",s)

Alternative method

n=int(input("Enter for n:"))


s=0
j=2
for i in range(1,n+1):
s=s+j
j=j+2
print("Sum of series=",s)

#Q write a program to find the sum of the following series


S=1+3+5+ ……. Upto n terms.

n=int(input("Enter for n:"))


s=0
j=1
for i in range(1,n+1):
s=s+j
j=j+2
print("Sum of series",s)

# Q-Write a program to find the sum of the following series.


S=1+x+x^2+x^3+…….. x^n where value of x and n are user input.

n=int(input("Enter for number of terms:"))


x=int(input("Enter for x:"))
s=1
for i in range(1,n+1):
s=s+(x**i)
print("Sum of series=",s)

#For finding smallest number using while loop

s=0
i=1
while True:
a=int(input("Enter number :"))
if a<=0:
break
if i==1:
s=a
i=i+1
if s>a:
s=a
print("Smaller number=",s)
#Q- WAP TO PRINT FIBONANCCI SERIES

n=int(input("Enter for number :"))


a=0
b=1
if n==1 :
print(a)
elif n==2 :
print(a)
print(b)
else :
print(a)
print(b)
for i in range(3,n+1):
c=a+b
print(c)
a=b
b=c

# WAP to check whether entered number is prime or not?

num=int(input("Enter Number:"))
for i in range(2,num):
if num%i==0:
print("Not a Prime Number")
break
else:
print("Prime Number")
Alternative method

n=int(input("Enter for n:"))


flag=0
for i in range(2,n//2+1):
r=n%i
if r==0:
flag=1
break
if flag==0:
print("Prime number")
else:
print(" Not a Prime number")

#Q-Q --WAP to interchange values of two variables –


Enter for a
Enter for b

x=int(input("Enter for x:"))


y=int(input("Enter for y:"))
z=x
x=y
y=z
print("Value of x after interchanging",(x))
print("Value of y after interchanging",(y))

Alternative Method

x=int(input("Enter number x:"))


y=int(input("Enter number y:"))
temp=x
x=y
y=temp
print("Value of x after interchanging",(x))
print("Value of y after interchanging",(y))

#Q- Wap to display all even no. upto 10


Where n is user input
n= int(input("Enter for numbers:"))
for i in range(0,n+1):
print(2*i)

#Q-WAP to print any table where num= user input.

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


print("Multiplication Table of", num)
for i in range(1,11):
print(num,"X",i,"=",num * i)

#Q-WAP to find the sum of series 1,x,x^2+x^3


...... X^n where n and x are user input.
n=int(input("Enter number of terms:"))
x=int(input("Enter for x:"))
s=1
for i in range(1,n+1):
s=s+(x**i)
print("Sum of series=",s)

#Q- WAP to For finding smallest number

s=0
i=1
while True:
a=int(input("Enter number:"))
if a<=0:
break
if i==1:
s=a
i=i+1
if s>a:
s=a
print("smallest number =",s)

#Q WAP TO PRINT FIBONANCCI SERIES

n=int(input("Enter for number :"))


a=0
b=1
if n==1 :
print(a)
elif n==2 :
print(a)
print(b)
else :
print(a)
print(b)
for i in range(3,n+1):
c=a+b
print(c)
a=b
b=c

Q- WAP TO check whether entered number is even or odd.

N=int(input(“Enter number:”))
If N%2==0:
print(“Even number”)
Else:
print(“Odd number”)

#Q-WAP to print any table where num= user input.

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


print("Multiplication Table of", num)
for i in range(1,11):
print(num,"X",i,"=",num * i)

#Q-WAP TO Find sum of positive numbers (using continue statement)

n=int(input("Enter for n:"))


s=0
for i in range(1,n+1):
a=int(input("Enter number:"))
if a<=0:
continue
else:
s=s+a
print("Sum of positive numbers=",s)

#Q- WAP to For finding largest number

l=0
while True:
a=int(input("Enter Number"))
if a<=0:
break
if l<a:
l=a
print("Largest number=",l)

#Q-WAP to input the day number , month number, year number and print the date; for eg. the
format is 25-SEPTEMBER-2020

d=int(input("Enter day number"))


m=int(input("Enter month number"))
y=int(input("Enter Year number"))
if m==1:
mn="January"
elif m==2:
mn="February"
elif m==3:
mn="March"
elif m==4:
n="April"
elif m==5:
mn="May"
elif m==6:
mn="june"
elif m==7:
mn="July"
elif m==8:
mn="August"
elif m==9:
mn="September"
elif m==10:
mn="October"
elif m==11:
mn="November"
elif m==12:
mn="December"
else:
mn='Out of Range'
print(d,"-",mn,"-",y)

#Q-WAP to print month name by month number


m=int(input("Enter month number"))
if m==1:
print("January")
elif m==2:
print("February")
elif m==3:
print("March")
elif m==4:
print("April")
elif m==5:
print("May")
elif m==6:
print("june")
elif m==7:
print("July")
elif m==8:
print("August")
elif m==9:
print("September")
elif m==10:
print("October")
elif m==11:
print("November")
elif m==12:
print("December")
else:
print("Out of Range")

#Q-WAP to print day name by day number

day=int(input("Enter day number :"))


if day==1:
print("Monday")
elif day==2:
print("Tuesday")
elif day==3:
print("Wednesday")
elif day==4:
print("Thursday")
elif day==5:
print("Friday")
elif day==6:
print("Saturday")
elif day==7:
print("Sunday")
else:
print("Out of Range")

#Q-WAP to print direct answers of tables for eg table of 2.

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


for i in range(1,n+1):
print(2*i)

#Q-WAP to print direct answers of tables of two number at a time. for eg table of 2 and 3 at a
time.

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


for i in range(1,n+1):
print(2*i," ",3*i)

#Q-WAP to Print all tables at a time and separated by " " (space).

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


for i in range(1,n+1):
print(2*i," ",3*i," ",4*i," ",5*i," ",6*i," ",7*i," ",8*i," ",9*i," ",10*i)

#Q-WAP to Print the sum of digits of given numbers

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


i=0
while n>0:
r=n%10
i=i+r
n=n//10
print("Sum of digits of given number",i)
#Q-Write a program to reverse the order of given number.

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


result=0
while n>0:
digit=n%10
result=result*10+digit
n=n//10
print("Reverse order of the given number=",result)

#Q-Write a python program to find largest number among elements.

n=int(input("Enter number of elements:"))


for i in range(1,n+1):
a=int(input("Enter number"))
if i == 1:
s=a
else:
if s > a:
s=a
print("Smallest number=",s)

#Q-Write a python program to find Smallest number among elements.

n=int(input("Enter number of elements:"))


for i in range(1,n+1):
a=int(input("Enter number"))
if i == 1:
l=a
else:
if l > a:
l=a
print("Largest number=",l)

#Q-WAP to print right pyramid like *


(star)
n=int(input("Enter for n:"))
for i in range(1,n+1):
for j in range(i+1):
print("*",end=" ")
print()
for i in range(n,0,-1):
for j in range(0,i-1):
print("*",end=" ")
print()

#Q-WAP to print right to left decreasing star *

n=int(input("Enter for n:"))


for i in range(n,0,-1):
for j in range(i,0,-1):
print("*",end=" ")
print()

#Q-WAP to print squARE star*

n=int(input("Enter for n:"))


for i in range(1,n):
for j in range(1,n+1):
print("*",end=" ")
print()

#Q-WAP to print left to right increasing star *

n=int(input("Enter for n:"))


for i in range(1,n+1):
for j in range(1,i+1):
print("*",end=" ")
print()

You might also like