// C++ program for functions with NO arguments, NO return #include < iostream > using namespace std; void myFunction () { cout << "I just got executed!"; } int main() { myFunction (); return 0; } Outputs I just got executed!
// C++ program for functions with NO arguments, NO return // A function can be called multiple times: #include < iostream > using namespace std; void myFunction () { cout << "I just got executed!\n"; } int main() { myFunction (); myFunction (); myFunction (); myFunction (); return 0; } Outputs I just got executed! I just got executed! I just got executed! I just got executed !
/ C++ program to find factorial of a number using iteration #include < iostream > using namespace std; i nt main() { int n = 5; int fact = 1 ; for ( int i = 1; i <= n; i ++) { fact = fact * i ; } cout << "Factorial of " << n << " is " << fact << endl ; return 0; }
// C++ program to find factorial of a number using no argument, no return #include < iostream > using namespace std; int main() { void factorial(void ); // function prototyping factorial(); //function call return 0; } void factorial(void) { int n, fact=1; c out <<“Enter the number : “; cin >>n ; if(n <= 1) fact=1; else for ( int i = 1; i <= n; i ++) fact = fact * i ; cout << "Factorial of " << n << " is " << fact << endl ; }
// C++ program to find factorial of a number using with argument, no return # include < iostream > using namespace std; int main() { void factorial( int ); // function prototyping int num; cout << " Enter the number : "; cin >>num; factorial(num ); //function call return ; } void factorial( int n ) { int fact=1; If (n <= 1) fact=1; else for ( int i = 1; i <= n; i ++) fact = fact * i ; cout << "Factorial of " << n << " is " << fact << endl ; }
// C++ program to find factorial of a number using no argument, with return #include < iostream > using namespace std; int main() { int factorial(void); // function prototyping int result; result=factorial (); //function call cout << result << endl ; return 0; } int factorial(void ) //function definition { int n; fact=1; cout << " Enter the number : "; cin >>n; if(n <= 1) fact=1; else for ( int i = 1; i <= n; i ++) fact = fact * i ; cout << "Factorial of " << n << " is "; return (fact); }
// C++ program to find factorial of a number using with argument , with return # include < iostream > using namespace std; Int main() { unsigned int factorial ( int ); // function prototyping unsigned int result; int num ; cout << " Enter the number : "; cin >>num; result=factorial(num ); //function call cout << result << endl ; return 0; } unsigned int factorial( int n ) //function definition { unsigned int fact=1; if(n <= 1) fact=1; else for ( int i = 1; i <= n; i ++) fact = fact * i ; cout << "Factorial of " << n << " is "; return (fact); }
// C++ program to find factorial of given number by using recursion # include < iostream > using namespace std; unsigned int factorial(unsigned int n) { if (n == 0) return 1; return n * factorial(n - 1); } int main() { unsigned int num ; cout <<" Enter the Number : "; cin >>num; cout << "Factorial of " << num << " is “ << factorial(num) << endl ; return 0; }
https://www.geeksforgeeks.org/calculate-power-of-a-number/ // C++ program for X raise to Y with arguments, with return #include <bits/ stdc ++.h> using namespace std; long power( int x, unsigned n) { long long pow = 1; for ( int i = 0; i < n; i ++) { pow = pow * x; } return pow ; } int main(void) { Int x = 2; unsigned n = 3; int result = power(x, n); // Function call cout << result << endl ; return 0; } OUTPUT 8