Some Programs In C++

  1. Simple Program To Swap Two Numbers
  2. Program To Swap Three Variables
  3. Program To Print Sum of Digits of Number
  4. Program To Find Number of Vowels In A Given String
  5. Program To Find The Smallest Number In a Given Array
  6. Program To Search A Number in an Array
  7. Program To Find The Largest Number And its Position In a Given Array
  8. Program To Find Sum of Two Matrices
  9. Program To Find Greatest of Two Numbers

Program To Swap Two Numbers

Program asked in February 2022 (CBCS) question paper.

#include <iostream>
using namespace std;
 
int main() {
    int num1 = 7, num2 = 5, temp;
 
    cout << "Before swapping:" << endl;
    cout << "num1 = " << num1 << endl;
    cout << "num2 = " << num2 << endl;
 
    // Swapping logic
    temp = num1;
    num1 = num2;
    num2 = temp;
 
    cout << "\nAfter swapping:" << endl;
    cout << "num1 = " << num1 << endl;
    cout << "num2 = " << num2 << endl;
 
    return 0;
}

Output:

Before swapping:
num1 = 7
num2 = 5
 
After swapping:
num1 = 5
num2 = 7

Explanation:

  • Before swapping, num1 is initialized to 7 and num2 to 5.
  • The program displays the values of num1 and num2 before swapping.
  • After swapping, the value of num1 becomes 5 (originally num2) and the value of num2 becomes 7 (originally num1).
  • The program then displays the values of num1 and num2 after swapping to confirm that they have been successfully swapped.

Program To Swap Three Variables

#include <iostream>
using namespace std;
 
int main() {
    // Declare variables to store the values of a, b, and c
    int a, b, c;
 
    // Prompt the user to enter the values of a, b, and c
    cout << "Enter the value of a - ";
    cin >> a;
    cout << "Enter the value of b - ";
    cin >> b;
    cout << "Enter the value of c - ";
    cin >> c;
    
    cout<<endl;
 
    // Output the values before swapping
    cout << "Before swapping -" << endl;
    cout << "a = " << a << ", b = " << b << ", c = " << c << endl;
 
    // Swap the values of a, b, and c
    int temp = a;
    a = c;
    c = b;
    b = temp;
    
    cout<<endl;
 
    // Output the values after swapping
    cout << "After swapping -" << endl;
    cout << "a = " << a << ", b = " << b << ", c = " << c << endl;
 
    return 0;
}

Output:

Enter the value of a - 7
Enter the value of b - 5
Enter the value of c - 9
 
Before swapping -
a = 7, b = 5, c = 9
 
After swapping -
a = 9, b = 7, c = 5

Explanation:

  • The program asks the user to enter values for a, b, and c.
  • A temporary variable temp is created to hold the value of a.
    • a is assigned the value of c.
    • c is assigned the value of b.
    • Finally, b is assigned the value stored in temp (which was originally a).
  • The program displays the original values of a, b, and c.
  • Then, it displays the swapped values of a, b, and c

Program To Print Sum of Digits of Number

Program asked in July 2022 (CBCS) question paper.

#include <iostream>
using namespace std;
 
int main() {
    int number, sum = 0, digit;
    
    // Input number from user
    cout << "Enter a positive integer: ";
    cin >> number;
 
    // Iterate through each digit of the number
    while (number > 0) {
        // Extract the last digit
        digit = number % 10;
        
        // Add the digit to the sum
        sum += digit;
        
        // Remove the last digit from the number
        number /= 10;
    }
 
    // Print the sum of digits
    cout << "Sum of digits: " << sum << endl;
 
    return 0;
}

Output:

Enter a positive integer: 7559
Sum of digits: 26

Explanation:

This program first prompts the user to enter a positive integer. Then, it iterates through each digit of the number by continuously dividing it by 10 and extracting the remainder. The remainder (i.e., the last digit) is added to the sum variable. Finally, the sum of digits is displayed to the user.

Program To Find Number of Vowels In A Given String

Program asked in July 2022 (CBCS) question paper.

#include <iostream>
using namespace std;
 
int main() {
    string str;
    int vowels = 0;
 
    // Input string from user
    cout << "Enter a string: ";
    getline(cin, str);
 
    // Iterate through each character of the string
    for (char ch : str) {
        // Convert character to lowercase
        ch = tolower(ch);
        
        // Check if character is a vowel
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            vowels++;
        }
    }
 
    // Print the number of vowels
    cout << "Number of vowels: " << vowels << endl;
 
    return 0;
}

Output:

Enter a string: Eniv
Number of vowels: 2

Explanation:

This program prompts the user to enter a string. Then, it iterates through each character of the string, converting each character to lowercase to simplify the comparison. If the character is a vowel ('a', 'e', 'i', 'o', 'u'), the count of vowels is incremented. Finally, it displays the total count of vowels in the string.

Program To Find The Smallest Number In a Given Array

Program asked in July 2022 (CBCS) question paper.

#include <iostream>
using namespace std;
 
int main() {
    // Array declaration and initialization
    int arr[] = {5, 8, 3, 1, 9, 2};
    int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
 
    // Initialize min with the first element of the array
    int min = arr[0];
 
    // Iterate through the array to find the smallest element
    for (int i = 1; i < size; ++i) {
        if (arr[i] < min) {
            min = arr[i];
        }
    }
 
    // Print the smallest number
    cout << "The smallest number in the array is: " << min << endl;
 
    return 0;
}

Output:

The smallest number in the array is: 1

Explanation:

  • We initialize an array arr with some values.
  • We calculate the size of the array using the formula sizeof(arr) / sizeof(arr[0]).
  • We initialize a variable min with the first element of the array.
  • We iterate through the array starting from the second element and compare each element with the current value of min. If the element is smaller than min, we update min to the value of that element.
  • Finally, we print the value of min, which represents the smallest number in the array.

Program To Search A Number in an Array

Program asked in July 2022 (CBCS) question paper.

#include <iostream>
using namespace std;
 
int main() {
    // Array declaration and initialization
    int arr[] = {5, 7, 4, 5, 9, 5};
    int size = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array
 
    int searchNumber;
    cout << "Enter the number you want to search - ";
    cin >> searchNumber;
 
    bool found = false;
 
    // Iterate through the array to search for the number
    for (int i = 0; i < size; ++i) {
        if (arr[i] == searchNumber) {
            found = true;
            break;
        }
    }
 
    // Check if the number was found
    if (found) {
        cout << "The number " << searchNumber << " is present in given array." << endl;
    } else {
        cout << "The number " << searchNumber << " was not found in the array." << endl;
    }
 
    return 0;
}

Output:

Enter the number you want to search - 4
The number 4 is present in given array.

Explanation:

  • The array arr is initialized with the values predefined values.
  • The program prompts the user to enter the number they want to search for.
  • The program iterates through the array using a for loop and checks if each element of the array is equal to the number entered by the user (searchNumber). If the number is found in the array, the found bool is set to true, and the loop breaks. If the number is not found, found bool remains false after the loop.
  • If found bool is true, it means the number was found in the array, so the program prints: "The number [searchNumber] is present in the given array." If found bool is false, it means the number was not found in the array, so the program prints: "The number [searchNumber] was not found in the array."

Program To Find The Largest Number And its Position In a Given Array

Program asked in December 2022 (CBCS) question paper.

#include <iostream>
using namespace std;
 
int main() {
    int arr[] = {5, 7, 2, 8, 6, 10};
    int size = sizeof(arr) / sizeof(arr[0]);
 
    int maxNum = arr[0]; // Initialize maxNum with the first element of the array
    int position = 0; // Initialize position with 0
 
    // Traverse the array to find the largest number and its position
    for (int i = 1; i < size; ++i) {
        if (arr[i] > maxNum) {
            maxNum = arr[i];
            position = i;
        }
    }
 
    cout << "The largest number in the array is - " << maxNum << endl;
    cout << "Its position in the array is - " << position << endl;
 
    return 0;
}

Output:

The largest number in the array is - 10
Its position in the array is - 5

Explanation:

This program initializes maxNum with the first element of the array and position with 0. Then, it traverses the array to compare each element with maxNum. If an element is greater than maxNum, it updates maxNum with the new maximum value and updates position with the index of that element. Finally, it prints the largest number and its position in the array.

Program To Find Sum of Two Matrices

Program asked in December 2022 (CBCS) question paper.

#include <iostream>
using namespace std;
 
int main() {
    int rows, cols;
 
    // Input the number of rows and columns for the matrices
    cout << "Enter the number of rows: ";
    cin >> rows;
    cout << "Enter the number of columns: ";
    cin >> cols;
 
    int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];
 
    // Input elements of first matrix
    cout << "Enter the elements of first matrix:" << endl;
    for(int i = 0; i < rows; ++i) {
        for(int j = 0; j < cols; ++j) {
            cout << "Enter element a" << i + 1 << j + 1 << " : ";
            cin >> matrix1[i][j];
        }
    }
    
     cout<<endl; 
     
    // Input elements of second matrix
    cout << "Enter the elements of second matrix:" << endl;
    for(int i = 0; i < rows; ++i) {
        for(int j = 0; j < cols; ++j) {
            cout << "Enter element b" << i + 1 << j + 1 << " : ";
            cin >> matrix2[i][j];
        }
    }
 
    // Adding corresponding elements of two matrices
    for(int i = 0; i < rows; ++i) {
        for(int j = 0; j < cols; ++j) {
            sum[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }
     
    cout<<endl; 
    
    // Displaying the sum matrix
    cout << "Sum of the matrices:" << endl;
    for(int i = 0; i < rows; ++i) {
        for(int j = 0; j < cols; ++j) {
            cout << sum[i][j] << "  ";
        }
        cout << endl;
    }
 
    return 0;
}

Output:

Enter the number of rows: 3
Enter the number of columns: 3
 
Enter the elements of first matrix:
Enter element a11 : 1
Enter element a12 : 2
Enter element a13 : 3
Enter element a21 : 4
Enter element a22 : 5
Enter element a23 : 6
Enter element a31 : 7
Enter element a32 : 8
Enter element a33 : 9
 
Enter the elements of second matrix:
Enter element b11 : 1
Enter element b12 : 2
Enter element b13 : 3
Enter element b21 : 4
Enter element b22 : 5
Enter element b23 : 6
Enter element b31 : 7
Enter element b32 : 8
Enter element b33 : 9
 
Sum of the matrices:
2  4  6  
8  10  12  
14  16  18 

Explanation:

  • The user inputs the number of rows and columns as 3.
  • The elements of the first matrix are entered row-wise (1, 2, 3, 4, 5, 6, 7, 8, 9).
  • Similarly, the elements of the second matrix are entered row-wise (1, 2, 3, 4, 5, 6, 7, 8, 9).
  • The nested loops calculates the sum of the corresponding elements of the two matrices.
    • The outer loop for(int i = 0; i < rows; ++i) iterates over each row of the matrices (i represents the row index).
    • The inner loop for(int j = 0; j < cols; ++j) iterates over each column within the current row (j represents the column index).
    • For each combination of i and j, the code accesses the corresponding elements from matrix1[i][j] and matrix2[i][j], adds them, and stores the result in sum[i][j]. This effectively calculates the sum of corresponding elements from the two input matrices.
  • The sum matrix is then displayed, which is the result of adding the elements of the first and second matrices.

Program To Find Greatest of Two Numbers

Program asked in July 2022 (CBCS) question paper.

#include <iostream>
using namespace std;
 
int main() {
    // Declare variables to store the numbers
    int num1, num2;
 
    // Prompt the user to enter the first number
    cout << "Enter the first number - ";
    cin >> num1;
 
    // Prompt the user to enter the second number
    cout << "Enter the second number - ";
    cin >> num2;
 
    // Check which number is greater
    if (num1 > num2) {
        cout << "The greatest number is - " << num1 << endl;
    } else if (num2 > num1) {
        cout << "The greatest number is - " << num2 << endl;
    } else {
        cout << "Both numbers are equal." << endl;
    }
 
    return 0;
}

Output:

Enter the first number - 7
Enter the second number - 9
The greatest number is - 9

Explanation:

  • The program prompts the user to enter the first number, and the user inputs 7.
  • Then, the program prompts the user to enter the second number, and the user inputs 9.
  • The program compares the two numbers (7 and 9) and finds that 9 is greater than 7.
  • Finally, it prints the message "The greatest number is - 9", indicating that 9 is the greatest number among the two input numbers.
How's article quality?

Last updated on -

Page Contents