From 88d752e7d82fc6cf5127110b506089094486c7a7 Mon Sep 17 00:00:00 2001 From: sivaprasath2004 Date: Tue, 28 May 2024 09:07:43 +0530 Subject: [PATCH 1/2] 3day added --- docs/day-03/_category_.json | 8 ++++ docs/day-03/datatypes.md | 96 +++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 docs/day-03/_category_.json create mode 100644 docs/day-03/datatypes.md diff --git a/docs/day-03/_category_.json b/docs/day-03/_category_.json new file mode 100644 index 000000000..afc85edb1 --- /dev/null +++ b/docs/day-03/_category_.json @@ -0,0 +1,8 @@ +{ + "label": "Day 03", + "position": 4, + "link": { + "type": "generated-index" + } + } + \ No newline at end of file diff --git a/docs/day-03/datatypes.md b/docs/day-03/datatypes.md new file mode 100644 index 000000000..84678179d --- /dev/null +++ b/docs/day-03/datatypes.md @@ -0,0 +1,96 @@ +# Data Types in C++ + +In C++, data types specify how we store and manipulate data. C++ provides several built-in data types to handle different kinds of data efficiently. + +## 1. Integer Types + +Integer types represent whole numbers. They can be signed (positive, negative, or zero) or unsigned (only positive or zero). + +### Examples: + +```cpp +#include + +int main() { + // Signed integers + int a = 10; // 4 bytes, range: -2,147,483,648 to 2,147,483,647 + short b = 20; // 2 bytes, range: -32,768 to 32,767 + long c = 30; // 4 bytes, platform-dependent + long long d = 40; // 8 bytes, range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 + + // Unsigned integers + unsigned int e = 50; // 4 bytes, range: 0 to 4,294,967,295 + unsigned short f = 60; // 2 bytes, range: 0 to 65,535 + unsigned long g = 70; // 4 bytes, platform-dependent + unsigned long long h = 80; // 8 bytes, range: 0 to 18,446,744,073,709,551,615 + + std::cout << "Signed Integers: " << a << ", " << b << ", " << c << ", " << d << std::endl; + std::cout << "Unsigned Integers: " << e << ", " << f << ", " << g << ", " << h << std::endl; + + return 0; +} +``` +## 2. Floating-Point Types + +Floating-point types represent real numbers. They can represent a wide range of values, but they have limited precision. + +### Example: +```cpp +#include + +int main() { + float a = 3.14f; // 4 bytes, 6 decimal places precision + double b = 3.14159; // 8 bytes, 15 decimal places precision + long double c = 3.1415926535L; // 10 or more bytes, platform-dependent precision + + std::cout << "Float: " << a << std::endl; + std::cout << "Double: " << b << std::endl; + std::cout << "Long Double: " << c << std::endl; + + return 0; +} +``` + +## 3. Character Types + +Character types represent single characters. They can be used to store individual characters and small integers. + +### Example: + +```cpp +#include + +int main() { + char a = 'A'; // 1 byte, range: -128 to 127 or 0 to 255 + wchar_t b = L'B'; // platform-dependent, at least 2 bytes + char16_t c = u'C'; // 2 bytes, Unicode character + char32_t d = U'D'; // 4 bytes, Unicode character + + std::cout << "Char: " << a << std::endl; + std::wcout << "Wide Char: " << b << std::endl; + std::cout << "Char16_t: " << c << std::endl; + std::cout << "Char32_t: " << d << std::endl; + + return 0; +} +``` + +## 4. Boolean Type + +Boolean type represents truth values. It can have two possible values: `true` or `false`. + +### Example: + +```cpp +#include + +int main() { + bool isTrue = true; + bool isFalse = false; + + std::cout << "isTrue: " << isTrue << std::endl; + std::cout << "isFalse: " << isFalse << std::endl; + + return 0; +} +``` From 6ee11b699248ea73a83a9918561422c386730ac8 Mon Sep 17 00:00:00 2001 From: sivaprasath2004 Date: Tue, 28 May 2024 17:16:06 +0530 Subject: [PATCH 2/2] proper content --- docs/day-03/1Basic_syntax.md | 109 +++++++++++++++++++++ docs/day-03/2Structure_of_a_C++ Program.md | 50 ++++++++++ docs/day-03/Comments_in_C++.md | 28 ++++++ docs/day-03/datatypes.md | 96 ------------------ 4 files changed, 187 insertions(+), 96 deletions(-) create mode 100644 docs/day-03/1Basic_syntax.md create mode 100644 docs/day-03/2Structure_of_a_C++ Program.md create mode 100644 docs/day-03/Comments_in_C++.md delete mode 100644 docs/day-03/datatypes.md diff --git a/docs/day-03/1Basic_syntax.md b/docs/day-03/1Basic_syntax.md new file mode 100644 index 000000000..cfe9a5433 --- /dev/null +++ b/docs/day-03/1Basic_syntax.md @@ -0,0 +1,109 @@ +# Basic C++ Syntax Explanation and Examples: + +## 1. Comments: + +Comments are used to document code for better understanding. They are ignored by the compiler. + +### Examples: + +```cpp +// This is a single-line comment + +/* + This is a multi-line comment +*/ + +``` +## 2. Main Function: + +The `main()` function is the entry point of any C++ program. It's where the execution of the program begins. + +### Example: +```cpp +#include + +int main() { + std::cout << "Hello, World!"; + return 0; +} + +``` + +## 3. Data Types: + +C++ supports various data types for different kinds of values. + +### Example: + +```cpp +int age = 25; // Integer +float pi = 3.14; // Floating point number +char grade = 'A'; // Character +bool isTrue = true; // Boolean (true/false) + + +``` + +## 4. Variables: + +Variables are used to store data values. + +### Example: + +```cpp +int x = 5; // Declare and initialize an integer variable +float y = 3.14; // Declare and initialize a floating point variable +``` + +## 5. Control Structures: + +Control structures help in controlling the flow of the program. + +### Example: + +```cpp +if (x > 0) { + // Code block executed if condition is true +} else { + // Code block executed if condition is false +} + +while (x > 0) { + // Code block executed repeatedly while condition is true +} + +for (int i = 0; i < 5; i++) { + // Code block executed repeatedly with incrementing i from 0 to 4 +} + +``` +## 6. Functions: + +Functions are blocks of code that perform a specific task. + +### Example: + +```cpp +int add(int a, int b) { + return a + b; +} + +``` +## 7. Classes: + +Classes are user-defined data types that contain data members and member functions. + +### Example: + +```cpp +class Rectangle { +public: + int width; + int height; + + int area() { + return width * height; + } +}; + +``` \ No newline at end of file diff --git a/docs/day-03/2Structure_of_a_C++ Program.md b/docs/day-03/2Structure_of_a_C++ Program.md new file mode 100644 index 000000000..e6560d2a0 --- /dev/null +++ b/docs/day-03/2Structure_of_a_C++ Program.md @@ -0,0 +1,50 @@ +# Structure of a C++ Program + +A typical C++ program consists of several parts: + +## Preprocessor Directives + +These are commands to the compiler and are preceded by a `#` symbol. Common preprocessor directives include `#include` for including header files and `#define` for defining constants. + +## Main Function + +Every C++ program must have a `main()` function, which serves as the entry point of the program. Execution of the program begins from here. + +## Function Definitions + +Additional functions may be defined to perform specific tasks within the program. These functions can be defined before or after the `main()` function. + +## Header Files + +C++ programs often use header files to declare functions, classes, and constants. These header files are included at the beginning of the program using the `#include` directive. + +## Namespace + +Namespace is used to avoid naming conflicts and to organize code into logical groups. Standard library elements are typically in the `std` namespace. + +## Comments + +Comments are used to document the code and improve its readability. They can be single-line (`//`) or multi-line (`/* */`). + +# Example C++ Program + +Here's an example of a simple C++ program that calculates the factorial of a number: + +```cpp +#include + +// Function to calculate factorial +int factorial(int n) { + if (n == 0 || n == 1) + return 1; + else + return n * factorial(n - 1); +} + +int main() { + int num; + std::cout << "Enter a number: "; + std::cin >> num; + std::cout << "Factorial of " << num << " is: " << factorial(num) << std::endl; + return 0; +} diff --git a/docs/day-03/Comments_in_C++.md b/docs/day-03/Comments_in_C++.md new file mode 100644 index 000000000..5ddffe206 --- /dev/null +++ b/docs/day-03/Comments_in_C++.md @@ -0,0 +1,28 @@ +# Comments in C++ + +Comments in C++ are essential for documenting code and making it more understandable. There are two types of comments in C++: + +## Single-line comments + +These comments begin with `//` and continue until the end of the line. They are often used for short, inline explanations. + +```cpp +// This is a single-line comment +int x = 5; // This is also a single-line comment +``` +# Multi-line Comments in C++ + +Multi-line comments in C++ begin with `/*` and end with `*/`. They can span multiple lines and are often used for longer explanations or for temporarily disabling blocks of code. + +```cpp +/* This is a multi-line comment + It can span multiple lines */ +int y = 10; + +/* +This is another example of a multi-line comment +that spans multiple lines +*/ +``` + +Comments are ignored by the compiler and do not affect the execution of the program. They are purely for the benefit of developers to understand the code better. It's good practice to include comments to explain complex logic, algorithmic steps, or to provide context for future maintainers of the code. \ No newline at end of file diff --git a/docs/day-03/datatypes.md b/docs/day-03/datatypes.md deleted file mode 100644 index 84678179d..000000000 --- a/docs/day-03/datatypes.md +++ /dev/null @@ -1,96 +0,0 @@ -# Data Types in C++ - -In C++, data types specify how we store and manipulate data. C++ provides several built-in data types to handle different kinds of data efficiently. - -## 1. Integer Types - -Integer types represent whole numbers. They can be signed (positive, negative, or zero) or unsigned (only positive or zero). - -### Examples: - -```cpp -#include - -int main() { - // Signed integers - int a = 10; // 4 bytes, range: -2,147,483,648 to 2,147,483,647 - short b = 20; // 2 bytes, range: -32,768 to 32,767 - long c = 30; // 4 bytes, platform-dependent - long long d = 40; // 8 bytes, range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 - - // Unsigned integers - unsigned int e = 50; // 4 bytes, range: 0 to 4,294,967,295 - unsigned short f = 60; // 2 bytes, range: 0 to 65,535 - unsigned long g = 70; // 4 bytes, platform-dependent - unsigned long long h = 80; // 8 bytes, range: 0 to 18,446,744,073,709,551,615 - - std::cout << "Signed Integers: " << a << ", " << b << ", " << c << ", " << d << std::endl; - std::cout << "Unsigned Integers: " << e << ", " << f << ", " << g << ", " << h << std::endl; - - return 0; -} -``` -## 2. Floating-Point Types - -Floating-point types represent real numbers. They can represent a wide range of values, but they have limited precision. - -### Example: -```cpp -#include - -int main() { - float a = 3.14f; // 4 bytes, 6 decimal places precision - double b = 3.14159; // 8 bytes, 15 decimal places precision - long double c = 3.1415926535L; // 10 or more bytes, platform-dependent precision - - std::cout << "Float: " << a << std::endl; - std::cout << "Double: " << b << std::endl; - std::cout << "Long Double: " << c << std::endl; - - return 0; -} -``` - -## 3. Character Types - -Character types represent single characters. They can be used to store individual characters and small integers. - -### Example: - -```cpp -#include - -int main() { - char a = 'A'; // 1 byte, range: -128 to 127 or 0 to 255 - wchar_t b = L'B'; // platform-dependent, at least 2 bytes - char16_t c = u'C'; // 2 bytes, Unicode character - char32_t d = U'D'; // 4 bytes, Unicode character - - std::cout << "Char: " << a << std::endl; - std::wcout << "Wide Char: " << b << std::endl; - std::cout << "Char16_t: " << c << std::endl; - std::cout << "Char32_t: " << d << std::endl; - - return 0; -} -``` - -## 4. Boolean Type - -Boolean type represents truth values. It can have two possible values: `true` or `false`. - -### Example: - -```cpp -#include - -int main() { - bool isTrue = true; - bool isFalse = false; - - std::cout << "isTrue: " << isTrue << std::endl; - std::cout << "isFalse: " << isFalse << std::endl; - - return 0; -} -```