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!

20 Must Practice Basic C++ Programs for Beginners

Last Updated on July 19, 2023 by Abhishek Sharma

In this article, we’ve covered the top 20 must practice basic C++ practice problems for beginners. It is advised to solve these questions from your side, i.e., this article should be a learning resource for starting your coding journey.

  1. The beginning of any programming language deals with your first “Hello World!” Can you print this “Hello World!” in C++?
    Ans. (Main function is always executed first)

    #include
    using namespace std;
    int main() {
        cout<<"Hello World!";
        return 0;
    }

    Output:

    Hello World
  2. Can you add two variables in C++?
    Ans. (Declare two variables and use ‘+’ operator)

    #include 
    using namespace std;
    int main() {
        int a = 2;
        int b = 6;
        cout<<a+b;
        return 0;
    }

    Output:

    8
  3. Find the maximum number in an array of integers.
    Ans. (Define a maximum variable which will store the maximum in each iteration)

    #include 
    using namespace std;
    int main() {
        int arr[5] = { 1, 2, 5, 4, 3 };
        int max = 0;
        for(int i = 0;i<5;i++)
            {
                if(max < arr[i])
                    max = arr[i];
            }
        cout<<max<<endl;
        return 0;
    }

    Output:

    5
  4. Can you find out the sum of the digits of a number?
    Ans. (run a loop until the number is zero and keep adding each digit)

    #include 
    using namespace std;
    int main() {
        int number = 145;
        int sum = 0;
        while(number!=0){
            int digit = number%10;
            number = number/10;
            sum += digit;
        }
        cout<<sum<<endl;
        return 0;
    }

    Output:

    10
  5. Try to swap two numbers with a third variable.
    Ans. (store one number is a temporary variable)

    #include 
    using namespace std;
    int main() {
        int number1 = 145;
        int number2 = 200;
        cout<<number1<<" "<<number2<<endl;
        int temp = number1;
        number1 = number2;
        number2 = temp;
        cout<<number1<<" "<<number2<<endl;
        return 0;
    }

    Output:

    145 200
    200 145
  6. Can you now swap two numbers without a third variable?
    Ans. (use the difference and sum operation)

    #include 
    using namespace std;
    int main() {
        int number1 = 145;
        int number2 = 200;
        cout<<number1<<" "<<number2<<endl;
        number1 = number1 + number2;
        number2 = number1 - number2;
        number1 = number1 - number2;
        cout<<number1<<" "<<number2<<endl;
        return 0;
    }

    Output:

    145 200
    200 145
  7. Can you check whether a number is prime or not?
    Ans. (simple check whether all number below it can divide it or not)

    #include 
    using namespace std;
    int main() {
        int a = 23;
        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
  8. Write a program to find the reverse of a number.
    Ans. (try to get each digit and multiply with its corresponding power of 10)

    #include 
    using namespace std;
    int main() {
        int a = 1223;
        int res = 0;
        while(a!=0){
            int dig = a%10;
            res = res*10 + dig;
            a =a/10;
        }
        cout<<res;
        return 0;
    }

    Output:

    3221
  9. Now when you know to reverse a number, can you try to check whether a number is a palindrome or not?
    Ans. (Palindrome is a number which when reversed we get the same value)

    #include 
    using namespace std;
    int reverse(int a){
        int res = 0;
        while(a!=0){
            int dig = a%10;
            res = res*10 + dig;
            a =a/10;
        }
        return res;
    }
    int main() {
        int a = 1221;
        int b = reverse(a);
        if(a == b)
            cout<<"Palindrome";
        else
            cout<<"Not palindrome";
        return 0;
    }

    Output:

    Palindrome
  10. Find the factorial of a number.
    Ans. (Factorial of n = 12345…(n-1) n, multiply all number below given numbers)

    #include 
    using namespace std;
    int factorial(int n){
        int res = 1;
        for(int i = 2;i<=n;i++)
            res = res*i;
        return res;
    }
    int main() {
        int a = 5;
        cout<<factorial(a)<<endl;
        return 0;
    }

    Output:

    120
  11. Write a C++ program to check whether a number is Armstrong number or not.
    Ans. (An Armstrong number is a number that is equal to the sum of cubes of its digits. Extract each digit and add their cubes)

    #include
    using namespace std;
    int sumofcubes(int n){
        int res = 0;
        while(n!=0)
            {
                int dig = n%10;
                res = res + (dig*dig*dig);
                n = n/10;
            }
        return res;
    }
    int main() {
        int a = 371;
        if(a == sumofcubes(a))
            cout<<"Armstrong Number";
        else cout<<"Not Armstrong Number";
        return 0;
    }
    

    Output:

    Armstrong Number
  12. Can you convert a decimal number to binary?
    Ans. (Divide by 2 and store the remainders in reverse order)

    #include
    using namespace std;
    int main () {
    int a[10], n, i;    
    n = 23;
    for(i=0; n>0; i++)    
    {    
        a[i]=n%2;    
        n= n/2;  
    }
    for(i = i-1;i>=0;i--)
        cout<
    
    						 
  13. Now can you revert back the original number from binary? Convert Binary to Decimal number.
    Ans. (Multiply each digit by position power of 2, 10 = 121+020)

    #include<iostream.h> 
    using namespace std;
    int convertBinaryToDecimal(int n)
    {
        int decimalNumber = 0, i = 0, dig;
        while (n!=0)
        {
            dig = n%10;
            n /= 10;
            decimalNumber += dig*pow(2,i);
            ++i;
        }
        return decimalNumber;
    }
    int main() {
    int n = 10111;
    cout<
    
    						 
  14. Do you know about Fibonacci Series? The series following 1,1,2,3,5,8…, can you try to print the series upto n elements?
    Ans. (Fibonacci series can be given by Fn=Fn-1+F(n-2), where F1=1, F2=1. We need to keep track of previous two elements and when we get our next element change the value of previous variables to new ones)

    Sample Descri
    #include
    using namespace std;
    void fib(int n)
    {
        if(n==1){
            cout<<1;return;
        }
        if(n==2){
            cout<<1<<" "<<2<<" ";
            return;
        }
        int a = 1,b = 1;
        cout<
    
    ption						 
  15. Write a C++ program to count all the vowels in a given string.
    Ans. Iterate through the string and increase count every time we find a vowel.

    #include
    using namespace std;
    
    int main() {
    string s = "abcde";
    int cnt =0 ;
    for(int i = 0;i
    
    						 
  16. Can you search for an element in a given array? Try to print that number if present else print -1.
    Ans. Iterate through the array and if an element is present then print the element.

    #include
    using namespace std;
    bool search(int arr[],int n,int k){
        for(int i =0;i
    
    						 
  17. Find GCD of two number.
    Ans. (GCD of two numbers is the greatest number which is a factor of both numbers and divides both numbers)

    #include 
    using namespace std;
    int gcd(int a,int b){
        if(b == 0)
            return a;
        return gcd(b,a%b);
    }
    int main() {
    int a = 35,b = 25;
    cout<
    
    						 
  18. Print out the Fibonacci Triangle.
    Ans. Fibonacci triangle is given by following –
    1
    1 1
    1 1 2
    1 1 2 3
    1 1 2 3 5 and so on

    #include#include
    using namespace std;
    int main() {
     int a=0,b=1,i,c,n,j;    
      n = 7;
        for(i=1; i<=n; i++)    
        {    
            a=0;    
            b=1;    
            cout<
    
    						 
  19. Check if a year is a leap year or not.
    Ans. A year is a leap year iff:
    The year is multiple of 400.
    The year is multiple of 4 and not multiple of 100.

    #include 
    using namespace std;
    bool checkleapyear(int year) 
    { 
        if (year % 400 == 0) 
            return true; 
    
        if (year % 100 == 0) 
            return false; 
    
        if (year % 4 == 0) 
            return true; 
        return false; 
    } 
    
    int main() {
     int yr = 2020;
     if(checkleapyear(yr))cout<<"Leap Year";
     else cout<<"Not Leap Year";
    return 0;
    }
    Output: Leap Year
    
  20. Given an array of integers, sort the given array in ascending order.
    Ans. We will use a simple algorithm here. It is called Bubble Sort. The concept here is very simple – we just need to swap the bigger element with the end of the loop elements. This will bubble out the bigger elements to the end of the array and hence we will have smaller elements at the left of the array and bigger elements to the right of the array.

    #include 
    using namespace std;
    void swap(int *a, int *b)  
    {  
        int temp = *a;  
        *a = *b;  
        *b = temp;  
    }  
    void bubblesort(int arr[], int n)  
    {   
        for (int i = 0; i < n-1; i++)      
            for (int j = 0; j  arr[j+1])  
                swap(&arr[j], &arr[j+1]);  
    
        for ( int i = 0; i < n; i++)  
            cout << arr[i] << " ";  
        cout << endl;  
    }  
    int main() {
     int arr[5] = {4,3,5,1,2};
     bubblesort(arr,5);
    return 0;
    }
    

    Time Complexity: O(n2), where n is the size of the array as we used two loops.

    Space Complexity: O(1), no extra space is required

Conclusion
This article tried to discuss the top 20 Important C++ Practice Problems for beginners. Hope this blog helps you understand the concept. You can check out more C++ programs for practice here. MYCODE | Competitive Programming.

Frequently Asked Questions (FAQs)

Q1. Is C++ good for beginners?
Ans. Both Python and C++ are popular, easy programming languages for beginners, and choosing the one to learn first is often a matter of personal preference. For one thing, these languages share many similarities.

Q2. Is C++ easy or hard?
Ans. Is C++ hard to learn? C++ is known to be one of the most difficult programming languages to learn over other popular languages like Python and Java.

Q3. What are the three most important concepts in C++?
Ans. The destructor, copy constructor, and assignment operator are the Big Three.

Q4. What should I start with in C++?
Ans. Do Some Tutorials and Learn C++ Syntax. Syntax is the grammar of a programming language and the foundation for everything written in C++. This grammar also defines how C++ code is written and comprehended.

Q5. Can I teach myself C++?
Ans. The simplest way to learn C++ is to enroll in a comprehensive course that will teach you everything you need to know to master C++.

Leave a Reply

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