20 Must Practice basic C++ Programs for beginners

1. 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<iostream.h>
using namespace std;
int main() {
    cout<<"Hello World!";
    return 0;
}
Output: Hello World

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

#include<iostream.h> 
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 <iostream.h>
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 digits of a number?
Ans. (run a loop until the number is zero and keep adding each digit)

#include<iostream.h> 
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<iostream.h> 
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<iostream.h> 
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<iostream.h> 
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<iostream.h> 
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 <iostream.h>
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 <iostream.h>
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 utpo 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 is a year is 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

This article tried to discuss Important C++ Practice Questions for beginners. Hope this blog helps you understand the concept. To practice more problems you can check out MYCODE | Competitive Programming.

Leave a Reply

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