User Defined Functions [SET – 1]
#include <iostream> using namespace std; int sum(int a, int b) { return a + b; } int main() { int num1, num2; cout << "Enter two numbers: "; cin >> num1 >> num2; int result = sum(num1, num2); cout << "Sum: " << result; return 0; }
#include <iostream> using namespace std; int factorial(int n) { if (n == 0 || n == 1) return 1; else return n * factorial(n - 1); } int main() { int num; cout << "Enter a number: "; cin >> num; int result = factorial(num); cout << "Factorial: " << result; return 0; }
#include <iostream> using namespace std; bool isPrime(int n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } void printPrimes(int start, int end) { for (int i = start; i <= end; i++) { if (isPrime(i)) { cout << i << " "; } } } int main() { int start, end; cout << "Enter two numbers: "; cin >> start >> end; cout << "Prime numbers between " << start << " and " << end << ": "; printPrimes(start, end); return 0; }
#include <iostream> #include <cmath> using namespace std; double power(double n, int p = 2) { return pow(n, p); } int main() { double num; int exponent; cout << "Enter a number: "; cin >> num; cout << "Enter an exponent (or press enter for default): "; cin >> exponent; cout << "Result: " << power(num, exponent); return 0; }
#include <iostream> using namespace std; void zero_small(int &a, int &b) { if (a < b) { a = 0; } else { b = 0; } } int main() { int num1, num2; cout << "Enter two numbers: "; cin >> num1 >> num2; zero_small(num1, num2); cout << "After zero_small(): " << num1 << " " << num2; return 0; }
#include <iostream.h> void X(int &A, int &B) { A = A + B; B = A - B; A = A - B; } void main() { int a = 4, b = 18; X(a, b); cout << a << "," << b; } // Output: 18,4
#include <iostream.h> void X(int A, int &B) { A = A + B; B = A - B; A = A - B; } void main() { int a = 4, b = 18; X(a, b); cout << a << "," << b; } // Output: 4,4
#include <iostream.h> void Execute(int &B, int C = 100) { int TEMP = B + C; B += TEMP; if (C == 100) cout << TEMP << " " << B << " " << C << endl; } void main() { int M = 90, N = 10; Execute(M); cout << M << " " << N << endl; Execute(M, N); cout << M << " " << N << endl; } // Output: // 190 280 100 // 280 10 // 290 10
#include <iostream.h> int global = 10; void func(int &x, int y) { x = x - y; y = x * 10; cout << x << " " << y << '\n'; } void main() { int global = 7; func(::global, global); cout << global << "," << ::global << '\n'; func(global, ::global); cout << global << "," << ::global << '\n'; } // Output: // 3 30 // 7,3 // 4 40 // 4,3
#include <iostream.h> static int i = 100; void abc() { static int i = 8; cout << "first=" << i++; } int main() { static int i = 2; abc(); cout << "second=" << i << endl; abc(); } // Output: // first=8second=2 // first=9
#include <iostream.h> int func(int &x, int y = 10) { if (x % y == 0) return ++x; else return y--; } void main() { int p = 20, q = 23; q = func(p, q); cout << p << " " << q << endl; p = func(q); cout << p << " " << q << endl; } // Output: // 20 22 // 23 22