# Permutation

A permutation is a mathematical concept in which it gives the total number of ways a set can be arranged.

A set can be of numbers or characters. I had made this project on permutation in which not only it will print the total number of ways but also print all the arrangements also.

In this project, you will be asked to give your name and then it will print all the possible arrangements of your name and also print the total number of ways. I had used the concept of recursion and also used the pointers.

## Explaining the code:-

I had made two methods in this program one is two swap the characters passed and other is the recursive method.
In swap() method, it takes two parameters both are pointers to a character whose values are swapped latter and to reflect this change on the actual parameters we used pointers which help to use the call by reference concept.

```void swap(char *a, char *b){
char temp = *a;
*a = *b;
*b = temp;
}```

Another method which is used is permute() which is a recursive method. The base case just prints the string and the recursive case firstly gives the characters to the swap method which swap the characters in the actual string then it gives the string and the position to the recursive method.

```void permute(string str, int start){
int n=str.length()-1;

if(start == n){
cout <<"\t\t" << str << endl;
count++;
}else{
for(int i=start;i<= n;i++){
swap(str[start],str[i]);
permute(str,start + 1);
}
}
}```

In the main function, we first take up the name of the user and then give this name to the permute method which performs all the permutations and displays them on the output screen and at the end, it prints the total number of ways.

## Here is the output:- ## Here is the code used :-

```#include <iostream>

using namespace std;
int count=0;
void swap(char *a, char *b){
char temp = *a;
*a = *b;
*b = temp;
}

void permute(string str, int start){
int n=str.length()-1;

if(start == n){
cout <<"\t\t" << str << endl;
count++;
}else{
for(int i=start;i<= n;i++){
swap(str[start],str[i]);
permute(str,start + 1);
}
}
}

int main()
{   string name;
cout << "Enter your name : "; cin >> name;
cout << "Here is all the permutations of your name : " << endl<< endl;
permute(name,0);
cout << endl <<"Total permutations of your name is " <<count;

return 0;
}```

### You can learn about many other C++ Programs Here.

#### Best Books for learning C++ programming language with Data Structure and Algorithms.

The following two tabs change content below. #### Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development. 