You are on page 1of 16

CTSD(22SC1101) LIST OF EXPERIMENTS- LAB SEM IN EXAM-II

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;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
3. Two kids are playing with each other. Game is one must give any integer number and the
other must count the number of digits that number contains. Your task is to develop a
program to count the digits of a number using recursion.
Ans: #include<stdio.h>
int occurrence(int, int);

int main()
{
int n, k;

printf("Enter a positive integer number\n");


scanf("%d", &n);

printf("Which digits occurrence you want to check for?\n");


scanf("%d", &k);

printf("\n%d has appeared %d times in %d.\n", k, occurrence(n, k), n);

return 0;
}

int occurrence(int num, int k)


{
if(num == 0)
return 0;
else if(k == (num%10))
return(1 + occurrence(num/10, k));
else
return(occurrence(num/10, k));
}
4. Write a program in C to count the digits of a given number using recursion. Example:
Input a number : 50 Expected Output : The number of digits in the number is : 2
Ans: #include <stdio.h>
#include<stdio.h>
int countDigits(int num)
{
static int count=0;

if(num>0)
{
count++;
countDigits(num/10);
}
else
{
return count;
}
}
int main()
{
int number;
int count=0;

printf("Enter a positive integer number: ");


scanf("%d",&number);

count=countDigits(number);

printf("Total digits in number %d is: %d\n",number,count);

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;
}

int hcf(int n1, int n2) {


if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
6. Write a C program to print Fibonacci series using recursive function.
Ans: #include<stdio.h>
int fibanocci(int n)
{
if(n=0)
return 0;
if(n=1)
return 1;
return fibanocci(n-1)+fibanocci(n-2);
}
int main()
{
int n;
scanf("%d",&n);
printf("%d",fibanocci(n));
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.

Ans: #include <iostream>

using namespace std;

int main() {

int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int i, N, len, j;

N=3;

len=10;

int temp=0;

for (i = 0; i < N; i++) {

int x = arr[0];

for (j = 0; j < len; j++) {

temp=arr[j];

arr[j] = arr[j + 1];

arr[j+1]=temp;

arr[len - 1] = x;
}

for (i = 0; i < len; i++) {

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)

Ans: #include <stdio.h>

void bubble_sort(int a[], int n) {

int i = 0, j = 0, tmp;

for (i = 0; i < n; i++) { // loop n times - 1 per element

for (j = 0; j < n - i - 1; j++) { // last i elements are sorted already

if (a[j] > a[j + 1]) { // swop if order is broken

tmp = a[j];

a[j] = a[j + 1];

a[j + 1] = tmp;

int main() {

int a[100], n, i, d, swap;

printf("Enter number of elements in the array:\n");

scanf("%d", &n);
printf("Enter %d integers\n", n);

for (i = 0; i < n; i++)

scanf("%d", &a[i]);

bubble_sort(a, n);

printf("Printing the sorted array:\n");

for (i = 0; i < n; i++)

printf("%d\n", a[i]);

return 0;

3. Given an array {9,18,27,36,45,54,63,72,81,90,99}. Trace the steps of binary search


algorithm to find the values 90 from the array. Write the C module to implement binary
search algorithm.

Ans: #include <stdio.h>

int binarySearch(int arr[], int l, int r, int x)

if (r >= l) {

int mid = l + (r - l) / 2;

if (arr[mid] == x)

return mid;

if (arr[mid] > x)

return binarySearch(arr, l, mid - 1, x);

return binarySearch(arr, mid + 1, r, x);

return -1;
}

int main(void)

int arr[] = { 9,18,27,36,45,54,63,72,81,90,99};

int n = sizeof(arr) / sizeof(arr[0]);

int x = 10;

int result = binarySearch(arr, 0, n - 1, x);

(result == -1)

? printf("Element is not present in array")

: printf("Element is present at index %d", result);

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 [].

5. Write a program in C to count a total number of duplicate elements in an array.

Ans: #include <stdio.h>

int main()

int arr[] = {1, 2, 3, 4, 2, 7, 8, 8, 3};

int length = sizeof(arr)/sizeof(arr[0]);

printf("Duplicate elements in given array: \n");

for(int i = 0; i < length; i++) {

for(int j = i + 1; j < length; j++) {

if(arr[i] == arr[j])

printf("%d\n", arr[j]);
}

return 0;

6. Write a program in C to find the second largest element in an array.


Ans: #include <stdio.h>
#include <limits.h>
#define MAX_SIZE 1000
int main()
{
int arr[MAX_SIZE], size, i;
int max1, max2;
printf("Enter size of the array (1-1000): ");
scanf("%d", &size);
printf("Enter elements in the array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
max1 = max2 = INT_MIN;
for(i=0; i<size; i++)
{
if(arr[i] > max1)
{
max2 = max1;
max1 = arr[i];
}
else if(arr[i] > max2 && arr[i] < max1)
{
max2 = arr[i];
}
}
printf("First largest = %d\n", max1);
printf("Second largest = %d", max2);
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.

Ans: public class Solution {

public int lengthOfLastWord(String s) {


int len = 0;
s = s.trim();
for (int i = s.length() - 1; i >=0; i --){
if (s.charAt(i) == ' '){
break;
}
else{
len ++;
}
}
return len;
}
}
5. Develop a C program to arrange the given array of strings in sorted order with
respect to length of the string. If more than one string exists with same length, then
display the string with respect to their alphabetical order. Sample Input: Ravi Radha
Akbar Amar Zee Sample Output: Zee Amar Ravi Akbar Radha
Ans: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int myCompare(const void* a, const void* b)
{
return strcmp(*(const char**)a, *(const char**)b);
}
void sort(const char* arr[], int n)
{
qsort(arr, n, sizeof(const char*), myCompare);
}
int main()
{
const char* arr[]
= { "geeksforgeeks", "geeksquiz", "clanguage" };
int n = sizeof(arr) / sizeof(arr[0]);
int i;
printf("Given array is\n");
for (i = 0; i < n; i++)
printf("%d: %s \n", i, arr[i]);
sort(arr, n);
printf("\nSorted array is\n");
for (i = 0; i < n; i++)
printf("%d: %s \n", i, arr[i]);
return 0;
}
6. A space explorer's ship crashed on Mars! They send a series of SOS messages to
Earth for help. Letters in some of the SOS messages are altered by cosmic radiation during
transmission. Given the signal received by Earth as a string, S, determine how many
letters of the SOS message have been changed by radiation. Example: The original
message was SOSSOS. Two of the message's characters were changed in transit.

Ans: #include <stdio.h>


#include <stdlib.h>
#include <string.h>
int main(){
char* S = (char *)malloc(10240 * sizeof(char));
scanf("%s",S);
int i;
int count=0;
for(i=0;S[i]!='\0';i+=3){
if(S[i]!='S'){
count++;
}
if(S[i+1]!='O'){
count++;
}
if(S[i+2]!='S'){
count++;
}
}
printf("%d",count);
return 0;
}

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.

Ans: #include <stdio.h>


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);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = 2*a[i][j] +3* b[i][j];
}
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}
3. A square matrix is said to be symmetric if transpose of a matrix is equal to the
original matrix. Develop a C program to check whether the given square matrix is
symmetric or not?
Ans: #include <stdio.h>
#define SIZE 3
{
int A[SIZE][SIZE];
int B[SIZE][SIZE];
int row, col, isSymmetric;
printf("Enter elements in matrix of size 3x3: \n");
for(row=0; row<SIZE; row++)
int main()
{
for(col=0; col<SIZE; col++)
{
scanf("%d", &A[row][col]);
}
}
for(row=0; row<SIZE; row++)
{
for(col=0; col<SIZE; col++)
{
B[row][col] = A[col][row];
}
}
isSymmetric = 1;
for(row=0; row<SIZE && isSymmetric; row++)
{
for(col=0; col<SIZE; col++)
{
if(A[row][col] != B[row][col])
{
isSymmetric = 0;
break;
}
}
}
if(isSymmetric == 1)
{
printf("\nThe given matrix is Symmetric matrix: \n");

for(row=0; row<SIZE; row++)


{
for(col=0; col<SIZE; col++)
{
printf("%d ", A[row][col]);
}

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.

You might also like