Thursday, August 29, 2024

Sum and Product of digits in a number

 # Take input from user

num = int(input("Enter any number : "))

temp = num

product = 1

sum=0

while(temp != 0):

    product = product * (temp % 10)

    sum = sum +  (temp % 10);

    temp = int(temp / 10)


print("\nProduct of all digits in", num, ":", product)

Strong Number in Python

 Strong Number in Python::

sum=0  

num=int(input("Enter a number:"))  

temp=num  

while(num):  

     i=1    

    fact=1  

    rem=num%10  

    while(i<=rem):  

        fact=fact*i  

        i=i+1  

    sum=sum+fact  

    num=num//10  

if(sum==temp):  

    print("Given number is a strong number")  

else:  

    print("Given number is not a strong number")  

Perfect Number by using For_loop

 # Python progam --- Perfect Number by using For_loop

Input_Number = int(input("enter a number"))

Sum = 0

for i in range(1, Input_Number):

    if(Input_Number % i == 0):

        Sum = Sum + i

if (Sum == Input_Number):

    print(&quot;Number is a Perfect Number.&quot;)

else:

    print(&quot;Number is not a Perfect Number.&quot;)

Python Program to Print all Prime Numbers in a range

 Python program to display all the prime numbers within a range

lower = int(input("enter a number")

upper = int(input("enter a number")


print("Prime numbers between", lower, "and", upper, "are:")


for num in range(lower, upper + 1):

   # all prime numbers are greater than 1

   if num > 1:

       for i in range(2, num):

           if (num % i) == 0:

               break

       else:

           print(num)

Python Program to Check Prime Number

 Python Program to Check Prime Number


num = int(input("Enter a number: "))

flag = False

if num == 0 or num == 1:

    print(num, "is not a prime number")

elif num > 1:

    for i in range(2, num):

        if (num % i) == 0:

            flag = True

            break

    if flag:

        print(num, "is not a prime number")

    else:

        print(num, "is a prime number")


Factorial of a Number using Loop

 

Factorial of a Number using Loop


num = int(input("Enter a number: "))

factorial = 1

if num < 0:

   print("factorial does not exist for negative numbers")

elif num == 0:

   print("The factorial of 0 is 1")

else:

   for i in range(1,num + 1):

       factorial = factorial*i

   print("The factorial of",num,"is",factorial)

FOR loop in PYTHON

 1.     Factorial of a Number using Loop  CLICK

2.   Python Program to Check Prime Number  CLICK

3.  Python Program to Print all Prime Numbers in a range click

4. Perfect Number by using For_loop CLICK

Python Program to Check Leap Year

 Python Program to Check Leap Year;:

year = int(input("Enter a year: "))




if (year % 400 == 0) and (year % 100 == 0):

    print( year, "is a leap year")

elif (year % 4 ==0) and (year % 100 != 0):

     print( year, "is a leap year")

else:

   print( year, "is not  a leap year")

Python Program to Find the Largest Among Three Numbers

 Python Program to Find the Largest Among Three Numbers:


num1 = float(input("Enter first number: "))

num2 = float(input("Enter second number: "))

num3 = float(input("Enter third number: "))


if (num1 >= num2) and (num1 >= num3):

   largest = num1

elif (num2 >= num1) and (num2 >= num3):

   largest = num2

else:

   largest = num3


print("The largest number is", largest)

Python Program to Check if a Number is Odd or Even

Python Program to Check if a Number is Odd or Even;


num = int(input("Enter a number: "))

if (num % 2) == 0:

   print("{0} is Even".format(num))

else:

   print("{0} is Odd".format(num))