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

Print the alphabet pattern ‘O’

In this python program, we will print the alphabet pattern ‘O’, with a special character *. pattern printing programs are very helpful to get expertise on loop fundamentals and flow design.

Please refer to the image, we consider a square of 7×7 size, which means 7 rows and 7 colons, Now in this square, the circle marked as green will be part of the ‘0’ pattern.

Steps to solve the program
o pattern2

1).  In this program have considered the matrix of 7*7 and where each row colon’s initial index position is zero as shown in the image.
2). In the code first loop act and the row and nested loop act as colons to print the pattern.
3). First loop value range is 0-6 and the nested loop range is the same 0-6.
4). First If condition makes sure only three * will print in the first and last row.
5). Second if the condition makes sure only two * should print at 1 and 5 indexes of the colon, for remaining 5 rows. 

 

				
					for row in range(0, 7):
        for column in range(0, 7):
            # here in first and last row we want to three *
            if (row == 0 or row == 6) and (1 < column < 5) :
                print("*", end=' ')
            # here from 2 to 6 row, * will print on 1 and 5 index only.
            elif (0 < row <= 5) and (column ==1 or column ==5):
                print("*", end=' ')
            else:
                print(" ", end=' ')
        print()
  
				
			

Output :

				
					---------

   ***
  *   *
  *   *
  *   *
  *   *
  *   *
   ***
   
---------   
				
			

accepts a string and calculates the number of digits and letters

print all natural numbers from 1 to n

Calculate the number of digits and letters

In this program, we will accept a string and calculate the number of digits and letters.

Steps to solve the program
  1. Take a word as input.
  2. Create two variables to calculate the number of digits and letters and assign their values equal to 0.
  3. Use for loop to iterate over each character from the word.
  4. Check whether the character is a letter or a digit using isaplha() and isnumeric().
  5. Add 1 to the corresponding variable according to the type of the charater.
  6. Print the output.
Solution 1
				
					word = "python1234"
digit = 0
character = 0

for ele in word:
    if ele.isalpha():
        character += 1
    elif ele.isnumeric():
        digit += 1
        
print("Digits :",digit)
print("Characters: ",character)
				
			
Output
				
					Digits : 4
Characters:  6
				
			
Solution 2
				
					inputstr = 'pythons12345'
digits = '0123456789'
letters = ''
# create small case and capital case sequence with ASCII
for i in range(1, 27):
    letters = letters + chr(65+i) + chr(97+i)

digit_count = 0
letter_count = 0
# iterate over each character with loop
for word in inputstr:
    # check if word is digit and increase digit count
    if word in digits:
        digit_count += 1
    # check if word is letters and increase letter count
    elif word in letters:
        letter_count += 1


print("Digit Count :", digit_count)
print("Letter Count :", letter_count)

				
			
Output
				
					Digits Count : 5
Letter Count : 7
				
			

converts all uppercases in the word to lowercase

print the alphabet pattern ‘O’

Convert all uppercase to lowercase in a word

In this program, we will take a word as input from the user and convert all uppercase to lowercase in a word.

Steps to solve the program
  1. Take a word as input from the user.
  2. Use for loop to iterate over each character of the word.
  3. Check whether the character is uppercase or not using isupper().
  4. If it is an uppercase character convert it to lowercase using lower().
  5. Print the output.
Solution 1 :
				
					input1 = input("Enter word: ")
result = ''
for char in input1:
    if char.isupper():
        print(char.lower(),end="")
    else:
        print(char,end="")
				
			
Output :
				
					Enter word: SQATOOLS
sqatools
				
			
Solution 2 :
				
					str1 = input("Enter input string:")
result = ""
for char in str1:
    # get character ascii value 
    char_ascii = ord(char)
    # check ascii value between 65 - 90 range
    # then character is in upper case
    if 65 <= char_ascii <= 90:
        # get character exact location
        char_position = char_ascii - 65
        # convert upper char ascii to lower case.
        # add character to the result
        result = result + chr(97+char_position)
    else:
        result = result + char

print("Result :", result)
				
			
Output
				
					Enter Input String: SQATOOLS Learning
Result : sqatools learning
				
			

Fizz-Buzz Program

accepts a string and calculates the number of digits and letters

Print Fizz Buzz for multiples of certain numbers

In this program, we will print Fizz Buzz and FizzBuzz for multiples of certain numbers.

Steps to solve the program
  1. Use for loop with the range function to iterate over all the numbers from 1 to 30.
  2. If a number is multiple of 3 and 5 then print FizzBuzz.
  3. If a number is multiple of 3 print Fizz.
  4. If a number is multiple of 5 print Buzz.
				
					#Printing output
for i in range(1,31):
    if i%3 == 0 and i%5 == 0:
        print("FizzBuzz")
    elif i%3 == 0:
        print("Fizz")
    elif i%5 == 0:
        print("Buzz")
				
			

Output :

				
					Fizz
Buzz
Fizz
Fizz
Buzz
Fizz
FizzBuzz
Fizz
Buzz
Fizz
Fizz
Buzz
Fizz
FizzBuzz
				
			

program to get the Fibonacci series

converts all uppercases in the word to lowercase

Get the Fibonacci series between 0 to 20.

In this program, we will get the Fibonacci series between 0 to 20.

Steps to solve the program
  1. Create two variables and assign their values equal to 0 and 1 respectively.
  2. Create another variable count and assign its value equal to 0.
  3. While the count variable is less than 20 print the num1.
  4. Add num1 and num2 and assign their addition to the n2 variable.
  5. Change the value of num1 with num2 and num2 with n2.
  6. Add 1 to the count variable.
  7. Print the Fibonacci series.
				
					num1 = 0
num2 = 1
count = 0

while count < 20:
    print(num1,end=" ")
    n2 = num1 + num2
    num1 = num2
    num2 = n2
    count += 1  
				
			

Output :

				
					0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 
				
			

prints all the numbers from 0 to 6 except 3 and 6

Fizz-Buzz program