Soti
Soti
and then convert it back to an integer. This will give you the
2. Write the code to find the Fibonacci series upto the nth term.
This problem asks to generate the Fibonacci sequence up to the nth
term. In this sequence, each number is the sum of the two preceding
ones, starting from 0 and 1.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34
(Writing: Start with 0 and 1 → 0+1=1 → 1+1=2 → 1+2=3 → 2+3=5 →
and so on.)
print()
gcdFunction(num1, num2)
4. Write code of Perfect number
number.
n = int(input(“Enter any number: “))
sump= 0
for i in range(1, n):
if(n % i == 0):
sump= sump + i
if (sump == n):
print(“The number is a Perfect number”)
else:
print(“The number is not a Perfect number”)
This code checks whether two given strings are anagrams of each
other.
Example: Are “listen” and “silent” anagrams?
Sort both:
“listen” → “eilnst”
“silent” → “eilnst”
Both are the same, so “listen” and “silent” are anagrams.
in the string.
Example:
int i, j, temp;
for (i = 0; i < size - 1; i++) {
void main() {
int a[10] = {
11,
9,
6,
19,
33,
64,
15,
75,
67,
88
};
int i;
int size = sizeof(a) / sizeof(a[0]);
display(a, size);
this rule.
Example:
int main() {
int year;
scanf("%d", & year);
if (year % 400 == 0)
printf("%d is a Leap Year", year);
else
printf("%d is not a Leap Year", year);
return 0;
}
the string.
if (max!= i)
{
// performing sorting logic by using temporary variable
temp = arr[i];
arr[i]= arr[max];
arr[max] = temp;
heapify(arr, size, max);
}
}
The rank of an element is its position in the sorted array (with ties
After replacing each element by its rank, the array will be [4, 1, 2,
3] (after sorting, the elements are [10, 20, 30, 40], so ranks are
[1, 2, 3, 4]).
def changeArr(input1):
newArray = input1.copy()
newArray.sort()
for i in range(len(input1)):
for j in range(len(newArray)):
if input1[i]==newArray[j]:
input1[i] = j+1;
break;
# Driver Code
arr = [100, 2, 70, 12 , 90]
changeArr(arr)
# Print the array elements
print(arr)
16. Write a code to find circular rotation of an array by K
positions.
This problem asks to find the circular rotation of an array by K
positions.
the array.
repeat.
# Python 3 program to count unique elements
def count(arr, n):
# Count frequency
count = 1
for j in range(i + 1, n, 1):
if (arr[i] == arr[j]):
visited[j] = True
count += 1
if count == 1 :
print(arr[i]);
# Driver Code
arr = [10, 30, 40, 20, 10, 20, 50, 10]
n = len(arr)
count(arr, n)
palindrome.
Example for array [“racecar”, “level”, “hello”, “madam”,
“world”]:
divisor = 1
while (int(n / divisor) >= 10):
divisor *= 10
while (n != 0):
leading = int(n / divisor)
trailing = n % 10
if (leading != trailing):
return False
return currentMax
# Driver Code
For example:
5! = 5 × 4 × 3 × 2 × 1 = 120
0! = 1 (by definition).
num = 5
output = 1
for i in range(2,num+1):
output*=i
print(output)
For example:
153.
370.
number = 371
num = number
digit, sum = 0, 0
length = len(str(num))
for i in range(length):
digit = int(num%10)
num = num/10
sum += pow(digit,length)
if sum==number:
print("Armstrong")
else:
print("Not Armstrong")
21. Write a program to find the sum of Natural Numbers using
Recursion.
This problem asks to find the sum of the first n natural numbers using
recursion. The sum of the first n natural numbers is given by the
formula 1 + 2 + 3 + … + n.
For example:
Sum of first 5 natural numbers: 1 + 2 + 3 + 4 + 5 = 15.
def getSum(num):
if num == 1:
return 1
return num + getSum(num-1)
num = 5
print(getSum(num))
The sum matrix will have the same dimensions as the input
matrices.
Example for matrices:
o Matrix A:
[1, 2, 3]
[4, 5, 6]
o Matrix B:
[7, 8, 9]
[10, 11, 12]
o Result:
[8, 10, 12]
[14, 16, 18]
C
#include
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
For example:
Sum of the first 5 natural numbers: 1 + 2 + 3 + 4 + 5 = 15.
def recursum(number):
if number == 0:
return number
return number + recursum(number-1)
number, sum = 6,0
print(recursum(number))
For example:
same backward.
# function which return reverse of a string
def isPalindrome(s):
return s == s[::-1]
# Driver code
s = "malayalam"
ans = isPalindrome(s)
if ans:
print("Yes")
else:
print("No")
For example:
For example:
last digit is 5.
last digit is 6.
int main()
{
//enter value
int num;
scanf("%d",&num);
//checking condition
if(checkAutomorphic(num))
printf("Automorphic");
else
printf("Not Automorphic");
return 0;
}
For example:
Example :
for i in range(len(arr)):
if arr[i] < mini:
mini = arr[i]
print (mini)
Example :
o Given an array: [1, 2, 3, 4, 5]
Example :
largest (100).
# List of Integers
numbers = [10, 30, 40, 20]
# Sorting list of Integers
numbers.sort()
print(numbers)
33. Write a code to Sort the element of the array without sort
method
This problem asks to sort the elements of an array without using the
built-in sort() method. The solution requires using the swap method to
manually arrange the array in ascending order.
Example:
Given an array:
[4, 2, 9, 1]
First Pass:
Second Pass:
o Compare 2 and 4. No swap needed: [2, 4, 1, 9]
Third Pass:
[1, 2, 4, 9]
# List of Integers
numbers = [10, 30, 40, 20]
print(numbers)
Example :
o Input String:
o Substitute:
Example :
o Input String:
o Output:
“Thisisateststring”
#take user input
String = "PrepInsta is fabulous"
#print String
print("After removing spaces string is :",String)
Example :
array = [6, 3, 5, 2, 7]
print("There are", inversion(array), "possible inversion")
Example :
Input Array:
[100, 4, 200, 1, 3, 2]
Step 1:
{100, 4, 200, 1, 3, 2}
Step 2:
Step 3:
array = [7, 8, 1, 5, 4, 3]
Example :
Input: 12345
o 5+4+3+2+1
Sum: 15
for i in num:
sum = sum + int(i)
print(sum)
Example :
Result: 32
Example:
Input: 789
Digits: 7, 8, 9
Sum: 7 + 8 + 9 = 24
for i in num:
sum = sum + int(i)
print(sum)
Example:
print(num1, "/", den1, " + ", num2, "/", den2, " = ", num3,
"/", lcm)
Largest Element: 67
Output: Largest element is: 67
a = [10, 89, 9, 56, 4, 80, 8]
max_element = a[0]
for i in range(len(a)):
if a[i] > max_element:
max_element = a[i]
print (max_element)
calculation results.
Example:
Input: a = 1, b = -3, c = 2
if a == 0:
print("Invalid")
return -1
d = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(d))
if d > 0:
print("Roots are real and different ")
print((-b + sqrt_val)/(2 * a))
print((-b - sqrt_val)/(2 * a))
elif d == 0:
print("Roots are real and same")
print(-b / (2*a))
else: # d<0
print("Roots are complex")
print(- b / (2*a), " + i", sqrt_val)
print(- b / (2*a), " - i", sqrt_val)
# Driver Program
a = 1
b = 4
c = 4
# Function call
findRoots(a, b, c)
The program checks each number and divides the given number
Input: 56
Process:
o Divide 56 by 2 → 56 / 2 = 28
o Divide 28 by 2 → 28 / 2 = 14
o Divide 14 by 2 → 14 / 2 = 7
o Prime Factors: 2, 2, 2, 7
n = 210
print(Prime_Factorial(n))
Example:
Input: 123
Process:
o 1 → “One”
o 2 → “Two”
o 3 → “Three”
Example:
Input: 5
Process:
o 5 * factorial(4)
o 4 * factorial(3)
o 3 * factorial(2)
num = 5
print("Factorial of", num, "is", factorial(num))
Example:
Input: [1, 2, 3, 4, 5]
Process:
o Array is reversed.
character.
Example:
Input:
o Pattern: “a*b?e”
o String: “ababe”
Process:
o * matches “ab”
o ? matches “b”
The traversal follows the pattern: left to right, top to bottom, right
Input:
o Matrix:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Process:
o First row: 1, 2, 3
o Last column: 6, 9
Output:
int main()
{
int a[4][4] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
return 0;
}
Example:
Input: 10
Process:
fibonacci(0) = 0
fibonacci(1) = 1
fibonacci(2) = 1 (0 + 1)
fibonacci(3) = 2 (1 + 1)
21, 34]
#Function for nth Fibonacci number
def Fibo(n):
if n<0:
print("Incorrect input")
#1st Fibonacci number is 0
elif n==0:
return 0
#2nd Fibonacci number is 1
elif n==1:
return 1
else:
return Fibo(n-1)+Fibo(n-2)
#Main Program
print(Fibo(9))