C++ Programming
C++ Programming
C++ Programming
#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
cout << "Enter a non-negative integer: ";
cin >> num;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
cin >> str;
if (isPalindrome(str))
cout << str << " is a palindrome" << endl;
else
cout << str << " is not a palindrome" << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
cin >> str;
cout << "Reversed string: " << reverseString(str) << endl;
return 0;
}
4. Fibonacci Series: Write a C++ function to print the Fibonacci series up to a given
number of terms.
#include <iostream>
using namespace std;
void fibonacci(int n) {
int prev = 0, curr = 1, next;
cout << "Fibonacci Series up to " << n << " terms: ";
for (int i = 0; i < n; i++) {
cout << prev << " ";
next = prev + curr;
prev = curr;
curr = next;
}
cout << endl;
}
int main() {
int numTerms;
cout << "Enter the number of terms: ";
cin >> numTerms;
fibonacci(numTerms);
return 0;
}
5. Sum of Array Elements: Write a C++ function to find the sum of all elements in an
array.
#include <iostream>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
cout << "Sum of array elements: " << arraySum(arr, size) << endl;
return 0;
}
6. Check Prime Number: Write a C++ function 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 a number: ";
cin >> num;
if (isPrime(num))
cout << num << " is a prime number" << endl;
else
cout << num << " is not a prime number" << endl;
return 0;
}
7. Count Vowels and Consonants: Write a C++ function to count the number of
vowels and consonants in a given string.
#include <iostream>
#include <cctype>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
countVowelsConsonants(str);
return 0;
}
8. Find Maximum and Minimum Element in an Array: Write a C++ function to find
the maximum and minimum elements in an array.
#include <iostream>
#include <climits>
using namespace std;
int main() {
int arr[] = {3, 1, 8, 4, 9, 2};
int size = sizeof(arr) / sizeof(arr[0]);
findMaxMin(arr, size);
return 0;
}
9. Check Armstrong Number: Write a C++ function to check whether a given number
is an Armstrong number or not.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isArmstrong(num))
cout << num << " is an Armstrong number" << endl;
else
cout << num << " is not an Armstrong number" << endl;
return 0;
}
10. Binary Search: Write a C++ function to perform binary search on a sorted array.
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 3, 5, 7, 9, 11};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 7;
int index = binarySearch(arr, size, target);
if (index != -1)
cout << "Element found at index " << index << endl;
else
cout << "Element not found" << endl;
return 0;