Dear friends, lets explore some C++ programs examples.
C++ Programs Examples 1 : To input and print two dimensional (2D) integer array
#include <iostream>
using namespace std;
int main() {
// Step 1: Define the dimensions of the 2D array
int rows, cols;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
// Step 2: Declare and initialize a 2D array
int array_2d[rows][cols];
// Step 3: Input values for the 2D array
cout << "Enter the elements of the 2D array:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << "Enter element at row " << i + 1 << ", column " << j + 1 << ": ";
cin >> array_2d[i][j];
}
}
// Step 4: Print the 2D array
cout << "The 2D array is:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << array_2d[i][j] << " "; // Print each element with a space
}
cout << endl; // Move to the next row
}
// Step 5: Print the 2D array as a grid
cout << "The 2D array as a grid:" << endl;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << array_2d[i][j] << "\t"; // Print each element with a tab
}
cout << endl; // Move to the next row
}
return 0;
}
Output:
Enter the number of rows: 3
Enter the number of columns: 4
Enter the elements of the 2D array:
Enter element at row 1, column 1: 1
Enter element at row 1, column 2: 2
Enter element at row 1, column 3: 3
Enter element at row 1, column 4: 4
Enter element at row 2, column 1: 5
Enter element at row 2, column 2: 6
Enter element at row 2, column 3: 7
Enter element at row 2, column 4: 8
Enter element at row 3, column 1: 9
Enter element at row 3, column 2: 10
Enter element at row 3, column 3: 11
Enter element at row 3, column 4: 12
The 2D array is:
1 2 3 4
5 6 7 8
9 10 11 12
The 2D array as a grid:
1 2 3 4
5 6 7 8
9 10 11 12
C++ Programs Examples 2 : Program to input a two dimensional array and print its transpose
#include <iostream>
int main() {
int row, col;
// Input the number of rows and columns
std::cout << "Enter the number of rows: ";
std::cin >> row;
std::cout << "Enter the number of columns: ";
std::cin >> col;
int originalArray[row][col];
int transposedArray[col][row];
// Input the elements of the original array
std::cout << "Enter the elements of the original array:" << std::endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
std::cout << "Enter element at position [" << i << "][" << j << "]: ";
std::cin >> originalArray[i][j];
}
}
// Transpose the original array
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
transposedArray[j][i] = originalArray[i][j];
}
}
// Print the original array
std::cout << "Original Array:" << std::endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
std::cout << originalArray[i][j] << " ";
}
std::cout << std::endl;
}
// Print the transposed array
std::cout << "Transposed Array:" << std::endl;
for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
std::cout << transposedArray[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
Output:
Enter the number of rows: 3
Enter the number of columns: 4
Enter the elements of the original array:
Enter element at position [0][0]: 1
Enter element at position [0][1]: 2
Enter element at position [0][2]: 3
Enter element at position [0][3]: 4
Enter element at position [1][0]: 5
Enter element at position [1][1]: 6
Enter element at position [1][2]: 7
Enter element at position [1][3]: 8
Enter element at position [2][0]: 9
Enter element at position [2][1]: 10
Enter element at position [2][2]: 11
Enter element at position [2][3]: 12
Original Array:
1 2 3 4
5 6 7 8
9 10 11 12
Transposed Array:
1 5 9
2 6 10
3 7 11
4 8 12
C++ Programs Examples 3 : Program to input two 2D arrays and find product (multiplication) of arrays
#include <iostream>
int main() {
int row1, col1, row2, col2;
// Input dimensions for the first matrix
std::cout << "Enter the number of rows for the first matrix: ";
std::cin >> row1;
std::cout << "Enter the number of columns for the first matrix: ";
std::cin >> col1;
// Input dimensions for the second matrix
std::cout << "Enter the number of rows for the second matrix: ";
std::cin >> row2;
std::cout << "Enter the number of columns for the second matrix: ";
std::cin >> col2;
// Check if matrix multiplication is possible
if (col1 != row2) {
std::cout << "Matrix multiplication is not possible. Column of the first matrix must be equal to the row of the second matrix." << std::endl;
return 0;
}
int matrix1[row1][col1];
int matrix2[row2][col2];
int product[row1][col2];
// Input elements of the first matrix
std::cout << "Enter the elements of the first matrix:" << std::endl;
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
std::cout << "Enter element at position [" << i << "][" << j << "]: ";
std::cin >> matrix1[i][j];
}
}
// Input elements of the second matrix
std::cout << "Enter the elements of the second matrix:" << std::endl;
for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
std::cout << "Enter element at position [" << i << "][" << j << "]: ";
std::cin >> matrix2[i][j];
}
}
// Perform matrix multiplication
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
product[i][j] = 0;
for (int k = 0; k < col1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
// Display the product of matrices
std::cout << "Product of the matrices:" << std::endl;
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
std::cout << product[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
Output:
Enter the number of rows for the first matrix: 2
Enter the number of columns for the first matrix: 2
Enter the number of rows for the second matrix: 2
Enter the number of columns for the second matrix: 2
Enter the elements of the first matrix:
Enter element at position [0][0]: 1
Enter element at position [0][1]: 2
Enter element at position [1][0]: 3
Enter element at position [1][1]: 4
Enter the elements of the second matrix:
Enter element at position [0][0]: 5
Enter element at position [0][1]: 6
Enter element at position [1][0]: 7
Enter element at position [1][1]: 8
Product of the matrices:
19 22
43 50
C++ Programs Examples 4 : Program to print name of months using string array
#include <iostream>
#include <string>
int main() {
// Define an array of strings to store the names of months
std::string months[] = {
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
// Loop through the array and print the names of months
for (int i = 0; i < 12; i++) {
std::cout << "Month " << (i + 1) << ": " << months[i] << std::endl;
}
return 0;
}
Output:
Month 1: January
Month 2: February
Month 3: March
Month 4: April
Month 5: May
Month 6: June
Month 7: July
Month 8: August
Month 9: September
Month 10: October
Month 11: November
Month 12: December
C++ Programs Examples 5 : Program to count number of characters in string without using string function
#include <iostream>
int main() {
// Initialize a string
char inputString[100];
// Ask the user to enter a string
std::cout << "Enter a string: ";
std::cin.getline(inputString, 100); // Read the string with a maximum length of 100
int count = 0; // Initialize a counter for characters
// Loop through the characters in the string until a null character is encountered
for (int i = 0; inputString[i] != '\0'; i++) {
count++;
}
// Display the count of characters in the string
std::cout << "Number of characters in the string: " << count << std::endl;
return 0;
}
Output:
Enter a string: Hello, World!
Number of characters in the string: 13
C++ Programs Examples 6 : Program to copy a string to another string without using string function
#include <iostream>
int main() {
// Initialize two character arrays to store the strings
char sourceString[100];
char destinationString[100];
// Ask the user to enter a string
std::cout << "Enter a string: ";
std::cin.getline(sourceString, 100); // Read the string with a maximum length of 100
// Copy the source string to the destination string character by character
int i;
for (i = 0; sourceString[i] != '\0'; i++) {
destinationString[i] = sourceString[i];
}
// Add a null character at the end of the destination string to terminate it
destinationString[i] = '\0';
// Display the copied string
std::cout << "Copied string: " << destinationString << std::endl;
return 0;
}
Output:
Enter a string: Hello, World!
Copied string: Hello, World!
C++ Programs Examples 7 : Program to convert string to lowercase and uppercase without using string function
#include <iostream>
int main() {
// Initialize a character array to store the string
char inputString[100];
// Ask the user to enter a string
std::cout << "Enter a string: ";
std::cin.getline(inputString, 100); // Read the string with a maximum length of 100
// Convert the string to lowercase
for (int i = 0; inputString[i] != '\0'; i++) {
if (inputString[i] >= 'A' && inputString[i] <= 'Z') {
inputString[i] += 32; // Convert uppercase to lowercase by adding 32 to the ASCII value
}
}
// Display the lowercase string
std::cout << "Lowercase: " << inputString << std::endl;
// Convert the string to uppercase
for (int i = 0; inputString[i] != '\0'; i++) {
if (inputString[i] >= 'a' && inputString[i] <= 'z') {
inputString[i] -= 32; // Convert lowercase to uppercase by subtracting 32 from the ASCII value
}
}
// Display the uppercase string
std::cout << "Uppercase: " << inputString << std::endl;
return 0;
}
Output:
Enter a string: Hello, World!
Lowercase: hello, world!
Uppercase: HELLO, WORLD!
C++ Programs Examples 8 : Program to compare two strings without using string function
#include <iostream>
int main() {
// Initialize character arrays to store the strings
char string1[100];
char string2[100];
// Ask the user to enter the first string
std::cout << "Enter the first string: ";
std::cin.getline(string1, 100); // Read the string with a maximum length of 100
// Ask the user to enter the second string
std::cout << "Enter the second string: ";
std::cin.getline(string2, 100); // Read the string with a maximum length of 100
// Compare the strings character by character
int i = 0;
while (string1[i] != '\0' || string2[i] != '\0') {
if (string1[i] != string2[i]) {
// If any characters don't match, the strings are not equal
std::cout << "The strings are not equal." << std::endl;
return 0;
}
i++;
}
// If we reach this point, the strings are equal
std::cout << "The strings are equal." << std::endl;
return 0;
}
Output:
Enter the first string: Hello, World!
Enter the second string: Hello, World!
The strings are equal.
Enter the first string: Hello, World!
Enter the second string: Goodbye, World!
The strings are not equal.