0% found this document useful (0 votes)
16 views

Complete_CPP_Code_Guide

Uploaded by

abasgneo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Complete_CPP_Code_Guide

Uploaded by

abasgneo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Complete C++ Code Guide

1. Hello World

// A simple C++ program to print Hello World

#include <iostream>

using namespace std;

int main() {

cout << "Hello, World!" << endl;

return 0;

2. Basic Input/Output

// Program to take user input and print it

#include <iostream>

using namespace std;

int main() {

int age;

cout << "Enter your age: ";

cin >> age;

cout << "You are " << age << " years old!" << endl;

return 0;
}

3. Conditional Statements

// Program to demonstrate if-else

#include <iostream>

using namespace std;

int main() {

int num;

cout << "Enter a number: ";

cin >> num;

if (num % 2 == 0) {

cout << num << " is even." << endl;

} else {

cout << num << " is odd." << endl;

return 0;

4. Loops

// Program to print multiplication table of 6


#include <iostream>

using namespace std;

int main() {

int num = 6;

cout << "Multiplication Table of " << num << ":" << endl;

for (int i = 1; i <= 10; ++i) {

cout << num << " x " << i << " = " << num * i << endl;

return 0;

5. Arrays

// Program to calculate the sum of an array

#include <iostream>

using namespace std;

int main() {

int arr[5] = {1, 2, 3, 4, 5};

int sum = 0;

for (int i = 0; i < 5; ++i) {

sum += arr[i];

}
cout << "Sum of array elements: " << sum << endl;

return 0;

6. Functions

// Program to calculate factorial using a function

#include <iostream>

using namespace std;

int factorial(int n) {

if (n <= 1)

return 1;

return n * factorial(n - 1);

int main() {

int num;

cout << "Enter a number: ";

cin >> num;

cout << "Factorial of " << num << " is " << factorial(num) << endl;

return 0;

}
7. Object-Oriented Programming

// A simple demonstration of OOP in C++

#include <iostream>

using namespace std;

class Car {

public:

string brand;

int year;

void displayInfo() {

cout << "Brand: " << brand << ", Year: " << year << endl;

};

int main() {

Car myCar;

myCar.brand = "Toyota";

myCar.year = 2020;

myCar.displayInfo();

return 0;

8. File Handling
// Program to write and read from a file

#include <iostream>

#include <fstream>

using namespace std;

int main() {

ofstream file("example.txt");

file << "Hello, file handling in C++!" << endl;

file.close();

ifstream inFile("example.txt");

string content;

while (getline(inFile, content)) {

cout << content << endl;

inFile.close();

return 0;

9. Dynamic Memory

// Program to demonstrate dynamic memory allocation

#include <iostream>

using namespace std;


int main() {

int* ptr = new int; // Allocate memory

*ptr = 100;

cout << "Value: " << *ptr << endl;

delete ptr; // Free memory

return 0;

You might also like