Q1 Verify Prime Number :
# Complete the function 'verifyPrime'
# @params
# n ->number which is to be checked from primality test
#
# @return
# true if the number is a prime number else false
from math import sqrt
def verifyPrime(n):
# Write your code here
if (n <= 1):
return False
for i in range(2, int(sqrt(n))+1):
if (n % i == 0):
return False
return True
Q2 Decimal to Binary(C++)
void decimalToBinary(int n)
{
//Write your Code here
int binaryNum[32];
// counter for binary array
int i = 0;
while (n > 0) {
// storing remainder in binary array
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--)
cout << binaryNum[j];
}
Q3 Fibonacci sequence
nterms = int(input())
#print the first nterms of the Fibonacci sequence
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
# check if the number of terms is valid
for i in range(nterms):
print(recur_fibo(i))
Q4 Add three numbers - digit by digit
# Complete the function 'addDigitByDigit'
# @params
# a,b,c => numbers which are to be added
# @return
# sum of the three numbers added digit by digit
def addDigitByDigit(a,b,c):
# Write your code here
sum = 0
for digit in str(a,b,c):
sum += int(digit)
return sum
Q5 Pyramid - 6
# Type your code here
row = int(input())
for i in range(1,row+1):
for j in range(i,0,-1):
print(j, end='')
for j in range(2,i+1):
print(j, end='')
print()
Q6 Factorial using recursion
# Complete the function 'factorial' given below
# @params
# n -> an integer whose factorial is to be calculated
# @return
# The factorial of integer n
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
Q7 Sum of all the digits of a number
# Type your code here
def sumDigits(no):
return 0 if no == 0 else int(no % 10) + sumDigits(int(no/10))
t = int(input().strip())
for _ in range(t):
n = int(input())
print(sumDigits(n))
Q8 Reverse a number
# Type your code here
rev = 0
n = int(input().strip())
while(n > 0):
a = n % 10
rev = rev * 10 + a
n = n // 10
print(rev)
Q9 Fibonacci sequence using recursion
# Type your code here
def fibo(n, a, b):
if (n > 0):
fibo(n - 1, b, a + b)
print(a, end="\n")
n = int(input().strip())
fibo(n,0,1)
Q10 Calculate power of a number
# Return the power
def power(num, Pow):
# Write your code here
if (Pow == 0):
return 1
elif (Pow < 0):
return -1
elif (int(Pow % 2) == 0):
return (power(num, int(Pow / 2)) * power(num, int(Pow / 2)))
else:
return (num * power(num, int(Pow / 2)) * power(num, int(Pow / 2)))
Q11 Array of Pointers - 1
Answer Syntax error
Q12 Array of Pointers - 2
Answer p is a pointer to a 5 elements integer array
Q13 Array of Pointers - 3
Answer 2 3
Q14 Arrays: Pointers - 1
Answer Syntax error
Q15 Arrays: Pointers - 2
Answer 1 2
Q16 Pointer to Pointer - 1
Answer assigns 5 to a
Q17 Pointer to Pointer - 2
Answer 8 8 8
Q18 Second Maximum in an Array
#include <limits.h>
#include <stdio.h>
void printlargest(int arr[], int arr_size)
{
int i, first, second;
if (arr_size < 2) {
printf(" Invalid Input ");
return;
}
first = second = INT_MIN;
for (i = 0; i < arr_size; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
}
else if (arr[i] > second && arr[i] != first)
second = arr[i];
}
if (second == INT_MIN)
printf("0");
else
printf("%d",second);
}
int main()
{
int i;
int arr[5];
for(i=0;i<=5;i++){
scanf("%d",&arr[i]);
}
int n = sizeof(arr) / sizeof(arr[0]);
printlargest(arr, n);
return 0;
}
Q20 Partition a Array
void partitionArray(int arr[], int n, int x)
{
int i, j, temp;
i = 0;
j = n-1;
while (i < j )
{
while (arr[i] <=x && i < j )
i++;
while (arr[j] > x && i < j )
j--;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
Q25 String is palindrome or not
# Return True if the string is palindrome, else return False
def isPalindrome(s):
return s == s[::-1]
Q26 Count words
def countWords(string):
# Write your code here
res = len(string.split())
return res
Q29 Strings are anagram or not
def isAnagram(string1,string2):
if len(string1)!=len(string2):
return 0
string1 = sorted(string1)
string2 = sorted(string2)
for i in range(0, len(string1)):
if string1[i] != string2[i]:
return 0
return("Yes")