Professional Documents
Culture Documents
ANSWERS
Recursion:
1. Srujan asks his son Arun to calculate the sum of total natural numbers present on the
board. Arun starts summing the natural numbers and after reaching up to 10 he gets
confused and again starts counting from first. Your task is to help Arun to calculate the
sum of n natural numbers recursively.
Ans: #include <stdio.h>
int findSum(int n)
{
int sum = 0;
for (int x = 1; x <= n; x++)
sum = sum + x;
return sum;
}
int main()
{
int n = 5;
printf("%d", findSum(n));
return 0;
}
2. Write a recursive module to find factorial of a given number.
Ans: #include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
int main()
{
int n, k;
return 0;
}
if(num>0)
{
count++;
countDigits(num/10);
}
else
{
return count;
}
}
int main()
{
int number;
int count=0;
count=countDigits(number);
return 0;
}
5. In mathematics, finding the GCD of a number is quite interesting. Hari is a genius in
programming, he wants to find the GCD of a number using his programming skills. Your
task is to help Hari in getting GCD of a number recursively with possible test cases.
Ans: #include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}
1D Arrays:
1. A left rotation operation on an array of size n shifts each of the array's elements 1 unit to
the left. For example, if 2 left rotations are performed on array [1,2,3,4,5], then the array
would become [3,4,5,1,2]. Given an array of n integers and a number, d, perform d left
rotations on the array. Then print the updated array as a single line of spaceseparated
integers.
int main() {
int i, N, len, j;
N=3;
len=10;
int temp=0;
int x = arr[0];
temp=arr[j];
arr[j+1]=temp;
arr[len - 1] = x;
}
cout<< arr[i]<<"\t";
2. Develop a program to accept the age of n students of class, rearrange the data in
ascending order and display the age of youngest and eldest student in class. (Use Bubble
Sort)
int i = 0, j = 0, tmp;
tmp = a[j];
a[j + 1] = tmp;
int main() {
scanf("%d", &n);
printf("Enter %d integers\n", n);
scanf("%d", &a[i]);
bubble_sort(a, n);
printf("%d\n", a[i]);
return 0;
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return -1;
}
int main(void)
int x = 10;
(result == -1)
return 0;
4. Given an array arr[] and size of array is n and one another key x, and give you a segment
size k. The task is to find that the key x present in every segment of size k in arr [].
int main()
if(arr[i] == arr[j])
printf("%d\n", arr[j]);
}
return 0;
Strings:
1. You have been given a String S. You need to find and print whether this string is a
palindrome or not. If yes, print"YES" (without quotes), else print "NO" (without quotes).
Ans: #include<stdio.h>
#include<string.h>
int main()
{
char s[100];
scanf("%s",s);
int i,l,j,flag=0;
l=strlen(s);
for(i=0,j=l-1;i<j;i++,j--)
{
if(s[i]!=s[j])
{
printf("NO\n");
return 0;
}
}
printf("YES\n");
}
2. Alice wrote a sequence of words in CamelCase as a string of letters, s, having the
following properties: It is a concatenation of one or more words consisting of English
letters., All letters in the first word are lowercase., For each of the subsequent words, the
first letter is uppercase and rest of the letters are lowercase. Given s, print the number of
words in s on a new line.Sample Input: saveChangesInTheEditor Sample Output: 5
3.Given a string, S, of length N that is indexed from 0 to N-1, print its even indexed and odd
indexed characters as 2 space-separated strings on a single line (see the Sample below for
more detail). 0 is an even index
Ans: #include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int n; char s[10000];
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",s);
myFunction(s);
}
}
void myFunction(char s[])
{
for(int i=0;i<strlen(s);i++)
{
if (i%2 == 0)
{
printf("%c",s[i]);
}
}
printf(" ");
for(int i=0;i<strlen(s);i++)
{
if (i%2 != 0)
{
printf("%c",s[i]);
}
}
printf("\n");
}
4.Given a string s consisting of words and spaces, return the length of the last word in the
string. A word is a maximal substring consisting of non-space characters only.
2D Arrays:
1. Given two matrices of order M1*N1 and M2*N2. Write function to perform matrix
multiplication and display the resultant matrix if matrix multiplication is possible
otherwise display the message "Invalid Matrices".
2. Develop a program to calculate XA+YB where A and B are the matrices and X=2
and Y=3.
printf("\n");
}
}
else
{
printf("\nThe given matrix is not Symmetric matrix.");
}
return 0;
}
4. The world contains 10 countries each country is suffering with 3 different types of
viruses (corona,Ebola,swine flu)
Write a program to print
a) The total no of persons infected in each country.
b) The total no of infected persons of each virus.
5. Write a program that contains 0’s and 1’s into a 4-by-4 square matrix, prints the
matrix, and finds the rows, columns, and diagonals with all 0s or 1s.
6. An n * n matrix is called a positive Markov matrix, if each element is positive and
the sum of the elements in each column is 1. Write a C program to check whether a matrix
is a Markov matrix or not.