1.To Check if the given number is even or odd
If a number is completely divided by 2 then it is even number. When the number is divided by 2, we use the remainder operator % to calculate the remainder. If the remainder is not zero, the number is odd.
SOURCE CODE:-
n=int(input("enter any number:- "))
if(n%2==0):
print(n,'is even number')
else:
print(n,'is odd number')
OUTPUT
1st Run
enter any number:- 2
2 is even number
2nd Run
enter any number:- 3
3 is odd number
In the above code we use "Simple if" statement. In the above program, we ask the user for input and check if the number is odd or even.
2.Factorial number
Factorial of a number defined as the product of all numbers from 1 to given number.
For example 5!(5 factorial)=5*4*3*2*1 is 120.
Note= factorial of 0 is 1
SOURCE CODE
n=int(input("Enter any number:- "))
fact=1
for i in range(1,n+1):
fact=fact*i
print('factorial of',n,'is',fact)
OUTPUT
1st Run
Enter any number:- 6
factorial of 6 is 720
2nd Run
Enter any number:- 4
factorial of 4 is 24
In the above program we used "for loop" to find factorial of a number.
READ NOW WHAT IS PYTHON
Comments
Post a Comment