Sum of three user input numbers
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")
In place of using multiple
if else , it is better to use
elif.
Read more how to use elif to get the grade from input mark.
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)
We can use
for loop with else to get all prime numbers upto 100 or any input value.
num=100 # change this value to get all prime numbers
for i in range(2,num-1):
for j in range(2,i-1):
if (i%j == 0):
break
else:
print( i," is a prime number")
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)
Solution 2
a=input("Enter a Number :")
sum=0
for i in range(0,len(a)):
sum=sum+int(a[i])
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)