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.
Let's see another example to swap two numbers using + and -.
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.
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++.
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.
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.
Let's see the factorial program in C++ using recursion.
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.
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.
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.
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.
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.
Floating point exception (core dumped)
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.
|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.
Let's see the simple example of string comparison using strcmp() function.
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.
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.