-
% is the modulus operator, which gives the remainder when one number is divided by another.
-
num % 2 == 0 means the number is divisible by 2 → even.
-
If the remainder is not 0 → odd.
-
End
✅ Examples
Enter a number: 7 , 7 is odd.
Enter a number: 12 , 12 is even.
-
Factorial of a number n is calculated as: n! = n × (n-1) × (n-2) × ... × 1
-
Initialize factorial = 1 because multiplying by 1 doesn’t change the value.
-
Use a loop from 1 to num to multiply the values step by step.
📌Special cases:
0! = 1
Factorial is not defined for negative numbers.
✅ Examples
Enter a non-negative integer: 5, The factorial of 5 is 120
Enter a non-negative integer: 0 , The factorial of 0 is 1
A prime number is a number greater than 1 and divisible only by 1 and itself.
We check divisibility from 2 to sqrt(num) because a larger factor would have a corresponding smaller factor.
If any divisor is found → not prime.
If loop completes without breaking → prime.
📌 Notes:
1, 0, and negative numbers are not prime.
✅ Example
Enter a number: 7, 7 is a prime number.
Enter a number: 10 , 10 is not a prime number.
-
Read the string from the user and store it in variable text
-
Initialize an empty string reversed_text
-
Repeat the following steps for each character char (loop variable) in text (from left to right):
-
Set reversed_text = char + reversed_text (This adds the current character to the front of the reversed string)
-
After the loop ends, reversed_text will contain the reversed string
-
Print reversed_text
-
End
✅ Example
Input: "hello" , Output: "olleh"
📌 What happens exactly?
If text = "hello":
1st Iteration: 'h' 'h'
2nd Iteration: 'e' 'eh'
3rd Iteration: 'l' 'leh'
4th Iteration: 'l' 'lleh'
5th Iteration: 'o' 'olleh'
So the first time the loop runs, " " will be the first character of the string.
A string that reads the same forward and backward, e.g.:
level , noon , 121 , A man a plan a canal Panama
-
Read a string from the user and store it in variable text
-
Remove all spaces from text and convert it to lowercase, with the help of functions text.replace(" ", "").lower()
-
Store the result in clean_text
-
Initialize an empty string reversed_text
-
For each character char in clean_text, repeat:
- Set reversed_text = char + reversed_text (This builds the reversed string)
-
Compare clean_text with reversed_text, If both are equal:
- Print "It is a palindrome!"
Else:
- Print "It is not a palindrome."
-
End
✅ Example
Input: level
→ Reversed: level
→ Output: It is a palindrome!
-
Converts the string to lowercase so both A and a are counted the same.
-
Loops through each character and checks if it is a vowel.
-
Increments count whenever a vowel is found.
✅ Example
Input: Hello World
Output: Number of vowels: 3
-
Read a string from the user and store it in variable text
-
Initialize a counter variable count to 0
-
For each character char in text, repeat:
- Increase count by 1
-
After the loop ends, count will store the total number of characters
-
Print the value of count
-
End
✅ Example
Input: hello
Loop runs 5 times → count = 5
Output: Length of the string: 5
-
Read the first string and store it in str1
-
Read the second string and store it in str2
-
Initialize an empty string common to store the common characters
-
For each character char in str1, do:
- If char exists in str2 and char is not already in common:
- Append char to common
- If char exists in str2 and char is not already in common:
-
After the loop ends, common contains all unique common characters
-
Print common
-
End
✅ Example
Input: str1 = "banana" , str2 = "cabana"
Output: ban