We can swap two numbers without using third variable. There are two common ways to swap two numbers without using third variable:
Let's see a simple C++ example to swap two numbers without using third variable.
Output:
Before swap a= 5 b= 10 After swap a= 10 b= 5
Let's see another example to swap two numbers using + and -.
Output:
Before swap a= 5 b= 10 After swap a= 10 b= 5
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.
Output:
Enter a number: 234 Reversed Number: 432
We can write the sum of digits program in C++ language by the help of loop and mathematical operation only.
To get sum of each digit by C++ program, use the following algorithm:
Let's see the sum of digits program in C++.
Output:
Enter a number: 23 Sum is= 5
Enter a number: 624 Sum is= 12
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.
Let's see the C++ program to check Armstrong Number.
Output:
Enter the Number= 371 Armstrong Number.
Enter the Number= 342 Not Armstrong Number.
Factorial Program in C++: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:
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.
Let's see the factorial Program in C++ using loop.
Output:
Enter any Number: 5 Factorial of 5 is: 120
Let's see the factorial program in C++ using recursion.
Output:
Enter any number: 6 Factorial of a number is: 720
A palindrome number is a number that is same after reverse. For example 121, 34543, 343, 131, 48984 are the palindrome numbers.
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.
Output:
Enter the Number=121 Number is Palindrome.
Enter the number=113 Number is not Palindrome.
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.
Output:
Enter the Number to check Prime: 17 Number is Prime.
Enter the Number to check Prime: 57 Number is not Prime.
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:
Let's see the fibonacci series program in C++ without recursion.
Output:Enter the number of elements: 10
0 1 1 2 3 5 8 13 21 34
Let's see the fibonacci series program in C++ using recursion.
Output:
Enter the number of elements: 15 Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
The new exception can be defined by overriding and inheriting exception class functionality.
Let's see the simple example of user-defined exception in which std::exception class is used to define the exception.
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.
In C++ programming, exception handling is performed using try/catch statement. The C++ try block is used to place the code that may occur exception. The catch block is used to handle the exception.
Output:
Floating point exception (core dumped)
Output:
Attempted to divide by zero!
Exception Handling in C++ is a process to handle runtime errors. We perform exception handling so the normal flow of the application can be maintained even after runtime errors.
In C++, exception is an event or object which is thrown at runtime. All exceptions are derived from std::exception class. It is a runtime error which can be handled. If we don't handle the exception, it prints exception message and terminates the program.
It maintains the normal flow of the application. In such case, rest of the code is executed even after exception.
In C++ standard exceptions are defined in <exception> class that we can use inside our programs. The arrangement of parent-child class hierarchy is shown below:
All the exception classes in C++ are derived from std::exception class. Let's see the list of C++ common exception classes.
Exception | Description |
---|---|
std::exception | It is an exception and parent class of all standard C++ exceptions. |
std::logic_failure | It is an exception that can be detected by reading a code. |
std::runtime_error | It is an exception that cannot be detected by reading a code. |
std::bad_exception | It is used to handle the unexpected exceptions in a c++ program. |
std::bad_cast | This exception is generally be thrown by dynamic_cast. |
std::bad_typeid | This exception is generally be thrown by typeid. |
std::bad_alloc | This exception is generally be thrown by new. |
In C++, we use 3 keywords to perform exception handling:
Moreover, we can create user-defined exception which we will learn in next chapters.
In C++, string is an object of std::string class that represents sequence of characters. We can perform many operations on strings such as concatenation, comparison, conversion etc.
Let's see the simple example of C++ string.
Output:
Hello C++
Let's see the simple example of string comparison using strcmp() function.
Output:
What is my favourite fruit? apple What is my favourite fruit? banana What is my favourite fruit? mango Answer is correct!!
Let's see the simple example of string concatenation using strcat() function.
Output:
Enter the key string: Welcome to Enter the buffer string: C++ Programming. Key = Welcome to C++ Programming. Buffer = C++ Programming.
Let's see the simple example of copy the string using strcpy() function.
Output:
Enter the key string: C++ Tutorial Key = C++ Tutorial Buffer = C++ Tutorial
Let's see the simple example of finding the string length using strlen() function.
Output:
Length of String = 26
Namespaces in C++ are used to organize too many classes so that it can be easy to handle the application.
For accessing the class of a namespace, we need to use namespacename::classname. We can use using keyword so that we don't have to use complete name all the time.
In C++, global namespace is the root namespace. The global::std will always refer to the namespace "std" of C++ Framework.
Let's see the simple example of namespace which include variable and functions.
Output:
Hello First Namespace Hello Second Namespace
Let's see another example of namespace where we are using "using" keyword so that we don't have to use complete name for accessing a namespace program.
Output:
Hello First Namespace