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

Print all natural numbers in reverse (n to 1)

In this program, we will print all natural numbers in reverse (from n to 1) using a while loop

Steps to solve the program
  1. Take the last number from which you want to print the natural numbers as input through the user.
  2. Create a variable count and assign its value equal to the input number.
  3. Use a while loop to print the numbers in reverse order. 
  4. While the count variable is not equal to 0 print count and subtracts 1 from it each time.
  5. Print the output.
				
					n = int(input("Enter the last number: "))
count = n

while count != 0:
    print(count,end=" ")
    count -= 1
				
			

Output :

				
					Enter the last number: 10
10 9 8 7 6 5 4 3 2 1 
				
			

print all natural numbers from 1 to n

program to print all alphabets from a to z

Print all natural numbers from 1 to n

In this program, we will print all natural numbers from 1 to n using a while loop.

Steps to solve the program
  1. Take the number up to which you want to print natural numbers from the user.
  2. Create a variable count and assign its value equal to 1.
  3. Use a while loop to print the natural numbers up to the input number.
  4. While the value of the count variable is less than the input number print count and add 1 to the count variable.
  5. Print the output.
				
					n = int(input("Enter the last number: "))
count = 1

while count <= n:
    print(count,end=" ")
    count += 1
				
			

Output :

				
					Enter the last number: 10
1 2 3 4 5 6 7 8 9 10 
				
			

print the alphabet pattern ‘O’

print all natural numbers in reverse