 ## C++ (49)

Thursday, 22 June 2017 12:51

### C++ Program to swap two numbers without third variable

Written by

We can swap two numbers without using third variable. There are two common ways to swap two numbers without using third variable:

1. By + and -
2. By * and /

## Program 1: Using * and /

Let's see a simple C++ example to swap two numbers without using third variable.

1.  #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a=5, b=10;
6. cout<<"Before swap a= "<<a<<" b= "<<b<<endl;
7. a=a*b; //a=50 (5*10)
8. b=a/b; //b=5 (50/10)
9. a=a/b; //a=10 (50/5)
10. cout<<"After swap a= "<<a<<" b= "<<b<<endl;
11. return 0;
12. }

Output:

```Before swap a= 5 b= 10
After swap a= 10 b= 5```

## Program 2: Using + and -

Let's see another example to swap two numbers using + and -.

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a=5, b=10;
6. cout<<"Before swap a= "<<a<<" b= "<<b<<endl;
7. a=a+b; //a=15 (5+10)
8. b=a-b; //b=5 (15-10)
9. a=a-b; //a=10 (15-5)
10. cout<<"After swap a= "<<a<<" b= "<<b<<endl;
11. return 0;
12. }

Output:

```Before swap a= 5 b= 10
After swap a= 10 b= 5```

Thursday, 22 June 2017 12:50

### C++ Program to reverse number

Written by

We can reverse a number in C++ using loop and arithmetic operators. In this program, we are getting number as input from the user and reversing that number.

Let's see a simple C++ example to reverse a given number.

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n, reverse=0, rem;
6. cout<<"Enter a number: ";
7. cin>>n;
8.   while(n!=0)
9.   {
10.      rem=n%10;
11.      reverse=reverse*10+rem;
12.      n/=10;
13.   }
14.  cout<<"Reversed Number: "<<reverse<<endl;
15. return 0;
16. }

Output:

```Enter a number: 234
Reversed Number: 432```
Thursday, 22 June 2017 12:49

Written by

# Sum of digits program in C++

We can write the sum of digits program in C++ language by the help of loop and mathematical operation only.

## Sum of digits algorithm

To get sum of each digit by C++ program, use the following algorithm:

• Step 1: Get number by user
• Step 2: Get the modulus/remainder of the number
• Step 3: sum the remainder of the number
• Step 4: Divide the number by 10
• Step 5: Repeat the step 2 while number is greater than 0.

Let's see the sum of digits program in C++.

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n,sum=0,m;
6. cout<<"Enter a number: ";
7. cin>>n;
8. while(n>0)
9. {
10. m=n%10;
11. sum=sum+m;
12. n=n/10;
13. }
14. cout<<"Sum is= "<<sum<<endl;
15. return 0;
16. }

Output:

```Enter a number: 23
Sum is= 5
```
```Enter a number: 624
Sum is= 12```
Thursday, 22 June 2017 12:47

### Armstrong Number in C++

Written by

Before going to write the C++ program to check whether the number is Armstrong or not, let's understand what is Armstrong number.

Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.

Let's try to understand why 371 is an Armstrong number.

1. 371 = (3*3*3)+(7*7*7)+(1*1*1)
2. where:
3. (3*3*3)=27
4. (7*7*7)=343
5. (1*1*1)=1
6. So:
7. 27+343+1=371

Let's see the C++ program to check Armstrong Number.

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n,r,sum=0,temp;
6. cout<<"Enter the Number=  ";
7. cin>>n;
8. temp=n;
9. while(n>0)
10. {
11. r=n%10;
12. sum=sum+(r*r*r);
13. n=n/10;
14. }
15. if(temp==sum)
16. cout<<"Armstrong Number."<<endl;
17. else
18. cout<<"Not Armstrong Number."<<endl;
19. return 0;
20. }

Output:

```Enter the Number= 371
Armstrong Number.
```
```Enter the Number= 342
Not Armstrong Number.	```

Thursday, 22 June 2017 12:46

### Factorial program in C++

Written by

Factorial Program in C++: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:

1. 4! = 4*3*2*1 = 24
2. 6! = 6*5*4*3*2*1 = 720

Here, 4! is pronounced as "4 factorial", it is also called "4 bang" or "4 shriek".

The factorial is normally used in Combinations and Permutations (mathematics).

There are many ways to write the factorial program in C++ language. Let's see the 2 ways to write the factorial program.

• Factorial Program using loop
• Factorial Program using recursion

## Factorial Program using Loop

Let's see the factorial Program in C++ using loop.

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5.    int i,fact=1,number;
6.   cout<<"Enter any Number: ";
7.  cin>>number;
8.   for(i=1;i<=number;i++){
9.       fact=fact*i;
10.   }
11.   cout<<"Factorial of " <<number<<" is: "<<fact<<endl;
12.   return 0;
13. }

Output:

```Enter any Number: 5
Factorial of 5 is: 120   ```

## Factorial Program using Recursion

Let's see the factorial program in C++ using recursion.

1. #include<iostream>
2. using namespace std;
3. int main()
4. {
5. int factorial(int);
6. int fact,value;
7. cout<<"Enter any number: ";
8. cin>>value;
9. fact=factorial(value);
10. cout<<"Factorial of a number is: "<<fact<<endl;
11. return 0;
12. }
13. int factorial(int n)
14. {
15. if(n<0)
16. return(-1); /*Wrong value*/
17. if(n==0)
18. return(1);  /*Terminating condition*/
19. else
20. {
21. return(n*factorial(n-1));
22. }
23. }

Output:

```Enter any number: 6
Factorial of a number is: 720```

Thursday, 22 June 2017 12:43

### Palindrome program in C++

Written by

palindrome number is a number that is same after reverse. For example 121, 34543, 343, 131, 48984 are the palindrome numbers.

## Palindrome number algorithm

• Get the number from user
• Hold the number in temporary variable
• Reverse the number
• Compare the temporary number with reversed number
• If both numbers are same, print palindrome number
• Else print not palindrome number

Let's see the palindrome program in C++. In this program, we will get an input from the user and check whether number is palindrome or not.

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5.   int n,r,sum=0,temp;
6.   cout<<"Enter the Number=";
7.   cin>>n;
8.  temp=n;
9.  while(n>0)
10. {
11.  r=n%10;
12.  sum=(sum*10)+r;
13.  n=n/10;
14. }
15. if(temp==sum)
16. cout<<"Number is Palindrome.";
17. else
18. cout<<"Number is not Palindrome.";
19.   return 0;
20. }

Output:

```Enter the Number=121
Number is Palindrome.
```
```Enter the number=113
Number is not Palindrome.```
Thursday, 22 June 2017 12:43

### Prime Number Program in C++

Written by

Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17, 19, 23.... are the prime numbers.

Let's see the prime number program in C++. In this C++ program, we will take an input from the user and check whether the number is prime or not.

1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5.   int n, i, m=0, flag=0;
6.   cout << "Enter the Number to check Prime: ";
7.   cin >> n;
8.   m=n/2;
9.   for(i = 2; i <= m; i++)
10.   {
11.       if(n % i == 0)
12.       {
13.           cout<<"Number is not Prime."<<endl;
14.           flag=1;
15.           break;
16.       }
17.   }
18.   if (flag==0)
19.       cout << "Number is Prime."<<endl;
20.   return 0;
21. }

Output:

```Enter the Number to check Prime: 17
Number is Prime.
```
```Enter the Number to check Prime: 57
Number is not Prime.```
Thursday, 22 June 2017 12:41

### Fibonacci Series in C++

Written by

Fibonacci Series in C++: In case of fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1.

There are two ways to write the fibonacci series program:

• Fibonacci Series without recursion
• Fibonacci Series using recursion

## Fibonaccci Series in C++ without Recursion

Let's see the fibonacci series program in C++ without recursion.

1. #include <iostream>
2. using namespace std;
3. int main() {
4.   int n1=0,n2=1,n3,i,number;
5.  cout<<"Enter the number of elements: ";
6.  cin>>number;
7.  cout<<n1<<" "<<n2<<" "//printing 0 and 1
8.  for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed
9.  {
10.   n3=n1+n2;
11.   cout<<n3<<" ";
12.   n1=n2;
13.   n2=n3;
14.  }
15.    return 0;
16.    }

Output:Enter the number of elements: 10

0 1 1 2 3 5 8 13 21 34

## Fibonnaci series using recursion in C++

Let's see the fibonacci series program in C++ using recursion.

1. #include<iostream>
2. using namespace std;
3. void printFibonacci(int n){
4.     static int n1=0, n2=1, n3;
5.     if(n>0){
6.          n3 = n1 + n2;
7.          n1 = n2;
8.          n2 = n3;
9.  cout<<n3<<" ";
10.          printFibonacci(n-1);
11.     }
12. }
13. int main(){
14.     int n;
15.     cout<<"Enter the number of elements: ";
16.     cin>>n;
17.     cout<<"Fibonacci Series: ";
18.     cout<<"0 "<<"1 ";
19.     printFibonacci(n-2);  //n-2 because 2 numbers are already printed
20.      return 0;
21. }

Output:

```Enter the number of elements: 15
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377```
Thursday, 22 June 2017 12:37

### C++ Programs

Written by
Wednesday, 21 June 2017 03:00

### C++ User-Defined Exceptions

Written by

The new exception can be defined by overriding and inheriting exception class functionality.

## C++ user-defined exception example

Let's see the simple example of user-defined exception in which std::exception class is used to define the exception.

1. #include <iostream>
2. #include <exception>
3. using namespace std;
4. class MyException : public exception{
5.     public:
6.         const char * what() const throw()
7.         {
8.             return "Attempted to divide by zero!\n";
9.         }
10. };
11. int main()
12. {
13.     try
14.     {
15.         int x, y;
16.         cout << "Enter the two numbers : \n";
17.         cin >> x >> y;
18.         if (y == 0)
19.         {
20.             MyException z;
21.             throw z;
22.         }
23.         else
24.         {
25.             cout << "x / y = " << x/y << endl;
26.         }
27.     }
28.     catch(exception& e)
29.     {
30.         cout << e.what();
31.     }
32. }

Output:

```Enter the two numbers :
10
2
x / y = 5
```

Output:

```Enter the two numbers :
10
0
Attempted to divide by zero!
```

-->

Note: In above example what() is a public method provided by the exception class. It is used to return the cause of an exception.

Page 1 of 4