Page No: 1 R.M.K. College of Engineering and Technology 2024-2028-CYS-B S.No: 1 Exp. Name: Factorial of a Number Date: 2026-01- 06 Aim: Write a C++ program to calculate the factorial of a given non-negative integer using recursion. The factorial of a number is defined as: • By definition: • 0! = 1 • 1! = 1 Constraints: • Input Format: • A single integer Output Format: • If , print Exceeds limit • If , print Negative • Otherwise, print the factorial of Source Code: n n ! = n × ( n − 1) × ( n − 2) × ⋯ × 1 0 <= n <= 12 n n > 12 n < 0 n Page No: 2 R.M.K. College of Engineering and Technology 2024-2028-CYS-B factorialNum.cpp #include <iostream> using namespace std; int factorial(int n) { if(n==0 ){ return 1; } if(n<0){ cout<<"Negative"<<endl; return -1; } if(n>12){ cout<<"Exceeds limit"<<endl; return -1; } else{ return n*factorial(n-1); } //return n * factorial(n - 1); } int main() { int n; cin >> n; int result = factorial(n); if (result != -1) cout << result; return 0; } Execution Results - All test cases have succeeded! Test Case - 1 User Output 0 1 Page No: 3 R.M.K. College of Engineering and Technology 2024-2028-CYS-B Test Case - 2 User Output 2 2 Test Case - 3 User Output 3 6 Test Case - 4 User Output 12 479001600 Test Case - 5 User Output -1 Negative Page No: 4 R.M.K. College of Engineering and Technology 2024-2028-CYS-B S.No: 2 Exp. Name: Sum of First n Natural Numbers Date: 2026-01- 06 Aim: Write a C++ program to find the sum of the first natural numbers using recursion. The sum of the first n natural numbers is defined as: • Constraints: • Input Format: • A single integer Output Format: • If , print Negative • If , print Exceeds limit • Otherwise, print the sum of the first natural numbers Source Code: n sum ( n ) = n + sum ( n − 1) 0 ≤ n ≤ 1000 n n < 0 n > 1000 n Page No: 5 R.M.K. College of Engineering and Technology 2024-2028-CYS-B sumOfNums.cpp #include <iostream> using namespace std; int sum(int n) { if(n==1){return 1;} if(n==0){return 0;} if(n<0){ cout<<"Negative"; return -1; } if(n>1000){ cout<<"Exceeds limit"; return -1; } else return n + sum(n - 1); } int main() { int n; cin >> n; int result = sum(n); if (result != -1) cout << result; return 0; } Execution Results - All test cases have succeeded! Test Case - 1 User Output 1001 Exceeds limit Test Case - 2 User Output Page No: 6 R.M.K. College of Engineering and Technology 2024-2028-CYS-B 755 285390 Test Case - 3 User Output 78 3081 Test Case - 4 User Output 102 5253 Test Case - 5 User Output 2 3 Test Case - 6 User Output 0 0 Test Case - 7 User Output -3 Negative Page No: 7 R.M.K. College of Engineering and Technology 2024-2028-CYS-B S.No: 3 Exp. Name: Square Pattern Date: 2026-01- 06 Aim: Write a C++ program to print a square pattern of stars * of size using loops. Constraints: • Input Format: • A single integer Output Format: • If , print Invalid • If , print Exceeds limit • Otherwise, print rows, each containing stars separated by a space Source Code: squarePattern.cpp // Type Content here... #include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; if(n<1){cout<<"Invalid";return 0;} if(n>1000){cout<<"Exceeds limit";return 0;} for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ cout<<"* "; } cout<<"\n"; } return 0; } n × n 1 ≤ n ≤ 1000 n n < 1 n > 1000 n n Page No: 8 R.M.K. College of Engineering and Technology 2024-2028-CYS-B Execution Results - All test cases have succeeded! Test Case - 1 User Output 3 * * * * * * * * * Test Case - 2 User Output 0 Invalid Test Case - 3 User Output 1001 Exceeds limit Test Case - 4 User Output 10 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Page No: 9 R.M.K. College of Engineering and Technology 2024-2028-CYS-B S.No: 4 Exp. Name: Count Number of Pairs Date: 2026-01- 06 Aim: Write a C++ program to count the number of pairs such that and and is even. Constraints: • Input Format: • A single integer Output Format: • If , print Invalid • If , print Exceeds limit • Otherwise, print the count of valid pairs Source Code: ( i , j ) 1 ≤ i ≤ n 1 ≤ j ≤ n i + j 1 ≤ n ≤ 1000 n n < 1 n > 1000 Page No: 10 R.M.K. College of Engineering and Technology 2024-2028-CYS-B pairCount.cpp #include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int count=0; if(n<=0){cout<<"Invalid";return 0;} if(n>1000){cout<<"Exceeds limit";return 0;} else{ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if((i+j)%2==0){count++;} } } } cout<<count; return 0; } Execution Results - All test cases have succeeded! Test Case - 1 User Output 3 5 Test Case - 2 User Output 0 Invalid Test Case - 3 Page No: 11 R.M.K. College of Engineering and Technology 2024-2028-CYS-B User Output 1001 Exceeds limit Test Case - 4 User Output 900 405000 Test Case - 5 User Output 452 102152 Test Case - 6 User Output 21 221 Page No: 12 R.M.K. College of Engineering and Technology 2024-2028-CYS-B S.No: 5 Exp. Name: Count of Number Division Date: 2026-01- 06 Aim: Write a C++ program to count how many times a given positive integer can be divided by 2 until it becomes 0. Constraints: • Input Format: • A single integer Output Format: • If , print Invalid • If , print Exceeds limit • Otherwise, print the count of divisions Source Code: numDiv.cpp #include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int count=0; if(n<=0){cout<<"Invalid";return 0;} if(n>pow(10,9)){cout<<"Exceeds limit";return 0;} else{ while(true){ if(n==0){cout<<count;return 0;} count++; n=n/2; } } cout<<count; return 0; } n 1 ≤ n ≤ 10 9 n n ≤ 0 n > 10 9 Page No: 13 R.M.K. College of Engineering and Technology 2024-2028-CYS-B Execution Results - All test cases have succeeded! Test Case - 1 User Output 8 4 Test Case - 2 User Output 1000000000 30 Test Case - 3 User Output 1000000001 Exceeds limit Test Case - 4 User Output 0 Invalid Test Case - 5 User Output 526235 20 Test Case - 6 User Output 854 10 Page No: 14 R.M.K. College of Engineering and Technology 2024-2028-CYS-B S.No: 6 Exp. Name: Binary Search with Index and Iteration Count Date: 2026-01- 06 Aim: You are given a sorted array of integers and a target value. Implement Binary Search to search for the target element. • If the element is found, print Found and its index (0-based). • If the element is not found, print Not Found. • In both cases, also print the number of iterations made during the search. Input Format: • First line: Integer — number of elements • Second line: space-separated sorted integers • Third line: Integer — element to search Output Format: • If the element is found, then print: Found Index: <X> Iterations: <Y> If not found, print: Not Found Iterations: <Y> Source Code: n n key Page No: 15 R.M.K. College of Engineering and Technology 2024-2028-CYS-B binarySearchComp.cpp #include <iostream> using namespace std; // Binary Search function int binarySearch(int arr[], int n, int key, int &iterations) { int l=0; int r=n-1; while(l<=r){ int mid=(l+r)/2; if(key>arr[mid]){ l=mid+1; } else if(key<arr[mid]){ r=mid-1; } else{ iterations++; return mid; } iterations++; } return -1; } int main() { int n, key; cin >> n; int arr[100]; for (int i = 0; i < n; i++) cin >> arr[i]; cin >> key; int iterations; int index = binarySearch(arr, n, key, iterations); if (index != -1) { cout << "Found\n"; cout << "Index: " << index << "\n"; } else { cout << "Not Found\n"; } Page No: 16 R.M.K. College of Engineering and Technology 2024-2028-CYS-B cout << "Iterations: " << iterations; return 0; } Execution Results - All test cases have succeeded! Test Case - 1 User Output 5 10 20 30 40 50 30 Found Index: 2 Iterations: 1 Test Case - 2 User Output 6 5 15 25 35 45 55 60 Not Found Iterations: 3 Page No: 17 R.M.K. College of Engineering and Technology 2024-2028-CYS-B S.No: 7 Exp. Name: Merge Sort with Comparison Count Date: 2026-02- 22 Aim: You are given an array of integers. Write a C++ program to sort the array using Merge Sort and also print the number of element comparisons performed during sorting. Write two functions: • merge() to combine two sorted subarrays while counting comparisons, and • mergeSort() to recursively divide the array and call merge() Input Format: • First line: Integer • Second line: space-separated integers Output Format: • Print the sorted array in ascending order • Print the total number of comparisons Source Code: n n Page No: 18 R.M.K. College of Engineering and Technology 2024-2028-CYS-B mergeSorComp.cpp #include <iostream> using namespace std; void merge(int arr[], int l, int m, int r, int &comparisons) { // Write the code... int n1 = m - l + 1; int n2 = r - m; int L[50], R[50]; for (int i = 0; i < n1; i++) L[i] = arr[l + i]; for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; int i = 0, j = 0, k = l; while (i < n1 && j < n2) { comparisons++; // count element comparison if (L[i] <= R[j]) { arr[k++] = L[i++]; } else { arr[k++] = R[j++]; } } while (i < n1) { arr[k++] = L[i++]; } while (j < n2) { arr[k++] = R[j++]; } } void mergeSort(int arr[], int l, int r, int &comparisons) { if (l < r) { Page No: 19 R.M.K. College of Engineering and Technology 2024-2028-CYS-B int m = (l + r) / 2; mergeSort(arr, l, m, comparisons); mergeSort(arr, m + 1, r, comparisons); merge(arr, l, m, r, comparisons); } } int main() { int n; cin >> n; int arr[100]; for (int i = 0; i < n; i++) cin >> arr[i]; int comparisons = 0; mergeSort(arr, 0, n - 1, comparisons); for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << "\nComparisons: " << comparisons; return 0; } Execution Results - All test cases have succeeded! Test Case - 1 User Output 5 5 4 3 2 1 1 2 3 4 5 Comparisons: 5 Test Case - 2 User Output 6 Page No: 20 R.M.K. College of Engineering and Technology 2024-2028-CYS-B 10 7 8 9 1 5 1 5 7 8 9 10 Comparisons: 11