Q1: How do you swap the values of two variables in C++?
A1: To swap the values of two variables in C++, you can use a temporary variable to
hold one of the values temporarily. Here's an example:
#include <iostream>
int main() {
int a = 5;
int b = 10;
int temp = a;
a = b;
b = temp;
std::cout << "After swapping, a = " << a << " and b = " << b << std::endl;
return 0;
}
After swapping, a = 10 and b = 5
The output will be:
Q2: How can you find the largest element in an array using C++?
A2: You can find the largest element in an array by iterating through each element and
comparing it with the current maximum value. Here's an example:
#include <iostream>
int main() {
int arr[] = {5, 8, 2, 11, 9};
int n = sizeof(arr) / sizeof(arr[0]);
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
std::cout << "The largest element in the array is: " << max << std::endl;
return 0;
}
The output will be: The largest element in the array is: 11
Q3: How do you reverse a string in C++?
A3: You can reverse a string in C++ by swapping the characters from the start and end
positions of the string until you reach the middle. Here's an example:
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
int start = 0;
int end = str.length() - 1;
while (start < end) {
std::swap(str[start], str[end]);
start++;
end--;
}
std::cout << "Reversed string: " << str << std::endl;
return 0;
}
The output will be: Reversed string: !dlroW ,olleH

cpp promo ppt.pptx

  • 2.
    Q1: How doyou swap the values of two variables in C++? A1: To swap the values of two variables in C++, you can use a temporary variable to hold one of the values temporarily. Here's an example: #include <iostream> int main() { int a = 5; int b = 10; int temp = a; a = b; b = temp; std::cout << "After swapping, a = " << a << " and b = " << b << std::endl; return 0; } After swapping, a = 10 and b = 5 The output will be:
  • 3.
    Q2: How canyou find the largest element in an array using C++? A2: You can find the largest element in an array by iterating through each element and comparing it with the current maximum value. Here's an example: #include <iostream> int main() { int arr[] = {5, 8, 2, 11, 9}; int n = sizeof(arr) / sizeof(arr[0]); int max = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) { max = arr[i]; } } std::cout << "The largest element in the array is: " << max << std::endl; return 0; } The output will be: The largest element in the array is: 11
  • 4.
    Q3: How doyou reverse a string in C++? A3: You can reverse a string in C++ by swapping the characters from the start and end positions of the string until you reach the middle. Here's an example: #include <iostream> #include <string> int main() { std::string str = "Hello, World!"; int start = 0; int end = str.length() - 1; while (start < end) { std::swap(str[start], str[end]); start++; end--; } std::cout << "Reversed string: " << str << std::endl; return 0; } The output will be: Reversed string: !dlroW ,olleH