21. Create a program to count the number of words in a string.

Required Input:

str = "Hello World"

Expected Output:

Number of words = 2

Code In Cpp

#include <iostream> #include <sstream> #include <string> int main() { std::string str = "Hello World"; // Logic to count words return 0; }

Run Code?

Click Run Button to view compiled output

22. Write a program to sort an array in ascending order.

Required Input:

arr = {5, 3, 8, 1, 4}

Expected Output:

Sorted Array = 1, 3, 4, 5, 8

Code In Cpp

#include <algorithm> #include <iostream> int main() { int arr[] = {5, 3, 8, 1, 4}; // Logic to sort the array return 0; }

Run Code?

Click Run Button to view compiled output

23. Create a program to find the largest element in an array.

Required Input:

arr = {3, 5, 7, 2, 8}

Expected Output:

Largest Element = 8

Code In Cpp

#include <iostream> int main() { int arr[] = {3, 5, 7, 2, 8}; // Logic to find the largest element return 0; }

Run Code?

Click Run Button to view compiled output

24. Write a program to find the average of an array of numbers.

Required Input:

arr = {4, 8, 6, 5}

Expected Output:

Average = 5.75

Code In Cpp

#include <iostream> int main() { int arr[] = {4, 8, 6, 5}; // Logic to calculate the average return 0; }

Run Code?

Click Run Button to view compiled output

25. Create a program to count the occurrences of a character in a string.

Required Input:

str = "Hello", ch = 'l'

Expected Output:

Occurrences of l = 2

Code In Cpp

#include <iostream> #include <string> int main() { std::string str = "Hello"; char ch = 'l'; // Logic to count occurrences of ch in str return 0; }

Run Code?

Click Run Button to view compiled output

26. Write a program to find the second largest number in an array.

Required Input:

arr = {5, 3, 8, 1, 4}

Expected Output:

Second Largest = 5

Code In Cpp

#include <iostream> int main() { int arr[] = {5, 3, 8, 1, 4}; // Logic to find the second largest number return 0; }

Run Code?

Click Run Button to view compiled output

27. Create a program to find the transpose of a 2D array.

Required Input:

2D array = {{1, 2}, {3, 4}}

Expected Output:

Transpose = 
1 3 
2 4 

Code In Cpp

#include <iostream> int main() { int arr[2][2] = {{1, 2}, {3, 4}}; // Logic to find transpose return 0; }

Run Code?

Click Run Button to view compiled output

28. Write a program to find the intersection of two arrays.

Required Input:

arr1 = {1, 2, 3, 4, 5}, arr2 = {4, 5, 6, 7, 8}

Expected Output:

Intersection = {4, 5}

Code In Cpp

#include <iostream> #include <vector> int main() { std::vector<int> arr1 = {1, 2, 3, 4, 5}; std::vector<int> arr2 = {4, 5, 6, 7, 8}; // Logic to find intersection return 0; }

Run Code?

Click Run Button to view compiled output

29. Create a program to calculate the sum of even numbers in an array.

Required Input:

arr = {1, 2, 3, 4, 5}

Expected Output:

Sum of even numbers = 6

Code In Cpp

#include <iostream> int main() { int arr[] = {1, 2, 3, 4, 5}; // Logic to calculate sum of even numbers return 0; }

Run Code?

Click Run Button to view compiled output

30. Write a program to merge two sorted arrays.

Required Input:

arr1 = {1, 3, 5}, arr2 = {2, 4, 6}

Expected Output:

Merged Array = {1, 2, 3, 4, 5, 6}

Code In Cpp

#include <iostream> #include <vector> int main() { std::vector<int> arr1 = {1, 3, 5}; std::vector<int> arr2 = {2, 4, 6}; // Logic to merge the arrays return 0; }

Run Code?

Click Run Button to view compiled output

ad vertical

3 of 3