Author: saqibkhan

  • Function Overloading in C++

    Function overloading in C++ allows you to define multiple functions with the same name but different parameters. Function overloading is used to achieve polymorphism which is an important concept of object-oriented programming systems.

    Syntax for Overloaded Functions

    Consider the following two function declarations having the same name but different parameters −

    return_type function_name(parameter1);
    return_type function_name(parameter2);

    Example of Function Overloading

    In the following example, we are defining three different functions with the same name but different parameters. This example demonstrates the implementation of function overloading −

    Open Compiler

    #include<iostream>usingnamespace std;// Adding two integers (Function definition 1)intaddition(int a,int b){return a + b;}// Adding three integers (Function definition 2)intaddition(int a,int b,int c){return a + b + c;}// Adding two floating-point numbers (Function definition 3)floataddition(float a,float b){return a + b;}intmain(){
       cout<<addition(10.5f,20.3f)<<endl;
       cout<<addition(10,20,30)<<endl;
       cout<<addition(10,20)<<endl;return0;}

    Output

    30.8
    60
    30
    

    How Function Overloading Works?

    In the case of different functions with the same name (function overloading), when the compiler reaches a specific function call, it checks with the different function definition based on the parameters type, order, or number of arguments, and executes the matched function definition.

    Example

    Let suppose, there are 3 different function definitions to add numbers with different parameters −

    // Adding two integers (Function definition 1)intaddition(int a,int b){return a + b;}// Adding three integers (Function definition 2)intaddition(int a,int b,int c){return a + b + c;}// Adding two floating-point numbers (Function definition 3)floataddition(float a,float b){return a + b;}

    And, you call the function in the following order −

    addition(10.5f, 20.3f); // Function call 1
    addition(10, 20, 30); // Function call 2
    addition(10, 20); // Function call 3
    

    In the above function calls, function definition will be called in the following order −

    • Function call 1 will execute Function definition 3, because here we are passing two float values.
    • Function call 2 will execute Function definition 2, because here we are passing three integer values.
    • Function call 3 will execute Function definition 1, because here we are passing two integer values.

    Function Overloading Based on Number of Parameters

    This method involves defining multiple functions with the same name but a different number of parameters.

    Syntax

    voiddisplay(int a);// takes one parametervoiddisplay(int a,double b);// takes two parameters

    Example

    The following example demonstrates the function overloading based on the number of parameters −

    #include <iostream>usingnamespace std;// Function overloads based on the number of parametersvoiddisplay(int a){
       cout <<"Display with one integer: "<< a << endl;}voiddisplay(int a,double b){
       cout <<"Display with an integer and a double: "<< a <<" and "<< b << endl;}intmain(){// Calls the first overloaddisplay(10);// Calls the second overloaddisplay(10,3.14);double:10and3.14return0;}

    Output

    Display with one integer: 10
    Display with an integer and a double: 10 and 3.14
    

    Function Overloading Based on Different Parameter Types

    This method involves defining multiple functions with the same name but different types of parameters.

    Syntax

    voidshow(int a);// parameter with int type voidshow(double a);// parameter with double type

    Example

    The following example demonstrates the function overloading based on the different parameter types −

    Open Compiler

    #include <iostream>usingnamespace std;// Function for integer inputvoidshow(int a){
       cout <<"Integer value: "<< a << std::endl;}// Function for double inputvoidshow(double a){
       cout <<"Double value: "<< a << std::endl;}intmain(){show(10);show(3.14);return0;}

    Output

    Integer value: 10
    Double value: 3.14
    

    Function Overloading Based on Different Parameter Order

    This method involves defining multiple functions with the same name but different sequences of parameters.

    Syntax

    // integer followed by a double, // can be any datatype(bool, char etc)voiddisplay(int a,double b){ 
       cout <<"Integer and Double: "<< a <<", "<< b << endl;}// double followed by an integervoiddisplay(double a,int b){  
       cout <<"Double and Integer: "<< a <<", "<< b << endl;}

    Example

    The following example demonstrates the function overloading based on the different parameter order −

    Open Compiler

    #include <iostream>usingnamespace std;voiddisplay(int a,double b){
       cout <<"Integer and Double: "<< a <<", "<< b << endl;}voiddisplay(double a,int b){
       cout <<"Double and Integer: "<< a <<", "<< b <<endl;}intmain(){display(10,3.14);display(3.14,10);return0;}

    Output

    Integer and Double: 10, 3.14
    Double and Integer: 3.14, 10
    

    Use Cases for Function Overloading

    Function overloading in C++ is a powerful feature that allows you to use the same function name to perform different tasks based on different parameter lists. This can lead to more readable and maintainable code. Here are some common scenarios and examples where function overloading is useful −

    • Different Data Types − Function overloading is useful for handling various data types with a common function name.
    • Different Number of Parameters − Function overloading provides flexibility with functions which have varying numbers of parameters.
    • Parameter Type and Order − Function overloading handles different parameter types or their order.
    • Different Operations − Function overloading supports similar operations for different data types or contexts.
    • Variant Contexts − Function overloading provides variations of a function to suit different requirements or levels of detail.
  • Return Statement in C++

    The return statement in C++ is used to exit a function and to send a value back to the function’s caller which is optional depending on requirement. It plays a very important role in controlling the flow of a program and making sure that functions will provide results to other parts of the code.

    Syntax

    Below is the syntax of using return statement in C++ −

    return[expression];

    Where, “expression” is optional, specifically used for function. If provided, it specifies the value to be returned to the caller.

    Example of return Statement

    The following is an example of return statement −

    Open Compiler

    #include <iostream>usingnamespace std;intsum(int a,int b){// Here, returning sum of a and breturn a + b;}intmain(){// Calling the functionint ans =sum(5,2);
       cout <<"The sum of two integers 5 and 2 is:  "<< ans << endl;// Returning from the main (),// 0 represents execution done without any errorreturn0;}

    Output

    The sum of two integers 5 and 2 is:  7
    
    
    

    Key Aspects of return Statement

    1. Function Termination

    When a return statement is executed, the function exits immediately, and optionally sends value back to the caller.

    2. Returning Types

    Return by Value

    In this the specified value in return statement is sent back to the caller. This is essential for functions which perform calculations or need to provide results.

    intAdd(int a,int b){return a + b;// Returns the sum of a and b}

    No Return Value (void)

    The functions which are declared with void, the return statement can be used without an expression to exit the function early.

    voidGetMessage(){
       cout <<"Hello, TutorialsPoint Learner!";return;// Exits the function}

    3. Multiple return Statements

    A function may consist of multiple return statements, which we generally get to see within conditional statements.

    intmax(int a,int b){if(a > b)return a;elsereturn b;}

    4. Returning Objects

    Functions can return objects, which can be useful for returning multiple values encapsulated in a class or struct.

    structpoint{int x, y;};
    
    point getOrigin(){return{0,0};}

    5. Early Exit

    The return statement can be used to exit a function early, which is useful for error handling or special conditions.

    intdivideInteger(int a,int b){if(b ==0){
    
      cer &lt;&lt;"Error: Division by zero!"&lt;&lt; endl;return-1;// Shows an error}return a / b;}</code></pre>

    Return Types and Value Handling in C++

    In C++, the return type of a function determines what kind of value (if any) a function will return to the caller. Proper handling of return types and values is important for making sure that functions behave as expected and integrate smoothly with other parts of the program.

    1. Primitive Data Types

    Primitive data types are the basic built-in types provided by C++. Common examples are like int, float, double, char etc.

    Example

    Open Compiler

    #include <iostream>usingnamespace std;// created a function which returns an integerintgetSquare(int num){return num * num;}intmain(){int value =5;int result =getSquare(value);// Calling the function and storing its result
       cout <<"The square of "<< value <<" is "<< result << endl;return0;}
    Output
    The square of 5 is 25
    

    2. User-Defined Types

    User-defined types include structs and classes. These types allow you to define complex data structures and customize them as per your specific requirements.

    Example with Struct

    Open Compiler

    #include <iostream>usingnamespace std;structPoint{int x;int y;};
    Point createPoint(int x,int y){
       Point p;
       p.x = x;
       p.y = y;return p;// will returns a Point object}intmain(){
       Point p =createPoint(10,20);
       cout <<"Point coordinates: ("<< p.x <<", "<< p.y <<")"<< endl;return0;}
    Output
    Point coordinates: (10, 20)
    

    Example with Classes

    Open Compiler

    #include <iostream>usingnamespace std;classrectangle{public:rectangle(int w,int h):width(w),height(h){}intgetArea()const{return width * height;}private:int width;int height;};
    
    rectangle createRectangle(int width,int height){returnrectangle(width, height);// Returns a Rectangle object}intmain(){
       rectangle rect =createRectangle(10,5);
       cout <<"Area of given Rectangle is: "<< rect.getArea()<< endl;return0;}
    Output
    Area of given Rectangle is: 50
    

    3. References and Pointers

    References and pointers are used to refer to variables or objects without making any copies. Which can be useful for efficiency and and easy to modify the original data when needed.

    Returning by Reference

    Open Compiler

    #include <iostream>usingnamespace std;int globalValue =100;int&getGlobalValue(){return globalValue;// Returns a reference to the global variable}intmain(){int& ref =getGlobalValue();
       ref =200;// Modifies the global variable
       cout <<"Global Value: "<< globalValue << endl;return0;}
    Output
    Global Value: 200
    

    Returning by Pointer

    Open Compiler

    #include <iostream>usingnamespace std;int*createArray(int size){int* array =newint[size];// Allocating memory dynamicallyfor(int i =0; i < size;++i){
    
      array&#91;i]= i *10;}return array;// Returning a pointer to the allocated array}intmain(){int* myArray =createArray(5);for(int i =0; i &lt;5;++i){
      cout &lt;&lt; myArray&#91;i]&lt;&lt;" ";}delete&#91;] myArray;// Free dynamically allocated memoryreturn0;}</code></pre>
    Output
    0 10 20 30 40 
    
  • Write a Program to Find the Greatest of the Three Numbers.

    C++// C++ program to find greatest // among three numbers using #include <iostream> using namespace std; int main() { int a = 10, b = 20, c = 30; cout << "The Greatest Among Three Numbers is : "; if (a >= b && a >= c) { cout << a << endl; } else if (b >= a && b >= c) { cout << b << endl; } else { cout << c << endl; } return 0; }

    Output

    The Greatest Among Three Numbers is : 30
  • Recursion (Recursive Function)

    Recursion is a programming technique where a function calls itself over again and again with modified arguments until it reaches its base case, where the recursion stops.

    It breaks a problem down into smaller, more manageable sub-problems, recursion allows for elegant and better solutions to complex problems.

    Recursive Function

    A recursive function is a function which is particularly used for recursion where a function calls itself, either directly or indirectly, to address a problem. It must include at least one base case to terminate the recursion and one recursive case where the function invokes itself.

    • Base Case − It’s a case where recursion stops or ends after reaching that particular condition.
    • Recursive Case − It’s a case where a function calls itself over again with decremented value until and unless it reaches its base case.

    Creating a Recursive Function

    The following syntax is used to implement a recursive function in C++ −

    function name(param_1, param_2..){<base condition><function body><return statement>}

    Here,

    • Where, function name(param_1, param_2..) is a function declared as “name” passing with multiple parameters in it as per requirement.
    • Now the function body is being divided into three sub-categories : base condition, function body and return statement.
    • In base condition we will define its base case, where recursion has to stop or end.
    • In the function body, a recursive case will be defined, where we need to call the function over again and again as per requirement.
    • At last, the return statement will return the final output of the function.

    Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

    Calling a Recursive Function

    Calling a recursive function is just like calling any other function, where you will use the function’s name and provide the necessary parameters in int main() body.

    To call a recursive function, use the following syntax −

    func_name(value);

    Example of Recursion

    Below is an example of a recursion function in C++. Here, we are calculating the factorial of a number using the recursion −

    #include <iostream>usingnamespace std;// Recursive Function to Calculate Factorialintfactorial(int num){// Base caseif(num <=1){return1;}// Recursive caseelse{return num *factorial(num -1);}}intmain(){int positive_number;
       cout <<"Enter a positive integer: ";
       cin >> positive_number;if(positive_number <0){
    
      cout &lt;&lt;"Wrong Input, Factorial is not Defined for Negative Integer"&lt;&lt; endl;}else{
      cout &lt;&lt;"Factorial of "&lt;&lt; positive_number &lt;&lt;" is "&lt;&lt;factorial(positive_number)&lt;&lt; endl;}return0;}</code></pre>

    Output

    Enter a positive integer: 4 (input)
    Factorial of 4 is 24
    

    Explanation

    If take input int positive_number as 4, It will send integer to function name 'factorial' as factorial(4)

    Initial Call: factorial(4)

    This function will check base case (n<=1), as it’s not satisfying the base case, therefore move forward to recursive case and will compute as "4 * factorial(3)".

    Second call: factorial(3)

    This function will again check base case, as it’s not satisfying it, therefor will again move forward to recursive case , and compute as "3 * factorial(2)".

    Third Call: factorial(2)

    Checks base case and computes "2 * factorial(1)"

    Fourth call: factorial(1)

    Checks base case, now since function satisfying this base case condition that’s less than or equal to 1, So it will return 1.

    Unwinding the Stack

    Now, the recursive calls will start returning: After the 4th call now it will again start from back, returning to the Third Call first.

    Return to Third Call: factorial(2)

    We already have factorial(1) = 1, therefor factorial(2) will return, "2 * factorial(1)", that’s "2 * 1" , which returns as factorial(2) equals to 2.

    Return to Second Call: factorial(3)

    Now, factorial(2) is 2, therefore factorial(3) equals to "3 * 2", that’s 6.

    Return to Initial Call: factorial(4)

    We have factoria(3) which returns 6, therefore, factorial(4) returns "4 * 6 = 24".

    Types of Recursion

    Recursion can be categorized into two main types where each with its own sub-categories −

    1. Direct Recursion

    Direct recursion occurs when a function calls itself directly −

    Simple Direct Recursion

    The function calls itself with a simpler or smaller instance of the problem. It is used for solving problems like factorial calculation, fibonacci sequence generation, etc.

    Tail Recursion

    A form of direct recursion where the recursive call is the last operation in the function. It is used for solving accumulative calculations and list processing problems.

    intfactorial(int n,int result =1){if(n <=1){return result;}else{returnfactorial(n -1, n * result);// Tail recursive call}}

    Head Recursion

    The recursive call is made before any other operation in the function. Processing occurs after the recursive call returns. It is used for tree traversals and output generation.

    voidprintNumbers(int n){if(n >0){printNumbers(n -1);// Recursive call first
    
      cout &lt;&lt; n &lt;&lt;" ";// Processing after recursive call}}</code></pre>

    Linear Recursion

    Each function call generates exactly one recursive call, forming a linear chain of calls. It is used for simple counting or summing.

    intlinearRecursion(int n){if(n <=0){return0;}else{returnlinearRecursion(n -1)+1;// Linear recursive call}}

    2. Indirect Recursion

    Indirect recursion occurs when a function calls another function, which eventually leads to the original function being called. This involves two or more functions calling each other.

    Mutual Recursion

    In mutual recursion, two or more functions call each other in a recursive manner, forming a cyclic dependency. It is used for even and odd number classification and grammar parsing.

    Open Compiler

    #include <iostream>usingnamespace std;voideven(int n);voidodd(int n);voideven(int n){if(n ==0){
    
      cout &lt;&lt;"Even"&lt;&lt; endl;}else{odd(n -1);// Calls odd}}voidodd(int n){if(n ==0){
      cout &lt;&lt;"Odd"&lt;&lt; endl;}else{even(n -1);// Calls even}}intmain(){even(4);// Outputs: Evenodd(5);// Outputs: Oddreturn0;}</code></pre>
    Output
    Even
    Even
    

    Nested Recursion

    The nested recursion is a form of indirect recursion where a recursive function makes another recursive call inside its own recursive call. It is used for solving complex mathematical and algorithmic problems.

    Open Compiler

    #include <iostream>usingnamespace std;intnestedRecursion(int n){if(n >100){return n -10;}else{returnnestedRecursion(nestedRecursion(n +11));// Nested recursive calls}}intmain(){
       cout <<nestedRecursion(95)<< endl;// Outputs: 105return0;}
    Output
    91
    

    Advantages of Recursion

    • Simplicity and reduced boilerplate code − Recursion helps to simplify solving problems which have a built-in recursive structure, like working with trees or solving combinatorial problems by making it easier to understand and implement.
    • Backtracking − Recursion is a great fit for backtracking algorithms, which involve examining all possible solutions to find one which meets certain criteria.
    • Effective solutions for divide-and-conquer problems − Recursion works perfectly for Divide-and-conquer algorithms, where problems are broken down into smaller sub parts and are solved one by one. This makes problem solving more efficient and easy.

    Recursion Vs. Iteration

    Recursion is a method where a function calls itself over again and again with modified arguments until it reaches its base case which stops the recursion. Whereas, an iteration involves using loops (such as for, while or do-while) where it involves repeatedly executing blocks of code until a certain condition is met.

    Recursion or Iteration: When to Use?

    Recursion

    • The problems which can be divided into similar sub-problems or which have natural recursive patterns such as tree traversal or combinational tasks and manageable depth.
    • When a user needs simple, cleaner and readable code as it provides clean proper arranged code.
    • Examples: Tree and graph traversals, divide-and-conquer algorithms like quicksort and mergesort, and problems involving backtracking like solving mazes or puzzles.

    Iteration

    • Iterative solutions are generally more efficient in terms of memory and execution time and which involves simple repetition.
    • For the problems which require simple looping because iteration is usually more straightforward and efficient.
    • Iteration is more stable for problems which require a large number of repetitions, as it doesn't risk stack overflow.
    • Examples: Looping of Array, Vectors and lists, where require simple mathematical computation and repeated execution of a block of code.

    Comparison between Recursion and Iteration

    RecursionIteration
    Time ComplexityIt can be greater because of its repeated function calls nature.Comparatively less.
    Space ComplexityRecursion often uses more Memory because of the call stack.Uses a fixed amount of Memory.
    Code SizeIn the recursion, the code size is smaller.Comparatively larger code size.
    Execution SpeedThe execution speed is slow when you use recursion.Execution speed is more.

    Limitations of Recursion

    The following are limitations of recursion −

    • Memory Consumption − Each recursive call adds a new frame to the call stack, which can consume a significant amount of memory.
    • Stack Overflow Risk − As recursion relies on call stack to manage function calls, Deep recursion can lead to stack overflow as it exceeds the stack size limit.
    • Performance Overhead − Recursive functions can be less efficient than iterative ones because they involve overhead from multiple function calls and managing the call stack, which can significantly impact performance, especially with deep recursion.
    • Debugging Complexity − Debugging Recursive code can be challenging, especially when dealing with complex recursion or large recursion depths. It needs careful handling of base cases and logic.
    • Space Complexity − Due to the call stack in recursion, it can lead to consuming a lot of memory.
  • Multiple Function Parameters in C++

    C++ multiple function parameters describe the ability of a function to accept more than one argument or can say functions with multiple parameters to perform operations using several inputs, this feature makes a function to execute more complex operations by working with multiple subset of data at once.

    Syntax

    In C++, you can define a function with multiple parameters by listing them in the function’s declaration and definition, separated by commas. Here’s the basic syntax −

    return_type function_name(param1_type param1_name, param2_type parame2_name,...);

    Where,

    param1_type and param1_name are parameter type (eg. int, char, bool, string) and its name respectively, similarly param2_type (eg. int, char, bool, string) and param2_name are parameter type and its name respectively and so on.

    Data Types for Multiple Function Parameters

    There are 2 types of passing data to multiple function parameters, as given in the following −

    1. Single Data Type for Multiple Parameters

    Functions where all parameters are of the same data type.

    Example

    Open Compiler

    #include <iostream>usingnamespace std;// Function taking multiple parameters or arguments of same data type (int)voidsum(int a,int b,int c){
       cout <<"Sum: "<<(a + b + c)<< endl;}intmain(){sum(1,2,3);return0;}
    Output
    Sum: 6
    

    2. Multiple Data Types for Multiple Parameters

    Functions where parameters can have different data types.

    Example

    Open Compiler

    #include <iostream>usingnamespace std;// Function taking arguments of different data typesvoidgetInfo(string name,int age,double height){
       cout << name <<" is "<< age <<" years old and "<< height <<" meters tall."<< endl;}intmain(){getInfo("Aman",26,1.78);getInfo("Naman",32,1.65);return0;}
    Output
    Aman is 26 years old and 1.78 meters tall.
    Naman is 32 years old and 1.65 meters tall.
    

    Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

    Multiple Parameters Passing Techniques

    Multiple parameters (or, single) passing techniques in C++ refer to the methods which are used to pass arguments to functions. These techniques define how the data is transferred and manipulated within the function. Here we will discuss few primary techniques −

    1. Pass by Value

    In pass by value, a copy of the actual parameter’s value is passed to the function. The changes made to the parameter inside the function do not affect the original argument. It is safe and straightforward but can be inefficient for large data structures due to copying.

    2. Pass by Reference

    This method passes a reference to the actual parameter, allowing the function to modify the original argument. The function works with the original data rather than a copy. It is efficient as it avoids copying, but requires careful handling to avoid unintended modifications.

    3. Mutable vs Immutable Types

    Mutable TypesImmutable Types
    These are types whose instances can be modified after they are created.These are types whose instances cannot be changed after they are created.
    Lists, dictionaries, and sets are common mutable types.Integers, strings, and tuples are common immutable types.

    Types of Multiple Function Parameters

    In C++, functions are capable of accepting multiple parameters, and these parameters can be categorized in various ways. Here’s a breakdown for different types of multiple function parameters in C++ −

    1. Fixed Number of Parameters

    A function with a fixed number of parameters where it has a specific and unchanging number of input parameters.

    Example

    Open Compiler

    #include <iostream>usingnamespace std;// Function with a fixed number of argumentsvoidgetDetails(int age,double height,const string& name){
       cout <<"Name: "<< name <<", Age: "<< age <<", Height: "<< height << endl;}intmain(){getDetails(25,5.6,"Sam");return0;}
    Output
    Name: Sam, Age: 25, Height: 5.6
    

    2. Variable Number of Parameters

    A function that can accept a variable number of parameters or arguments. This is typically implemented using variadic functions or template parameter packs.

    Example

    Open Compiler

    #include <iostream>usingnamespace std;// Variadic template functiontemplate<typename... Args>voidprintNumbers(Args... args){(cout <<...<< args)<< endl;}intmain(){printNumbers(1,2,3);// Outputs: 123printNumbers(4,5,6,7,8);// Outputs: 45678return0;}
    Output
    123
    45678
    

    3. Default Parameters

    The default parameters are the parameters with default values that can be omitted when calling the function. The function uses the default values if no arguments are provided for those parameters.

    Example

    Open Compiler

    #include <iostream>usingnamespace std;// Function with default parametersvoidgreet(const string& name ="Guest",int age =0){
       cout <<"Hello, "<< name;if(age >0){
    
      cout &lt;&lt;". You are "&lt;&lt; age &lt;&lt;" years old.";}
    cout << endl;}intmain(){greet();// Outputs: Hello, Guestgreet("Alice");// Outputs: Hello, Alicegreet("Bob",30);// Outputs: Hello, Bob. You are 30 years old.return0;}
    Output
    Hello, Guest
    Hello, Alice
    Hello, Bob. You are 30 years old.
    

    4. Named Parameters (C++ Specific)

    Though C++ does not support named parameters directly, you can achieve similar functionality using a class or struct to encapsulate parameters.

    Example

    Open Compiler

    #include <iostream>usingnamespace std;// Structure to encapsulate parametersstructPerson{
       string name;int age;double height;};// Function that takes a parameter objectvoidprintPerson(const Person& p){
       cout <<"Name: "<< p.name <<", Age: "<< p.age <<", Height: "<< p.height << endl;}intmain(){
       Person alice ={"Alice",25,5.9};printPerson(alice);return0;}
    Output
    Name: Alice, Age: 25, Height: 5.9
    

    5. Parameter Objects

    A single parameter that is a complex type like a class or struct, encapsulating multiple related values.

    Example

    Open Compiler

    #include <iostream>usingnamespace std;// Class to hold multiple parametersclassRectangle{public:int width;int height;Rectangle(int w,int h):width(w),height(h){}};// Function that takes a parameter objectvoidcalculateArea(const Rectangle& rect){
       cout <<"Area: "<<(rect.width * rect.height)<< endl;}intmain(){
       Rectangle rect(10,5);calculateArea(rect);// Outputs: Area: 50return0;}
    Output
    Area: 50
    
  • Write a C++ Program to Check Whether a Number is a Positive or Negative Number.

    C++// C++ Program to check whether a number is positive or // negative #include <iostream> using namespace std; int main() { int number; number = -100; if (number >= 0) { cout << number << " is a positive number." << endl; } else { cout << number << " is a negative number." << endl; } return 0; }

    Output

    -100 is a negative number.
  • News Aggregator

    Step 1: Set Up Your Environment

    1. Install Flask and Requests:bashCopy codepip install Flask requests
    2. Sign Up for News API:
      • Go to News API and sign up to get your API key.

    Step 2: Create Your Flask App

    Create a file named app.py:

    pythonCopy codefrom flask import Flask, render_template, request
    import requests
    
    app = Flask(__name__)
    
    API_KEY = 'YOUR_NEWS_API_KEY'  # Replace with your News API key
    
    @app.route('/')
    def index():
    
    query = request.args.get('query', '')
    articles = &#91;]
    if query:
        url = f'https://newsapi.org/v2/everything?q={query}&amp;apiKey={API_KEY}'
        response = requests.get(url)
        data = response.json()
        articles = data.get('articles', &#91;])
    return render_template('index.html', articles=articles, query=query)
    if __name__ == '__main__':
    app.run(debug=True)

    Step 3: Create the HTML Template

    Create a folder named templates in the same directory as app.py, and inside that folder, create a file named index.html:

    htmlCopy code<!DOCTYPE html>
    <html lang="en">
    <head>
    
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;title&gt;News Aggregator&lt;/title&gt;
    </head> <body>
    &lt;h1&gt;News Aggregator&lt;/h1&gt;
    &lt;form method="get"&gt;
        &lt;input type="text" name="query" placeholder="Search news..." value="{{ query }}"&gt;
        &lt;button type="submit"&gt;Search&lt;/button&gt;
    &lt;/form&gt;
    &lt;ul&gt;
        {% for article in articles %}
            &lt;li&gt;
                &lt;a href="{{ article.url }}" target="_blank"&gt;{{ article.title }}&lt;/a&gt;
                &lt;p&gt;{{ article.description }}&lt;/p&gt;
            &lt;/li&gt;
        {% else %}
            &lt;li&gt;No articles found.&lt;/li&gt;
        {% endfor %}
    &lt;/ul&gt;
    </body> </html>

    Step 4: Run Your Application

    To run your application, execute the following command in your terminal:

    bashCopy codepython app.py
    

    Step 5: Access Your News Aggregator

    Open your web browser and go to http://127.0.0.1:5000/. You can enter a search query to fetch news articles related to that query.

  • Markdown Editor

    HTML Structure

    htmlCopy code<!DOCTYPE html>
    <html lang="en">
    <head>
    
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;title&gt;Markdown Editor&lt;/title&gt;
    &lt;link rel="stylesheet" href="styles.css"&gt;
    </head> <body>
    &lt;div class="container"&gt;
        &lt;h1&gt;Markdown Editor&lt;/h1&gt;
        &lt;textarea id="markdown" placeholder="Type your Markdown here..."&gt;&lt;/textarea&gt;
        &lt;div id="preview" class="preview"&gt;&lt;/div&gt;
    &lt;/div&gt;
    &lt;script src="script.js"&gt;&lt;/script&gt;
    </body> </html>

    CSS (styles.css)

    cssCopy codebody {
    
    font-family: Arial, sans-serif;
    margin: 0;
    padding: 20px;
    background-color: #f4f4f4;
    } .container {
    max-width: 800px;
    margin: auto;
    background: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    } h1 {
    text-align: center;
    } textarea {
    width: 100%;
    height: 200px;
    margin-bottom: 20px;
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 16px;
    } .preview {
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    background: #fafafa;
    }

    JavaScript (script.js)

    javascriptCopy code// Function to convert Markdown to HTML
    function markdownToHtml(markdown) {
    
    // Using a simple regex for basic Markdown conversion
    let html = markdown
        .replace(/#/g, '&lt;h1&gt;')
        .replace(/#\s/g, '&lt;/h1&gt;')
        .replace(/\*\*(.*?)\*\*/g, '&lt;strong&gt;$1&lt;/strong&gt;')
        .replace(/\*(.*?)\*/g, '&lt;em&gt;$1&lt;/em&gt;')
        .replace(/\n/g, '&lt;br&gt;');
    
    return html;
    } // Event listener for textarea document.getElementById('markdown').addEventListener('input', function() {
    const markdownText = this.value;
    const html = markdownToHtml(markdownText);
    document.getElementById('preview').innerHTML = html;
    });

    How to Use

    1. Create three files: index.html, styles.css, and script.js.
    2. Copy the respective code into each file.
    3. Open index.html in a web browser.
    4. Start typing Markdown in the textarea, and the HTML preview will update in real-time.
  • Dashboard for IoT Devices

    Step 1: Set Up Your Environment

    1. Install Flask: Make sure you have Python and pip installed. Then, run:bashCopy codepip install Flask
    2. Create Your Project Structure:arduinoCopy code/iot_dashboard ├── app.py ├── static │ └── style.css └── templates └── index.html

    Step 2: Create app.py

    This file will handle the backend logic.

    pythonCopy codefrom flask import Flask, render_template, jsonify, request
    import random
    
    app = Flask(__name__)
    
    # Simulate IoT device data
    devices = {
    
    'device1': {'temperature': 20, 'humidity': 30},
    'device2': {'temperature': 22, 'humidity': 40}
    } @app.route('/') def index():
    return render_template('index.html')
    @app.route('/api/devices', methods=['GET']) def get_devices():
    return jsonify(devices)
    @app.route('/api/update', methods=['POST']) def update_device():
    data = request.json
    device_id = data.get('id')
    if device_id in devices:
        devices&#91;device_id]&#91;'temperature'] = data.get('temperature', devices&#91;device_id]&#91;'temperature'])
        devices&#91;device_id]&#91;'humidity'] = data.get('humidity', devices&#91;device_id]&#91;'humidity'])
        return jsonify(success=True)
    return jsonify(success=False), 404
    if __name__ == '__main__':
    app.run(debug=True)

    Step 3: Create index.html

    This file will be the frontend for your dashboard.

    htmlCopy code<!DOCTYPE html>
    <html lang="en">
    <head>
    
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;title&gt;IoT Dashboard&lt;/title&gt;
    &lt;link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"&gt;
    &lt;script src="https://code.jquery.com/jquery-3.6.0.min.js"&gt;&lt;/script&gt;
    </head> <body>
    &lt;h1&gt;IoT Device Dashboard&lt;/h1&gt;
    &lt;div id="devices"&gt;&lt;/div&gt;
    &lt;script&gt;
        function fetchDevices() {
            $.getJSON('/api/devices', function(data) {
                $('#devices').empty();
                $.each(data, function(id, device) {
                    $('#devices').append(`&lt;div class="device"&gt;
                        &lt;h3&gt;${id}&lt;/h3&gt;
                        &lt;p&gt;Temperature: ${device.temperature}°C&lt;/p&gt;
                        &lt;p&gt;Humidity: ${device.humidity}%&lt;/p&gt;
                        &lt;button onclick="updateDevice('${id}')"&gt;Update&lt;/button&gt;
                    &lt;/div&gt;`);
                });
            });
        }
        function updateDevice(id) {
            const temperature = prompt("Enter new temperature:");
            const humidity = prompt("Enter new humidity:");
            if (temperature !== null &amp;&amp; humidity !== null) {
                $.ajax({
                    url: '/api/update',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify({ id: id, temperature: temperature, humidity: humidity }),
                    success: function() {
                        fetchDevices();
                    }
                });
            }
        }
        $(document).ready(function() {
            fetchDevices();
        });
    &lt;/script&gt;
    </body> </html>

    Step 4: Create style.css

    Add some basic styles.

    cssCopy codebody {
    
    font-family: Arial, sans-serif;
    } .device {
    border: 1px solid #ccc;
    padding: 10px;
    margin: 10px;
    }

    Step 5: Run Your Application

    1. Navigate to your project directory:bashCopy codecd iot_dashboard
    2. Run your Flask app:bashCopy codepython app.py
    3. Open your web browser and go to http://127.0.0.1:5000.
  • Real-time Polling App

    Backend (Node.js)

    1. Setup your project:
    bashCopy codemkdir polling-app
    cd polling-app
    npm init -y
    npm install express socket.io cors
    
    1. Create a server file (server.js):
    javascriptCopy code// server.js
    const express = require('express');
    const http = require('http');
    const socketIo = require('socket.io');
    const cors = require('cors');
    
    const app = express();
    const server = http.createServer(app);
    const io = socketIo(server, {
      cors: {
    
    origin: '*', // Change this to your frontend URL in production
    methods: &#91;'GET', 'POST']
    } }); app.use(cors()); app.use(express.json()); let pollData = { question: "What's your favorite fruit?", options: ["Apple", "Banana", "Orange"], votes: [0, 0, 0] }; app.get('/poll', (req, res) => { res.json(pollData); }); app.post('/vote', (req, res) => { const { optionIndex } = req.body; if (optionIndex >= 0 && optionIndex < pollData.votes.length) {
    pollData.votes&#91;optionIndex]++;
    io.emit('voteUpdated', pollData);
    res.sendStatus(200);
    } else {
    res.sendStatus(400);
    } }); io.on('connection', (socket) => { console.log('A user connected'); socket.emit('voteUpdated', pollData); }); const PORT = process.env.PORT || 3000; server.listen(PORT, () => { console.log(Server is running on port ${PORT}); });

    Frontend (HTML/CSS/JavaScript)

    1. Create an index.html file:
    htmlCopy code<!-- index.html -->
    <!DOCTYPE html>
    <html lang="en">
    <head>
    
    &lt;meta charset="UTF-8"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
    &lt;title&gt;Real-Time Polling App&lt;/title&gt;
    &lt;style&gt;
        body { font-family: Arial, sans-serif; }
        #poll { margin: 20px; }
        button { margin-top: 10px; }
    &lt;/style&gt;
    </head> <body>
    &lt;div id="poll"&gt;
        &lt;h1 id="question"&gt;&lt;/h1&gt;
        &lt;ul id="options"&gt;&lt;/ul&gt;
    &lt;/div&gt;
    &lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt;
    &lt;script&gt;
        const socket = io();
        
        async function fetchPoll() {
            const response = await fetch('/poll');
            const pollData = await response.json();
            document.getElementById('question').innerText = pollData.question;
            const optionsList = document.getElementById('options');
            optionsList.innerHTML = '';
            pollData.options.forEach((option, index) =&gt; {
                const li = document.createElement('li');
                li.innerHTML = ${option} - &amp;lt;span id="vote-${index}"&amp;gt;0&amp;lt;/span&amp;gt; votes;
                const button = document.createElement('button');
                button.innerText = 'Vote';
                button.onclick = () =&gt; vote(index);
                li.appendChild(button);
                optionsList.appendChild(li);
            });
        }
        async function vote(optionIndex) {
            await fetch('/vote', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ optionIndex })
            });
        }
        socket.on('voteUpdated', (pollData) =&gt; {
            pollData.votes.forEach((voteCount, index) =&gt; {
                document.getElementById(vote-${index}).innerText = voteCount;
            });
        });
        fetchPoll();
    &lt;/script&gt;
    </body> </html>

    Running the Application

    1. Start the server:
    bashCopy codenode server.js
    
    1. Open your browser:

    Navigate to http://localhost:3000 to see your polling app in action.

    Explanation

    • Backend:
      • We set up a basic Express server.
      • We store poll data in memory (for simplicity).
      • When a vote is submitted, we update the votes and emit a Socket.io event to update connected clients.
    • Frontend:
      • We use the Fetch API to get poll data and submit votes.
      • We listen for updates via Socket.io to refresh the vote counts in real-time.