Function and Pointers Programs in C++

#### 1. Write a C++ program to swap the values of two variables

```#include<iostream>
using namespace std;
void swap(int, int);
int main( ){
int x,y;
cout<<"Enter X: ";
cin>>x;
cout<<"Enter Y: ";
cin>>y;
cout<<"\nBEFORE SWAP:\n";
cout<<"X = "<< x<<" and Y = "<< y;
swap(x,y);
return 0;
}
void swap(int x, int y){
int temp = x;
x = y;
y = temp;
cout<<"\n\nAFTER SWAP : \n";
cout<<"X= "<< x<<" and Y= "<< y;
}

```
OUTPUT
```Enter X: 15
Enter Y: 20

BEFORE SWAP:
X=15 and Y=20

AFTER SWAP:
X=20 and Y=15```

#### 2. Write a C++ program to Calculate Factorial of a given Number.

```#include<iostream>
using namespace std;
int fact(int);

int main(){
int n,res;
cout<<"Enter number: ";
cin>>n;
res = fact(n);
cout<<"Factorial : "<< res;
return 0;
}

int fact(int x){
int i, f=1;
for(i=1; i<=x; i++){
f = f*i;
}
return f;
}
```
OUTPUT
```Enter a Number : 6
Factorial = 720```

#### 3. Write a C++ program to find out ncr factor by using a user defined function for factorial. FORMULA : ncr = n! / ((n-r)! * r!)

```#include<iostream>
using namespace std;
int fact(int);

int main(){
int n,r, res;
cout<<"Enter n: ";
cin>>n;
cout<<"Enter r: ";
cin>>r;
res = fact(n)/(fact(n-r)*fact(r));
cout<<"Result nCr Is: "<< res;
}

int fact(int x){
int i, f=1;
for(i=1; i<=x; i++){
f = f*i;
}
return f;
}
```
OUTPUT
```Enter n: 5
Enter r: 3
Result nCr Is : 10```

#### 4. Write a C++ program to Calculate Factorial of a given Number using Recursion.

```#include<iostream>
using namespace std;
int fact(int);

int main(){
int n,res;
cout<<"Enter number: ";
cin>>n;
res = fact(n);
cout<<"Factorial : "<< res;
return 0;
}

int fact(int x){
if(x==0 || x==1){
return 1;
}
else{
return x*fact(x-1);
}
}
```
OUTPUT
```Enter a Number : 5
Factorial = 120```

#### 5. Write a C++ program to Calculate HCF and LCM of two Number

```#include<iostream>
using namespace std;
int lcm(int , int);
int hcf(int , int);
int main( ) {
int a,b;
cout<<"Enter two Number : ";
cin>>a>>b;
int lc=lcm(a,b);
int hc =(a>b)?hcf(a,b):hcf(b,a);
cout<<"LCM = "<< lc<< endl;
cout<<"HCF = "<< hc<< endl;
return 0;
}
int lcm(int a, int b) {
int res = a*b/hcf(a,b);
return res;
}
int hcf(int a, int b) {
int rem;
while( b != 0) {
rem = a%b;
a = b;
b = rem;
}
return a;
}
```
OUTPUT
```Enter two Number : 15 50
LCM = 150
HCF = 5
```

#### 6. Write a C++ program to calculate 1!+2!+3!+4!+5!

```#include<iostream>
using namespace std;
int fact(int);
int main( ){
int i, sum=0;
for(i=1; i<=5; i++){
sum = sum+fact(i);
}
cout<<"SUM = "<< sum;
return 0;
}
int fact(int x){
if(x==0 || x==1){
return 1;
}
else{
return x*fact(x-1);
}
}
```
OUTPUT
`SUM = 153`

#### 7. Write a C++ program to calculate 1/1!+2/2!+3^2/3!+4^3/4!+5^4/5!+6^5/6!

```#include< iostream >
int fact(int);
float power(int,int);
int main( ){
int i;
float sum;
for(i=0; i<=6; i++){
sum = sum+power(i,i-1)/fact(i);
}
cout("Sum = %f\n",sum);
return 0;
}

int fact(int x){
if(x==0 || x==1){
return 1;
}
else{
return x*fact(x-1);
}
}
float power(int x, int y){
int j, result=1;
for(j=1; j<=y; j++){
result = x*result;
}
return result;
}
```
OUTPUT
`Sum = 22.174999`

#### 8. Write a C++ program to compute the cosine series using function. =1-x2/2!+x4/4!-x6/6!----

```#include<iostream>
#include<math.h>
using namespace std;
float cal_cos(int, int);
int fact(int);
int main( )
{

int x, n;
cout<<"Enter the value of x : ";
cin>>x;
cout<<"Enter the value of n : ";
cin>>n;
float r = cal_cos(x, n);
cout<<"Result = "<< r;
return 0;
}
float cal_cos(int a, int n) {
int i,j =2 ;
float sum = 1;
for(i=1; i<=n; i++)
{
if(i%2 ==0)
{
sum = sum+pow(a,j)/(float)fact(j);
}
else
{
sum = sum-pow(a,j)/fact(j);
}
}
return sum;
}

int fact(int x){
int i, f=1;
for(i=1; i<=x; i++){
f = f*i;
}
return f;
}

```
OUTPUT
``` Enter the Value of x : 3
Enter the Value of n : 3
Result = -1.250000```

#### 9. Write a C++ program to calculate sum of two number using Pointer

```#include<iostream>
using namespace std;
int main( ) {
int a, b, *p, *q, sum;
cout<<"Enter two number : ";
cin>>a>>b;
p = & a;
q = & b;
sum = *p + *q;
cout<<"SUM = "<< sum<< endl;
return 0;
}

```
OUTPUT
```Enter two number : 10 20
SUM = 30```

#### 10. Write a C++ program to swap two Number using Pointer

```#include<iostream>
using namespace std;
int main( ) {
int x, y, *a, *b, temp;
cout<<"Enter the value of X and Y : ";
cin>>x>>y;
cout<<"Before Swapping\n";
cout<<"X = "<< x<<"\nY = " << y;
cout<< endl;
a = & x;
b = & y;
temp = *b;
*b = *a;
*a = temp;
cout<<"After Swapping\n";
cout<<"X = "<< x<<"\nY = " << y;
return 0;
}

```
OUTPUT
```Enter the value of X and Y : 10 20
Before Swapping
X = 10
Y = 20
After Swapping
X = 20
Y = 10
```

#### 11. Write a C++ program to find the length of String using Pointer

```#include<iostream>
using namespace std;
int main( ) {
char str[20];
int length = 0;
cout<<"Enter any String : ";
cin.getline(str,20);
char* p = str;
while (*p != '\0') {
length++;
p++;
}
cout<<"Length of "<< str<<" = ";
cout<< length<< endl;
return 0;
}

```
OUTPUT
```Enter any String : prowess
Length of prowess is : 7```

#### 12. Write a C++ program to Access Array Elements Using Pointer

```#include<iostream>
using namespace std;
int main( ) {
int arr[5], i;
cout<<"Enter Elements of Array:";
for(i=0;i<5;i++){
cin>>arr[i];
}
cout<<"You have entered : ";
for(i=0;i<5;i++){
cout<< *(arr+i)<<"  ";
}
return 0;
}
```
OUTPUT
```Enter Elements of Array : 1 4 6 7 8
You have entered : 1 4 6 7 8```

#### 13. Write a C++ program to convert binary to decimal number using user define function.

```#include<iostream>
#include<math.h>
using namespace std;
long binToDec(long n);
int power(int, int);
int main() {
long bi;
cout<<"Enter a binary no. :";
cin>>bi;
cout<<"BINARY : "<< bi<< endl;
cout<<"DECIMAL: "<< binToDec(bi);
return 0;
}

long binToDec(long n) {
int rem;
long dec = 0, i=0;
while(n != 0) {
rem = n%10;
n = n/10;
dec = dec +(rem*power(2,i));
++i;
}
return dec;
}
int power(int a, int b) {
int res= 1;
for(int i =0; i< b; i++) {
res = res*b;
}
return res;
}
```
OUTPUT
```Enter a binary no. :11011
BINARY  : 11001
DECIMAL : 27```

#### 14. Write a C++ program to convert decimal to binary number using user define function

```#include<iostream>
#include<math.h>
using namespace std;
long decToBin(long n);
int main() {
long n;
cout<<"Enter a decimal no. :";
cin>>n;
cout<<"DECIMAL: "<< n<< endl;
cout<<"BINARY : "<< decToBin(n);
return 0;
}

long decToBin(long n) {
int rem;
long binary = 0, i = 1;
while(n != 0) {
rem = n%2;
n = n/2;
binary= binary + (rem*i);
i = i*10;
}
return binary;
}
```
OUTPUT
``` Enter a decimal no. : 27
DECIMAL : 27
BINARY  : 11001```

#### 15. Write a C++ program to reverse of a number using recursion.

```#include<iostream>
#include<math.h>
using namespace std;
int rev(int, int);
int main() {
int num, res;
int length = 0, temp;
cout<<"Enter a number: ";
cin>>num;
temp = num;
while (temp != 0) {
length++;
temp = temp / 10;
}
res = rev(num, length);
cout<<"Reverse = "<< res<< endl;
return 0;
}

int rev(int n, int len) {
int x;
cout<< " this is x"<< n<< endl;
if (len == 1) {
return n+1;
}
else {
x = ((n % 10) * pow(10, len-1));

return ( x + rev(n / 10,--len));
}
}
```
OUTPUT
```Enter a number: 1234
Reverse = 4321```

#### CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824