SlideShare a Scribd company logo
• 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
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
#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.
#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
• 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
C++ Nested loops, matrix and fuctions.pdf
#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
#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
#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
2x2 matrix using array (contnd)
// Inner loop for columns
for(int j = 0; j < 2; j++) {
cout << "Element at (" << i << ", " << j << "): " << matrix[i][j] << endl;
}
}
return 0;
}
#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
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)
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];
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;
}
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;
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;
}
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++) {
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;
}
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;
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];
}
}
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;
}
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
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];
}
}
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;
}
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.
Syntax
return_type function_name(parameter list) {
// function body
}
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'.
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;
}
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;
}
#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
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)
#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
if (isEven(num))
cout << num << " is even.";
else
cout << num << " is odd.";
return 0;
}
Number is even or odd (contd)
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
#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
cin >> b;
int sum = add(a, b); // Call the add function
cout << "The sum is: " << sum << endl;
return 0;
}
Add function (contnd)
#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
cout << num << " is even." << endl;
} else {
cout << num << " is odd." << endl;
}
return 0;
}
Check Even or Odd Using a Function (contnd)

More Related Content

Similar to C++ Nested loops, matrix and fuctions.pdf (20)

PPTX
Arrays_in_c++.pptx
MrMaster11
 
PPTX
C++ Programm.pptx
Åįjâž Ali
 
DOC
Oops lab manual2
Mouna Guru
 
PPT
Lecture#6 functions in c++
NUST Stuff
 
PDF
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
ssuserd6b1fd
 
PDF
Introduction to cpp (c++)
Arun Umrao
 
PPTX
Two dimensional arrays
Neeru Mittal
 
DOCX
Array assignment
Ahmad Kamal
 
PPTX
Array and string in C++_093547 analysis.pptx
JumanneChiyanda
 
PDF
C++ TUTORIAL 3
Farhan Ab Rahman
 
PPTX
Arrays
Neeru Mittal
 
DOCX
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Make Mannan
 
PPT
2DArrays.ppt
Nooryaseen9
 
PPTX
Data structure array
MajidHamidAli
 
PDF
C++ L04-Array+String
Mohammad Shaker
 
DOC
CBSE Class XII Comp sc practical file
Pranav Ghildiyal
 
PDF
Arrays and library functions
Swarup Boro
 
PPTX
Chapter1.pptx
WondimuBantihun1
 
PPT
Chap 6 c++
Venkateswarlu Vuggam
 
PDF
C++ TUTORIAL 5
Farhan Ab Rahman
 
Arrays_in_c++.pptx
MrMaster11
 
C++ Programm.pptx
Åįjâž Ali
 
Oops lab manual2
Mouna Guru
 
Lecture#6 functions in c++
NUST Stuff
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
ssuserd6b1fd
 
Introduction to cpp (c++)
Arun Umrao
 
Two dimensional arrays
Neeru Mittal
 
Array assignment
Ahmad Kamal
 
Array and string in C++_093547 analysis.pptx
JumanneChiyanda
 
C++ TUTORIAL 3
Farhan Ab Rahman
 
Arrays
Neeru Mittal
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Make Mannan
 
2DArrays.ppt
Nooryaseen9
 
Data structure array
MajidHamidAli
 
C++ L04-Array+String
Mohammad Shaker
 
CBSE Class XII Comp sc practical file
Pranav Ghildiyal
 
Arrays and library functions
Swarup Boro
 
Chapter1.pptx
WondimuBantihun1
 
C++ TUTORIAL 5
Farhan Ab Rahman
 

Recently uploaded (20)

PDF
Carbonate formation and fluctuating habitability on Mars
Sérgio Sacani
 
PDF
soil and environmental microbiology.pdf
Divyaprabha67
 
PPTX
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
DOCX
Paper - Suprasegmental Features (Makalah Presentasi)
Sahmiral Amri Rajagukguk
 
PPTX
Class12_Physics_Chapter2 electric potential and capacitance.pptx
mgmahati1234
 
PPTX
Neuroinflammation and microglial subtypes
KanakChaudhary10
 
PPTX
Bacillus thuringiensis.crops & golden rice
priyadharshini87125
 
PPTX
Microbiome_Engineering_Poster_Fixed.pptx
SupriyaPolisetty1
 
PPTX
Ghent University Global Campus: Overview
Ghent University Global Campus
 
PPTX
Basal_ganglia_Structure_Function_Importance
muralinath2
 
PDF
High-speedBouldersandtheDebrisFieldinDARTEjecta
Sérgio Sacani
 
PDF
Calcium in a supernova remnant as a fingerprint of a sub-Chandrasekhar-mass e...
Sérgio Sacani
 
PPTX
770043401-q1-Ppt-pe-and-Health-7-week-1-lesson-1.pptx
AizaRazonado
 
PDF
Unit-5 ppt.pdf unit 5 organic chemistry 3
visionshukla007
 
PDF
Rapid protoplanet formation in the outer Solar System recorded in a dunite fr...
Sérgio Sacani
 
PDF
Carbon-richDustInjectedintotheInterstellarMediumbyGalacticWCBinaries Survives...
Sérgio Sacani
 
PPTX
ION EXCHANGE CHROMATOGRAPHY NEW PPT (JA).pptx
adhagalejotshna
 
PPTX
Q1_Science 8_Week3-Day 1.pptx science lesson
AizaRazonado
 
PDF
Annual report 2024 - Inria - English version.pdf
Inria
 
PDF
Plankton and Fisheries Bovas Joel Notes.pdf
J. Bovas Joel BFSc
 
Carbonate formation and fluctuating habitability on Mars
Sérgio Sacani
 
soil and environmental microbiology.pdf
Divyaprabha67
 
Systamatic Acquired Resistence (SAR).pptx
giriprasanthmuthuraj
 
Paper - Suprasegmental Features (Makalah Presentasi)
Sahmiral Amri Rajagukguk
 
Class12_Physics_Chapter2 electric potential and capacitance.pptx
mgmahati1234
 
Neuroinflammation and microglial subtypes
KanakChaudhary10
 
Bacillus thuringiensis.crops & golden rice
priyadharshini87125
 
Microbiome_Engineering_Poster_Fixed.pptx
SupriyaPolisetty1
 
Ghent University Global Campus: Overview
Ghent University Global Campus
 
Basal_ganglia_Structure_Function_Importance
muralinath2
 
High-speedBouldersandtheDebrisFieldinDARTEjecta
Sérgio Sacani
 
Calcium in a supernova remnant as a fingerprint of a sub-Chandrasekhar-mass e...
Sérgio Sacani
 
770043401-q1-Ppt-pe-and-Health-7-week-1-lesson-1.pptx
AizaRazonado
 
Unit-5 ppt.pdf unit 5 organic chemistry 3
visionshukla007
 
Rapid protoplanet formation in the outer Solar System recorded in a dunite fr...
Sérgio Sacani
 
Carbon-richDustInjectedintotheInterstellarMediumbyGalacticWCBinaries Survives...
Sérgio Sacani
 
ION EXCHANGE CHROMATOGRAPHY NEW PPT (JA).pptx
adhagalejotshna
 
Q1_Science 8_Week3-Day 1.pptx science lesson
AizaRazonado
 
Annual report 2024 - Inria - English version.pdf
Inria
 
Plankton and Fisheries Bovas Joel Notes.pdf
J. Bovas Joel BFSc
 
Ad

C++ Nested loops, matrix and fuctions.pdf

  • 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
  • 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
  • 10. 2x2 matrix using array (contnd) // Inner loop for columns for(int j = 0; j < 2; j++) { cout << "Element at (" << i << ", " << j << "): " << matrix[i][j] << endl; } } return 0; }
  • 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)