1. • Used to execute a set of operations multiple times, with one loop
inside another.
• It allows for iterating over multidimensional data structures,
among other use cases.
• The outer loop completes one full iteration for each single
iteration of the inner loop, creating a loop within a loop.
Nested Loops
2. for (initialization; condition; increment) {
// Outer loop body
for (initialization; condition; increment) {
// Inner loop body
// Code to be executed for each iteration of the
inner loop
}
// Additional code can be executed in the outer
loop
}
Structure of Nested Loops
3. #include <iostream>
using namespace std;
int main() {
for(int i = 1; i <= 10; i++) { // Outer loop
for(int j = 1; j <= 10; j++) { // Inner loop
cout << i*j << "t"; // Multiply i and j, separated by a
tab
}
cout << endl;
}
return 0;
}
Multiplication table up to 10 using nested loops.
4. #include <iostream>
using namespace std;
int main() {
for(int i = 0; i < 5; i++) { // Outer loop for rows
for(int j = 0; j < 5; j++) { // Inner loop for columns
cout << "* ";
}
cout << endl; // Move to the next line after each row
}
return 0;
}
Drawing a square pattern
5. • Arrays in C++ provide a way to store a collection of variables of the
same type.
• An array can be declared by specifying the type of its elements,
followed by the array name and the number of elements it will hold
inside square brackets.
int numbers [5]
Arrays
6.
7. #include <iostream>
using namespace std;
int main() {
// Declare and initialize an array of integers with 5 elements
int numbers[5] = {10, 20, 30, 40, 50};
// Access and print each element of the array
for(int i = 0; i < 5; i++) {
cout << "Element at index " << i << ": " << numbers[i] << endl;
}
return 0;
}
An integer array named numbers with 5 elements
8. #include <iostream>
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
int sum = 0;
// Calculate sum of array elements
for(int i = 0; i < 5; i++) {
sum += numbers[i];
}
cout << "Sum of array elements: " << sum << endl;
return 0;
}
Calculating the sum of Array elements
9. #include <iostream>
using namespace std;
int main() {
int matrix[2][2] = {
{1, 2}, // First row
{3, 4} // Second row
};
// Access and print each element of the matrix
// Outer loop for rows
for(int i = 0; i < 2; i++) {
2x2 matrix using array
11. #include <iostream>
using namespace std;
int main() {
// Declare and initialize two 2x2 matrices
int matrixA[2][2] = {{1, 2}, {3, 4}};
int matrixB[2][2] = {{5, 6}, {7, 8}};
int sumMatrix[2][2]; // To store the sum of the matrices
// Calculate the sum of the matrices
for(int i = 0; i < 2; i++) { // Loop over rows
for(int j = 0; j < 2; j++) { // Loop over columns
Calculating the sum of matrices
12. sumMatrix[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
cout << "Sum of Matrix A and Matrix B is:" << endl;
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
cout << sumMatrix[i][j] << " ";
}
cout << endl; // New line for each row
}
return 0;
}
Calculating the sum of matrices (continued)
13. Program to Input and Display Array Elements
#include <iostream>
using namespace std;
int main() {
int arr[5]; // Declaring an array of size 5
// Input array elements
cout << "Enter 5 integers:" << endl;
for(int i = 0; i < 5; i++) {
cin >> arr[i];
14. Program to Input and Display Array Elements (contnd)
}
// Display array elements
cout << "Array elements are:" << endl;
for(int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
return 0;
}
15. Program to Calculate the Sum and Average of Array Elements
#include <iostream>
using namespace std;
int main() {
int n, sum 0;
float average;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements:" << endl;
16. contnd
for(int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i]; // Adding elements to sum
}
average = sum / n; // Calculating average
cout << "Sum of array elements is: " << sum << endl;
cout << "Average of array elements is: " << average << endl;
return 0;
}
17. Program to Find the Largest Element in an Array
#include <iostream>
using namespace std;
int main() {
int n, max;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array:" << endl;
for(int i = 0; i < n; i++) {
18. contnd
cin >> arr[i];
}
max = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] > max)
max = arr[i];
}
cout << “Largest element in the array is: " << max << endl;
return 0;
}
19. Inputs Elements Of A Matrix Then Displays In Matrix Format
#include <iostream>
using namespace std;
int main() {
int rows, cols;
// Prompt user for the dimensions of the matrix
cout << "Enter number of rows: ";
cin >> rows;
cout << "Enter number of columns: ";
cin >> cols;
20. contnd
// Create a 2D array (matrix) based on the input dimensions
int matrix[rows][cols];
cout << "Enter elements of the matrix:" << endl;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
cin >> matrix[i][j];
}
}
21. contnd
// Display the matrix in matrix format
cout << "The matrix is:" << endl;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
cout << matrix[i][j] << " ";
}
cout << endl; // Move to the next line after printing each
row
}
return 0;
}
22. Sum of Matrix
#include <iostream>
using namespace std;
int main() {
int rows, cols;
// Prompt for matrix size
cout << "Enter the number of rows and columns for the
matrices: ";
cin >> rows >> cols;
int matrix1[rows][cols], matrix2[rows][cols],
sum[rows][cols];
// Input elements for the first matrix
23. Sum of Matrix (contnd)
cout << "Enter elements of the first matrix:" << endl;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
cin >> matrix1[i][j];
}
}
// Input elements for the second matrix
cout << "Enter elements of the second matrix:" << endl;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
cin >> matrix2[i][j];
}
}
24. Sum of Matrix (contnd)
// Calculate the sum of the 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];
}
}
// Display the resulting matrix
cout << "Sum of the two matrices:" << endl;
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
cout << sum[i][j] << " ";
} cout << endl;
}
return 0;
}
25. Functions
• Functions are blocks of code designed to perform a specific task.
• Used to structure programs into segments.
Declaration and Definition
• Declaration: Tells the compiler about a function's name, return type,
and parameters (if any). It's also known as a function prototype.
• Definition: Contains the actual body of the function, detailing the
statements that execute when the function is called.
27. Syntax
• 'return_type ': The data type of the value the function returns.
'function_name': The name of the function, following the rules for identifiers.
• 'parameter list': A comma-separated list of input parameters that are passed
into the function, each specified with a type and name. If the function takes no
parameters, this can be left empty or explicitly defined with 'void'.
28. Add function
#include <iostream>
using namespace std;
// Function declaration
int add(int, int);
int main() {
int result;
// Function call
result = add(5, 3);
cout << "The sum is: " << result << endl;
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
29. Find the Maximum of Two Numbers
#include <iostream>
using namespace std;
// Function to find the maximum of two numbers
int max(int num1, int num2) {
if (num1 > num2)
return num1;
else
return num2;
}
int main() {
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;
cout << "The maximum is: " << max(a, b);
return 0;
}
30. #include <iostream>
using namespace std;
float PI = 3.14159; // Use float for PI
// Function to calculate the area of a circle
float calculateArea(float radius) {
return PI * radius * radius;
}
Area of a Circle
31. int main() {
float radius;
cout << "Enter the radius of the circle: ";
cin >> radius;
float area = calculateArea(radius); // Change type to float
cout << "The area of the circle is: " << area;
return 0;
}
Area of a Circle (contd)
32. #include <iostream>
using namespace std;
// Function to check even or odd
int isEven(int number) {
return (number % 2 == 0); // This still returns 1 (true) for even and 0 (false) for
odd
}
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
Number is even or odd
33. if (isEven(num))
cout << num << " is even.";
else
cout << num << " is odd.";
return 0;
}
Number is even or odd (contd)
34. cin >> num;
if (num < 0) {
cout << "Factorial of a negative number doesn't exist.";
} else {
long long result = factorial(num);
cout << "The factorial of " << num << " is: " << result << endl;
}
return 0;
}
Factorial
35. #include <iostream>
using namespace std;
// Function to add two numbers
int add(int num1, int num2) {
return num1 + num2;
}
int main() {
int a, b;
cout << "Enter first number: ";
cin >> a;
cout << "Enter second number: ";
Add function
36. cin >> b;
int sum = add(a, b); // Call the add function
cout << "The sum is: " << sum << endl;
return 0;
}
Add function (contnd)
37. #include <iostream>
using namespace std;
// Function to check if a number is even or odd
int isEven(int num) {
return (num % 2 == 0);
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isEven(num)) {
Check Even or Odd Using a Function
38. cout << num << " is even." << endl;
} else {
cout << num << " is odd." << endl;
}
return 0;
}
Check Even or Odd Using a Function (contnd)