Wednesday, 14 December 2016

To convert Fahrenheit into Celsius or vice versa

print("enter choice 'c' to convert  fahrenheit  to Celsius")
print("enter choice 'f' to  convert Celsius  to  fahrenheit ")


#to accept choice of user
ch=input('enter choice(c/f):')

#to accept temperature from user
t=float(input('enter temperature:'))


if ch=='c':
    #to convert  fahrenheit  into Celsius
    cel = (t-32)*5/9
    print(' fahrenheit :',t,'\n','Celsius:',cel)
elif ch=='f':
    #to convert Celsius into  fahrenheit
    far=(t*9/5)+32
    print('Celsius:',t,'\n',' fahrenheit :',far)
else:
    #if choice is other than 'c' or 'f' display invalid choice
    print("invalid choice")


OUTPUT :

Monday, 12 December 2016

Class to find out fibonacci series,table of number,pyramid


#define class
class ftp:

    #define function to find out fibonacci series
    def fib(self,n):
        self.a=0
        self.b=1
        print("fib series=",1,' ',end='')
        for i in range(1,n):
            self.s=self.a+self.b
            print(self.s,' ',end='')
            self.a=self.b
            self.b=self.s


    #define function to find out table of number
    def table(self,n):
        print("\n""table of",n)
        for i in range(1,11):
            print(n*i)

    #define function to display pyramid
    def py(self,n):
        print("Pyramid")
        for i in range(0,n+1):
            self.s=' '*(n-i)
            self.s=self.s+ '*'*(i*2-1)
            print (self.s)
ftp=ftp()  
n=int(input("enter number:"))
ftp.fib(n)
ftp.table(n)
ftp.py(n)


OUTPUT:

Sunday, 11 December 2016

To find the reverse of number

#To find the reverse of number

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

#to store reverse of number
rev=0

#using while loop to find reverse of number
while(n!=0):
    r=n%10
    n=n//10
    rev=rev*10+r
   

#To display reverse of number
print('the reverse of number :',rev)


OUTPUT:





Monday, 5 December 2016

To check the year is leap year or not


# To check the year is leap year or not

#to accept year
y=int(input('enter year'))

#giving condition in while loop
while(y%4==0):
    print( y , 'is leap year')
    break
else:
    print( y , 'is not a leap year')



Sunday, 4 December 2016

Factors/Divisors of number

#Divisors of number/Factors of number
#Divisor/factor of an integer n, is an integer that can be multiplied by some other integer to produce n. 

#accepting number from user
n=int(input('enter no:'))

#each number is divisible by 1 as well as by itself.
print("1 &", n , ' are factor of ' , n)

#to display other divisors/factors
for i in range(2,n):
    if(n%i==0):
        print(i , ' , ' ,end='')
print(' are factors of ', n)

OUTPUT:



Table of number...


#table of number

#accepting number from user
n=int(input("enter no:"))

print("table of",n,":")

for i in range(1,11):
    print(n*i,'',end=',')

OUTPUT:

factorial of number



#factorial of number

#In mathematics, the factorial of a non-negative integer n, denoted by n!.
#It is the product of all positive integers less than or equal to n.

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

#initiating variable
fact=1

#defining conditions
if(n<0):
    print('factorial of no. cannot be calculated')
elif(n==0):
    print("factorial of 0 is 1")
else:
    for i in range(1,n+1):
        fact=fact*i
    print('The factorial of', n , 'is' ,fact)

OUTPUT:

Saturday, 26 November 2016

convert tuple to list

#convert a list of numbers into tuple

#defining list
l=[]

#entering numbers into list
ch='y'
while(ch!='n'):
    n=int(input("enter no:"))
    l.append(n)
    ch=input('do you want to continue:')

#to display list
print('list of entered numbers:',l)

#to display tuple
print('tuple of list:',tuple(l))

OUTPUT:

   

swapping of two numbers


#swapping of two numbers


#taking two numbers from user
a=int(input('enter first number:'))
b=int(input('enter second number:'))

#to display the entered numbers
print("first number:",a)
print('second number:',b)


#to swap the numbers
a,b=b,a

#to display the numbers after swapping
print('after swapping of numbers')
print("first number:",a)
print('second number:',b)


OUTPUT:

Saturday, 12 November 2016

sum of digits of number

n=int(input("enter no:"))
sum=0
while(n!=0):
    r=n%10
    n=n//10
    sum=sum+r
print("sum of digits:",sum)

Thursday, 3 November 2016

Convert a number to BINARY DIGIT

d=1
bin=0
n=int(input("enter no.:"))
while(n!=0):
    r=n%2
    bin=r*d+bin
    d=d*10
    n=n//2
print(bin)

Sunday, 30 October 2016

Example of list comprehension

#Example of list comprehension


str=input("enter string:")

l=[i for i in str if i in ('a','e','i','o','u')]
print("number of vowels present in entered string:",len(l))

Saturday, 29 October 2016

check armstrong number or not...


# Python program to check if the number provided by the user is an Armstrong number or not...


n = int(input("Enter a number: "))
sum = 0
t = n
while t > 0:
   r = t % 10
   sum += r ** 3
   t //= 10
if n == sum:
   print(n,"is an Armstrong number")
else:
   print(n,"is not an Armstrong number")




Wednesday, 12 October 2016

Fibonacci series using while loop

n=int(input("enter no:"))
n0=1
n1=1
count=2
if n<=0:
    print("Please enter positive integer")
elif n==1:
    print("fibonacci series:",n0)
else:
    print("fibonacci series:",n0,n1,'',end='')
    while count<n:
        nth=n0+n1
        print(nth,'',end='')
        n0=n1
        n1=nth
        count+=1

Monday, 10 October 2016

Dictionary for days of week & temperature

#program to accept the day of week & temperature of that day and display them in the form of dictionary


d={} 
ch='y'
for i in range(7):
    day=input('enter day:')
    temp=float(input("enter temperature:"))
    d[day]=temp
print('DAY & TEMPERATURE')
print(d)

Wednesday, 5 October 2016

dictionary for student's roll no. and name

#program to accept roll no & name of students as many as user want & store them into a dictionary

d={}
ch='y'
while ch!='n':
    rno=int(input('enter roll number:'))
    sname=input("enter student's name:")
    ch=input('do you want to continue...?(y/n)')
    d[rno]=sname
print('list of students')
print(d)

Sunday, 2 October 2016

prime numbers from upper range to lower range

a=int(input("enter lower range="))
b=int(input("enter upper range="))

for num in range(a,b+1):
   # prime numbers are greater than 1
   if num > 1:
       for i in range(2,num):
           if (num % i) == 0:
               break
       else:
           print(num)

To check number is palindrome or not


n=int(input('enter no:'))
rev=0;t=n
while(n!=0):
    r=n%10
    n=n//10
    rev=rev*10+r
if(t==rev):
    print('the number is palindrome')
else:
    print('no palindrome')