Monday, 8 March 2021

Basic C++ Questions and its Solution

 Question 1: Make a simple Calculator for addition, subtraction, multiplication and division using user                             define function.

Solution:

#include<iostream>
#include<string>
using namespace std;

int add(int aint b)
{
    return a+b;
}
int subtract(int aint b)
{
    return a-b;
}
int multiply(int aint b)
{
    return a*b;
}
int divide(int aint b)
{
    return a/b;
}

int main()
{
    int var1var2;
    cout<<"Enter First Number: ";
    cin>>var1;
    cout<<"Enter Second Number: ";
    cin>>var2;
    cout<<"1. Add"<<endl;
    cout<<"2. Subtract"<<endl;
    cout<<"3. Multiply"<<endl;
    cout<<"4. Divide"<<endl;
    int choice,a;
    cout<<"Enter Your Choice: ";
    cin>>choice;
    switch (choice)
    {
    case 1:
        {
            a=add(var1var2);
            cout<<"Sum is: "<<a<<endl;
            break;
        }
    case 2:
        {
            a=subtract(var1var2);
            cout<<"Difference is: "<<a<<endl;
            break;
        }
    case 3:
        {
            a=multiply(var1var2);
            cout<<"Multiplication is: "<<a<<endl;
            break;
        }
    case 4:
        {
            a=divide(var1var2);
            cout<<"Division is: "<<a<<endl;
            break;
        }
    
    default:
        break;
    }
}

Question 2: Write a program to find factorial of any numbers using
         recursive method.
Solution:
#include<iostream>
#include<string>
using namespace std;
int factorial(int n)
{
    if(n==1)
    {
        return 1;
    }
    else
    {
        return n*factorial(n-1);
    }
}
int main()
{
    int fact,n;
    cout<<"enter Number to find Factorial: ";
    cin>>n;
    fact=factorial(n);
    cout<<"Factorial of "<<n<<"is: "<<fact<<endl;
    return 0;
}
Question 3:What is difference between Object Oriented Programming(OOPs)
        and Procedural Programming Language?
Solution:
Procedural Programming Language:
a)In this program is divided into small parts called function.
b)It follows top down approach.
c)Adding new data and function is not easy.
d)It does not have any proper way for hiding data so it is less secure.
e)Overloading is also not possible in this.
Object Oriented Programming Language:
a)In this program is divided into small parts called objects.
b)It follows bottom up approach.
c)Adding new data and function is easy.
d)It provides data hiding so it is more secure.
e)Overloading is possible in this.

Question 4:
Solution:
#include<iostream>
#include<string>
using namespace std;

void leftRotation(int arr[], int n)
{
    int temp;
    temp=arr[0];
    for(int i=0;i<n;i++)
    {
        arr[i]=arr[i+1];
    }
    arr[n-1]=temp;
}
void printArray(int arr[], int n)
{
    for(int i=0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }
}
int main()
{
    int n;
    cout<<"enter n: ";
    cin>>n;
    int arr[n];

    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    leftRotation(arrn);
    printArray(arrn);
}


No comments:

Post a Comment

Full Specification of Redmi Note 10, Note 10 Pro and Note 10 Pro Max

  Redmi Note 10 Pro Max General Brand Xiaomi Model Redmi Note 10 Pro Max Price in India ₹18,999 Release date 4th March 2021 Launched in Indi...