1) Count number of even and odd elements in an array
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter size of array: ";
cin >> n;
int arr[n];
cout << "Enter elements: ";
for(int i=0;i<n;i++) cin >> arr[i];
int even=0, odd=0;
for(int i=0;i<n;i++){
if(arr[i] % 2 == 0) even++;
else odd++;
}
cout << "Even: " << even << ", Odd: " << odd;
return 0;
}
2) Average numbers in array
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter size of array: ";
cin >> n;
int arr[n];
cout << "Enter elements: ";
for(int i=0;i<n;i++) cin >> arr[i];
double sum=0;
for(int i=0;i<n;i++) sum += arr[i];
cout << "Average = " << sum/n;
return 0;
}
3) Program to print the given digit in words
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a digit (0-9): ";
cin >> num;
string words[] = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
if(num>=0 && num<=9) cout << words[num];
else cout << "Invalid input";
return 0;
}
4) Check if a large number is divisible by 6 or not
#include <iostream>
using namespace std;
int main() {
string num;
cout << "Enter large number: ";
cin >> num;
int lastDigit = num[num.size()-1] - '0';
int sum=0;
for(char c: num) sum += (c-'0');
if(lastDigit % 2 == 0 && sum % 3 == 0)
cout << "Divisible by 6";
else
cout << "Not divisible by 6";
return 0;
}
5) Check if a number is Palindrome
#include <iostream>
using namespace std;
int main() {
int n, rev=0, temp;
cout << "Enter number: ";
cin >> n;
temp = n;
while(temp>0){
rev = rev*10 + temp%10;
temp /= 10;
}
if(rev == n) cout << "Palindrome";
else cout << "Not Palindrome";
return 0;
}
6) Program to count vowels in a string
#include <iostream>
using namespace std;
int main() {
string str;
cout << "Enter string: ";
getline(cin, str);
int count=0;
for(char c: str){
c = tolower(c);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u') count++;
}
cout << "Vowels = " << count;
return 0;
}
7) Check if given number is perfect square
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n;
cout << "Enter number: ";
cin >> n;
int root = sqrt(n);
if(root*root == n) cout << "Perfect Square";
else cout << "Not Perfect Square";
return 0;
}
8) Program to find the maximum element in a Matrix
#include <iostream>
using namespace std;
int main() {
int r,c;
cout << "Enter rows and cols: ";
cin >> r >> c;
int arr[r][c];
cout << "Enter elements: ";
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cin >> arr[i][j];
}
}
int maxVal = arr[0][0];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(arr[i][j] > maxVal) maxVal = arr[i][j];
}
}
cout << "Max element = " << maxVal;
return 0;
}
9) Program for Sum of the digits of a number
#include <iostream>
using namespace std;
int main() {
int n, sum=0;
cout << "Enter number: ";
cin >> n;
while(n>0){
sum += n%10;
n /= 10;
}
cout << "Sum of digits = " << sum;
return 0;
}