C
C
C
Before starting the abcd of C++ language, you need to learn how to write, compile and run
the first C++ program.
To write the first C++ program, open the C++ console and write the following code:
1. #include <iostream.h>
2. #include<conio.h>
3. void main() {
4. clrscr();
5. cout << "Welcome to C++ Programming.";
6. getch();
7. }
#include <conio.h> includes the console input output library functions. The getch()
function is defined in conio.h file.
void main() The main() function is the entry point of every program in C++
language. The void keyword specifies that it returns no value.
cout << "Welcome to C++ Programming." is used to print the data "Welcome to
C++ Programming." on the console.
getch() The getch() function asks for a single character. Until you press any key, it
blocks the screen.
How to compile and run the C++ program
There are 2 ways to compile and run the C++ program, by menu and by shortcut.
By menu
Now click on the compile menu then compile sub menu to compile the c++ program.
Then click on the run menu then run sub menu to run the c++ program.
By shortcut
Or, press ctrl+f9 keys compile and run the program directly.
You can view the user screen any time by pressing the alt+f5 keys.
If bytes flow from main memory to device like printer, display screen, or a network
connection, etc, this is called as output operation.
If bytes flow from device like printer, display screen, or a network connection, etc to main
memory, this is called as input operation.
I/O Library Header Files
Let us see the common header files used in C++ programming are:
<iostream> It is used to define the cout, cin and cerr objects, which correspond to standard outpu
standard input stream and standard error stream, respectively.
<iomanip> It is used to declare services useful for performing formatted I/O, such as setprecision
1. #include <iostream>
2. using namespace std;
3. int main( ) {
4. char ary[] ="Welcome to C++ tutorial";
5. cout << "Value of ary is: " << ary << endl;
6. }
Output:
1. #include <iostream>
2. using namespace std;
3. int main( ) {
4. int age;
5. cout << "Enter your age: ";
6. cin >> age;
7. cout << "Your age is: " << age << endl;
8. }
Output:
1. #include <iostream>
2. using namespace std;
3. int main( ) {
4. cout << "C++ Tutorial";
5. cout << " Javatpoint"<<endl;
6. cout << "End of line"<<endl;
7. }
Output:
C++ Variable
A variable is a name of memory location. It is used to store data. Its value can be changed
and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.
1. type variable_list;
The example of declaring variable is given below:
1. int x;
2. float y;
3. char z;
Here, x, y, z are variables and int, float, char are data types.
We can also provide values while declaring the variables as given below:
A variable name can start with alphabet and underscore only. It can't start with digit.
A variable name must not be any reserved word or keyword e.g. char, float etc.
1. int a;
2. int _ab;
3. int a30;
1. int 4;
2. int x y;
3. int double;
The memory size of basic data types may change according to 32 or 64 bit operating
system.
Let's see the basic data types. It size is given according to 32 bit OS.
float 4 byte
double 8 byte
C++ Keywords
A keyword is a reserved word. You cannot use it as a variable name, constant name etc. A
list of 32 Keywords in C++ Language which are also available in C language are
given below.
A list of 30 Keywords in C++ Language which are not available in C language are
given below.
C++ Operators
An operator is simply a symbol that is used to perform operations. There can be many types
of operations like arithmetic, logical, bitwise etc.
o Arithmetic Operators
o Relational Operators
o Logical Operators
o Bitwise Operators
o Assignment Operator
o Unary operator
o Ternary or Conditional Operator
o Misc Operator
Precedence of Operators in C++
The precedence of operator species that which operator will be evaluated first and next. The
associativity specifies the operators direction to be evaluated, it may be left to right or right
to left.
1. int data=5+10*10;
The "data" variable will contain 105 because * (multiplicative operator) is evaluated before
+ (additive operator).
Additive +- Rig
Bitwise OR | Rig
Logical OR || Lef
Conditional ?: Rig
Comma , Lef
C++ if-else
In C++ programming, if statement is used to test the condition. There are various types of
if statements in C++.
o if statement
o if-else statement
o nested if statement
o if-else-if ladder
C++ IF Statement
The C++ if statement tests the condition. It is executed if condition is true.
1. if(condition){
2. //code to be executed
3. }
C++ If Example
1. #include <iostream>
2. using namespace std;
3.
4. int main () {
5. int num = 10;
6. if (num % 2 == 0)
7. {
8. cout<<"It is even number";
9. }
10. return 0;
11. }
Output:/p>
It is even number
1. if(condition){
2. //code if condition is true
3. }else{
4. //code if condition is false
5. }
C++ If-else Example
1. #include <iostream>
2. using namespace std;
3. int main () {
4. int num = 11;
5. if (num % 2 == 0)
6. {
7. cout<<"It is even number";
8. }
9. else
10. {
11. cout<<"It is odd number";
12. }
13. return 0;
14. }
Output:
It is odd number
Output:
Enter a number:11
It is odd number
Output:
Enter a number:12
It is even number
1. if(condition1){
2. //code to be executed if condition1 is true
3. }else if(condition2){
4. //code to be executed if condition2 is true
5. }
6. else if(condition3){
7. //code to be executed if condition3 is true
8. }
9. ...
10. else{
11. //code to be executed if all the conditions are false
12. }
Output:
Output:
1. switch(expression){
2. case value1:
3. //code to be executed;
4. break;
5. case value2:
6. //code to be executed;
7. break;
8. ......
9.
10. default:
11. //code to be executed if all cases are not matched;
12. break;
13. }
C++ Switch Example
1. #include <iostream>
2. using namespace std;
3. int main () {
4. int num;
5. cout<<"Enter a number to check grade:";
6. cin>>num;
7. switch (num)
8. {
9. case 10: cout<<"It is 10"; break;
10. case 20: cout<<"It is 20"; break;
11. case 30: cout<<"It is 30"; break;
12. default: cout<<"Not 10, 20 or 30"; break;
13. }
14. }
Output:
Enter a number:
10
It is 10
Output:
Enter a number:
55
Not 10, 20 or 30
The C++ for loop is same as C/C#. We can initialize variable, check condition and
increment/decrement value.
Flowchart:
C++ For Loop Example
1. #include <iostream>
2. using namespace std;
3. int main() {
4. for(int i=1;i<=10;i++){
5. cout<<i <<"\n";
6. }
7. }
Output:
1
2
3
4
5
6
7
8
9
10
1. #include <iostream>
2. using namespace std;
3.
4. int main () {
5. for(int i=1;i<=3;i++){
6. for(int j=1;j<=3;j++){
7. cout<<i<<" "<<j<<"\n";
8. }
9. }
10. }
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Output:
1. while(condition){
2. //code to be executed
3. }
Flowchart:
C++ While Loop Example
Let's see a simple example of while loop to print table of 1.
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int i=1;
5. while(i<=10)
6. {
7. cout<<i <<"\n";
8. i++;
9. }
10. }
Output:
1
2
3
4
5
6
7
8
9
10
Let's see a simple example of nested while loop in C++ programming language.
1. #include <iostream>
2. using namespace std;
3. int main () {
4. int i=1;
5. while(i<=3)
6. {
7. int j = 1;
8. while (j <= 3)
9. {
10. cout<<i<<" "<<j<<"\n";
11. j++;
12. }
13. i++;
14. }
15. }
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
1. #include <iostream>
2. using namespace std;
3. int main () {
4. while(true)
5. {
6. cout<<"Infinitive While Loop";
7. }
8. }
Output:
The C++ do-while loop is executed at least once because condition is checked after loop
body.
1. do{
2. //code to be executed
3. }while(condition);
Flowchart:
C++ do-while Loop Example
Let's see a simple example of C++ do-while loop to print the table of 1.
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int i = 1;
5. do{
6. cout<<i<<"\n";
7. i++;
8. } while (i <= 10) ;
9. }
Output:
1
2
3
4
5
6
7
8
9
10
C++ Nested do-while Loop
In C++, if you use do-while loop inside another do-while loop, it is known as nested do-
while loop. The nested do-while loop is executed fully for each outer do-while loop.
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int i = 1;
5. do{
6. int j = 1;
7. do{
8. cout<<i<<"\n";
9. j++;
10. } while (j <= 3) ;
11. i++;
12. } while (i <= 3) ;
13. }
Output:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
1. do{
2. //code to be executed
3. }while(true);
Output:
1. jump-statement;
2. break;
Flowchart:
C++ Break Statement Example
Let's see a simple example of C++ break statement which is used inside the loop.
1. #include <iostream>
2. using namespace std;
3. int main() {
4. for (int i = 1; i <= 10; i++)
5. {
6. if (i == 5)
7. {
8. break;
9. }
10. cout<<i<<"\n";
11. }
12. }
Output:
1
2
3
4
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. for(int i=1;i<=3;i++){
6. for(int j=1;j<=3;j++){
7. if(i==2&&j==2){
8. break;
9. }
10. cout<<i<<" "<<j<<"\n";
11. }
12. }
13. }
Output:
1 1
1 2
1 3
2 1
3 1
3 2
3 3
1. jump-statement;
2. continue;
Output:
1
2
3
4
6
7
8
9
10
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. for(int i=1;i<=3;i++){
6. for(int j=1;j<=3;j++){
7. if(i==2&&j==2){
8. continue;
9. }
10. cout<<i<<" "<<j<<"\n";
11. }
12. }
13. }
Output:
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
C++ Functions
The function in C++ language is also known as procedure or subroutine in other
programming languages.
To perform any task, we can create function. A function can be called many times. It
provides modularity and code reusability.
Advantage of functions in C
There are many advantages of functions.
1) Code Reusability
By creating functions in C++, you can call it many times. So we don't need to write the
same code again and again.
2) Code optimization
Suppose, you have to check 3 numbers (531, 883 and 781) whether it is prime number or
not. Without using function, you need to write the prime number logic 3 times. So, there is
repetition of code.
But if you use functions, you need to write the logic only once and you can reuse it several
times.
Types of Functions
There are two types of functions in C programming:
1. Library Functions: are the functions which are declared in the C++ header files such as
ceil(x), cos(x), exp(x), etc.
2. User-defined functions: are the functions which are created by the C++ programmer,
so that he/she can use it many times. It reduces complexity of a big program and optimizes
the code.
Declaration of a function
The syntax of creating function in C++ language is given below:
1. #include <iostream>
2. using namespace std;
3. void func() {
4. static int i=0; //static variable
5. int j=0; //local variable
6. i++;
7. j++;
8. cout<<"i=" << i<<" and j=" <<j<<endl;
9. }
10. int main()
11. {
12. func();
13. func();
14. func();
15. }
Output:
i= 1 and j= 1
i= 2 and j= 1
i= 3 and j= 1
Trignometric functions
Method Description
Method Description
Exponential functions
Method Description
frexp(value_type x,int* exp) It breaks a number into significand and 2 raised to the power exp
Method Description
Power functions
Method Description
Method Description
llround(x) It rounds off the value of x and cast to long long integer.
lrint(x) It rounds off the value of x using rounding mode and cast to long integer.
llrint(x) It rounds off the value x and cast to long long integer.
Other functions
Method Description
Method Description
fpclassify(x) It returns the value of type that matches one of the macro constants.
Method Description
Method Description
C++ Iterators
Iterators are just like pointers used to access the container elements.
Important Points:
o Iterators are used to traverse from one element to another element, a process is
known as iterating through the container.
o The main advantage of an iterator is to provide a common interface for all the
containers type.
o Iterators make the algorithm independent of the type of the container used.
o Iterators provide a generic approach to navigate through the elements of a
container.
Syntax
1. <ContainerType> :: iterator;
2. <ContainerType> :: const_iterator;
Operations Performed on the Iterators:
o Operator (*) : The '*' operator returns the element of the current position pointed
by the iterator.
o Operator (++) : The '++' operator increments the iterator by one. Therefore, an
iterator points to the next element of the container.
o Operator (==) and Operator (!=) : Both these operators determine whether the
two iterators point to the same position or not.
o Operator (=) : The '=' operator assigns the iterator.
The container classes provide two basic member functions that allow to iterate or move
through the elements of a container:
o begin(): The begin() function returns an iterator pointing to the first element of the
container.
o end(): The end() function returns an iterator pointing to the past-the-last element
of the container.
1. #include <iostream>
2. #include<iterator>
3. #include<vector>
4. using namespace std;
5. int main()
6. {
7. std::vector<int> v{1,2,3,4,5};
8. vector<int>::iterator itr;
9. for(itr=v.begin();itr!=v.end();itr++)
10. {
11. std::cout << *itr <<" ";
12. }
13. return 0;
14. }
Output:
1 2 3 4 5
Iterator Categories
An iterator can be categorized in the following ways:
o Input Iterator
o Output Iterator
o Forward Iterator
o Bidirectional Iterator
o Random Access Iterator
Input Iterator: An input iterator is an iterator used to access the elements from the
container, but it does not modify the value of a container.
o Increment operator(++)
o Equal operator(==)
o Not equal operator(!=)
o Dereference operator(*)
Output Iterator: An output iterator is an iterator used to modify the value of a container,
but it does not read the value from a container. Therefore, we can say that an output
iterator is a write-only iterator.
o Increment operator(++)
o Assignment operator(=)
Forward Iterator: A forward iterator is an iterator used to read and write to a container. It
is a multi-pass iterator.
o Increment operator(++)
o Assignment operator(=)
o Equal operator(=)
o Not equal operator(!=)
o Increment operator(++)
o Assignment operator(=)
o Equal operator(=)
o Not equal operator(!=)
o Decrement operator(--)
Random Access Iterator: A Random Access iterator is an iterator provides random access
of an element at an arbitrary location. It has all the features of a bidirectional iterator plus it
adds one more feature, i.e., pointer addition and pointer subtraction to provide random
access to an element.
Providers Of Iterators
Forward iterator
Disadvantages of iterator
o If we want to move from one data structure to another at the same time, iterators
won't work.
o If we want to update the structure which is being iterated, an iterator won?t allow us
to do because of the way it stores the position.
o If we want to backtrack while processing through a list, the iterator will not work in
this case.
Advantages of iterator
Following are the advantages of an iterator:
1. #include <iostream>
2. #include<vector>
3. #include<iterator>
4. using namespace std;
5. int main()
6. {
7. vector<int> v{1,2,3,4,5};
8. vector<int>::iterator itr;
9. for(int i=0;i<5;i++) // Traversal without using an iterator.
10. {
11. cout<<v[i]<<" ";
12. }
13. cout<<'\n';
14. for(itr=v.begin();itr!=v.end();itr++) // Traversal by using an iterator.
15. {
16. cout<<*itr<<" ";
17. }
18. v.push_back(10);
19. cout<<'\n';
20. for(int i=0;i<6;i++)
21. {
22. cout<<v[i]<<" ";
23. }
24. cout<<'\n';
25. for(itr=v.begin();itr!=v.end();itr++)
26. {
27. cout<<*itr<<" ";
28. }
29. return 0;
30. }
Output:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5 10
1 2 3 4 5 10
In the above example, we observe that if we traverse the elements of a vector without
using an iterator, then we need to keep track of the number of elements added in the
container.
o Code Reusability: A code can be reused if we use iterators. In the above example,
if we replace vector with the list, and then the subscript operator[] would not work to
access the elements as the list does not support the random access. However, we
use iterators to access the elements, then we can also access the list elements.
o Dynamic Processing: C++ iterators provide the facility to add or delete the data
dynamically.
1. #include <iostream>
2. #include<vector>
3. #include<iterator>
4. using namespace std;
5. int main()
6. {
7. vector<int> v{1,2,3,4,5}; // vector declaration
8. vector<int>::iterator itr;
9. v.insert(v.begin()+1,10);
10. for(itr=v.begin();itr!=v.end();itr++)
11. {
12. cout<<*itr<<" ";
13. }
14. return 0;
15. }
Output:
1 10 2 3 4 5
In the above example, we insert a new element at the second position by using insert()
function and all other elements are shifted by one.
C++ Namespaces
Namespaces in C++ are used to organize too many classes so that it can be easy to handle
the application.
In C++, global namespace is the root namespace. The global::std will always refer to the
namespace "std" of C++ Framework.
C++ namespace Example
Let's see the simple example of namespace which include variable and functions.
1. #include <iostream>
2. using namespace std;
3. namespace First {
4. void sayHello() {
5. cout<<"Hello First Namespace"<<endl;
6. }
7. }
8. namespace Second {
9. void sayHello() {
10. cout<<"Hello Second Namespace"<<endl;
11. }
12. }
13. int main()
14. {
15. First::sayHello();
16. Second::sayHello();
17. return 0;
18. }
Output:
1. #include <iostream>
2. using namespace std;
3. namespace First{
4. void sayHello(){
5. cout << "Hello First Namespace" << endl;
6. }
7. }
8. namespace Second{
9. void sayHello(){
10. cout << "Hello Second Namespace" << endl;
11. }
12. }
13. using namespace First;
14. int main () {
15. sayHello();
16. return 0;
17. }
Output:
C++ Vector
A vector is a sequence container class that implements dynamic array, means size
automatically changes when appending elements. A vector stores the elements in
contiguous memory locations and allocates the memory as needed at run time.
Syntax
Consider a vector 'v1'. Syntax would be:
1. vector<object_type> v1;
Example
Let's see a simple example.
1. #include<iostream>
2. #include<vector>
3. using namespace std;
4. int main()
5. {
6. vector<string> v1;
7. v1.push_back("javaTpoint ");
8. v1.push_back("tutorial");
9. for(vector<string>::iterator itr=v1.begin();itr!=v1.end();++itr)
10. cout<<*itr;
11. return 0;
12. }
Output:
javaTpoint tutorial
In this example, vector class has been used to display the string.
Function Description
rend() It points the element preceding the first element of the vector.
crend() It refers to the element preceding the first element of the vector.
shrink_to_fit() It reduces the capacity and makes it equal to the size of the vector.
C++ Programs
C++ programs are frequently asked in the interview. These programs can be asked from
basics, array, string, pointer, linked list, file handling etc. Let's see the list of top c++
programs.
1) Fibonacci Series
Write a c++ program to print fibonacci series without using recursion and using recursion.
Input: 10
Output: 0 1 1 2 3 5 8 13 21 34
2) Prime number
Input: 17
Input: 57
3) Palindrome number
Input: 121
Input: 113
4) Factorial
Input: 5
Output: 120
Input: 6
Output: 720
5) Armstrong number
Input: 371
Output: armstrong
Input: 342
6) Sum of Digits
Input: 23
Output: 5
Input: 624
Output: 12
7) Reverse Number
Input: 234
Output: 432
Write a c++ program to swap two numbers without using third variable.
Input: a=5 b=10
9) Matrix Multiplication
Input:
Output:
Input: 9
Output: 1001
Input: 20
Output: 10100
Output:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
Input: 7
Output:
Input: 5
Output:
1
1 1
1 1 2
1 1 2 3
1 1 2 3 5
Input: 74254
Input: 203
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 printe
d
9. {
10. n3=n1+n2;
11. cout<<n3<<" ";
12. n1=n2;
13. n2=n3;
14. }
15. return 0;
16. }
Output:
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:
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:
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:
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".
There are many ways to write the factorial program in C++ language. Let's see the 2 ways
to write the factorial program.
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:
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:
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.
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
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:
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
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:
1. By + and -
2. By * 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=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
In matrix multiplication first matrix one row element is multiplied by second matrix all
column elements.
Let's try to understand the matrix multiplication of 3*3 and 3*3 matrices by the figure
given below:
Let's see the program of matrix multiplication in C++.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
6. cout<<"enter the number of row=";
7. cin>>r;
8. cout<<"enter the number of column=";
9. cin>>c;
10. cout<<"enter the first matrix element=\n";
11. for(i=0;i<r;i++)
12. {
13. for(j=0;j<c;j++)
14. {
15. cin>>a[i][j];
16. }
17. }
18. cout<<"enter the second matrix element=\n";
19. for(i=0;i<r;i++)
20. {
21. for(j=0;j<c;j++)
22. {
23. cin>>b[i][j];
24. }
25. }
26. cout<<"multiply of the matrix=\n";
27. for(i=0;i<r;i++)
28. {
29. for(j=0;j<c;j++)
30. {
31. mul[i][j]=0;
32. for(k=0;k<c;k++)
33. {
34. mul[i][j]+=a[i][k]*b[k][j];
35. }
36. }
37. }
38. //for printing result
39. for(i=0;i<r;i++)
40. {
41. for(j=0;j<c;j++)
42. {
43. cout<<mul[i][j]<<" ";
44. }
45. cout<<"\n";
46. }
47. return 0;
48. }
Output:
Decimal Number
Decimal number is a base 10 number because it ranges from 0 to 9, there are total 10 digits
between 0 to 9. Any combination of digits is decimal number such as 223, 585, 192, 0, 7
etc.
Binary Number
Binary number is a base 2 number because it is either 0 or 1. Any combination of 0 and 1 is
binary number such as 1001, 101, 11111, 101010 etc.
Let's see the some binary numbers for the decimal number.
Decimal Binary
1 0
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
Decimal to Binary Conversion Algorithm
Step 1: Divide the number by 2 through % (modulus operator) and store the remainder in
array
Step 3: Repeat the step 2 until the number is greater than zero
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a[10], n, i;
6. cout<<"Enter the number to convert: ";
7. cin>>n;
8. for(i=0; n>0; i++)
9. {
10. a[i]=n%2;
11. n= n/2;
12. }
13. cout<<"Binary of the given number= ";
14. for(i=i-1 ;i>=0 ;i--)
15. {
16. cout<<a[i];
17. }
18. }
Output:
Output:
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. char ch='A';
6. int i, j, k, m;
7. for(i=1;i<=5;i++)
8. {
9. for(j=5;j>=i;j--)
10. cout<<" ";
11. for(k=1;k<=i;k++)
12. cout<<ch++;
13. ch--;
14. for(m=1;m<i;m++)
15. cout<<--ch;
16. cout<<"\n";
17. ch='A';
18. }
19. return 0;
20. }
Output:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int i,j,k,l,n;
6. cout<<"Enter the Range=";
7. cin>>n;
8. for(i=1;i<=n;i++)
9. {
10. for(j=1;j<=n-i;j++)
11. {
12. cout<<" ";
13. }
14. for(k=1;k<=i;k++)
15. {
16. cout<<k;
17. }
18. for(l=i-1;l>=1;l--)
19. {
20. cout<<l;
21. }
22. cout<<"\n";
23. }
24. return 0;
25. }
Output:
This project uses RFID technology for making a note of every student entering in a
classroom and also calculates the time resides in a class.
In this system every student is allotted with an RFID tag. The attendance can be done by
placing card near RFID reader.
What is RFID?
The Radio Frequency Identification (RFID) is an electronic device which includes a small
antenna and a chip. This device is used for transmitting information between reader and
RFID tag using radio frequency electromagnetic fields.
The operating frequency ranges of these devices include low, mid and high ranges. The low
frequency range is from 30 KHz to 500 KHz, middle frequency range is from 500 KHz to 900
KHz and high frequency range is from 2.4 MHz to 2.5 MHz
The block diagram of this system includes preset circuit, a microcontroller, an oscillator
circuit, LCD display and an RFID reader.
o Microcontroller-The microcontroller used in a RFID based attendance system is
AT89C52 from the family of 8051. It includes 4-I/O ports and 40-pins.
o Oscillator Circuit-Oscillator circuit is connected in between the pins of 18 and 19 of
a microcontroller. This circuit is built with a combination of oscillator and two 33 pF
capacitors at 11.0592 MHz operational frequency.
o Preset Circuit-The microcontroller AT89C52 9th pin is reset pin. The circuit of the
preset is built with a capacitor, a resistor and a switch. When switch is pressed, the
reset pin is connected with and the microcontroller becomes reset.
o LCD Display-LCD display is consists of 16 pins, where 3 pins are connected with and
remaining all pins are connected with the port-2 of the microcontroller.
o RFID Reader-RFID reader is used for reading the information which is stored in the
RFID tags. It incorporates with any type of hardware design.
If the data of the card matches with RFID reader, then information will display on LCD. By
using this system the operational time can be save as all the information is stored directly
inside the database.
Source Code:
Consider the embedded system program required for operating the microcontroller
system as RFID based attendance system is:-
1. #include<reg51.h>
2. #include<string.h>
3.
4. //0000 to 7FFF
5.
6. sbit RS = P0^7;
7. sbit EN = P0^6;
8.
9. sbit SDA = P1^0;
10. sbit SCL = P1^1;
11. sbit RELAY = P1^2;
12.
13. code unsigned char RFID_1[] = "34006C9C04C0"; //34006C9C04+NULL
14. code unsigned char RFID_2[] = "34006C549C90";
15. code unsigned char RFID_3[] = "1300F8FAC1D0";
16. code unsigned char RFID_4[] = "34006CD5AD20";
17. code unsigned char RFID_5[] = "420061231E1E";
18.
19. code unsigned char name_1[] = "SANJAY JAIN";
20. code unsigned char name_2[] = "SHEKHAT HARSH";
21. code unsigned char name_3[] = "DHOLARIYA RAKSHIT";
22. code unsigned char name_4[] = "DIVYANG SINGH ";
23. code unsigned char name_5[] = "NAKUL JAIN";
24.
25. unsigned char rs[15];
26.
27. unsigned int no_of_records;
28.
29. void delay()
30. {
31. int j;
32. for(j=0;j<500;j++);
33. }
34.
35. void long_delay()
36. {
37. unsigned int j;
38. for(j=0;j<65000;j++);
39. }
40.
41. void idelay()
42. {
43. unsigned int j;
44. for(j=0;j<10000;j++);
45. }
46.
47. void lcd_command(char lc)
48. {
49. P2 = lc;
50. RS = 0;
51. EN = 1;
52. delay();
53. EN = 0;
54. }
55.
56. void lcd_data(char ld)
57. {
58. P2 = ld;
59. RS = 1;
60. EN = 1;
61. delay();
62. EN = 0;
63. }
64.
65. void lcd_init()
66. {
67. lcd_command(0x38);
68. lcd_command(0x0E);
69. lcd_command(0x01);
70. }
71.
72. void serial_init()
73. {
74. TMOD = 0x20;
75. SCON = 0x50;
76. TH1 = 0xFD;
77. TR1 = 1;
78. }
79.
80. void transmit(unsigned char tx)
81. {
82. SBUF = tx;
83. while(TI==0);
84. TI = 0;
85. }
86.
87. void send_string(unsigned char *str)
88. {
89. int j;
90. for(j=0;str[j]!='\0';j++)
91. transmit(str[j]);
92. }
93.
94. unsigned char receive()
95. {
96. char rx;
97. while(RI==0);
98. RI = 0;
99. rx = SBUF;
100. return(rx);
101. }
102.
103. void lcd_string(char add,char *str)
104. {
105. int j;
106. lcd_command(add);
107. for(j=0;str[j]!='\0';j++)
108. lcd_data(str[j]);
109. }
110.
111. void start()
112. {
113. SDA = 1;
114. SCL = 1;
115. SDA = 0;
116. }
117.
118. void stop()
119. {
120. SDA = 0;
121. SCL = 1;
122. SDA = 1;
123. }
124.
125. void write(unsigned char w)
126. {
127. int j;
128. SCL = 0;
129. for(j=0;j<8;j++)
130. {
131. if((w & 0x80)==0)
132. SDA = 0;
133. else
134. SDA = 1;
135. SCL = 1;
136. SCL = 0;
137. ww = w << 1;
138. }
139. SCL = 1;
140. SCL = 0;
141. }
142.
143. unsigned char read()
144. {
145. int j;
146. unsigned char r = 0x00;
147. SDA = 1;
148.
149. for(j=0;j<8;j++)
150. {
151. SCL = 1;
152. rr = r << 1;
153. if(SDA == 1)
154. rr = r | 0x01;
155. SCL = 0;
156. }
157. return(r);
158. }
159.
160. void ack()
161. {
162. SDA = 0;
163. SCL = 1;
164. SCL = 0;
165. }
166.
167. void nack()
168. {
169. SDA = 1;
170. SCL = 1;
171. SCL = 0;
172. }
173.
174. void rtc_read()
175. {
176. unsigned char ss,mm,hh,day,mn,date,yr;
177. start();
178. write(0xD0);
179. write(0x00);
180. stop();
181. start();
182. write(0xD1);
183. ss = read();
184. ack();
185. mm = read();
186. ack();
187. hh = read();
188. ack();
189. day = read();
190. ack();
191. date = read();
192. ack();
193. mn = read();
194. ack();
195. yr = read();
196. nack();
197. stop();
198.
199. rs[0] = hh/0x10 + 48;
200. rs[1] = hh%0x10 + 48;
201. rs[2] = ':';
202. rs[3] = mm/0x10 + 48;
203. rs[4] = mm%0x10 + 48;
204. rs[5] = ',';
205. rs[6] = date/0x10 + 48;
206. rs[7] = date%0x10 + 48;
207. rs[8] = '/';
208. rs[9] = mn/0x10 + 48;
209. rs[10] = mn%0x10 + 48;
210. rs[11] = '/';
211. rs[12] = yr/0x10 + 48;
212. rs[13] = yr%0x10 + 48;
213. rs[14] = '\0';
214. }
215.
216. void rtc_init()
217. {
218. start();
219. write(0xD0);
220. write(0x00);
221. write(0x00);
222. write(0x00);
223. write(0x13);
224. write(0x05);
225. write(0x12);
226. write(0x04);
227. write(0x12);
228. stop();
229.
230. }
231.
232. void write_records(unsigned char *str);
233. void read_records();
234.
235. void main()
236. {
237. unsigned char rec_data[13],i,t;
238.
239. RELAY = 0;
240.
241. lcd_init();
242. serial_init();
243. rtc_init();
244. idelay();
245. start();
246. write(0xA0);
247. write(0x7F);
248. write(0xFF);
249. stop();
250. start();
251. write(0xA1);
252. no_of_records = read();
253. nack();
254. stop();
255.
256. // no_of_records = 0;
257.
258. while(1)
259. {
260. start:
261. lcd_command(0x01);
262. lcd_string(0x80,"RFID ATTENDANCE");
263. lcd_string(0xC5,"SYSTEM");
264.
265. j = 0;
266. while(1)
267. {
268. if(RI==1)
269. {
270. RI = 0;
271. t = receive();
272. if(t == '+')
273. {
274. read_records();
275. goto start;
276. }
277. else
278. {
279. rec_data[j] = t;
280. for(j=1;j<12;j++)
281. rec_data[j] = receive();
282. rec_data[j] = '\0';
283. break;
284. }
285. }
286. }
287.
288. j = strcmp(RFID_1,rec_data); //match => j = 0
289.
290. lcd_command(0x01);
291.
292. if(j==0)
293. {
294. RELAY = 1;
295. lcd_string(0x80,name_1);
296. rtc_read();
297. lcd_string(0xC0,rs);
298. long_delay();
299. write_records(name_1);
300. RELAY = 0;
301. goto start;
302. }
303.
304. //
305. j = strcmp(RFID_2,rec_data); //match => j = 0
306.
307. if(j==0)
308. {
309. RELAY = 1;
310. lcd_string(0x80,name_2);
311. rtc_read();
312. lcd_string(0xC0,rs);
313. long_delay();
314. write_records(name_2);
315. RELAY = 0;
316. goto start;
317. }
318.
319. //
320. j = strcmp(RFID_3,rec_data); //match => j = 0
321.
322. if(j==0)
323. {
324. RELAY = 1;
325. lcd_string(0x80,name_3);
326. rtc_read();
327. lcd_string(0xC0,rs);
328. long_delay();
329. write_records(name_3);
330. RELAY = 0;
331. goto start;
332. }
333.
334. j = strcmp(RFID_4,rec_data); //match => j = 0
335.
336. if(j==0)
337. {
338. RELAY = 1;
339. lcd_string(0x80,name_4);
340. rtc_read();
341. lcd_string(0xC0,rs);
342. long_delay();
343. write_records(name_4);
344. RELAY = 0;
345. goto start;
346. }
347.
348. j = strcmp(RFID_5,rec_data); //match => j = 0
349.
350. if(j==0)
351. {
352. RELAY = 1;
353. lcd_string(0x80,name_5);
354. no_of_records = 0;
355. start();
356. write(0xA0);
357. write(0x7F);
358. write(0xFF);
359. write(0x00);
360. stop();
361. lcd_string(0xC0,"MEMORY CLEARED");
362. long_delay();
363. RELAY = 0;
364. goto start;
365. }
366. lcd_string(0x80,"ERROR");
367. lcd_string(0xC0,rec_data);
368. long_delay();
369. }
370. }