Function C++
Function C++
#include<iostream>
#include<math.h>
using namespace std;
int main() {
float num;
float v=-2.5;
cout<<"enter num";
cin>>num;
cout<<”square of num:”<< sqrt(num)<<endl;
cout<<”power of num:”<< pow(num,2)<<endl;
cout<<”absolute value of v: <<abs(v)<<endl;
}
OUTPUT
enter num 25
square of num:5
power of num:625
absolute value of v:2.5
EXPLAINATION
1. The cmath header file contains definitions for C++ for computing common mathematical functions.
2.Declare two floating point variable ‘num’and ‘v’ .’v’ is initialize with value -2.5;
2.take input from user and stored int ‘num’ variable using cin.
3.we calculate square of num using sqrt() function,and print it .
4.we calculate power of num,by using pow() function and print .
5.we make absolute value of variable ‘v’ using abs() function and print using cout function.
2. Write a program that calculates 6^5. Declare your own function to do this
#include<iostream>
using namespace std;
void power(int num,int p);
int main() {
power(5,6);
}
void power(int num,int p){
int n=num;
for(int i=1;i<p;i++){
num=num*n;
}
cout<<”power of 5^6 is :”num;
}
O
OUTPUT
Power of 5^6 is: 15625
EXPLAINATION
1. void power( int num,int p) we are declaring the function takes two parameter ‘num’ and ‘p’.
2. then in main function we are calling power function passing ‘5’ and ‘6’ :power(5,6).
3. Then we in definition section we are declaring n and initialize with num parameter.
4. We are creating loop
#include<iostream>
using namespace std;
int main() {
int a=10,b=5;
printf(“CALL ALL THE ARITHMETIC FUNCTION”);
add(a,b);
substract(a,b);
multipication(a,b);
division(a,b);
}
void add(int a,int b){
cout<<”addition of a and b:<<a+b<<endl;
}
void substract(int a,int b){
cout<<”substraction of a and b:<<a-b<<endl;
}
void multipication(int a,int b){
cout<<”multiplication of a and b :<<a*b<<endl;
}
void division(int a,int b){
cout<<a/b<<endl;
}
OUTPUT
CALL ALL THE ARITHMETIC FUNCTION
addition of a and b :15
substraction of a and b :5
multiplication of a and b :50
division of a and b :2
EXPLAINATION
These lines declare the prototypes of four functions that will perform arithmetic
operations on two integer inputs. The function names are add, subtract, multiplication,
and division.
The main() function is the entry point of the program. It defines two integer variables a and
b with values 10 and 5, respectively.
Then, it calls each of the arithmetic functions (add, subtract, multiplication, and division)
with the values of a and b as arguments.
Then in function definition part add ,substract ,multipicaton, division all functions takes
two inter function then do this ‘+’, ‘-‘, ‘*’, ‘/’ restpectively and print the result
4. Write a program to print the Fibonacci series in C++ Using Function .
#include<iostream>
using namespace std;
EXPLAINATION
void fib(int n);: This is a function prototype for the fib function. It indicates that the fib
function is defined later in the code and takes an integer parameter n.
int n;: Declares an integer variable n to store the user-specified term., take input from
user for n.
void fib(int n): This function is defined to generate and print the Fibonacci sequence up to
the nth
Then declare 3 variables a,b, fib intiialize with value 0,1,0.
Then we start a loop which will execute till nth term.
Inside loop first print the value of ‘fib’ then update ‘a’ by putting value of ‘b’,and update
‘b’ by putting value of ‘fib’ , the add ‘a’ and ‘b’ and store into fib;
5. Write a C++ Program To Swap Two Numbers Without Using Third Variable Using Functions
#include <iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
cout << "Value of a before: " << a << endl;
cout << "Value of b before: " << b << endl;
swap(a, b);
cout << "Value of a after swap: " << a << endl;
cout << "Value of b after swap: " << b << endl;
OUTPUT
Value of a before: 10
Value of b before: 20
Value of a now: 20
Value of b now: 10
EXPLAINATION
int a = 10; and int b = 20;: Declare and initialize two integer variables a and b with the values 10
and 20, respectively.
swap(a, b);: This line of code calls the swap function provided by the C++ standard library to
exchange the values of a and b. After this call, a will hold the value of b and vice versa.
Then print the swapped value.