C++ Basic Programs – Notes with Explanation
These notes cover fundamental C++ programs commonly asked in exams and practicals. Each
program is explained step by step for easy understanding.
1. Program to Find Sum of Digits of a Number
Explanation:
• The program takes an integer input from the user.
• Using a while loop, each digit is extracted using the modulus operator (% 10).
• The extracted digit is added to the variable 'sum'.
• The number is reduced by dividing it by 10 in each iteration.
• The loop continues until the number becomes zero.
Program:
#include <iostream> using namespace std; int main() { int n, sum = 0; cout << "Enter a number:
"; cin >> n; while(n != 0) { sum = sum + n % 10; n = n / 10; } cout << "Sum of digits = " <<
sum; return 0; }
2. Program to Find Sum of Array Elements
Explanation:
• An integer array of size 5 is declared.
• A for loop is used to take input for all elements of the array.
• Each element is added to the variable 'sum'.
• After the loop ends, the total sum is displayed.
Program:
#include <iostream> using namespace std; int main() { int a[5], sum = 0; cout << "Enter 5
elements:\n"; for(int i = 0; i < 5; i++) { cin >> a[i]; sum += a[i]; } cout << "Sum = " <<
sum; return 0; }
3. Program to Find Largest Element in an Array
Explanation:
• An array of size 5 is used to store elements.
• The first element of the array is assumed as the maximum.
• The remaining elements are compared one by one with the current maximum.
• If a larger value is found, it replaces the maximum value.
• Finally, the largest element is printed.
Program:
#include <iostream> using namespace std; int main() { int a[5], max; cout << "Enter 5
elements:\n"; for(int i = 0; i < 5; i++) cin >> a[i]; max = a[0]; for(int i = 1; i < 5; i++) {
if(a[i] > max) max = a[i]; } cout << "Largest element = " << max; return 0; }
4. Program to Count Even and Odd Numbers in an Array
Explanation:
• An array of 5 integers is taken as input.
• Two variables 'even' and 'odd' are initialized to zero.
• Each element is checked using the modulus operator (% 2).
• If the remainder is zero, the number is even; otherwise, it is odd.
• The final count of even and odd numbers is displayed.
Program:
#include <iostream> using namespace std; int main() { int a[5], even = 0, odd = 0; cout <<
"Enter 5 numbers:\n"; for(int i = 0; i < 5; i++) { cin >> a[i]; if(a[i] % 2 == 0) even++; else
odd++; } cout << "Even = " << even << "\nOdd = " << odd; return 0; }
5. Program to Reverse an Array
Explanation:
• The program stores 5 elements in an array.
• A for loop is used to print the array in reverse order.
• The loop starts from the last index and moves towards the first index.
• This does not change the array, it only displays it in reverse.
Program:
#include <iostream> using namespace std; int main() { int a[5]; cout << "Enter 5
elements:\n"; for(int i = 0; i < 5; i++) cin >> a[i]; cout << "Reversed array:\n"; for(int i =
4; i >= 0; i--) cout << a[i] << " "; return 0; }
6. Program Using Function to Add Two Numbers
Explanation:
• A user-defined function 'add()' is created to add two integers.
• The function takes two parameters and returns their sum.
• In the main function, two numbers are taken as input.
• The add() function is called and the result is displayed.
Program:
#include <iostream> using namespace std; int add(int a, int b) { return a + b; } int main() {
int x, y; cout << "Enter two numbers: "; cin >> x >> y; cout << "Sum = " << add(x, y); return
0; }