1. Basic Program
#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}2. Explanation of Program
-
#include iostream: Used for input and output operations.
-
using namespace std;: Allows use of standard library objects without writing std::.
-
cout: Used to display output.
-
int main(): Main function where program execution begins.
-
cout: Used to display output.
-
return 0;: Indicates successful execution
3. Applications of C++
| Field | Usage |
|---|---|
| Game Development | Game engines |
| Operating Systems | Windows, Linux components |
| Web Browsers | Browser engines |
| Embedded Systems | Microcontrollers |
| Databases | MySQL |
| Competitive Programming | Fast execution |
4. Variables & Data Types
4.1 Variables
Variables are named memory locations that hold values.
4.2 Data Types
Data types define the kind of data a variable can hold.
| Data Type | Size | Example |
|---|---|---|
| int | 4 Bytes | 10 |
| float | 4 Bytes | 3.14 |
| double | 8 Bytes | 10.5678 |
| char | 1 Byte | 'A' |
| bool | 1 Byte | true |
| short | 2 Bytes | 120 |
| long | 4 or 8 Bytes | 123456 |
| long long | 8 Bytes | 9876543210 |
| string | Depends on Length | "Hello" |
#include <iostream>
using namespace std;
int main()
{
int num = 534;
cout << num << endl;
long int lnum = 34;
cout << lnum << endl;
float x = 35.99;
cout << x << endl;
double a = 93.98;
cout << a << endl;
char c = 'A';
cout << c << endl;
bool isTrue = false;
cout << isTrue << endl;
string str = "Hello World!";
cout << str << endl;
return 0;
}5. Type Casting
Type casting is the process of converting one data type to another.
5.1 Implicit Type Casting
- Automatic conversion of lower precision to higher precision data type.
- No risk of data loss.
Example:
#include <iostream>
using namespace std;
int main()
{
int a = 10;
double b = a;
cout << b << endl;
return 0;
}5.2 Explicit Type Casting
- Manual conversion of higher precision to lower precision data type.
- Risk of data loss.
Example:
#include <iostream>
using namespace std;
int main()
{
double a = 10.5;
int b = (int)a;
// int b = static_cast<int>(a);
cout << b << endl;
return 0;
}6. Conditional Statement
Conditional statements are used to make decisions on the basis of a condition.
Write a program to check if a number is even or odd using if-else statement.
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n; //? read the user input
if (n % 2 == 0) {
cout << n << " is even no." << endl;
} else {
cout << n << " is odd no." << endl;
}
return 0;
}7. Loop Statement
Loop statements are used to repeat a block of code until a condition condition is met.
- for loop
- while loop
- do-while loop
7.1 for Loop
- Used to repeat a block of code a fixed number of times.
- Syntax: for (initialization; condition; increment/decrement)
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 5; i++)
{
cout << i << endl;
}
return 0;
}7.2 while Loop
- Used to repeat a block of code until a condition is met.
- Syntax: while (condition)
#include <iostream>
using namespace std;
int main()
{
int i = 0;
while (i < 5)
{
cout << i << endl;
i++;
}
return 0;
}7.3 do-while Loop
- Used to repeat a block of code at least once.
- Syntax: do while (condition);
#include <iostream>
using namespace std;
int main()
{
int i = 0;
do
{
cout << i << endl;
i++;
} while (i < 5);
return 0;
}8. Operators
Operators are symbols that are used to perform operations on variables and values.
8.1 Arithmetic Operators
- + (Addition)
- - (Subtraction)
- * (Multiplication)
- / (Division)
- % (Modulus)
8.2 Assignment Operators
- = (Assignment)
- += (Addition Assignment)
- -= (Subtraction Assignment)
- *= (Multiplication Assignment)
- /= (Division Assignment)
- %= (Modulus Assignment)
8.3 Comparison Operators
- == (Equal)
- != (Not Equal)
- == (Identity)
- !== (Not Identity)
- < (Less Than)
- > (Greater Than)
- <= (Less Than or Equal)
- >= (Greater Than or Equal)
8.4 Logical Operators
- && (AND)
- || (OR)
- ! (NOT)
8.5 Bitwise Operators
- & (AND)
- | (OR)
- ~ (NOT)
- ^ (XOR)
- << (Left Shift)
- >> (Right Shift)
8.6 Pre & Post Increment/Decrement Operators
- ++ (Pre-Increment): first increment then use the value
- -- (Pre-Decrement): first decrement then use the value
- ++ (Post-Increment): first use the value then increment
- -- (Post-Decrement): first use the value then decrement
Example:
#include <iostream>
using namespace std;
int main() {
//* Arithmetic Operators
int a = 5, b = 3;
cout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
cout << "a * b = " << a * b << endl;
cout << "a / b = " << a / b << endl;
cout << "a % b = " << a % b << endl;
//* Relational Operators
cout << "a == b = " << (a == b) << endl;
cout << "a != b = " << (a != b) << endl;
cout << "a > b = " << (a > b) << endl;
cout << "a < b = " << (a < b) << endl;
cout << "a >= b = " << (a >= b) << endl;
cout << "a <= b = " << (a <= b) << endl;
//* Logical Operators
cout << "a && b = " << (a && b) << endl;
cout << "a || b = " << (a || b) << endl;
//* Bitwise Operators
cout << "a & b = " << (a & b) << endl;
cout << "a | b = " << (a | b) << endl;
cout << "~a = " << (~a) << endl;
cout << "a << b = " << (a << b) << endl;
cout << "a >> b = " << (a >> b) << endl;
//* Assignment Operators
cout << "a += b = " << (a += b) << endl;
cout << "a -= b = " << (a -= b) << endl;
cout << "a *= b = " << (a *= b) << endl;
cout << "a /= b = " << (a /= b) << endl;
cout << "a %= b = " << (a %= b) << endl;
return 0;
}9. Switch Statement, Break & Continue
9.1 Switch Statement
- Used to execute one of many blocks of code based on the value of a variable.
- Syntax: switch (expression) { case label: code; break; default: code; break; }
#include <iostream>
using namespace std;
int main()
{
int a = 3;
switch (a)
{
case 1:
cout << "a is 1" << endl;
break;
case 2:
cout << "a is 2" << endl;
break;
case 3:
cout << "a is 3" << endl;
break;
default:
cout << "a is not 1, 2, or 3" << endl;
break;
}
return 0;
}9.2 Break Statement
- Used to exit a loop or switch statement.
- Syntax: break;
9.3 Continue Statement
- Used to skip the current iteration of a loop and continue with the next iteration.
- Syntax: continue;
10. Some Basic Programs
10.1 Multiplication Table of n
Write a program to print the multiplication table of n.
Code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter value of n: ";
cin >> n;
for (int i = 1; i <= 10; i++)
{
cout << n << " * " << i << " = " << n * i << endl;
}
return 0;
}Output:
Enter value of n: 12 12 * 1 = 12 12 * 2 = 24 12 * 3 = 36 12 * 4 = 48 12 * 5 = 60 12 * 6 = 72 12 * 7 = 84 12 * 8 = 96 12 * 9 = 108 12 * 10 = 120
10.2 Power of a Number
Write a program to calculate the power of a number.
Code:
#include <iostream>
using namespace std;
int main()
{
int base, power;
cout << "Enter a base: ";
cin >> base;
cout << "Enter a power: ";
cin >> power;
int res = base;
for (int i = 1; i < power; i++)
{
res = res * base;
}
cout << base << "^" << power << " = " << res << endl;
return 0;
}Output:
Enter a base: 3 Enter a power: 3 3^3 = 27
10.3 Check if a Number is Prime or Not
Write a program to check if a number is prime or not.
Code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a no: ";
cin >> n;
bool isPrime = false;
if (n < 2)
{
isPrime = false;
}
else if (n == 2)
{
isPrime = true;
}
else if (n % 2 == 0)
{
isPrime = false;
}
else
{
for (int i = 3; i * i <= n; i+=2)
{
if (n % i == 0)
{
isPrime = false;
break;
}
}
isPrime = true;
}
if (isPrime)
{
cout << n << " is a prime no." << endl;
}
else
{
cout << n << " is not a prime no." << endl;
}
return 0;
}Output:
Enter a no: 12 12 is not a prime no.
10.4 Fibonacci Series
Write a program to print the first n Fibonacci numbers.
n = 11: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,
Code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter a no: ";
cin >> n;
int a = 0, b = 1, c = 0;
for (int i = 1; i <= n; i++)
{
cout << c << ", ";
a = b;
b = c;
c = a + b;
}
return 0;
}Output:
Enter a no: 12 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,