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

Print all odd numbers between 1 to 100

In this program, we will print all odd numbers between 1 to 100.

Steps to solve the program
  1. Use for loop with range function to iterate over each number from 1 to 100.
  2. If a number is not divisible by 2 then only print the number.
				
					
for i in range(1,101):
    if i%2 != 0:
        print(i,end=" ")
				
			

Output :

				
					1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 
49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91
93 95 97 99
				
			

print all even numbers between 1 to 100

find the sum of all natural numbers between 1 to n

Print all even numbers between 1 to 100.

In this program, we will print all even numbers between 1 to 100.

Steps to solve the program
  1. Use for loop with range function to iterate over each number from 1 to 100.
  2. If a number is divisible by 2 then only print the number.
				
					
for i in range(1,101):
    if i%2 == 0:
        print(i,end=" ")
				
			

Output :

				
					2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 
44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 
84 86 88 90 92 94 96 98 100 
				
			

program to print all alphabets from a to z

print all odd numbers between 1 to 100

Print all alphabets from a-z and A-Z

In this program, we will print all alphabets from a-z using a for loop.

Steps to solve the program
  1. Import string.
  2. Using for loop print all lowercase alphabets from a-z using string.ascii_lowercase.
  3. Using for loop print all uppercase alphabets from a-z using string.ascii_uppercase.
Solution 1:
				
					import string

print("Alphabet from a-z:")
for letter in string.ascii_lowercase:
    print(letter, end =" ")

print("\nAlphabet from A-Z:")
for letter in string.ascii_uppercase:
    print(letter, end =" ")
				
			
Output :
				
					Alphabet from a-z:
a b c d e f g h i j k l m n o p q r s t u v w x y z 

Alphabet from A-Z:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
				
			
Solution 2:
				
					# small letter ascii range from 97-122
print("Alphabet from a-z")
for i in range(26):
    # increase small letter ascii value by 1
    # and print character with ascii value
    print(chr(97 + i), end=' ')

# capital letter ascii range from 65-90
print("\nAlphabet from A-Z")
for i in range(26):
    # increase capital letter ascii value by 1
    # and print character with ascii value
    print(chr(65 + i), end=' ')
				
			
Output :
				
					Alphabet from a-z:
a b c d e f g h i j k l m n o p q r s t u v w x y z 

Alphabet from A-Z:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 
				
			

print all natural numbers in reverse

print all even numbers between 1 to 100