Saturday, 13 May 2017

Greatest number among 3 numbers which are user entered

#Greatest number among 3 numbers which are user entered

#Accepting 3 numbers from user
a=int(input('enter number:'))
b=int(input('enter number:'))
c=int(input('enter number:'))

# find which is greatest number
if a>b:
    if a>c:
        print(a,'is greatest number')
    else:
        print(c,'is greatest number')
else:
    if b>c:
        print(b,'is greatest number')
    else:
        print(c,'is greatest number')

OUTPUT:

Fibonacci series using for loop

#Fibonacci series using for loop

#Asking user that how much terms he want in the fibonacci series
n=int(input("How much terms you want:"))

#displaying fibonacci terms
a,b=0,1
print("fib series=\n",1)
for i in range(1,n):
    s=a+b
    print(s)
    a,b=b,s

OUTPUT:

Dictionary of Days and their Temperature

#Dictionary of Days and their Temperature

#defining an empty dictionary to add days and their temperature
d={}

#defining a list of name of days
days=['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']

#take entry of temperature from user
for i in days:      
    temp=int(input("enter temperature of "+i+" :"))
    d[i]=temp

#to display the day and its temperature in the form of dictionary
print('DAY & TEMPERATURE')
print(d)

#find maximum temperature
tmax=max(d.values())
print('maximum temperature' , tmax)

OUTPUT:


Monday, 9 January 2017

Linear search example

Linear search example

The linear search is used to find an item in a list.
The items do not have to be in order.
To search for an item, start at the beginning of the list and continue searching until either the end of the list is reached or the item is found.

#simple program to explain linear search by using funcion


#defining the function
def linearsearch(list,s_element):
    for i in range(0,len(list)):
        if list[i]==s_element:
            return i
    return -1

#defining any list with elements
alist=[12,5,3,0,8]

#displaying the list
print(alist)

#accepting element from user to search
se=int(input("enter element to search:"))

#defining variable to store the result of linear search
loc=linearsearch(alist,se)

#displaying the result
if loc==-1:
    print("element is not in a list")
else:
    print("index of element in list:",loc)

OUTPUT :





Sunday, 8 January 2017

basic numeric functions performing on list



#defining list
l=[]

#storing flag in one variable
ch='y'

#giving condition
while ch!='n':
    n=int(input('enter number:'))  #accepting values from user
    ch=input("do u want to continue:") #taking choice from user
    l.append(n)  #appending the values in list

#displaying list
print("list of values:" ,l)
print("length of list:",len(l))
print("sum of numbers from list:",sum(l))
print("minimum number from list:",min(l))
print("maximum number from list:",max(l))
print("average of numbers from list:",sum(l)/len(l))




To make a list of values entered by user

#To make a list of values  entered by user 

#defining list
l=[]

#storing flag in one variable
ch='y'

#giving condition
while ch!='n':
    n=input('enter value:')  #accepting values from user
    ch=input("do u want to continue:") #taking choice from user
    l.append(n)  #appending the values in list

#displaying list
print("list of values:" ,l)

OUTPUT :


Tuesday, 3 January 2017

To check the number is positive or negative


#To check the number entered by user is positive or negative

#to accept number from user
n=int(input("enter number:"))

#giving condition to check the number entered by user is positive , negative or zero
if n<0:
    print(n,"is negative number")#A negative number is a number that is less than zero.
elif n==0:
    print("zero is neither negative or positive") #zero is not positive and is also not negative.
else:
    print(n,"is positive number") #A positive number is a number that is bigger than zero.

OUTPUT :



Sunday, 1 January 2017

To check number is prime or composite

#To check number is prime or composite


#to accept the number from user
n=int(input("enter number:"))

#taking any variable as flag and taking its value "true"
x= True

#giving condition to check number is prime or not
for i in range(2,n):
        if (n%i==0):
            x= False
            break  # ends the for loop
            # no else block because it does nothing ...

#displaying the number is prime or not      
if x:
    print(n,"is prime number")
else:
    print(n,"is composite number")

OUTPUT :





To check the number is odd or even.

#To check the number is odd or even.

#to accept the number from user
n=int(input("enter number:"))

#check the number is odd or even using if-else
if (n%2==0):
    print(n,"is even number")
else:
    print(n,"is odd number")

OUTPUT: