Easy C++ Code Snippets
Explore these C++ code examples that you can easily copy and use in your own projects.
Basic Hello World
// Include iostream #include int main() { std::cout << "Hello World!" << std::endl; return 0; }
Simple For Loop
// Include iostream #include int main() { for (int i = 0; i < 10; i++) { std::cout << i << std::endl; } return 0; }
Basic Calculator
// Include iostream #include int main() { double num1, num2; char op; std::cout << "Enter the first number: "; std::cin >> num1; std::cout << "Enter the operator (+, -, *, /): "; std::cin >> op; std::cout << "Enter the second number: "; std::cin >> num2; switch (op) { case '+': std::cout << "Result: " << num1 + num2 << std::endl; break; case '-': std::cout << "Result: " << num1 - num2 << std::endl; break; case '*': std::cout << "Result: " << num1 * num2 << std::endl; break; case '/': std::cout << "Result: " << num1 / num2 << std::endl; break; default: std::cout << "Invalid operator" << std::endl; } return 0; }
Temperature Converter
// Include iostream #include int main() { double celsius, fahrenheit; std::cout << "Enter temperature in Celsius: "; std::cin >> celsius; fahrenheit = (celsius * 9.0 / 5.0) + 32.0; std::cout << "Temperature in Fahrenheit: " << fahrenheit << std::endl; return 0; }
Guess the Number Game
// Include iostream, cstdlib and ctime #include int main() { srand(time(0)); int secretNumber = rand() % 100 + 1; int guess = 0; std::cout << "Guess the number between 1 and 100." << std::endl; while (guess != secretNumber) { std::cout << "Enter your guess: "; std::cin >> guess; if (guess < secretNumber) std::cout << "Higher." << std::endl; else if (guess > secretNumber) std::cout << "Lower." << std::endl; } std::cout << "Correct! You guessed the number." << std::endl; return 0; }
Simple ATM Interface
// Include iostream #include int main() { int balance = 1000; int withdrawal; std::cout << "Welcome to ATM!" << std::endl; std::cout << "Enter amount to withdraw: "; std::cin >> withdrawal; if (withdrawal <= balance) { balance -= withdrawal; std::cout << "Withdrawal successful. New balance: " << balance << std::endl; } else { std::cout << "Insufficient balance." << std::endl; } return 0; }
Palindrome Checker
// Include iostream, string and algorithm #include int main() { std::string input, reversed; std::cout << "Enter a string: "; std::cin >> input; reversed = input; std::reverse(reversed.begin(), reversed.end()); if (input == reversed) { std::cout << "It's a palindrome!" << std::endl; } else { std::cout << "Not a palindrome." << std::endl; } return 0; }
Simple Interest Calculator
// Include iostream #include int main() { double principal, rate, time; std::cout << "Enter principal amount: "; std::cin >> principal; std::cout << "Enter rate of interest (in %): "; std::cin >> rate; std::cout << "Enter time (in years): "; std::cin >> time; double interest = (principal * rate * time) / 100; std::cout << "Simple Interest: " << interest << std::endl; return 0; }
Factorial Calculator
// Include iostream, string and algorithm #include int main() { int number, factorial = 1; std::cout << "Enter a number: "; std::cin >> number; for (int i = 1; i <= number; i++) { factorial *= i; } std::cout << "Factorial of " << number << " is " << factorial << std::endl; return 0; }
Fibonacci Series Generator
// Include iostream #include int main() { int terms, first = 0, second = 1, next; std::cout << "Enter the number of terms: "; std::cin >> terms; for (int i = 0; i < terms; i++) { if (i <= 1) { next = i; } else { next = first + second; first = second; second = next; } std::cout << next << " "; } std::cout << std::endl; return 0; }
Prime Number Checker
// Include iostream and cmath #include int main() { int number; bool isPrime = true; std::cout << "Enter a number: "; std::cin >> number; if (number < 2) isPrime = false; for (int i = 2; i <= sqrt(number); i++) { if (number % i == 0) { isPrime = false; break; } } if (isPrime) std::cout << number << " is a prime number." << std::endl; else std::cout << number << " is not a prime number." << std::endl; return 0; }