Check whether a number is a palindrome or not

In this program, we will check whether a number is a palindrome or not.

Steps to solve the program
  1. Take a number as input through the user and create a variable rev and assign its value equal to 0.
  2. Use a while loop to find the reverse of a number.
  3. While num is greater than 0 divide the number by 10 using % to find the remainder.
  4. Multiply the rev by 10 and add the remainder to it.
  5. Now divide the number by 10 using //.
  6. If the reversed number is equal to the input number then it is a palindrome.
				
					num=n=int(input("Enter a number: "))
rev = 0

while n>0:
    r = n%10
    rev = (rev*10) + r
    n = n//10
print("Reverse number: ",rev)

if num == rev:
    print("It is a palindrome")
else:
    print("It is not a palindrome")
				
			

Output :

				
					Enter a number: 121
Reverse number:  121
It is a palindrome
				
			

enter a number and print its reverse

find the frequency of each digit in a given integer

Print the reverse of a number.

In this program, we will print the reverse of a number.

Steps to solve the program
  1. Take a number as input through the user and create a variable rev and assign its value equal to 0.
  2. Use a while loop to find the reverse of a number.
  3. While num is greater than 0 divide the number by 10 using to find the remainder.
  4. Multiply the rev by 10 and add the remainder to it.
  5. Now divide number by 10 using //.
  6. Print the final output.
				
					n = int(input("Enter a number: "))
rev = 0

while n>0:
    r = n%10
    rev = (rev*10) + r
    n = n // 10

print("Reverse number: ",rev)
				
			

Output :

				
					Enter a number: 256
Reverse number:  652
				
			

calculate the product of digits of a number

check whether a number is a palindrome or not

Calculate the product of digits of a number

In this program, we will calculate the product of digits of a number.

Steps to solve the program
  1. Take a number as input through the user.
  2. Create a variable product and assign its value equal to 1.
  3. Using a while loop find the sum of digits of a number.
  4. While the number is greater than 0 divide the number by 10 using % and store the remainder in a variable.
  5. Multiply the remainder by the product variable.
  6. Now divide the number by 10 using //.
  7. Print the final output.
				
					num = int(input("Enter a number: "))
product = 1

while num > 0:
    rem = num%10
    product = product * rem
    num = num // 10

print("Product of given number is: ",product)
				
			

Output :

				
					Enter a number: 56
Product of given number is:  30
				
			

calculate the sum of digits of a number

enter a number and print its reverse

Calculate the sum of digits of a number

In this program, we will calculate the sum of digits of a number.

Steps to solve the program
  1. Take a number as input through the user.
  2. Create a variable total and assign its value equal to 0.
  3. Using a while loop find the sum of digits of a number.
  4. While the number is greater than 0 divide the number by 10 using and store the remainder in a variable.
  5. Add the remainder to the total variable.
  6. Now divide the number by 10 using //.
  7. Print the final output.
				
					num = int(input("Enter a number: "))
total = 0

while num > 0:
    rem = num%10
    total = total + rem
    num = num // 10

print("Sum of given number is: ",total)
				
			

Output :

				
					Enter a number: 256
Sum of given number is:  13
				
			

find the sum of the first and last digits of a number

calculate the product of digits of a number

Sum of the first and last digits of a number

In this program, we will find the sum of the first and last digits of a number.

Steps to solve the program
  1. Take a number as input and convert it into a string using str() and store it in another variable.
  2. Create a variable total and assign its value equal to 0.
  3. Use for loop with the range function to find the first and last digit of the number logically.
  4. Add only those number to the total variable.
  5. Print the output
				
					num = 2665
str1 = str(num)
total= 0

for i in range(len(str1)):
    if i == 0:
        total += int(str1[i])
    elif i == len(str1)-1:
        total += int(str1[i])
        
print("Sum of first and last number: ",total)
				
			

Output :

				
					Sum of first and last number:  7
				
			

find the first and last digits of a number

calculate the sum of digits of a number

Find the first and last digits of a number.

In this program, we will find the first and last digits of a number.

Steps to solve the program
  1. Take a number as input and convert it into a string using str().
  2. Use for loop to iterate over indexes of the number.
  3. Using logic print the first and last digits of a number.
				
					num = 2665
str1 = str(num)

for i in range(len(str1)):
    if i == 0:
        print("First number in the gievn number : ",str1[i])
    elif i == len(str1)-1:
        print("Last number in the gievn number : ",str1[i])
				
			

Output :

				
					First number in the gievn number :  2
Last number in the gievn number :  5
				
			

count the number of digits in a number

find the sum of the first and last digits of a number

Count the number of digits in a number

In this program, we will count the number of digits in a number.

Steps to solve the program
  1. Take a number in string format as input.
  2. Create a variable and assign its value equal to 0.
  3. Use for loop to iterate over each digit of the number and during iteration add 1 to the created variable.
  4. Print the final output.
				
					num = '12345'
count = 0

for ele in num:
    count += 1
    
print(f"Total digits in {num}: ",count)
				
			

Output :

				
					Total digits in 12345:  5
				
			

find the sum of all odd numbers between 1 to n

find the first and last digits of a number

Find the sum of odd numbers between 1 to n

In this program, we will find the sum of odd numbers between 1 to n.

Steps to solve the program
  1. Use for loop with range function to iterate over numbers between 1-n.
  2. Create a variable total and assign its value equal to 0.
  3. During the iteration check, if the number is odd and add only those numbers to the total variable.
  4. Print the output.
				
					n= int(input("Enter the last number: "))
total = 0

for i in range(1,n+1):
    if i%2 != 0:
        total += i
    
print(total)
				
			

Output :

				
					25
				
			

find the sum of all even numbers between 1 to n

count the number of digits in a number

Find the sum of even numbers between 1 to n

In this program, we will find the sum of even numbers between 1 to n.

Steps to solve the program
  1. Use for loop with range function to iterate over numbers between 1-n.
  2. Create a variable total and assign its value equal to 0.
  3. During the iteration check, if the number is even and add only those numbers to the total variable.
  4. Print the output.
				
					n= int(input("Enter the last number: "))
total = 0

for i in range(1,n+1):
    if i%2 == 0:
        total += i
    
print(total)
				
			

Output :

				
					30
				
			

find the sum of all natural numbers between 1 to n

find the sum of all odd numbers between 1 to n

Find the sum of natural numbers between 1-n

In this program, we will find the sum of natural numbers between 1 to n.

Steps to solve the program
  1. Take n as the last natural number as input through the user.
  2. Create a variable named total and assign its value equal to 0.
  3. Use for loop with range function to iterate over 1-n natural numbers.
  4. During each iteration add that number to the total.
  5. Print the output.
				
					n= int(input("Enter the last number: "))
total = 0

for i in range(1,n+1):
    total += i
    
print(total)
				
			

Output :

				
					55
				
			

print all odd numbers between 1 to 100

find the sum of all even numbers between 1 to n