Basic C++ Programs
1) Count number of even and odd elements in an array
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5, 6};
int n = 6, 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;
}
/* Output:
Even: 3, Odd: 3
*/
2) Average numbers in array
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = 5, sum = 0;
for(int i=0; i<n; i++) sum += arr[i];
cout << "Average = " << (float)sum/n;
return 0;
}
/* Output:
Average = 30
*/
3) Program to print the given digit in words
#include <iostream>
using namespace std;
int main() {
int digit;
cout << "Enter a digit: ";
cin >> digit;
string words[] = {"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
if(digit >= 0 && digit <= 9) cout << words[digit];
else cout << "Not a single digit";
return 0;
}
/* Example Input: 5
Output: Five
*/
4) Check if a large number is divisible by 6 or not
#include <iostream>
using namespace std;
int main() {
string num = "123456";
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;
}
/* Output:
Divisible by 6
*/
5) Check if a number is Palindrome
#include <iostream>
using namespace std;
int main() {
int n = 121, rev = 0, temp = n;
while(temp) {
rev = rev*10 + temp%10;
temp /= 10;
}
if(rev == n) cout << "Palindrome";
else cout << "Not Palindrome";
return 0;
}
/* Output:
Palindrome
*/
6) Program to count vowels in a string
#include <iostream>
using namespace std;
int main() {
string str = "Hello World";
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;
}
/* Output:
Vowels: 3
*/
7) Check if given number is perfect square
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n = 49;
int root = sqrt(n);
if(root*root == n) cout << "Perfect Square";
else cout << "Not Perfect Square";
return 0;
}
/* Output:
Perfect Square
*/
8) Program to find the maximum element in a Matrix
#include <iostream>
using namespace std;
int main() {
int mat[2][3] = {{1,2,3},{7,5,4}};
int maxVal = mat[0][0];
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
if(mat[i][j] > maxVal) maxVal = mat[i][j];
}
}
cout << "Max: " << maxVal;
return 0;
}
/* Output:
Max: 7
*/
9) Program for Sum of the digits of a given number
#include <iostream>
using namespace std;
int main() {
int n = 1234, sum = 0;
while(n) {
sum += n % 10;
n /= 10;
}
cout << "Sum of digits: " << sum;
return 0;
}
/* Output:
Sum of digits: 10
*/
10) Sum of square-sums of first n natural numbers
#include <iostream>
using namespace std;
int main() {
int n = 5, sum = 0;
for(int i=1; i<=n; i++) sum += i*i;
cout << "Sum = " << sum;
return 0;
}
/* Output:
Sum = 55
*/
11) Program for Armstrong Numbers
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n = 153, sum = 0, temp = n;
int digits = to_string(n).size();
while(temp) {
sum += pow(temp%10, digits);
temp /= 10;
}
if(sum == n) cout << "Armstrong Number";
else cout << "Not Armstrong";
return 0;
}
/* Output:
Armstrong Number
*/
12) Program for factorial of a number
#include <iostream>
using namespace std;
int main() {
int n = 5, fact = 1;
for(int i=1; i<=n; i++) fact *= i;
cout << "Factorial = " << fact;
return 0;
}
/* Output:
Factorial = 120
*/
13) Program to check if an array is sorted or not
#include <iostream>
using namespace std;
int main() {
int arr[] = {1,2,3,4,5};
int n = 5, sorted = 1;
for(int i=0; i<n-1; i++) {
if(arr[i] > arr[i+1]) { sorted = 0; break; }
}
if(sorted) cout << "Sorted";
else cout << "Not Sorted";
return 0;
}
/* Output:
Sorted
*/
14) Program to print multiplication table of a number
#include <iostream>
using namespace std;
int main() {
int n = 5;
for(int i=1; i<=10; i++) cout << n << " x " << i << " = " << n*i << endl;
return 0;
}
/* Output:
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
*/
15) Program to Prime number
#include <iostream>
using namespace std;
int main() {
int n = 29, flag = 1;
for(int i=2; i*i<=n; i++) {
if(n % i == 0) { flag = 0; break; }
}
if(flag && n>1) cout << "Prime";
else cout << "Not Prime";
return 0;
}
/* Output:
Prime
*/
16) Write a program to reverse digits of a number
#include <iostream>
using namespace std;
int main() {
int n = 12345, rev = 0;
while(n) {
rev = rev*10 + n%10;
n /= 10;
}
cout << "Reversed: " << rev;
return 0;
}
/* Output:
Reversed: 54321
*/
17) Find Second largest element in an array
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 4, 45, 99};
int n = 5, first = -1, second = -1;
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 << "Second Largest: " << second;
return 0;
}
/* Output:
Second Largest: 45
*/
18) Display the Longest Name
#include <iostream>
using namespace std;
int main() {
string names[] = {"Ravi", "Shyam", "Alexander", "John"};
int n = 4, maxLen = 0, idx = 0;
for(int i=0; i<n; i++) {
if(names[i].size() > maxLen) {
maxLen = names[i].size();
idx = i;
}
}
cout << "Longest Name: " << names[idx];
return 0;
}
/* Output:
Longest Name: Alexander
*/