C++ Programs: From Basics to Advanced Concepts | Updated 2025

C++ Programs: Frequently Asked Examples with Solutions

CyberSecurity Framework and Implementation article ACTE

About author

Manoj (Web Developer )

Manoj is a C++ instructor who focuses on writing and evaluating C++ programs covering fundamental subjects. He specializes in algorithmic problem solving and hands-on programming. Using structured logic and real-world examples, he teaches how to write effective, readable code. He is an expert in control flow, functions, arrays, recursion, and object-oriented design.

Last updated on 15th Sep 2025| 10722

(5.0) | 32961 Ratings

Introduction to C++ Programs

C++ is a powerful, high-level programming language that is an extension of the C language. It supports object-oriented, procedural, and generic programming features, making it a versatile language used in systems development, embedded programming, game engines, and more. To complement such multi-paradigm versatility with practical web development expertise, exploring Web Developer Training equips learners to apply these programming concepts in JavaScript, TypeScript, and full-stack frameworks building dynamic, scalable web applications that integrate robust logic with interactive user interfaces. As one of the most foundational languages in computer science, learning to write C++ programs helps in understanding core programming concepts like memory management, data structures, and object-oriented design. C++ provides strong typing, compile-time checking, and efficient memory management. With the Standard Template Library (STL), it also offers a vast collection of pre-built classes and functions to make programming easier.


To Earn Your Web Developer Certification, Gain Insights From Leading Data Science Experts And Advance Your Career With ACTE’s Web Developer Courses Today!


Basic Input/Output in C++ Programs

Basic Input/Output in C++ are performed using the cin and cout streams, provided by the <iostream> header. These are part of the standard namespace std. To complement such foundational programming constructs with modern code management practices, exploring Ultimate Guide to Git and Version Control reveals how developers can track changes, collaborate efficiently, and maintain clean commit histories leveraging Git’s distributed architecture to streamline development workflows across teams and projects.

  • #include <iostream>
  • using namespace std;
  • int main() {
  • int num;
  • cout << “Enter a number: “;
  • cin >> num;
  • cout << “You entered: ” << num << endl;
  • return 0;
  • }

Basic Input/Output program takes an integer input and prints it using cout. The << and >> operators are used for output and input respectively. You can also read characters and strings using cin.get() and getline().

    Subscribe To Contact Course Advisor

    Arithmetic Operations Program

    Arithmetic Operations in C++ can be performed using operators like +, -, *, /, and %. To complement such low-level computational logic with scalable software architecture, exploring What are Microservices reveals how modern applications are built as loosely coupled services each handling specific business functions independently, enabling rapid deployment, fault isolation, and efficient resource utilization across distributed systems.

    • #include <iostream>
    • using namespace std;
    • int main() {
    • int a, b;
    • cout << “Enter two numbers: “;
    • cin >> a >> b;
    • cout << “Addition: ” << a + b << endl;
    • cout << “Subtraction: ” << a – b << endl;
    • cout << “Multiplication: ” << a * b << endl;
    • cout << “Division: ” << (b != 0 ? (a / b) : 0) << endl;
    • cout << “Modulus: ” << a % b << endl;
    • return 0;
    • }

    This program demonstrates how all five arithmetic operations work. It’s important to handle exceptions such as division by zero in more robust programs.


    Would You Like to Know More About Web Developer? Sign Up For Our Web Developer Courses Now!


    Conditional Statements (if, else if, switch)

    Conditional Statements (if, else if, switch)

    • int age;
    • cin >> age;
    • if (age >= 18)
    • cout << “Eligible to vote.”;
    • else
    • cout << “Not eligible.”;
    • // Switch-case is used when we have multiple discrete options.
    • int option;
    • cin >> option;
    • switch(option) {
    • case 1: cout << “Option 1 selected”; break;
    • case 2: cout << “Option 2 selected”; break;
    • default: cout << “Invalid option”;
    • }

    The switch statement is particularly useful for menu-driven programs and simplifying long if-else chains.

    Course Curriculum

    Develop Your Skills with Web Developer Certification Course

    Weekday / Weekend BatchesSee Batch Details

    Looping Constructs (for, while, do-while)

    Looping Constructs are used to execute a block of code multiple times. To complement such control flow mechanisms with high-level scripting capabilities, exploring What is Python Programming reveals how Python’s simple syntax and dynamic semantics make it ideal for implementing loops, iterators, and range-based operations enabling developers to write clean, readable code for automation, data processing, and web development tasks.

    For Loop:

    • for(int i = 1; i <= 5; i++)
    • cout << i << ” “;

    While Loop:

    • int i = 1;
    • while(i <= 5) {
    • cout << i << ” “;
    • i++;
    • }

    Do-While Loop:

    • int i = 1;
    • do {
    • cout << i << ” “;
    • i++;
    • } while(i <= 5);

    Loops can be nested or controlled using break and continue. They are essential for iterating through arrays and performing repetitive computations.


    Are You Interested in Learning More About Web Developer? Sign Up For Our Web Developer Courses Today!


    Functions and Recursion in C++ Programs

    Functions help in breaking down a program into smaller, manageable pieces. To complement such modular programming practices with hands-on development experience, exploring Web Developer Training equips learners to structure code efficiently using reusable functions, callbacks, and event-driven logic building scalable web applications with clean architecture and maintainable components.

    • int add(int x, int y) {
    • return x + y;
    • }

    Recursive Function for Factorial:

    • int factorial(int n) {
    • if (n <= 1)
    • return 1;
    • return n * factorial(n – 1);
    • }

    Recursive functions are elegant but should be used with care to avoid stack overflow errors. Use iteration if recursion depth is unpredictable.

    Web Development Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

    Arrays and Matrix Manipulation

    • // 1D Array:
    • int arr[5] = {1, 2, 3, 4, 5};
    • for(int i = 0; i < 5; i++)
    • cout << arr[i] << ” “;
    • // 2D Array Addition:
    • int A[2][2] = {{1,2}, {3,4}};
    • int B[2][2] = {{5,6}, {7,8}};
    • int C[2][2];
    • for(int i = 0; i < 2; i++) {
    • for(int j = 0; j < 2; j++) {
    • C[i][j] = A[i][j] + B[i][j];
    • }
    • }

    You can also write programs for transpose, multiplication, and determinant calculation. To complement such matrix-based operations with flexible data handling, exploring Need To Know About Python List reveals how Python lists serve as dynamic containers supporting nested structures, element-wise manipulation, and integration with libraries like NumPy for efficient numerical computation.

    String Handling Programs

    C++ provides the string class for robust string operations. To complement such low-level manipulation capabilities with broader software development insights, exploring What Is a Software Developer outlines the responsibilities, tools, and methodologies involved in designing, coding, testing, and maintaining software systems bridging the gap between technical implementation and real-world application needs.

    String Handling Programs Article

    Palindrome Check:

    • string s;
    • cin >> s;
    • string rev(s.rbegin(), s.rend());
    • if (s == rev)
    • cout << “Palindrome”;
    • else
    • cout << “Not Palindrome”;
    • // Concatenation and Length:
    • string s1 = “Hello”, s2 = “World”;
    • string s3 = s1 + ” ” + s2;
    • cout << s3 << “, length: ” << s3.length();
    • // Other operations include substring, find, replace, and compare.

    Other operations include substring, find, replace, and compare.


    Object-Oriented Programs (Classes and Objects)

    • // Object-Oriented Programs (Classes and Objects)
    • class Student {
    • public:
    • string name;
    • int age;
    • void display() {
    • cout << name << ” is ” << age << ” years old.”;
    • }
    • };
    • int main() {
    • Student s1;
    • s1.name = “John”;
    • s1.age = 20;
    • s1.display();
    • }

    You can enhance this by using constructors, inheritance, and polymorphism.


    File Handling in C++

    • // Write to a file:
    • #include <fstream>
    • ofstream fout(“data.txt”);
    • fout << “Hello, File!”;
    • fout.close();
    • // Read from a file:
    • ifstream fin(“data.txt”);
    • string line;
    • while(getline(fin, line)) {
    • cout << line << endl;
    • }
    • fin.close();

    Also consider checking file status using is_open().


    Standard Template Library (STL) Programs

    • // Vector:
    • #include <vector>
    • vector<int> v = {1,2,3};
    • v.push_back(4);
    • for(int x : v)
    • cout << x << ” “;
    • // Map:
    • #include <map>
    • map<string, int> m;
    • m[“Alice”] = 25;
    • m[“Bob”] = 30;
    • for(auto p : m)
    • cout << p.first << ” : ” << p.second << endl;

    STL also provides set, queue, stack, deque, and many algorithms like sort, count, and find.


    Real-world Examples

    Calculator:

    • char op;
    • float a, b;
    • cin >> a >> op >> b;
    • switch(op) {
    • case ‘+’: cout << a + b; break;
    • case ‘-‘: cout << a – b; break;
    • case ‘*’: cout << a * b; break;
    • case ‘/’: cout << (b != 0 ? a / b : 0); break;
    • default: cout << “Invalid operation”;
    • }

    Prime Check:

    • int n;
    • bool prime = true;
    • cin >> n;
    • for(int i = 2; i <= n/2; i++) {
    • if(n % i == 0) {
    • prime = false;
    • break;
    • }
    • }
    • cout << (prime ? “Prime” : “Not Prime”);

    Fibonacci:

    • int n, a = 0, b = 1, c;
    • cin >> n;
    • cout << a << ” ” << b << ” “;
    • for(int i = 2; i < n; i++) {
    • c = a + b;
    • cout << c << ” “;
    • a = b;
    • b = c;
    • }

    You can also practice programs for sorting algorithms, number theory, and simulations.


    Best practices

    • Use comments and meaningful variable names.
    • Test edge cases and handle input errors.
    • Use STL containers and algorithms for better performance.
    • Break large problems into functions and classes.
    • Avoid memory leaks by managing dynamic memory properly.
    • Use const wherever applicable.
    • Prefer nullptr over NULL in modern C++.
    • Adopt RAII (Resource Acquisition Is Initialization) for resource safety.
    Best practices Article

    By practicing these examples and applying them to solve problems, you will develop a strong foundation in C++ programming that can be extended to advanced topics like algorithms, data structures, multithreading, and system-level programming. To complement such foundational expertise with modern language comparison, exploring Kotlin vs Java highlights key differences in syntax, performance, and developer productivity helping you evaluate which language better suits Android development, enterprise applications, or cross-platform solutions.


    Conclusion

    C++ Programs require knowing the syntax and the logical ideas behind key elements like control structures, functions, data structures, and object-oriented features. To improve your skills, explore advanced algorithms in the Standard Template Library (STL). Also, working with data structures such as linked lists, stacks, and queues can strengthen your understanding. As you advance, tackling mini-projects like a student database, banking system, or inventory management can give you practical experience. To complement such foundational programming skills with real-world application development, exploring Web Developer Training equips learners to apply data structures in dynamic web environments building full-stack projects that integrate front-end interfaces with efficient back-end logic and storage. Joining coding contests using C++ will also help you sharpen your skills. Mastering C++ can open up various career paths in software development, competitive programming, and systems engineering, making it a worthwhile addition to your skill set.

    Upcoming Batches

    Name Date Details
    Web Developer Certification Course

    15 - Sep- 2025

    (Weekdays) Weekdays Regular

    View Details
    Web Developer Certification Course

    17 - Sep - 2025

    (Weekdays) Weekdays Regular

    View Details
    Web Developer Certification Course

    20 - Sep - 2025

    (Weekends) Weekend Regular

    View Details
    Web Developer Certification Course

    21 - Sep - 2025

    (Weekends) Weekend Fasttrack

    View Details