es
Feedback
C++ Codes - Basic to Advanced 💻

C++ Codes - Basic to Advanced 💻

Ir al canal en Telegram

💡 Daily C++ Codes with Output & Logic Any queries, message 👉 @sai7981 🤖 Get instant code explanations: @cpp_codes_bot 🔗 Join now: @cpp_code_snippets #cpp #dsa #coding #programming

Mostrar más
3 188
Suscriptores
Sin datos24 horas
+357 días
+12030 días
Archivo de publicaciones
#cpp #typecasting #conversion #integers

Explicitly convert float to int and display
#include <iostream>

int main() {
 float float_value = 3.14159f;
 int int_value1 = static_cast<int>(float_value);
 int int_value2 = (int)float_value;
 int int_value3 = int(float_value);

 std::cout << "Float value: " << float_value << std::endl;
 std::cout << "Integer value (static_cast): " << int_value1 << std::endl;
 std::cout << "Integer value (C-style cast): " << int_value2 << std::endl;
 std::cout << "Integer value (functional cast): " << int_value3 << std::endl;

 return 0;
}

#cpp #evenodd #modulo #interview

Check if a number is even or odd
#include <iostream>

bool isEven(int num) {
 return (num % 2 == 0);
}

int main() {
 int number;
 std::cin >> number;
 if (isEven(number)) {
 std::cout << "Even" << std::endl;
 } else {
 std::cout << "Odd" << std::endl;
 }
 return 0;
}

#cpp #datatypes #sizeof #memorymanagement

Find the size of different data types
#include <iostream>

int main() {
  std::cout << "Size of char: " << sizeof(char) << " bytes" << std::endl;
  std::cout << "Size of short: " << sizeof(short) << " bytes" << std::endl;
  std::cout << "Size of int: " << sizeof(int) << " bytes" << std::endl;
  std::cout << "Size of long: " << sizeof(long) << " bytes" << std::endl;
  std::cout << "Size of long long: " << sizeof(long long) << " bytes" << std::endl;
  std::cout << "Size of float: " << sizeof(float) << " bytes" << std::endl;
  std::cout << "Size of double: " << sizeof(double) << " bytes" << std::endl;
  std::cout << "Size of long double: " << sizeof(long double) << " bytes" << std::endl;
  std::cout << "Size of bool: " << sizeof(bool) << " bytes" << std::endl;
  return 0;
}

#cpp #algorithms #interviewquestions #swapping #coding

Swap two numbers without using a third variable
#include <iostream>

void swap(int& a, int& b) {
 a = a + b;
 b = a - b;
 a = a - b;
}

int main() {
 int x = 5;
 int y = 10;
 std::cout << "Before swap: x = " << x << ", y = " << y << std::endl;
 swap(x, y);
 std::cout << "After swap: x = " << x << ", y = " << y << std::endl;
 return 0;
}

#cpp #ascii #char #iostream

Read a character and print its ASCII value
#include <iostream>

int main() {
  char ch;
  std::cin >> ch;
  int asciiValue = static_cast<int>(ch);
  std::cout << asciiValue << std::endl;
  return 0;
}

#cpp #basic #iostream #inputoutput

Read two integers and print their sum
#include <iostream>

int main() {
  int a, b;
  std::cin >> a >> b;
  std::cout << a + b << std::endl;
  return 0;
}

#cpp #smartpointers #unique_ptr #memorymanagement

Print Hello World
#include <iostream>
#include <memory>

class MyClass {
public:
    MyClass(int value) : data(value) { std::cout << "MyClass created with value: " << data << std::endl; }
    ~MyClass() { std::cout << "MyClass destroyed with value: " << data << std::endl; }
    void printValue() { std::cout << "Value: " << data << std::endl; }
private:
    int data;
};

int main() {
    std::unique_ptr<MyClass> ptr1 = std::make_unique<MyClass>(10);
    ptr1->printValue();

    std::unique_ptr<MyClass> ptr2;
    ptr2 = std::move(ptr1);

    if (ptr1 == nullptr) {
        std::cout << "ptr1 is now null." << std::endl;
    }

    if (ptr2 != nullptr) {
        ptr2->printValue();
    }

    return 0;
}

#cpp #smartpointers #memorymanagement #unique_ptr

Print Hello World
#include <iostream>
#include <memory>

class Resource {
public:
    Resource(int id) : id_(id) { std::cout << "Resource " << id_ << " acquired.\n"; }
    ~Resource() { std::cout << "Resource " << id_ << " released.\n"; }
    void use() { std::cout << "Using resource " << id_ << ".\n"; }
private:
    int id_;
};

int main() {
    std::unique_ptr<Resource> ptr1 = std::make_unique<Resource>(1);
    ptr1->use();

    std::unique_ptr<Resource> ptr2;
    ptr2 = std::move(ptr1);

    if (ptr2) {
        ptr2->use();
    }

    return 0;
}

#cpp #algorithms #interviewprep #coding #swap

Swap two numbers without using a third variable
#include <iostream>

void swap(int& a, int& b) {
    a = a + b;
    b = a - b;
    a = a - b;
}

int main() {
    int x = 5;
    int y = 10;

    std::cout << "Before swap: x = " << x << ", y = " << y << std::endl;
    swap(x, y);
    std::cout << "After swap: x = " << x << ", y = " << y << std::endl;

    return 0;
}

#cpp #ascii #iostream #char #casting

Read a character and print its ASCII value
#include <iostream>

int main() {
    char character;
    std::cout << "Enter a character: ";
    std::cin >> character;
    int asciiValue = static_cast<int>(character);
    std::cout << "The ASCII value of '" << character << "' is: " << asciiValue << std::endl;
    return 0;
}