Some Programs In C++
- Simple Program To Swap Two Numbers
- Program To Swap Three Variables
- Program To Print Sum of Digits of Number
- Program To Find Number of Vowels In A Given String
- Program To Find The Smallest Number In a Given Array
- Program To Search A Number in an Array
- Program To Find The Largest Number And its Position In a Given Array
- Program To Find Sum of Two Matrices
- Program To Find Greatest of Two Numbers
Program To Swap Two Numbers
Program asked in February 2022 (CBCS) question paper.
Output:
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
Output:
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.
Output:
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.
Output:
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.
Output:
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.
Output:
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.
Output:
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.
Output:
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 outer loop
- 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.
Output:
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.
Last updated on -