Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

10 Simple C++ Programs for Beginners

Last Updated on June 14, 2023 by Abhishek Sharma

C++ is one the most popular language in the programming world. In this article we will be looking towards 10 basic C++ programs for beginners in CPP. C++ is a powerful general-purpose programming language that was developed in the early 1980s as an extension of the C programming language. It is widely used for developing a wide range of applications, including system software, game development, embedded systems, high-performance applications, and more.

C++ combines both high-level and low-level programming features, offering a balance between performance and abstraction. It supports procedural, object-oriented, and generic programming paradigms, giving developers flexibility in designing and implementing their solutions.

10 Simple C++ Programs for Beginners

Below are the top 10 simple C++ programs for beginners:

  1. Wrtie a program for Adding two numbers in C++.
    To Add two numbers in C++ we will read two numbers a and b from the user then perform add operation to add a and b together to print the addition of two numbers in C++.

    #include <iostream>
    using namespace std;
    int main() {
    int a ;
    int b ;
    cin>>a>>b;
    cout<<a+b;
    return 0;
    }
    Input: 2 5
    Output: 7
  2. C++ program to Check if a number is even or odd.
    TO Check if a given number is even or odd in C++, we simply divide the given number by 2, if the remainder is 0 then it is even otherwise odd.

    #include <iostream>
    using namespace std;
    int main() {
    int a ;
    cin>>a;
    if(a%2 == 0)  // if remainder is zero then even number
        cout<<”even”;
    else       
        cout<<”odd”;
    return 0;
    }
    Input: 8
    Output: even
  3. Write a program to swap two numbers in C++.
    We will use a temporary variable to store one of the numbers to perform the swap operation.

    #include <iostream>
    using namespace std;
    int main() {
    int a = 10;
    int b = 20;
    cout<<a<<" "<<b<<endl;
    int temp = a;
    a = b;
    b = temp;
    cout<<a<<" "<<b<<endl;
    return 0;
    }
    Output: 10 20
    20 10
  4. Write a C++ program to find the largest number among three numbers.
    A number will be largest if number is greater than both the other numbers.

    #include <iostream>
    using namespace std;
    int main() {
    float a, b, c;
    cin >> a >> b >> c;
    if(a >= b && a >= c)
        cout << "Largest number: " << a;
    if(b >= a && b >= c)
        cout << "Largest number: " << b;
    if(c >= a && c >= b)
        cout << "Largest number: " << c;
    return 0;
    }
    Input: 1 2 3
    Largest number: 3
  5. Write a C++ Program to Find the sum of all the natural numbers from 1 to n.
    To find the sum of all the natural number from 1 to n in C++, We have two methods, one is by iterating from 1 to n and adding them up while the other way is using the summation formula –

    summation of i from 1 to n=n(n+1)/2=1+2+3+4+..+(n-1)+n
    #include <iostream>
    using namespace std;
    int main()
    {
    int n, sum = 0;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        sum += i;
    }
    // or sum = n*(n+1)/2;
    cout << sum;
    return 0;
    }
    Input: 5
    Output: 15
  6. Write a program in C++ to check whether a number is prime or not.
    A prime number is not divisible by any number smaller than it except 1. Basically, it has only two factors 1 and itself.

    #include <iostream>
    using namespace std;
    int main() {
    int a ;
    cin>>a;
    int b = 2;
    //start from b as 1 can divide any number
    bool prime = true;
    while(b!=a){
        if(a%b == 0)
            {
                prime = false;
                break;
            }
        b++;
    }
    if(prime)
        cout<<"prime";
    else 
        cout<<"not prime";
    return 0;
    }
    Output: prime
  7. Write a C++ program to Compute the power a given number to a given power.
    To compute the power of a given number in C++, We initialize a variable result to 1. Then, we’ll use a while loop to multiply the result by base for power number of times.

    3^3=3*3*3=27
    #include <iostream>
    using namespace std;
    int main() 
    {
    int power;
    float base, result = 1;
    cin >> base >>power;
    while (power != 0) {
        result *= base;
        power--;
    }
    cout << result;
    return 0;
    }
    Input: 3 3
    Output: 27
  8. Write a C++ program to Calculate the average of all the elements present in an array.
    We iterate over each element of the array and calculate the sum of all the elements. Then, we divide the sum by the size of the array to get the average. The average is stored in a variable of type float and returned.

    average= summation〖arr[i]〗/n
    #include <iostream>
    using namespace std;
    int main()
    {
    int n;
    cin>>n;
    int arr[n];
    float sum = 0.0;
    for(int i = 0;i<n;i++)
        cin>>arr[i];
    for(int i = 0;i<n;i++)
        sum += arr[i];
    cout<<(float)(sum/(float)n);
    return 0;
    }
    Input: 3
    1 4 5
    Output: 3.33333
  9. Write a program to find the GCD of two numbers in C++.
    GCD is the greatest common divisor of the two numbers.

    #include <iostream>
    using namespace std;
    int gcd(int a,int b){
    if(b == 0)
        return a;
    return gcd(b,a%b);
    }
    int main() {
    int a ,b ;
    cin>>a>>b;
    cout<<gcd(a,b);
    return 0;
    }
    Input: 35 25
    Output: 5
  10. Write a function to find the length of a string in C++.
    To find the length of a string in C++, we will Iterate through the string and increase count by 1 till we reach the end of the string.

    #include <iostream>
    using namespace std;
    int main() 
    {
    string str;
    cin>>str;
    int count = 0;
    for(int i = 0;str[i];i++) // till the string character is null
        count++;
    cout<<count;
    }
    Input: abcde
    Output: 5

Summary

  • Building Basic C++ programs is a great way for beginners to learn the basics of the language and gain confidence in programming.
  • By working on these programs, beginners can practice fundamental concepts like variables, data types, control structures, and functions.
  • The 10 simple C++ programs cover a range of topics, including arithmetic operations, input/output, conditionals, loops, and arrays.
  • Working through these programs allows beginners to apply their knowledge and develop problem-solving skills.
  • It is important to understand and analyze the code of these programs to grasp the underlying concepts fully.
  • As beginners progress, they can modify and expand upon these programs to create more complex applications.

FAQs related to 10 simple C++ programs for beginners:

Some FAQs related to Basic C++ programs for Beginners are listed below:
Q1. What are some recommended programs for beginners in C++?
Some recommended programs for beginners include calculating the area of a triangle, finding the factorial of a number, generating Fibonacci series, converting temperature units, and implementing a simple calculator.

Q2. How do I compile and run these programs?
To compile and run C++ programs, you need a C++ compiler installed on your system. Popular compilers include GCC (GNU Compiler Collection) and Microsoft Visual C++. You can use a command-line interface or an Integrated Development Environment (IDE) like Code::Blocks, Dev-C++, or Visual Studio Code to write, compile, and run your programs.

Q3. What are some common errors beginners may encounter?
Beginners may encounter errors such as syntax errors (e.g., missing semicolons or parentheses), logical errors (e.g., incorrect conditions or loop structures), or runtime errors (e.g., dividing by zero or accessing invalid memory locations). These errors can be resolved by carefully analyzing the code and using debugging techniques.

Q4. How can I improve my programming skills beyond these simple programs?
To improve your programming skills, you can challenge yourself by tackling more complex programs and projects. Explore advanced topics like object-oriented programming, file handling, data structures, and algorithms. Additionally, participate in coding competitions, join programming communities, and practice regularly to enhance your skills.

Q5. Are there any additional resources to learn C++ for beginners?
Yes, there are numerous resources available for beginners to learn C++. Some recommended resources include online tutorials, video courses, interactive coding platforms and C++ programming books. These resources often provide step-by-step guidance and exercises to reinforce learning.

One thought on “10 Simple C++ Programs for Beginners

Leave a Reply

Your email address will not be published. Required fields are marked *