fa
Feedback
C++ Codes - Basic to Advanced 💻

C++ Codes - Basic to Advanced 💻

رفتن به کانال در 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

نمایش بیشتر
3 188
مشترکین
اطلاعاتی وجود ندارد24 ساعت
+357 روز
+12030 روز
آرشیو پست ها
#C++ #Factorial #WhileLoop #Interview #Programming

Calculate factorial of a number using while loop
#include <iostream>

int main() {
    int n;
    std::cout << "Enter a non-negative integer: ";
    std::cin >> n;

    if (n < 0) {
        std::cout << "Factorial is not defined for negative numbers.\n";
        return 1;
    }

    long long factorial = 1;
    int i = 1;

    while (i <= n) {
        factorial *= i;
        i++;
    }

    std::cout << "Factorial of " << n << " = " << factorial << std::endl;
    return 0;
}

#C++ #Factorial #WhileLoop #InterviewQuestion #Algorithm

Calculate factorial of a number using while loop
#include <iostream>

int main() {
    int n;
    unsigned long long factorial = 1;

    std::cout << "Enter a positive integer: ";
    std::cin >> n;

    if (n < 0) {
        std::cout << "Error! Factorial of a negative number doesn't exist.";
    } else {
        int i = 1;
        while (i <= n) {
            factorial *= i;
            i++;
        }
        std::cout << "Factorial of " << n << " = " << factorial;
    }

    return 0;
}

#ForLoop #NaturalNumbers #CPP

Print all natural numbers up to N using for loop
#include <iostream>

int main() {
    int n;
    std::cout << "Enter a positive integer: ";
    std::cin >> n;

    if (n <= 0) {
        std::cout << "Please enter a positive integer.\n";
        return 1;
    }

    for (int i = 1; i <= n; ++i) {
        std::cout << i << " ";
    }
    std::cout << std::endl;

    return 0;
}

#CharType #CPP #Coding

Check if a character is uppercase, lowercase, digit or special character
#include <iostream>

using namespace std;

string checkCharacterType(char ch) {
    if (isupper(ch)) {
        return "Uppercase";
    } else if (islower(ch)) {
        return "Lowercase";
    } else if (isdigit(ch)) {
        return "Digit";
    } else {
        return "Special Character";
    }
}

int main() {
    char inputChar;
    cout << "Enter a character: ";
    cin >> inputChar;
    cout << checkCharacterType(inputChar) << endl;
    return 0;
}

#Calculator #SwitchCase #Cpp

Simple calculator using switch-case
#include <iostream>

int main() {
    char operation;
    double num1, num2;

    std::cout << "Enter operator (+, -, *, /): ";
    std::cin >> operation;

    std::cout << "Enter two operands: ";
    std::cin >> num1 >> num2;

    switch (operation) {
        case '+':
            std::cout << num1 << " + " << num2 << " = " << num1 + num2 << std::endl;
            break;
        case '-':
            std::cout << num1 << " - " << num2 << " = " << num1 - num2 << std::endl;
            break;
        case '*':
            std::cout << num1 << " * " << num2 << " = " << num1 * num2 << std::endl;
            break;
        case '/':
            if (num2 != 0) {
                std::cout << num1 << " / " << num2 << " = " << num1 / num2 << std::endl;
            } else {
                std::cout << "Error! Division by zero.";
            }
            break;
        default:
            std::cout << "Error! Operator is not correct";
    }

    return 0;
}

#C++ #if-else #LargestNumber

Find largest among three numbers using if-else
#include <iostream>

int main() {
    int num1, num2, num3, largest;

    std::cout << "Enter three numbers: ";
    std::cin >> num1 >> num2 >> num3;

    if (num1 >= num2 && num1 >= num3) {
        largest = num1;
    } else if (num2 >= num1 && num2 >= num3) {
        largest = num2;
    } else {
        largest = num3;
    }

    std::cout << "The largest number is: " << largest << std::endl;

    return 0;
}

#cpp #prime #algorithm #interview

Check if a number is prime
#include <iostream>
#include <cmath>

using namespace std;

bool isPrime(int n) {
    if (n <= 1) return false;
    for (int i = 2; i <= sqrt(n); ++i) {
        if (n % i == 0) return false;
    }
    return true;
}

int main() {
    int num = 29;
    if (isPrime(num)) {
        cout << num << " is a prime number." << endl;
    } else {
        cout << num << " is not a prime number." << endl;
    }
    return 0;
}

#cpp #increment #prepost #cplusplus #operators

Pre-increment and post-increment demonstration
#include <iostream>

int main() {
    int i = 5;
    int preIncrement = ++i;
    int j = 5;
    int postIncrement = j++;

    std::cout << "Pre-increment:" << std::endl;
    std::cout << "i: " << i << std::endl;
    std::cout << "preIncrement: " << preIncrement << std::endl;

    std::cout << "\nPost-increment:" << std::endl;
    std::cout << "j: " << j << std::endl;
    std::cout << "postIncrement: " << postIncrement << std::endl;

    return 0;
}

#cpp #ternaryoperator #conditional #maxvalue

Find maximum of two numbers using ternary operator
#include <iostream>

int main() {
    int a = 10;
    int b = 20;
    int max = (a > b) ? a : b;
    std::cout << "Maximum value: " << max << std::endl;
    return 0;
}

#CPP #BitwiseOperators #LeftShift #RightShift #CPPLanguage

Left shift and right shift demonstration
#include <iostream>
using namespace std;

int main() {
    int a = 5;
    int b = a << 2;
    int c = a >> 1;
    cout << "a = " << a << endl;
    cout << "a << 2 = " << b << endl;
    cout << "a >> 1 = " << c << endl;
    return 0;
}