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

C++ is one the most popular language in the programming world. In this article we will be looking towards 10 simple 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. Adding two numbers in C++.
    Ans. Take two variables and take user input and add them.

    #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. Check if a number is even or odd.
    Ans. If the given number is divisible by 2 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.
    Ans. Use a temporary variable to store one of the numbers.

    #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 program to find the largest number among three numbers.
    Ans. 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. Find the sum of all the natural numbers from 1 to n.
    Ans. We can do this using two ways, 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 to check whether a number is prime or not.
    Ans. 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. Compute the power a given number to a given power.
    Ans. We need to keep multiplying the same number power 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. Calculate the average of all the elements present in an array.
    Ans.

    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.
    Ans. 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 CPP.
    Ans. 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 simple 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 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:

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 (such as Codecademy, Udemy, or Coursera), 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 *