100 Programs - CSU1287 - CSE 2026 - Shoolini University

100 Programs in C++

1. WAP in CPP to find the missing number in an array of 1-100.

/*
* -----------------------------------------------------------
* OOPS with C++ (CSU1287)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code and all the codes henceforth is a part of the educational
* initiative by dmj.one with aim of empowering and inspiring learners
* in the field of Computer Science and Engineering through the
* respective courses.
*
* (c) 2023, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <iostream>
using namespace std;

int findMissingNumber(int arr[], int n) {
    int total = ((n+1)*(n+2))/2;  
    for(int i=0; i < n; i++)
        total -= arr[i];
    return total;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    int missing = findMissingNumber(arr, n);
    cout << "Missing number is: " <<  missing <<  endl;
    return 0;
}
    

Output:

  Enter the number of elements: 99
Enter the elements of the array: 1 2 3 ... 99 100
Missing number is: 51

2. WAP in CPP to find the duplicate numbers in an array of 1-100.


#include <iostream>
using namespace std;

void findDuplicates(int arr[], int n) {
    bool dupFlag = false;
    cout << "Duplicate numbers are: ";
    for(int i=0; i < n; i++) {
        for(int j=i+1; j < n; j++) {
            if(arr[i] == arr[j]) {
                cout <<  arr[i] << " ";
                dupFlag = true;
            }
        }
    }
    if(!dupFlag) {
        cout << "No duplicates found.";
    }
    cout <<  endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    findDuplicates(arr, n);
    return 0;
}
    

Output:

  Enter the number of elements: 101
Enter the elements of the array: 1 2 3 ... 99 100 100
Duplicate numbers are: 100

3. WAP in CPP to find the first duplicate number in a given array.


#include <iostream>
using namespace std;

void findFirstDuplicate(int arr[], int n) {
    bool dupFlag = false;
    for(int i=0; i < n; i++) {
        for(int j=i+1; j < n; j++) {
            if(arr[i] == arr[j]) {
                cout << "First duplicate number is: " <<  arr[i] <<  endl;
                dupFlag = true;
                break;
            }
        }
        if(dupFlag) break;
   

 }
    if(!dupFlag) {
        cout << "No duplicate number found." <<  endl;
    }
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    findFirstDuplicate(arr, n);
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 3 2 4
First duplicate number is: 2

4. WAP in CPP to remove duplicate elements from an array.


#include <iostream>
using namespace std;

void removeDuplicates(int arr[], int &n) {
    if (n==0 || n==1)
        return;
 
    int temp[n];
 
    int j = 0;
    for (int i=0; i < n-1; i++)
        if (arr[i] != arr[i+1])
            temp[j++] = arr[i];
    temp[j++] = arr[n-1];
 
    for (int i=0; i < j; i++)
        arr[i] = temp[i];
 
    n = j;
}

void printArray(int arr[], int n) {
    for (int i=0; i < n; i++)
        cout  <<  arr[i]  <<  " ";
    cout  <<  endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin >>arr[i];
    removeDuplicates(arr, n);
    printArray(arr, n);
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 2 3 3
1 2 3

5. WAP in CPP to find the number that is not present in the second array, given two arrays.


#include <iostream>
using namespace std;

void findMissingInSecondArray(int arr1[], int arr2[], int n, int m) {
    bool foundFlag = false;
    cout << "Numbers not present in the second array are: ";
    for(int i=0; i < n; i++) {
        foundFlag = false;
        for(int j=0; j < m; j++) {
            if(arr1[i] == arr2[j]) {
                foundFlag = true;
                break;
            }
        }
        if(!foundFlag) {
            cout <<  arr1[i] << " ";
        }
    }
    cout <<  endl;
}

int main() {
    int n, m;
    cout << "Enter the number of elements in the first array: ";
    cin>>n;
    int

 arr1[n];
    cout << "Enter the elements of the first array: ";
    for(int i=0;  i <  n; i++)
        cin>>arr1[i];
    
    cout << "Enter the number of elements in the second array: ";
    cin>>m;
    int arr2[m];
    cout << "Enter the elements of the second array: ";
    for(int i=0;  i <  m; i++)
        cin>>arr2[i];

    findMissingInSecondArray(arr1, arr2, n, m);
    return 0;
}
    

Output:

  Enter the number of elements in the first array: 5
Enter the elements of the first array: 1 2 3 4 5
Enter the number of elements in the second array: 4
Enter the elements of the second array: 1 2 3 4
Numbers not present in the second array are: 5

6. WAP in CPP to compare two arrays for equality in size.


#include <iostream>
using namespace std;

void compareSize(int n, int m) {
    if(n == m)
        cout << "The sizes of both arrays are equal." <<  endl;
    else
        cout << "The sizes of both arrays are not equal." <<  endl;
}

int main() {
    int n, m;
    cout << "Enter the size of the first array: ";
    cin>>n;
    int arr1[n];
    cout << "Enter the size of the second array: ";
    cin>>m;
    int arr2[m];
    compareSize(n, m);
    return 0;
}
    

Output:

  Enter the size of the first array: 5
Enter the size of the second array: 5
The sizes of both arrays are equal.

7. WAP in CPP to find the largest and smallest numbers in an array.


#include <iostream>
using namespace std;

void findLargestSmallest(int arr[], int n) {
    int min = arr[0], max = arr[0];
    for(int i=1;  i <  n; i++) {
        if(arr[i] < min) min = arr[i];
        if(arr[i] > max) max = arr[i];
    }
    cout << "Smallest number: " << min <<  endl;
    cout << "Largest number: " << max <<  endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    findLargestSmallest(arr, n);
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 5 3 7 2 8
Smallest number: 2
Largest number: 8

8. WAP in CPP to find the second highest number in an integer array.


#include <iostream>
#include <limits.h>
using namespace std;

void findSecondHighest(int arr[], int n) {
    int first = INT_MIN, second = INT_MIN;
    for (int i = 0; i < n; 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)
        cout << "There is no second largest element\n";
    else
        cout << "The second largest element is " <<  second <<  endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    findSecondHighest(arr, n);
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 5 3 7 2 8
The second largest element is 7

9. WAP in CPP to find the top two maximum numbers in an array.


#include <iostream>
#include <limits.h>
using namespace std;

void findTopTwoMax(int arr[], int n) {
    int first = INT_MIN, second = INT_MIN;
    for (int i = 0; i < n; i++) {
        if (arr[i] > first) {
            second = first;
            first = arr[i];
        } else if (arr[i] > second && arr[i] != first) {
            second = arr[i];
        }
    }
    cout << "The two maximum numbers are " <<  first  << " and " <<  second  <<  endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    findTopTwoMax(arr, n);
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 5 3 7 2 8
The two maximum numbers are 8 and 7

10. WAP in CPP to print an array in reverse order.


#include <iostream>
using namespace std;

void printReverse(int arr[], int n) {
    for(int i=n-1; i>=0; i--)
        cout <<  arr[i] << " ";
    cout <<  endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    printReverse(arr, n);
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
5 4 3 2 1

11. WAP in CPP to reverse an array in two ways.


#include <iostream>
using namespace std;

void reverseArraySwap(int arr[], int n) {
    int start = 0, end = n-1, temp;
    while(start < end) {
        temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

void printArray(int arr[], int n) {
    for(int i=0; i < n; i++)
        cout <<  arr[i] << " ";
    cout <<  endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    cout << "Original array: ";
    printArray(arr, n);
    reverseArraySwap(arr, n);
    cout << "Reversed array (using swap): ";
    printArray(arr, n);
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
Original array: 1 2 3 4 5
Reversed array (using swap): 5 4 3 2 1

12. WAP in CPP to calculate the length of an array.


#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "The length of the array is: " <<  n  <<  endl;
    return 0;
}
    

Output:

  Enter the number of elements: 5
The length of the array is: 5

13. WAP in CPP to insert an element at the end of an array.


#include <iostream>
using namespace std;

int main() {
    int n, x;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n+1]; // Creating an array with size n+1 to accommodate the new element
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    cout << "Enter the element to be inserted: ";
    cin>>x;
    arr[n] = x; // Adding the new element at the end
    cout << "Array after insertion: ";
    for(int i=0;  i < =n; i++)
        cout <<  arr[i] << " ";
    cout <<  endl;
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
Enter the element to be inserted: 6
Array after insertion: 1 2 3 4 5 6

14. WAP in CPP to insert an element at a given location in an array.


#include <iostream>
using namespace std;

int main() {
    int n, x, pos;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n+1]; // Creating an array with size n+1 to accommodate the new element
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    cout << "Enter the element to be inserted: ";
    cin>>x;
    cout << "Enter the position where the element is to be inserted (0-indexed): ";
    cin>>pos;
    for(int i=n; i>=pos; i--) // Shifting elements from the position to the end by one place to the right
        arr[i+1] = arr[i];
    arr[pos] = x; // Inserting the new element at the position
    cout << "Array after insertion: ";
    for(int i=0;  i < =n; i++)
        cout <<  arr[i] << " ";
    cout <<  endl;
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
Enter the element to be inserted: 6
Enter the position where the element is to be inserted (0-indexed): 2
Array after insertion: 1 2 6 3 4 5

15. WAP in CPP to delete the element at the end of an array.


#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    n--; // Decreasing the size of the array by 1 to remove the last element
    cout << "Array after deletion: ";
    for(int i=0; i < n; i++)
        cout <<  arr[i] << " ";
    cout <<  endl;
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
Array after deletion: 1 2 3 4

16. WAP in CPP to delete a given element from an array.


#include <iostream>
using namespace std;

void deleteElement(int arr[], int &n, int key) {
    int i;
    for (i=0; i < n; i++)
        if (arr[i] == key)
            break;
    if (i < n) {
        n = n - 1;
        for (int j=i; j < n; j++)
            arr[j] = arr[j+1];
    }
}

void printArray(int arr[], int n) {
    for (int i=0; i < n; i++)
        cout  <<  arr[i]  <<  " ";
    cout  <<  endl;
}

int main() {
    int n;
    int key;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    cout << "Enter the element to be deleted: ";
    cin>>key;
    deleteElement(arr, n, key);
    printArray(arr, n);
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
Enter the element to be deleted: 3
1 2 4 5

17. WAP in CPP to delete an element from an array at a given index.


#include <iostream>
using namespace std;

void deleteAtIndex(int arr[], int &n, int index) {
    if (index>=0 && index < n) {
        for (int i=index; i < n-1; i++)
            arr[i] = arr[i+1];
        n--;
    }
}

void printArray(int arr[], int n) {
    for (int i=0; i < n; i++)
        cout  <<  arr[i]  <<  " ";
    cout  <<  endl;
}

int main() {
    int n;
    int index;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    cout << "Enter the index of the element to be deleted: ";
    cin>>index;
    deleteAtIndex(arr, n, index);
    printArray(arr, n);
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
Enter the index of the element to be deleted: 2
1 2 4 5

18. WAP in CPP to find the sum of array elements.


#include <iostream>
using namespace std;

int findSum(int arr[], int n) {
    int sum = 0;
    for (int i=0; i < n; i++)
        sum += arr[i];
    return sum;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    cout << "The sum of the elements is: "  <<  findSum(arr, n)  <<  endl;
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
The sum of the elements is: 15

19. WAP in CPP to print all even numbers in an array.


#include <iostream>
using namespace std;

void printEvenNumbers(int arr[], int n) {
    cout << "The even numbers are: ";
    for (int i=0; i < n; i++)
        if (arr[i] % 2 == 0)
            cout  <<  arr[i]  <<  " ";
    cout <<  endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    printEvenNumbers(arr, n);
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
The even numbers are: 2 4

20. WAP in CPP to print all odd numbers in an array.


#include <iostream>
using namespace std;

void printOddNumbers(int arr[], int n) {
    cout << "The odd numbers are: ";
    for (int i=0; i < n; i++)
        if (arr[i] % 2 != 0)
            cout  <<  arr[i]  <<  " ";
    cout <<  endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    printOddNumbers(arr, n);
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
The odd numbers are: 1 3 5

21. WAP in CPP to perform a left rotation of array elements by two positions.


#include <iostream>
using namespace std;

void rotateLeft(int arr[], int n, int d) {
    int temp[d];
    for(int i=0; i < d; i++)
        temp[i] = arr[i];
    for(int i=d; i < n; i++)
        arr[i-d] = arr[i];
    for(int i=0; i < d; i++)
        arr[n-d+i] = temp[i];
}

void printArray(int arr[], int n) {
    for(int i=0; i < n; i++)
        cout <<  arr[i] << " ";
    cout <<  endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    rotateLeft(arr, n, 2);
    cout << "Array after left rotation by two positions: ";
    printArray(arr, n);
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
Array after left rotation by two positions: 3 4 5 1 2

22. WAP in CPP to perform a right rotation of array elements by two positions.


#include <iostream>
using namespace std;

void rotateRight(int arr[], int n, int d) {
    int temp[d];
    for(int i=0; i < d; i++)
        temp[i] = arr[n-d+i];
    for(int i=n-d-1; i>=0; i--)
        arr[i+d] = arr[i];
    for(int i=0; i < d; i++)
        arr[i] = temp[i];
}

void printArray(int arr[], int n) {
    for(int i=0; i < n; i++)
        cout <<  arr[i] << " ";
    cout <<  endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    rotateRight(arr, n, 2);
    cout << "Array after right rotation by two positions: ";
    printArray(arr, n);
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
Array after right rotation by two positions: 4 5 1 2 3

23. WAP in CPP to merge two arrays.


#include <iostream>
using namespace std;

void mergeArrays(int arr1[], int arr2[], int n1, int n2) {
    int arr3[n1+n2];
    for(int i=0; i < n1; i++)
        arr3[i] = arr1[i];
    for(int i=0; i < n2; i++)
        arr3[n1+i] = arr2[i];
    cout << "Merged array is: ";
    for(int i=0; i < n1+n2; i++)
        cout <<  arr3[i] << " ";
    cout <<  endl;
}

int main() {
    int n1, n2;
    cout << "Enter the number of elements in first array: ";
    cin>>n1;
    int arr1[n1];
    cout << "Enter the elements of the first array: ";
    for(int i=0; i < n1; i++)
        cin>>arr1[i];
    cout << "Enter the number of elements in second array: ";
    cin>>n2;
    int arr2[n2];
    cout << "Enter the elements of the second array: ";
    for(int i=0; i < n2; i++)
        cin>>arr2[i];
    mergeArrays(arr1, arr2, n1, n2);
    return 0;
}
    

Output:

  Enter the number of elements in first array: 3
Enter the elements of the first array: 1 2 3
Enter the number of elements in second array: 2
Enter the elements of the second array: 4 5
Merged array is: 1 2 3 4 5

24. WAP in CPP to find the highest frequency element in an array.


#include <iostream>
#include <map>
using namespace std;

int findMaxFreq(int arr[], int n) {
    map<int, int> freq;
    for(int i=0; i < n; i++)
        freq[arr[i]]++;
    int max_count = 0, res = -1;
    for(auto i: freq) {
        if (max_count < i.second) {
            res = i.first;
            max_count = i.second;
        }
    }
    return res;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin>>n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin>>arr[i];
    cout << "The element with the highest frequency is: " <<  findMaxFreq(arr, n)  <<  endl;
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 1 2 2 3 3
The element with the highest frequency is: 2

25. WAP in CPP to add two numbers using recursion.


#include <iostream>
using namespace std;

int addNumbers(int n1, int n2) {
    if(n2 == 0)
        return n1;
    else
        return addNumbers( n1 ^ n2, (n1 & n2)  <<  1 );
}

int main() {
    int n1, n2;
    cout << "Enter the first number: ";
    cin>>n1;
    cout << "Enter the second number: ";
    cin>>n2;
    cout << "The sum of the numbers is: " <<  addNumbers(n1, n2)  <<  endl;
    return 0;
}
    

Output:

  Enter the first number: 5
Enter the second number: 7
The sum of the numbers is: 12

26. WAP in CPP to find the sum of the digits of a number using recursion.


#include <iostream>
using namespace std;

int sumOfDigits(int n) {
    if(n==0)
        return 0;
    else
        return (n%10 + sumOfDigits(n/10));
}

int main() {
    int n;
    cout << "Enter the number: ";
    cin>>n;
    cout << "Sum of the digits is: " << sumOfDigits(n) << endl;
    return 0;
}
    

Output:

  Enter the number: 123
Sum of the digits is: 6

27. Write a method in CPP to remove a given character from a string.


#include <iostream>
#include <string>
using namespace std;

string removeCharacter(string str, char ch) {
    string result;
    for(int i=0;  i < str.length(); i++)
        if(str[i] != ch)
            result += str[i];
    return result;
}

int main() {
    string str;
    char ch;
    cout << "Enter the string: ";
    getline(cin, str);
    cout << "Enter the character to be removed: ";
    cin>>ch;
    cout << "String after removal: " << removeCharacter(str, ch) << endl;
    return 0;
}
    

Output:

  Enter the string: Hello World
Enter the character to be removed: l
String after removal: Heo Word

28. WAP in CPP to count the occurrence of a given character in a string.


#include <iostream>
#include <string>
using namespace std;

int countCharacter(string str, char ch) {
    int count = 0;
    for(int i=0;  i < str.length(); i++)
        if(str[i] == ch)
            count++;
    return count;
}

int main() {
    string str;
    char ch;
    cout << "Enter the string: ";
    getline(cin, str);
    cout << "Enter the character to be counted: ";
    cin>>ch;
    cout << "The character " <<  ch  << " occurs " <<  countCharacter(str, ch)  << " times in the string." << endl;
    return 0;
}
    

Output:

  Enter the string: Hello World
Enter the character to be counted: l
The character l occurs 3 times in the string.

29. WAP in CPP to check if two strings are anagrams.


#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool checkAnagram(string str1, string str2) {
    if(str1.length() != str2.length())
        return false;
    sort(str1.begin(), str1.end());
    sort(str2.begin(), str2.end());
    return str1 == str2;
}

int main() {
    string str1, str2;
    cout << "Enter the first string: ";
    getline(cin, str1);
    cout << "Enter the second string: ";
    getline(cin, str2);
    if(checkAnagram(str1, str2))
        cout << "The strings are anagrams." << endl;
    else
        cout << "The strings are not anagrams." << endl;
    return 0;
}
    

Output:

  Enter the first string: silent
Enter the second string: listen
The strings are anagrams.

30. WAP in CPP to check if a string is a palindrome.


#include <iostream>
#include <string>
using namespace std;

bool checkPalindrome(string str) {
    int start = 0, end = str.length()-1;
    while(start < end) {
        if(str[start] != str[end])
            return false;
        start++;
        end--;
    }
    return true;
}

int main() {
    string str;
    cout << "Enter the string: ";
    getline(cin, str);
    if(checkPalindrome(str))
        cout << "The string is a palindrome." << endl;
    else
        cout << "The string is not a palindrome." << endl;
    return 0;
}
    

Output:

  Enter the string: madam
The string is a palindrome.

31. WAP in CPP to check if a given character is a vowel or consonant.


#include <iostream>
using namespace std;

bool isVowel(char ch) {
    ch = tolower(ch);
    return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}

int main() {
    char ch;
    cout << "Enter a character: ";
    cin>>ch;
    if(isVowel(ch))
        cout << "The character is a vowel." << endl;
    else
        cout << "The character is a consonant." << endl;
    return 0;
}
    

Output:

  Enter a character: a
The character is a vowel.

32. WAP in CPP to check if a given character is a digit or not.

< article class="syntax">

#include <iostream>
using namespace std;

bool isDigit(char ch) {
    return (ch >= '0' && ch <= '9');
}

int main() {
    char ch;
    cout << "Enter a character: ";
    cin>>ch;
    if(isDigit(ch))
        cout << "The character is a digit." << endl;
    else
        cout << "The character is not a digit." << endl;
    return 0;
}
    

Output:

  Enter a character: 3
The character is a digit.

33. WAP in CPP to replace the spaces in a string with a given character.


#include <iostream>
#include <string>
using namespace std;

string replaceSpaces(string str, char ch) {
    for(int i=0;  i < str.length(); i++)
        if(str[i] == ' ')
            str[i] = ch;
    return str;
}

int main() {
    string str;
    char ch;
    cout << "Enter the string: ";
    getline(cin, str);
    cout << "Enter the character to replace spaces with: ";
    cin>>ch;
    cout << "String after replacement: " << replaceSpaces(str, ch) << endl;
    return 0;
}
    

Output:

  Enter the string: Hello World
Enter the character to replace spaces with: _
String after replacement: Hello_World

34. WAP in CPP to convert lowercase characters to uppercase in a string.


#include <iostream>
#include <string>
using namespace std;

string toUpperCase(string str) {
    for(int i=0;  i < str.length(); i++)
        str[i] = toupper(str[i]);
    return str;
}

int main() {
    string str;
    cout << "Enter the string: ";
    getline(cin, str);
    cout << "String in uppercase: " << toUpperCase(str) << endl;
    return 0;
}
    

Output:

  Enter the string: Hello World
String in uppercase: HELLO WORLD

35. WAP in CPP to convert lowercase vowels to uppercase in a string.


#include <iostream>
#include <string>
using namespace std;

bool isLowercaseVowel(char ch) {
    return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}

string convertVowels(string str) {
    for(int i=0; i < str.length(); i++)
        if(isLowercaseVowel(str[i]))
        str[i] = toupper(str[i]);
        return str;
    }

int main() {
    string str;
    cout << "Enter the string: ";
    getline(cin, str);
    cout << "String after conversion: " << convertVowels(str) << endl;
    return 0;
}

Output:

  Enter the string: Hello World
String after conversion: HEllO WOrld

36. WAP in CPP to delete vowels from a given string.


#include <iostream>
#include <string>
using namespace std;

void deleteVowels(string &s) {
    int n = s.length();
    string temp;
    for(int i=0;  i < n; i++) {
        char ch = s[i];
        ch = tolower(ch);
        if(ch!='a' && ch!='e' && ch!='i' && ch!='o' && ch!='u')
            temp += s[i];
    }
    s = temp;
}

int main() {
    string s;
    cout << "Enter the string: ";
    getline(cin, s);
    deleteVowels(s);
    cout << "String after deleting vowels: " << s << endl;
    return 0;
}
    

Output:

  Enter the string: Hello World
String after deleting vowels: Hll Wrld

37. WAP in CPP to count the occurrence of vowels and consonants in a string.


#include <iostream>
#include <string>
using namespace std;

void countVowelsAndConsonants(string s) {
    int vowels = 0, consonants = 0;
    int n = s.length();
    for(int i=0;  i < n; i++) {
        char ch = s[i];
        if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) {
            ch = tolower(ch);
            if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
                vowels++;
            else
                consonants++;
        }
    }
    cout << "Vowels: " << vowels << endl;
    cout << "Consonants: " << consonants << endl;
}

int main() {
    string s;
    cout << "Enter the string: ";
    getline(cin, s);
    countVowelsAndConsonants(s);
    return 0;
}
    

Output:

  Enter the string: Hello World
Vowels: 3
Consonants: 7

38. WAP in CPP to print the highest frequency character in a string.


#include <iostream>
#include <string>
#define ASCII_SIZE 256
using namespace std;

char getHighestFreqChar(string str) {
    int count[ASCII_SIZE] = {0};

    int len = str.length();
    for (int i=0;  i < len; i++)
        count[str[i]]++;

    int max = -1; 
    char result;   
 
    for (int i = 0; i < len; i++) {
        if (max < count[str[i

]]) {
            max = count[str[i]];
            result = str[i];
        }
    }

    return result;
}

int main() {
    string str;
    cout << "Enter the string: ";
    getline(cin, str);
    cout << "Highest frequency character is: " << getHighestFreqChar(str) << endl;
    return 0;
}
    

Output:

  Enter the string: hello world
Highest frequency character is: l

39. WAP in CPP to replace the first occurrence of a vowel with '-' in a string.


#include <iostream>
#include <string>
using namespace std;

void replaceFirstVowel(string &s) {
    int n = s.length();
    for(int i=0;  i < n; i++) {
        char ch = s[i];
        ch = tolower(ch);
        if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') {
            s[i] = '-';
            break;
        }
    }
}

int main() {
    string s;
    cout << "Enter the string: ";
    getline(cin, s);
    replaceFirstVowel(s);
    cout << "String after replacing first vowel: " << s << endl;
    return 0;
}
    

Output:

  Enter the string: hello world
String after replacing first vowel: h-llo world

40. WAP in CPP to count the number of alphabets, digits, and special characters in a string.


#include <iostream>
#include <string>
using namespace std;

void countCharacters(string s) {
    int alphabets = 0, digits = 0, specials = 0;
    int n = s.length();
    for(int i=0;  i < n; i++) {
        char ch = s[i];
        if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
            alphabets++;
        else if(ch>='0' && ch<='9')
            digits++;
        else
            specials++;
    }
    cout << "Alphabets: " << alphabets << endl;
    cout << "Digits: " << digits << endl;
    cout << "Special characters: " << specials << endl;
}

int main() {
    string s;
    cout << "Enter the string: ";
    getline(cin, s);
    countCharacters(s);
    return 0;
}
    

Output:

  Enter the string: hello world! 123
Alphabets: 10
Digits: 3
Special characters: 4

41. WAP in CPP to separate characters in a given string.


#include <iostream>
#include <string>
using namespace std;

string separateCharacters(string s) {
    int n = s.length();
    string result = "";
    for(int i=0;  i < n; i++) {
        result += s[i];
        if(i != n-1)
            result += " ";
    }
    return result;
}

int main() {
    string s;
    cout << "Enter the string: ";
    getline(cin, s);
    cout << "String after separating characters: " << separateCharacters(s) << endl;
    return 0;
}
    

Output:

  Enter the string: hello
String after separating characters: h e l l o

42. WAP in CPP to remove blank spaces from a string.


#include <iostream>
#include <string>
using namespace std;

void removeSpaces(string &s) {
    s.erase(remove(s.begin(), s.end(), ' '), s.end());
}

int main() {
    string s;
    cout << "Enter the string: ";
    getline(cin, s);
    removeSpaces(s);
    cout << "String after removing spaces: " << s << endl;
    return 0;
}
    

Output:

  Enter the string: hello world
String after removing spaces: helloworld

43. WAP in CPP to remove duplicate characters from a string.


#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

string removeDuplicates(string str) {
    unordered_map <char, bool> visited;
    string res = "";
    for(int i=0;  i < str.length(); i++) {
        if(visited[str[i]] == false) {
            visited[str[i]] = true;
            res += str[i];
        }
    }
    return res;
}

int main() {
    string str;
    cout << "Enter the string: ";
    getline(cin, str);
    cout << "String after removing duplicates: " << removeDuplicates(str) << endl;
    return 0;
}

Output:

  Enter the string: hello world
String after removing duplicates: helo wrd

44. WAP in CPP to concatenate two strings.


#include <iostream>
#include <string>
using namespace std;

string concatenateStrings(string s1, string s2) {
    return s1 + s2;
}

int main() {
    string s1, s2;
    cout << "Enter the first string: ";
    getline(cin, s1);
   

 cout << "Enter the second string: ";
    getline(cin, s2);
    cout << "Concatenated string: " << concatenateStrings(s1, s2) << endl;
    return 0;
}

Output:

  Enter the first string: hello
Enter the second string: world
Concatenated string: helloworld

45. WAP in CPP to remove repeated characters from a string.


#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

string removeRepeats(string str) {
    unordered_map<char, int> charCount;
    string res = "";
    for(int i=0;  i < str.length(); i++) {
        charCount[str[i]]++;
        if(charCount[str[i]] == 1) {
            res += str[i];
        }
    }
    return res;
}

int main() {
    string str;
    cout << "Enter the string: ";
    getline(cin, str);
    cout << "String after removing repeated characters: " << removeRepeats(str) << endl;
    return 0;
}

Output:

  Enter the string: hello world
String after removing repeated characters: helo wrd

46. WAP in CPP to calculate the sum of integers in a string.


#include <iostream>
#include <sstream>
using namespace std;

int findSum(string str) {
    stringstream ss;
    ss  <<  str;
    string temp;
    int sum = 0;
    while (!ss.eof()) {
        ss  >> temp;
        if (stringstream(temp)  >> sum)
            sum += stoi(temp);
        temp = "";
    }
    return sum;
}

int main() {
    string str;
    cout << "Enter the string: ";
    cin >>str;
    cout << "Sum of integers in string: " << findSum(str) << endl;
    return 0;
}
    

Output:

  Enter the string: 123abc456def
Sum of integers in string: 579

47. WAP in CPP to print all non-repeating characters in a string.


#include <iostream>
#include <unordered_map>
using namespace std;

void printNonRepeating(string str) {
    unordered_map count;
    for (int i = 0; i < str.size(); i++)
        count[str[i]]++;
    cout << "Non-repeating characters are: ";
    for (auto i: count)
        if (i.second == 1)
            cout  <<  i.first  <<  " ";
    cout << endl;
}

int main() {
    string str;
    cout << "Enter the string: ";
    getline(cin, str);
    printNonRepeating(str);
    return 0;
}
    

Output:

  Enter the string: hello world
Non-repeating characters are: h e w r d

48. WAP in CPP to copy one string to another string.


#include <iostream>
#include <string>
using namespace std;

int main() {
    string str1, str2;
    cout << "Enter the string to copy: ";
    getline(cin, str1);
    str2 = str1;
    cout << "Copied string: " << str2 << endl;
    return 0;
}
    

Output:

  Enter the string to copy: hello
Copied string: hello

49. WAP in CPP to sort the characters of a string.


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    string str;
    cout << "Enter the string: ";
   

 getline(cin, str);
    sort(str.begin(), str.end());
    cout << "Sorted string: " << str << endl;
    return 0;
}
    

Output:

  Enter the string: hello
Sorted string: ehllo

50. WAP in CPP to sort the characters of a string in descending order.


#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    string str;
    cout << "Enter the string: ";
    getline(cin, str);
    sort(str.begin(), str.end(), greater<char>());
    cout << "Sorted string in descending order: " << str << endl;
    return 0;
}
    

Output:

  Enter the string: hello
Sorted string in descending order: ollhe

51. WAP in CPP to reverse an integer.


#include <iostream>
using namespace std;

int reverseNumber(int n) {
    int reversed = 0;
    while(n != 0) {
        reversed = reversed * 10 + n % 10;
        n = n / 10;
    }
    return reversed;
}

int main() {
    int num;
    cout << "Enter the number: ";
    cin >>num;
    cout << "Reversed number: " << reverseNumber(num) << endl;
    return 0;
}
    

Output:

  Enter the number: 12345
Reversed number: 54321

52. WAP in CPP to check whether an integer is an Armstrong number or not.


#include <iostream>
#include <cmath>
using namespace std;

bool isArmstrong(int n) {
    int temp = n, sum = 0;
    int digits = floor(log10(n) + 1);
    while(temp != 0) {
        int remainder = temp%10;
        sum += pow(remainder, digits);
        temp = temp/10;
    }
    return (sum == n);
}

int main() {
    int num;
    cout << "Enter the number: ";
    cin >>num;
    if(isArmstrong(num))
        cout << num << " is an Armstrong number." << endl;
    else
        cout << num << " is not an Armstrong number." << endl;
    return 0;
}
    

Output:

  Enter the number: 153
153 is an Armstrong number.

53 . WAP in CPP to check whether a given number is prime or not.


#include <iostream>
using namespace std;

bool isPrime(int n) {
    if(n <= 1)
        return false;
    for(int i=2; i * i <= n; i++) {
        if(n % i == 0)
            return false;
    }
    return true;
}

int main() {
    int num;
    cout << "Enter the number: ";
    cin >>num;
    if(isPrime(num))
        cout << num << " is a prime number." << endl;
    else
        cout << num << " is not a prime number." << endl;
    return 0;
}
    

Output:

  Enter the number: 17
17 is a prime number.

54. WAP in CPP to print the Fibonacci series using iteration.


#include <iostream>
using namespace std;

void fibonacci(int n) {
    int t1 = 0, t2 = 1, nextTerm = 0;
    for (int i = 1; i <= n; ++i) {
        if(i == 1) {
            cout  <<  t1  <<  ", ";
            continue;
        }
        if(i == 2) {
            cout  <<  t2  <<  ", ";
            continue;
        }
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
        cout  <<  nextTerm  <<  ", ";
    }
    cout << endl;
}

int main() {
    int num;
    cout << "Enter the number of terms for Fibonacci series: ";
    cin >>num;
    fibonacci(num);
    return 0;
}
    

Output:

  Enter the number of terms for Fibonacci series: 10
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

55. WAP in CPP to print the Fibonacci series using recursion.


#include <iostream>
using namespace std;

int fibonacci(int n) {
    if(n <= 1)
        return n;
    else
        return(fibonacci(n-1) + fibonacci(n-2));
}

int main() {
    int num;
    cout << "Enter the number of terms for Fibonacci series: ";
    cin >>num;
    for(int i=0; i < num; i++)
        cout << fibonacci(i) << ", ";
    cout << endl;
    return 0;
}
    

Output:

  Enter the number of terms for Fibonacci series: 10
0, 1, 1, 2, 3, 5, 8, 13, 21 , 34,

56. WAP in CPP to check whether a number is a palindrome or not using iteration.


#include <iostream>
using namespace std;

bool isPalindrome(int n) {
    int temp = n, reversed = 0;
    while(temp != 0) {
        reversed = reversed * 10 + temp % 10;
        temp = temp / 10;
    }
    return (n == reversed);
}

int main() {
    int num;
    cout << "Enter the number: ";
    cin >>num;
    if(isPalindrome(num))
        cout << num << " is a palindrome." << endl;
    else
        cout << num << " is not a palindrome." << endl;
    return 0;
}
    

Output:

  Enter the number: 121
121 is a palindrome.

57. WAP in CPP to check whether a number is a palindrome or not using recursion.


#include <iostream>
using namespace std;

bool isPalindrome(int n, int& originalN) {
    if (n == 0)
        return true;
    if (isPalindrome(n / 10, originalN)) {
        if (n % 10 == originalN % 10) {
            originalN /= 10;
            return true;
        }
        else
            return false;
    }
    else
        return false;
}

int main() {
    int num;
    cout << "Enter the number: ";
    cin >>num;
    int originalN = num;
    if(isPalindrome(num, originalN))
        cout << num << " is a palindrome." << endl;
    else
        cout << num << " is not a palindrome." << endl;
    return 0;
}
    

Output:

  Enter the number: 121
121 is a palindrome.

58. WAP in CPP to find the greatest among three integers.


#include <iostream>
using namespace std;

int main() {
    int num1, num2, num3;
    cout << "Enter three numbers: ";
    cin >>num1 >>num2 >>num3;
    cout << "The greatest number is: " << max({num1, num2, num3}) << endl;
    return 0;
}
    

Output:

  Enter three numbers: 10 20 30
The greatest number is: 30

59. WAP in CPP to check if a number is binary.

#include <iostream>
using namespace std;
bool isBinary(int num) {
    while(num != 0) {
        int digit = num % 10;
        if(digit > 1)
            return false;
        num = num / 10;
    }
    return true;
}

int main() {
    int num;
    cout << "Enter the number: ";
    cin >> num;
    if(isBinary(num))
        cout << num << " is a binary number." << endl;
    else
        cout << num << " is not a binary number." << endl;
    return 0;
}

Output:

  Enter the number: 101001
101001 is a binary number.

60. WAP in CPP to find the sum of the digits of a number using recursion.


#include <iostream>
using namespace std;
int sumOfDigits(int num) {
    if(num == 0)
        return 0;
    else
        return (num % 10 + sumOfDigits(num / 10));
}

int main() {
    int num;
    cout << "Enter the number: ";
    cin>>num;
    cout << "Sum of digits: " << sumOfDigits(num) << endl;
    return 0;
}

Output:

  Enter the number: 1234
Sum of digits: 10

61. WAP in CPP to swap two numbers without using a third variable.


#include <iostream>
using namespace std;

int main() {
    int a, b;
    cout << "Enter two numbers: ";
    cin >> a >> b;
    a = a + b;
    b = a - b;
    a = a - b;
    cout << "After swapping, a = " << a << " and b = " << b << endl;
    return 0;
}
    

Output:

  Enter two numbers: 3 4
After swapping, a = 4 and b = 3

62. WAP in CPP to swap two numbers using a third variable.


#include <iostream>
using namespace std;

int main() {
    int a, b, temp;
    cout << "Enter two numbers: ";
    cin >> a >> b;
    temp = a;
    a = b;
    b = temp;
    cout << "After swapping, a = " << a << " and b = " << b << endl;
    return 0;
}
    

Output:

  Enter two numbers: 3 4
After swapping, a = 4 and b = 3

63. WAP in CPP to find the prime factors of a given integer.


#include <iostream>
using namespace std;

void primeFactors(int n) {
    while (n%2 == 0) {
        cout << 2 << " ";
        n = n/2;
    }

    for (int i = 3; i*i <= n; i = i+2) {
        while (n%i == 0) {
            cout << i << " ";
            n = n/i;
        }
    }

    if (n > 2)
        cout << n;
    cout << endl;
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    cout << "Prime factors of " << n << " are: ";
    primeFactors(n);
    return 0;
}
    

Output:

  Enter a number: 315
Prime factors of 315 are: 3 3 5 7

64. WAP in CPP to add two integers without using the arithmetic '+' operator.


#include <iostream>
using namespace std;

int add(int a, int b) {
    while (b != 0) {
        int carry = a & b;


        a = a ^ b;
        b = carry << 1;
    }
    return a;
}

int main() {
    int a, b;
    cout << "Enter two numbers: ";
    cin >> a >> b;
    cout << "Sum is: " << add(a, b) << endl;
    return 0;
}
    

Output:

  Enter two numbers: 4 5
Sum is: 9

65. WAP in CPP to check if a given number is perfect or not.


#include <iostream>
using namespace std;

void isPerfect(int n) {
    int sum = 1;
    for (int i=2; i*i <=n; i++) {
        if (n%i==0) {
            if(i*i!=n)
                sum = sum + i + n/i;
            else
                sum = sum + i;
        }
    } 
    if (sum == n && n!=1)
        cout << "The number is a perfect number." << endl;
    else
        cout << "The number is not a perfect number." << endl;
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    isPerfect(n);
    return 0;
}
    

Output:

  Enter a number: 6
The number is a perfect number.

66. WAP in CPP to find the average of numbers with explanations.


#include <iostream>
using namespace std;

float findAverage(int arr[], int n) {
    int sum = 0;
    for(int i=0; i > n;
    int arr[n];
    cout << "Enter the elements: ";
    for(int i=0; i > arr[i];
    }
    float avg = findAverage(arr, n);
    cout << "Average of given numbers is: " << avg << endl;
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements: 1 2 3 4 5
Average of given numbers is: 3

67. WAP in CPP to calculate the factorial using the iterative method.


#include <iostream>
using namespace std;

long long factorial(int n) {
    long long fact = 1;
    for(int i=2; i <=n; i++) {
        fact *= i

;
    }
    return fact;
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    cout << "Factorial of " << n << " is: " << factorial(n) << endl;
    return 0;
}
    

Output:

  Enter a number: 5
Factorial of 5 is: 120

68. WAP in CPP to calculate the factorial using recursion.


#include <iostream>
using namespace std;

long long factorial(int n) {
    if(n == 0) {
        return 1;
    } else {
        return n * factorial(n-1);
    }
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    cout << "Factorial of " << n << " is: " << factorial(n) << endl;
    return 0;
}
    

Output:

  Enter a number: 5
Factorial of 5 is: 120

69. WAP in CPP to check if a given number is even or odd.


#include <iostream>
using namespace std;

void checkOddEven(int n) {
    if(n % 2 == 0) {
        cout << "The number is even." << endl;
    } else {
        cout << "The number is odd." << endl;
    }
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    checkOddEven(n);
    return 0;
}
    

Output:

  Enter a number: 4
The number is even.

70. WAP in CPP to print the first n prime numbers with explanations.


#include <iostream>
using namespace std;

bool isPrime(int n) {
    if (n <= 1) return false;
    if (n <= 3) return true;
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
           return false;
    return true;
}

void printPrime(int n) {
    int count = 0;
    int currNum = 2;
    while(count < n) {
        if(isPrime(currNum)) {
            cout << currNum << " ";
            count++;
        }
        currNum++;
    }
    cout << endl;
}

int main() {
    int n;
    cout << "Enter the number of prime numbers

 you want: ";
    cin >> n;
    printPrime(n);
    return 0;
}
    

Output:

  Enter the number of prime numbers you want: 5
2 3 5 7 11

71. WAP in CPP to print prime numbers in a given range.


#include <iostream>
using namespace std;

bool isPrime(int n) {
    if (n <= 1) return false;
    if (n <= 3) return true;
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
           return false;
    return true;
}

void printPrime(int start, int end) {
    for(int i=start; i <=end; i++) {
        if(isPrime(i)) {
            cout << i << " ";
        }
    }
    cout << endl;
}

int main() {
    int start, end;
    cout << "Enter the range (start end): ";
    cin >> start >> end;
    printPrime(start, end);
    return 0;
}
    

Output:

  Enter the range (start end): 10 50
11 13 17 19 23 29 31 37 41 43 47

72. WAP in CPP to find the smallest number among three numbers.


#include <iostream>
using namespace std;

int smallestOfThree(int a, int b, int c) {
    if(a < b && a < c) return a;
    else if(b < c) return b;
    else return c;
}

int main() {
    int a, b, c;
    cout << "Enter three numbers: ";
    cin >> a >> b >> c;
    cout << "Smallest number is: " << smallestOfThree(a, b, c) << endl;
    return 0;
}
    

Output:

  Enter three numbers: 3 2 1
Smallest number is: 1

73. WAP in CPP to calculate the power using the `pow` method.


#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int base, exponent;
    cout << "Enter base and exponent: ";
    cin >> base >> exponent;
    double power = pow(base, exponent);
    cout << base << " raised to the power " << exponent << " is

: " << power << endl;
    return 0;
}
    

Output:

  Enter base and exponent: 2 3
2 raised to the power 3 is: 8

74. WAP in CPP to calculate the power without using the `pow` function.


#include <iostream>
using namespace std;

int power(int base, int exponent) {
    int power = 1;
    while(exponent != 0) {
        power *= base;
        --exponent;
    }
    return power;
}

int main() {
    int base, exponent;
    cout << "Enter base and exponent: ";
    cin >> base >> exponent;
    cout << base << " raised to the power " << exponent << " is: " << power(base, exponent) << endl;
    return 0;
}
    

Output:

  Enter base and exponent: 2 3
2 raised to the power 3 is: 8

75. WAP in CPP to calculate the square of a given number.


#include <iostream>
using namespace std;

int square(int n) {
    return n * n;
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    cout << "Square of " << n << " is: " << square(n) << endl;
    return 0;
}
    

Output:

  Enter a number: 4
Square of 4 is: 16

76. WAP in CPP to calculate the cube of a given number.


#include <iostream>
using namespace std;

int cube(int n) {
    return n * n * n;
}

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    cout << "Cube of " << n << " is: " << cube(n) << endl;
    return 0;
}
    

Output:

  Enter a number: 3
Cube of 3 is: 27

77. WAP in CPP to calculate the square root of a given number.


#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int n;
    cout << "Enter a number: ";
    cin >> n;
    cout << "Square

 root of " << n << " is: " << sqrt(n) << endl;
    return 0;
}
    

Output:

  Enter a number: 16
Square root of 16 is: 4

78. WAP in CPP to calculate the least common multiple (LCM) of two numbers.


#include <iostream>
using namespace std;

int gcd(int a, int b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int lcm(int a, int b) {
    return (a / gcd(a, b)) * b;
}

int main() {
    int a, b;
    cout << "Enter two numbers: ";
    cin >> a >> b;
    cout << "LCM of " << a << " and " << b << " is: " << lcm(a, b) << endl;
    return 0;
}
    

Output:

  Enter two numbers: 12 15
LCM of 12 and 15 is: 60

79. WAP in CPP to find the greatest common divisor (GCD) or highest common factor (HCF) of two numbers.


#include <iostream>
using namespace std;

int gcd(int a, int b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int main() {
    int a, b;
    cout << "Enter two numbers: ";
    cin >> a >> b;
    cout << "GCD of " << a << " and " << b << " is: " << gcd(a, b) << endl;
    return 0;
}
    

Output:

  Enter two numbers: 60 48
GCD of 60 and 48 is: 12

80. WAP in CPP to find the GCD of two numbers using recursion.


#include <iostream>
using namespace std;

int gcd(int a, int b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int main() {
    int a, b;
    cout << "Enter two numbers: ";
    cin >> a >> b;
    cout << "GCD of " << a << " and " << b << " is: " << gcd(a, b) << endl;
    return 0;
}
    

Output:

  Enter two numbers: 60 48
GCD of 60 and 48 is: 12

81. WAP in CPP to convert a decimal number into a binary number.


#include <iostream>
#include <bitset>
using namespace std;

int main() {
    int n;
    cout << "Enter a decimal number: ";
    cin >> n;
    cout << "Binary representation: " << bitset<32>(n) << endl;
    return 0;
}
    

Output:

  Enter a decimal number: 18
Binary representation: 00000000000000000000000000010010

82. WAP in CPP to convert a decimal number to an octal number.


#include <iostream>
using namespace std;

void decToOctal(int n) {
    int octalNum[100];
    int i = 0;
    while (n != 0) {
        octalNum[i] = n % 8;
        n = n / 8;
        i++;
    }
    cout << "Octal representation: ";
    for (int j = i - 1; j >= 0; j--)
        cout << octalNum[j];
    cout << endl;
}

int main() {
    int n;
    cout << "Enter a decimal number: ";
    cin >> n;
    decToOctal(n);
    return 0;
}
    

Output:

  Enter a decimal number: 18
Octal representation: 22

83. WAP in CPP to check if a given year is a leap year or not.


#include <iostream>
using namespace std;

int main() {
    int year;
    cout << "Enter a year: ";
    cin >> year;
    if (year % 4 == 0) {
        if (year % 100 == 0) {
            if (year % 400 == 0)
                cout << year << " is a leap year." << endl;
            else
                cout << year << " is not a leap year." << endl;
        } else
            cout << year << " is a leap year." << endl;
    } else
        cout << year << " is not a leap year." << endl;
    return 0;
}
    

Output:

  Enter a year: 2000
2000 is a leap year.

84. WAP in CPP to convert Celsius to Fahrenheit.


#include <iostream>


using namespace std;

int main() {
    float celsius;
    cout << "Enter temperature in Celsius: ";
    cin >> celsius;
    float fahrenheit = (celsius * 9.0) / 5.0 + 32;
    cout << "Temperature in Fahrenheit: " << fahrenheit << endl;
    return 0;
}
    

Output:

  Enter temperature in Celsius: 37
Temperature in Fahrenheit: 98.6

85. WAP in CPP to convert Fahrenheit to Celsius.


#include <iostream>
using namespace std;

int main() {
    float fahrenheit;
    cout << "Enter temperature in Fahrenheit: ";
    cin >> fahrenheit;
    float celsius = (fahrenheit - 32) * 5.0/9.0;
    cout << "Temperature in Celsius: " << celsius << endl;
    return 0;
}

Output:

  Enter temperature in Fahrenheit: 98.6
Temperature in Celsius: 37

86. WAP in CPP to calculate the simple interest with explanations.


#include <iostream>
using namespace std;

int main() {
    float p, r, t;
    cout << "Enter the principal amount: ";
    cin >> p;
    cout << "Enter the rate of interest: ";
    cin >> r;
    cout << "Enter the time in years: ";
    cin >> t;
    float si = (p * r * t) / 100;
    cout << "Simple Interest is: " << si << endl;
    return 0;
}

Output:

  Enter the principal amount: 1000
Enter the rate of interest: 5
Enter the time in years: 1
Simple Interest is: 50

87. WAP in CPP to perform matrix multiplication.


#include <iostream>
using namespace std;

int main() {
    int r1, c1, r2, c2;
    cout << "Enter rows and columns for the first matrix: ";
    cin >> r1 >> c1;
    cout << "Enter rows and columns for the second matrix: ";
    cin >> r2 >> c2;

    if (c1 != r2) {
        cout << "Matrix multiplication not possible." << endl;
        return 0;
    }

    int a[r1][c1], b[r2][c2], mult[r1][c2];

    cout << "Enter elements of matrix 1: " << endl;
    for(int i = 0; i < r1; ++i)


        for(int j = 0; j < c1; ++j)
            cin >> a[i][j];

    cout << "Enter elements of matrix 2: " << endl;
    for(int i = 0; i < r2; ++i)
        for(int j = 0; j < c2; ++j)
            cin >> b[i][j];

    for(int i = 0; i < r1; ++i)
        for(int j = 0; j < c2; ++j) {
            mult[i][j] = 0;
            for(int k = 0; k < c1; ++k)
                mult[i][j] += a[i][k] * b[k][j];
        }

    cout << "Output Matrix: " << endl;
    for(int i = 0; i < r1; ++i) {
        for(int j = 0; j < c2; ++j)
            cout << mult[i][j] << " ";
        cout << endl;
    }

    return 0;
}

Output:

  Enter rows and columns for the first matrix: 2 2
Enter rows and columns for the second matrix: 2 2
Enter elements of matrix 1:
1 2
3 4
Enter elements of matrix 2:
1 2
3 4
Output Matrix:
7 10
15 22

88. WAP in CPP to solve a quadratic equation.


#include <iostream>
#include <cmath>
using namespace std;

int main() {
    float a, b, c, discriminant, root1, root2;
    cout << "Enter coefficients a, b and c: ";
    cin >> a >> b >> c;
    discriminant = b*b - 4*a*c;
    if (discriminant > 0) {
        root1 = (-b + sqrt(discriminant)) / (2*a);
        root2 = (-b - sqrt(discriminant)) / (2*a);
        cout << "Roots are real and different." << endl;
        cout << "Root 1 = " << root1 << endl;
        cout << "Root 2 = " << root2 << endl;
    } else if (discriminant == 0) {
        root1 = -b/(2*a);
        cout << "Roots are real and the same." << endl;
        cout << "Root 1 = Root 2 = " << root1 << endl;
    } else {
        float realPart = -b/(2*a);
        float imaginaryPart = sqrt(-discriminant)/(2*a);
        cout << "Roots are complex and different." << endl;
        cout << "Root 1 = " << realPart << " + " << imaginaryPart << "i" << endl;
        cout << "Root 2 = " << realPart << " - " << imaginaryPart << "i" << endl;
    }

    return 0;
}

Output:

  Enter coefficients a, b and c : 1 -3 2
Roots are real and different.
Root 1 = 2
Root 2 = 1

89. WAP in CPP to implement the Bubble Sort.


#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n) {
   for(int i = 0; i < n-1; i++) {     
       for (int j = 0; j < n-i-1; j++) { 
           if (arr[j] > arr[j+1]) {
              int temp = arr[j];
              arr[j] = arr[j+1];
              arr[j+1] = temp;
           }
       }
   }
}

int main() {
   int n;
   cout << "Enter the number of elements: ";
   cin>>n;
   int arr[n];
   cout << "Enter elements: ";
   for(int i = 0; i < n; i++) {
      cin>>arr[i];
   }
   bubbleSort(arr, n);
   cout << "Array after bubble sort:";
   for(int i = 0; i < n; i++) {
      cout << " " << arr[i];
   }
   cout << endl;
   return 0;
}

Output:

  Enter the number of elements: 5
Enter elements: 5 3 2 4 1
Array after bubble sort: 1 2 3 4 5

90. WAP in CPP to implement the Selection Sort.


#include <iostream>
using namespace std;

void selectionSort(int arr[], int n) {
   int i, j, minIndex, temp; 
   for (i = 0; i < n-1; i++) {
      minIndex = i;
      for (j = i+1; j < n; j++)
         if (arr[j] < arr[minIndex])
            minIndex = j;
      temp = arr[minIndex];
      arr[minIndex] = arr[i];
      arr[i] = temp;
   }
}

int main() {
   int n;
   cout << "Enter the number of elements: ";
   cin>>n;
   int arr[n];
   cout << "Enter elements: ";
   for(int i = 0; i < n; i++) {
      cin>>arr[i];
   }
   selectionSort(arr, n);
   cout << "Array after Selection Sort:";
   for(int i = 0; i < n; i++) {
      cout << " " << arr[i];
   }
   cout << endl;
   return 0;
}

Output:

  Enter the number of elements: 5
Enter elements: 5 3 2 4 1
Array after Selection Sort: 1 2 3 4 5

91. WAP in CPP to implement the Insertion Sort.


#include <iostream>
using namespace std;
void insertionSort(int arr[], int n) {
    int i, key, j;
    for (i = 1; i < n; i++) {
        key = arr[i];
        j = i - 1;
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
    arr[j + 1] = key;
    }
}

void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin >> n;
    int arr[n];
    cout << "Enter the elements: ";
    for(int i=0; i < n; i++)
        cin >> arr[i];
    insertionSort(arr, n);
    printArray(arr, n);
    return 0;
}

Output:

  Enter the number of elements: 5
Enter the elements: 5 4 3 2 1
1 2 3 4 5

92. WAP in CPP to implement the Quick Sort.


#include <iostream>
using namespace std;
void swap(int* a, int* b) {
    int t = *a;
    *a = *b;
    *b = t;
}

int partition (int arr[], int low, int high) {
    int pivot = arr[high];
    int i = (low - 1);
    for (int j = low; j <= high - 1; j++) {
        if (arr[j] < pivot) {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}

void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin >> n;
    int arr[n];
    cout << "Enter the elements: ";
    for(int i=0; i < n; i++)
        cin >> arr[i];
    quickSort(arr, 0, n-1);
    printArray(arr, n);
    return 0;
}

Output:

  Enter the number of elements: 5
Enter the elements: 5 4 3 2 1
1 2 3 4 5

93. WAP in CPP to implement the Merge Sort.


#include <iostream>
using namespace std;
void merge(int arr[], int l, int m, int r) {
    int i, j, k;
    int n1 = m - l + 1;
    int n2 = r - m;
    int L[n1], R[n2];
    for (i = 0; i < n1; i++)
        L[i] = arr[l + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[m + 1 + j];
    i = 0;
    j = 0;
    k = l;
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        } else {
            arr[k] = R[j];
            j++;
        }
        k++;
    } 
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}

void mergeSort(int arr[], int l, int r) {
    if (l < r) {
        int m = l + (r - l) / 2;
        mergeSort(arr, l, m);
        mergeSort(arr, m + 1, r);
        merge(arr, l, m, r);
    }
}

void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin >> n;
    int arr[n];
    cout << "Enter the elements: ";
    for(int i=0; i < n; i++)
        cin >> arr[i];
    mergeSort(arr, 0, n - 1);
    printArray(arr, n);
    return 0;
}

Output:

  Enter the number of elements: 5
Enter the elements: 5 4 3 2 1
1 2 3 4 5

94. WAP in CPP to implement the Heap Sort.


#include <iostream>
using namespace std;
void heapify(int arr[], int n, int i) {
    int largest = i;
    int left = 2i + 1;
    int right = 2i + 2;
    if (left < n && arr[left] > arr[largest])
        largest = left;
    if (right < n && arr[right] > arr[largest])
        largest = right;
    if (largest != i) {
        swap(arr[i], arr[largest]);
        heapify(arr, n, largest);
    }
}

void heapSort(int arr[], int n) {
    for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);
    for (int i=n-1; i>=0; i--) {
        swap(arr[0], arr[i]);
        heapify(arr, i, 0);
    }
}

void printArray(int arr[], int n) {
    for (int i=0; i < n; ++i)
        cout << arr[i] << " ";
    cout << "\n";
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin >> n;
    int arr[n];
    cout << "Enter the elements: ";
    for(int i=0; i < n; i++)
        cin >> arr[i];
    heapSort(arr, n);
    printArray(arr, n);
    return 0;
}

Output:

  Enter the number of elements: 5
Enter the elements: 5 4 3 2 1
1 2 3 4 5

95. WAP in CPP to implement Binary Search.


#include <iostream>
using namespace std;
int binarySearch(int arr[], int l, int r, int x) {
    while (l <= r) {
        int m = l + (r - l) / 2;
        if (arr[m] == x)
            return m;
        if (arr[m] < x)
            l = m + 1;
        else
            r = m - 1;
    }
    return -1;
}

int main() {
    int n, x;
    cout << "Enter the number of elements: ";
    cin >> n;
    int arr[n];
    cout << "Enter the elements in sorted order: ";
    for(int i=0; i < n; i++)
        cin >> arr[i];
    cout << "Enter the number to search: ";
    cin >> x;
    int result = binarySearch(arr, 0, n - 1, x);
    (result == -1) ? cout << "Element is not present in array\n": cout << "Element is present at index " <<  result  << endl;
    return 0;
}

Output:

  Enter the number of elements: 5
Enter the elements in sorted order: 1 2 3 4 5
Enter the number to search: 3
Element is present at index 2

96. WAP in CPP to implement Linear Search.


#include <iostream>
using namespace std;

int linearSearch(int arr[], int n, int x) {
    for (int i = 0; i < n; i++)
        if (arr[i] == x)
            return i;
    return -1;
}

int main() {
    int n;
    cout << "Enter the number of elements: ";
    cin >> n;
    int arr[n];
    cout << "Enter the elements of the array: ";
    for(int i=0; i < n; i++)
        cin >> arr[i];
    int x;
    cout << "Enter the number to search: ";
    cin >> x;
    int result = linearSearch(arr, n, x);
    (result == -1) ? cout << "Element is not present in array"
                   : cout << "Element is present at index "  << result;
    return 0;
}
    

Output:

  Enter the number of elements: 5
Enter the elements of the array: 10 20 30 40 50
Enter the number to search: 30
Element is present at index 2

97. WAP in CPP to implement Depth First Search (DFS) on a graph.


#include<iostream>
#include<list>
using namespace std;

class Graph {
    int V;
    list<int> *adj;
public:
    Graph(int V);
    void addEdge(int v, int w);
    void DFS(int v);
};

Graph::Graph(int V) {
    this->V = V;
    adj = new list<int>[V];
}

void Graph::addEdge(int v, int w) {
    adj[v].push_back(w);
}

void Graph::DFS(int v) {
    bool *visited = new bool[V];
    for (int i = 0; i < V; i++)
        visited[i] = false;

    list<int> stack;
    visited[v] = true;
    stack.push_back(v);

    while(!stack.empty()) {
        v = stack.back();
        cout << v << " ";
        stack.pop_back();
        list<int>::iterator i;
        for(i = adj[v].begin(); i != adj[v].end(); ++i) {
            if (!visited[*i]) {
                stack.push_back(*i);
                visited[*i] = true;
            }
        }
    }
}

int main() {
    int nodes, edges, x, y, start;
    cout << "Enter number of nodes: ";
    cin >> nodes;
    Graph g(nodes);
    cout << "Enter number of edges: ";
    cin >> edges;
    for(int i=0; i < edges; i++) {
        cout << "Enter edge (Format: x y): ";
        cin >> x >> y;
        g.addEdge(x, y);
    }
    cout << "Enter initial node to traverse from: ";
    cin >> start;
    cout << "DFS traversal starting from node " << start << ": ";
    g.DFS(start);
    return 0;
}

Output:

  Enter number of nodes: 4
Enter number of edges: 6
Enter edge (Format: x y): 0 1
Enter edge (Format: x y): 0 2
Enter edge (Format: x y): 1 2
Enter edge (Format: x y): 2 0
Enter edge (Format: x y): 2 3
Enter edge (Format: x y): 3 3
Enter initial node to traverse from: 2
DFS traversal starting from node 2: 2 3 0 1

98. WAP in CPP to implement Breadth First Search (BFS) on a graph.


#include <iostream>
#include <list>
using namespace std;

class Graph {
    int V; 
    list<int> *adj;   
public:
    Graph(int V); 
    void addEdge(int v, int w);
    void BFS(int s); 
};

Graph::Graph(int V) {
    this->V = V;
    adj = new list<int>[V];
}

void Graph::addEdge(int v, int w) {
    adj[v].push_back(w); 
}

void Graph::BFS(int s) {
    bool *visited = new bool[V];
    for(int i = 0; i < V; i++)
        visited[i] = false;

    list<int> queue;
    visited[s] = true;
    queue.push_back(s);

    list<int>::iterator i;

    while(!queue.empty()) {
        s = queue.front();
        cout << s << " ";
        queue.pop_front();

        for (i = adj[s].begin(); i != adj[s].end(); ++i) {
            if (!visited[*i]) {
                queue.push_back(*i);
                visited[*i] = true;
            }
        }
    }
}

int main() {
    int nodes, edges, x, y, start;
    cout << "Enter number of nodes: ";
    cin >> nodes;
    Graph g(nodes);
    cout << "Enter number of edges: ";
    cin >> edges;
    for(int i=0; i < edges; i++) {
        cout << "Enter edge (Format: x y): ";
        cin >> x >> y;
        g.addEdge(x, y);
    }
    cout << "Enter initial node to traverse from: ";
    cin >> start;
    cout << "BFS traversal starting from node " << start << ": ";
    g.BFS(start);
    return 0;
}
    

Output:

  Enter number of nodes: 4
Enter number of edges: 6
Enter edge (Format: x y): 0 1
Enter edge (Format: x y): 0 2
Enter edge (Format: x y): 1 2
Enter edge (Format: x y): 2 0
Enter edge (Format: x y): 2 3
Enter edge (Format: x y): 3 3
Enter initial node to traverse from: 2
BFS traversal starting from node 2: 2 0 3 1

99. WAP in CPP to implement a Binary Search Tree (BST).


#include <iostream>
using namespace std;

struct Node {
    int key;
    struct Node *left, *right;
};

struct Node *newNode(int item) {
    struct Node *temp = new Node;
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}

void inorder(struct Node *root) {
    if (root != NULL) {
        inorder(root->left);
        cout << root->key << " ";
        inorder(root->right);
    }
}

struct Node* insert(struct Node* Node, int key) {
    if (Node == NULL) return newNode(key);

    if (key < Node->key)
        Node->left = insert(Node->left, key);
    else
        Node->right = insert(Node->right, key);

    return Node;
}

int main() {
    struct Node *root = NULL;
    int n, val;
    cout << "Enter the number of nodes: ";
    cin >> n;
    for(int i=0; i < n; i++) {
        cout << "Enter the value for node " << i+1 << ": ";
        cin >> val;
        if(i == 0) {
            root = insert(root, val);
        } else {
            insert(root, val);
        }
    }
    cout << "Inorder traversal of the BST: ";
    inorder(root);
    return 0;
}

Output:

  Enter the number of nodes: 5
Enter the value for node 1: 50
Enter the value for node 2: 30
Enter the value for node 3: 20
Enter the value for node 4: 40
Enter the value for node 5: 70
Inorder traversal of the BST: 20 30 40 50 70

100. WAP in CPP to implement Dijkstra's algorithm.


#include <iostream>
#include <limits.h>
#define V 9
using namespace std;

int minDistance(int dist[], bool sptSet[]) {
    int min = INT_MAX, min_index;
    for (int v = 0; v < V; v++)
        if (sptSet[v] == false && dist[v] <= min)
            min = dist[v], min_index = v;
    return min_index;
}

void printSolution(int dist[]) {
    cout << "Vertex \t\t Distance from Source\n";
    for (int i = 0; i < V; i++)
        cout << i << " \t\t " << dist[i] << "\n";
}

void dijkstra(int graph[V][V], int src) {
    int dist[V];
    bool sptSet[V];
    for (int i = 0; i < V; i++)
        dist[i] = INT_MAX, sptSet[i] = false;

    dist[src] = 0;
    for (int count

 = 0; count < V-1; count++) {
        int u = minDistance(dist, sptSet);
        sptSet[u] = true;

        for (int v = 0; v < V; v++)
            if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
                && dist[u] + graph[u][v] < dist[v])
                dist[v] = dist[u] + graph[u][v];
    }
    printSolution(dist);
}

int main() {
    int graph[V][V] = {
        {0, 4, 0, 0, 0, 0, 0, 8, 0},
        {4, 0, 8, 0, 0, 0, 0, 11, 0},
        {0, 8, 0, 7, 0, 4, 0, 0, 2},
        {0, 0, 7, 0, 9, 14, 0, 0, 0},
        {0, 0, 0, 9, 0, 10, 0, 0, 0},
        {0, 0, 4, 14, 10, 0, 2, 0, 0},
        {0, 0, 0, 0, 0, 2, 0, 1, 6},
        {8, 11, 0, 0, 0, 0, 1, 0, 7},
        {0, 0, 2, 0, 0, 0, 6, 7, 0}
    };
    dijkstra(graph, 0);
    return 0;
}

Output:

Vertex   Distance from Source
   0          0
   1          4
   2          12
   3          19
   4          21
   5          11
   6          9
   7          8
   8          14