C++ Programs Based on Functions with Source Code and Output
List of Practical Programs
Swapping Two Numbers Using Call by Value
Swapping Two Numbers Using Call by Reference
Factorial Using a Function
Factorial Using Recursion
Fibonacci Series Using a Function
Fibonacci Series Using Recursion
Prime Number Checking Using a Function
Greatest of Two Numbers Using a Function
Demonstration of Default Arguments
Demonstration of Function Overloading
Demonstration of Inline Functions
Demonstration of Lambda Expressions
1. C++ Program to Swap Two Numbers Using Call by Value
Explanation:
In call by value, copies of the actual arguments are passed to the function. Any changes made to the formal parameters do not affect the original variables.
Call by Value में actual arguments की copies function को pass की जाती हैं। Function के अंदर formal parameters में किए गए changes original variables को affect नहीं करते हैं।
Source Code:
#include <iostream>using namespace std;void swapNumbers(int a, int b){int temp = a;a = b;b = temp;cout << "Inside function:" << endl;cout << "a = " << a << endl;cout << "b = " << b << endl;}int main(){int x = 10, y = 20;cout << "Before swapping:" << endl;cout << "x = " << x << endl;cout << "y = " << y << endl;swapNumbers(x, y);cout << "\nAfter function call:" << endl;cout << "x = " << x << endl;cout << "y = " << y << endl;return 0;}
Output:
Before swapping:x = 10y = 20Inside function:a = 20b = 10After function call:x = 10y = 20
Result: The values are swapped inside the function, but the original variables remain unchanged.
इस प्रोग्राम में values function के अंदर swap होती हैं, लेकिन original variables की values unchanged रहती हैं।
2. C++ Program to Swap Two Numbers Using Call by Reference
Explanation:
In call by reference, reference parameters are used to access the original variables. Therefore, changes made inside the function directly affect the original variables.
Call by Reference में reference parameters का उपयोग करके original variables को access किया जाता है। इसलिए function के अंदर किए गए changes सीधे original variables को affect करते हैं।
Source Code:
#include <iostream>using namespace std;void swapNumbers(int &a, int &b){int temp = a;a = b;b = temp;}int main(){int x = 10, y = 20;cout << "Before swapping:" << endl;cout << "x = " << x << endl;cout << "y = " << y << endl;swapNumbers(x, y);cout << "\nAfter swapping:" << endl;cout << "x = " << x << endl;cout << "y = " << y << endl;return 0;}
Output:
Before swapping:x = 10y = 20After swapping:x = 20y = 10
Result: The original values are successfully swapped because the function receives references to the original variables.
इस प्रोग्राम में original variables की values successfully swap हो जाती हैं क्योंकि function को original variables के references मिलते हैं।
3. C++ Program to Find Factorial Using a Function
Explanation:
The factorial of a positive integer is the product of all positive integers from 1 to that number. The factorial of zero is 1.
किसी positive integer का Factorial उस संख्या से लेकर 1 तक की सभी positive integers का product होता है। Zero का factorial भी 1 होता है।
For example:
Source Code:
#include <iostream>using namespace std;unsigned long long factorial(int n){unsigned long long fact = 1;for (int i = 1; i <= n; i++){fact = fact * i;}return fact;}int main(){int n;cout << "Enter a number: ";cin >> n;if (n < 0 || n > 20){cout << "Please enter a number from 0 to 20.";}else{cout << "Factorial = " << factorial(n);}return 0;}
Output:
Enter a number: 5Factorial = 120
Result: The factorial() function calculates and returns the factorial of the given number.
इस प्रोग्राम में factorial() function दिए गए number का factorial calculate करके return करता है। यहाँ 0 से 20 तक की input range रखी गई है ताकि result unsigned long long में store हो सके।
4. C++ Program to Find Factorial Using Recursion
Explanation:
Recursion is a process in which a function calls itself. A recursive function must have a base condition to stop further recursive calls.
Recursion एक ऐसी प्रक्रिया है जिसमें कोई function स्वयं को call करता है। Recursive function में recursive calls को रोकने के लिए base condition होना आवश्यक है।
Source Code:
#include <iostream>using namespace std;unsigned long long factorial(int n){if (n == 0 || n == 1){return 1;}return n * factorial(n - 1);}int main(){int n;cout << "Enter a number: ";cin >> n;if (n < 0 || n > 20){cout << "Please enter a number from 0 to 20.";}else{cout << "Factorial = " << factorial(n);}return 0;}
Output:
Enter a number: 5Factorial = 120
Result: The function calls itself with a smaller value until the base condition is reached.
इस प्रोग्राम में function स्वयं को छोटी value के साथ call करता है, जब तक कि base condition प्राप्त नहीं हो जाती।
5. C++ Program to Print Fibonacci Series Using a Function
Explanation:
The Fibonacci series is a sequence in which each term is the sum of the previous two terms. The series generally starts with 0 and 1.
Fibonacci Series एक ऐसी संख्या श्रेणी है जिसमें प्रत्येक अगली संख्या पिछली दो संख्याओं के योग के बराबर होती है। यह सामान्यतः 0 और 1 से शुरू होती है।
Source Code:
#include <iostream>using namespace std;void fibonacci(int n){int a = 0, b = 1, next;for (int i = 1; i <= n; i++){cout << a << " ";next = a + b;a = b;b = next;}}int main(){int n;cout << "Enter number of terms: ";cin >> n;if (n < 1 || n > 93){cout << "Enter terms between 1 and 93.";}else{cout << "Fibonacci Series: ";fibonacci(n);}return 0;}
Output:
Enter number of terms: 8Fibonacci Series: 0 1 1 2 3 5 8 13
Result: The fibonacci() function prints the required number of Fibonacci terms.
इस प्रोग्राम में fibonacci() function आवश्यक Fibonacci terms को print करता है। Input range 1 से 93 रखी गई है ताकि generated terms int में सुरक्षित रहें।
6. C++ Program to Print Fibonacci Series Using Recursion
Explanation:
In this program, a recursive function is used to calculate each Fibonacci term. The function calls itself to calculate the previous two terms.
इस प्रोग्राम में Fibonacci Series की प्रत्येक term calculate करने के लिए recursive function का उपयोग किया गया है। Function पिछली दो terms calculate करने के लिए स्वयं को call करता है।
Source Code:
#include <iostream>using namespace std;int fibonacci(int n){if (n == 0)return 0;if (n == 1)return 1;return fibonacci(n - 1) + fibonacci(n - 2);}int main(){int n;cout << "Enter number of terms: ";cin >> n;if (n < 1 || n > 47){cout << "Enter terms between 1 and 47.";}else{cout << "Fibonacci Series: ";for (int i = 0; i < n; i++){cout << fibonacci(i) << " ";}}return 0;}
Output:
Enter number of terms: 8Fibonacci Series: 0 1 1 2 3 5 8 13
Result: The recursive function calculates Fibonacci terms using the sum of the previous two terms.
यह recursive function पिछली दो terms के योग से Fibonacci numbers calculate करता है।
Note: This simple recursive approach repeats many calculations and becomes slow for larger inputs. For practical applications, an iterative or memoized approach is more efficient.
यह simple recursive method कई calculations को बार-बार करता है, इसलिए बड़ी input values के लिए धीमा हो जाता है। Practical applications में iterative या memoization method अधिक efficient होती है।
7. C++ Program to Check Prime Number Using a Function
Explanation:
A prime number is a natural number greater than 1 that has exactly two positive factors: 1 and itself.
Prime Number वह natural number है जो 1 से बड़ा होता है तथा जिसके केवल दो positive factors होते हैं: 1 और वह संख्या स्वयं।
Source Code:
#include <iostream>using namespace std;bool isPrime(int n){if (n <= 1)return false;for (int i = 2; i <= n / i; i++){if (n % i == 0)return false;}return true;}int main(){int n;cout << "Enter a number: ";cin >> n;if (isPrime(n))cout << n << " is a Prime Number.";elsecout << n << " is not a Prime Number.";return 0;}
Output:
Enter a number: 1717 is a Prime Number.
Result: The isPrime() function checks whether the given number is prime and returns true or false.
इस प्रोग्राम में isPrime() function दिए गए number के prime होने की जाँच करता है और true या false return करता है।
8. C++ Program to Find the Greatest of Two Numbers Using a Function
Explanation:
A function can be used to compare two numbers and return the greater number.
दो numbers की तुलना करके उनमें से बड़ी संख्या return करने के लिए function का उपयोग किया जा सकता है।
Source Code:
#include <iostream>using namespace std;int greatest(int a, int b){if (a > b)return a;else if (b > a)return b;elsereturn a;}int main(){int x, y;cout << "Enter two numbers: ";cin >> x >> y;if (x == y)cout << "Both numbers are equal.";elsecout << "Greatest number = " << greatest(x, y);return 0;}
Output:
Enter two numbers: 25 40Greatest number = 40
Result: The greatest() function compares two numbers and returns the greater one.
इस प्रोग्राम में greatest() function दो numbers की तुलना करके बड़ी संख्या return करता है। यदि दोनों numbers समान हों, तो main function equality का message display करता है।
9. C++ Program to Demonstrate Default Arguments
Explanation:
Default arguments are values provided in a function declaration. If an argument is omitted during a function call, its default value is used.
Default Arguments वे values होती हैं जो function declaration में provide की जाती हैं। यदि function call करते समय कोई argument नहीं दिया जाता, तो उसकी default value का उपयोग किया जाता है।
Source Code:
#include <iostream>using namespace std;int addition(int a, int b = 10){return a + b;}int main(){cout << "Addition with two arguments: "<< addition(5, 20) << endl;cout << "Addition with one argument: "<< addition(5) << endl;return 0;}
Output:
Addition with two arguments: 25Addition with one argument: 15
Result: When two arguments are supplied, both are used. When only one argument is supplied, the default value of b is used.
जब दो arguments दिए जाते हैं, तो दोनों का उपयोग होता है। केवल एक argument देने पर b की default value 10 का उपयोग किया जाता है।
10. C++ Program to Demonstrate Function Overloading
Explanation:
Function overloading is a feature of C++ that allows multiple functions to have the same name but different parameter lists. The compiler selects the appropriate function based on the arguments supplied.
Function Overloading C++ का एक feature है जिसमें multiple functions का नाम समान हो सकता है, लेकिन उनके parameter lists अलग-अलग होते हैं। Compiler दिए गए arguments के आधार पर appropriate function select करता है।
Source Code:
#include <iostream>using namespace std;int addition(int a, int b){return a + b;}double addition(double a, double b){return a + b;}int addition(int a, int b, int c){return a + b + c;}int main(){cout << "Addition of two integers: "<< addition(10, 20) << endl;cout << "Addition of two decimal numbers: "<< addition(5.5, 4.5) << endl;cout << "Addition of three integers: "<< addition(10, 20, 30) << endl;return 0;}
Output:
Addition of two integers: 30Addition of two decimal numbers: 10Addition of three integers: 60
Result: The same function name addition() performs different operations based on the number and types of arguments.
इस प्रोग्राम में समान function name addition() arguments की संख्या और उनके data types के अनुसार अलग-अलग काम करता है।
11. C++ Program to Demonstrate Inline Functions
Explanation:
An inline function is a function declared using the inline keyword. It allows the compiler to consider replacing a function call with the function body, although the compiler is not required to do so.
Inline Function वह function होता है जिसे inline keyword की सहायता से declare किया जाता है। इससे compiler को function call के स्थान पर function body रखने का सुझाव मिलता है, लेकिन compiler ऐसा करने के लिए बाध्य नहीं होता।
Source Code:
#include <iostream>using namespace std;inline int square(int n){return n * n;}int main(){int num = 5;cout << "Number = " << num << endl;cout << "Square = " << square(num);return 0;}
Output:
Number = 5Square = 25
Result: The inline function square() returns the square of the given number.
इस प्रोग्राम में inline function square() दिए गए number का square return करता है।
12. C++ Program to Demonstrate Lambda Expressions
Explanation:
A lambda expression is an anonymous function that can be defined directly within an expression. It is useful for writing short functions without declaring a separate named function.
Lambda Expression एक anonymous function होता है जिसे किसी expression के अंदर define किया जा सकता है। इसका उपयोग अलग से named function declare किए बिना छोटे functions लिखने के लिए किया जाता है।
Source Code:
#include <iostream>using namespace std;int main(){auto addition = [](int a, int b){return a + b;};cout << "Addition = " << addition(10, 20);return 0;}
Output:
Addition = 30Result: The lambda expression calculates the sum of two numbers and returns the result.
इस प्रोग्राम में Lambda Expression दो numbers का sum calculate करके result return करता है।
Comments
Post a Comment