Sample codes in Python

Sum of three user input numbers

a=int(input("Enter first Number"))
b=int(input("Enter second Number"))
c=int(input("Enter third Number"))
sum=a+b+c
print(sum)

Enter mark to get status of your division

 m=int(input("Enter your marks"))
if(m>=90):
    print("You have Passed with First Distinction")

else:
    if(m>=60):
        print("You have passed with Second Distinction")
    else:
        if(m>=35):
            print("You have just passed work hard next time")
        else:
            print("Failed")

Factors of an input number

a=int(input("Enter A number"))
for x in range(1,a-1):
 if(a%x==0):
  print(x)

Prime numbers upto an input value

a=int(input("Enter upto what value prime numbers are required: "))
b=0
for i in range(2,a):
    b=0
    for j in range(2,i):
        if(i%j==0):
            b=1
    if(b<=0):
        print(i)

Factorial of an input number ( n! = 1*2*3 ... n )

a=int(input("Enter a number :"))
b=1
for i in range(1,a+1):
    b=b*i
print("The factorial of the number is %d"%b)

Fibonacci series upto a given number

n=10 # maximum value of the series 
i=0  
a=0
b=1
while(i<n):
     next=a+b
     a=b
     b=next
     print(next)
     i=i+1

Multiplication table upto 10

for i in range(1,10):
    print("\n")
    for j in range(1,10):
        c=i*j
        print("{:2d} ".format(c),end='')

Sum of digits of an input number

a=int(input("Enter a Number :"))
sum=0
while(a>0):
    i=a%10
    sum=sum+i
    a=a//10

print("sum of digits in number=%d"%sum)

Check if input number is prime or not

a=int(input("Enter number :"))
for i in range(2,a):
    if(a%i==0):
        print("composite/Not a prime number")
        break
else:
 print("prime number")

Converting Decimal to Binary number

def my_binary(n):
    i=''
    while n>1:
        a = n%2
        n = n//2
        i=str(a)+i
    i=  str(n%2) +i   
    print (i)
    
a=my_binary(156) 

round a number without using round() function

x=float(input("input number "))
y=int(x+0.5)
print(y)
Your Rating




Post your comments , suggestion , error , requirements etc here




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer