on Leave a Comment

C++ Program to Display Factors of a Number

This C++ program display factors of a number using for loop and if statement. First you have to enter a number using keyboard and factors of that numbers will display on the screen.

Factors of a number are the numbers that can completely divide that number.

C++ Program to Display Factors of a Number

#include <iostream>
using namespace std;
int main()
{
    int i, num;
    cout << "Enter a Number: ";
    cin >> num;
    cout << "Factors of " << num<<": ";
    for(i = 1; i <= num; i++)
    {
        if(num%i == 0)
        {
            cout << i << "  ";
        }
    }
    return 0;
}

OUTPUT:

Enter a Number: 100
Factors of 100: 1  2  4  5  10  20  25  50  100

See also:




on Leave a Comment

Sizeof Operator in C++

C++ has a keyword called sizeof, that determines the size of any data type either predefined data type or user defined data type. The sizeof operator is used to get the size of any data type, for example classes, structure, union or any data type.

When we defining a class, we actually define a new data type and this is same for structure and union. The sizeof operator is a compile time operator.

The sizeof operator determines the size of data type in bytes.

Note: sizeof is a operator, it is not a function.

Syntax of sizeof operator

sizeof(data-type)

Here data type can be any predefined data type or user defined data type.

Example:

int y = sizeof(int);
cout<<y;

Output:

4

Program to understand sizeof operator in C++

Example 1:

#include<iostream>
using namespace std;
class A
{
public:
    int x, y;
};
class B: public A
{
public:
    int a, b;
};
int main()
{
 int y = sizeof(B);
 cout<<y;
 return 0;
}

OUTPUT:

16

Size of class B is 16 because it also contains the variables of class A.

Example 2:

#include<iostream>
using namespace std;
int main()
{
          cout<<"Size of int is "<<sizeof(int);
          cout<<"\nSize of char is "<<sizeof(char);
          cout<<"\nSize of float is "<<sizeof(float);
          cout<<"\nSize of double is "<<sizeof(double);
          cout<<"\nSize of wchar_t is "<<sizeof(wchar_t);
          cout <<"\nSize of short int : "<<sizeof(short int);
          cout <<"\nSize of long int : "<<sizeof(long int);
          return 0;
}

OUTPUT:

Size of int is 4
Size of char is 1
Size of float is 4
Size of double is 8
Size of wchar_t is 2
Size of short int : 2
Size of long int : 4


on Leave a Comment

C++ Program for Insertion Sort

This c++ program sorts an array of n elements using insertion sort technique. In this program, first you have to enter size of an array, then you have to enter elements of array using keyboard and finally sorted array is displayed on the screen.

C++ Program for Insertion Sort

#include<iostream>
using namespace std;
void insertionSort(int arr[], int size);
int main()
{
        int arr[50], n, i, j;
        cout<<"Enter Array Size : ";
        cin>>n;
        cout<<"Enter Array Elements : ";
        for(i=0; i<n; i++)
        {
                cin>>arr[i];
        }
        insertionSort(arr, n);
        return 0;
}
void insertionSort(int arr[], int size)
{
    int i, j, temp;
    for(i=1; i<size; i++)
        {
                temp=arr[i];
                j=i-1;
                while((temp<arr[j]) && (j>=0))
                {
                        arr[j+1]=arr[j];
                        j=j-1;
                }
                arr[j+1]=temp;
        }
        cout<<"Sorted Array : ";
        for(i=0; i<size; i++)
        {
                cout<<arr[i]<<"  ";
        }
}

OUTPUT:

Enter Array Size : 5
Enter Array Elements : 9 8 4 2 7
Sorted Array : 2  4  7  8  9

See also:


on Leave a Comment

C++ Program for Bubble Sort

This c++ program sorts an array of n elements using bubble sort technique. In bubble sort technique, adjacent elements of array is compared. In this program, first you have to enter size of an array, then you have to enter elements of array using keyboard and finally sorted array is displayed on the screen.

C++ Program for Bubble Sort

#include<iostream>
using namespace std;
int main()
{
    int arr[50], n, i, j, temp;
    cout<<"Enter the size of array: ";
    cin>>n;
    cout<<"Enter the array elements: ";
    for(i=0; i<n; i++)
        cin>>arr[i];
    for(i=1; i<n; i++)
    {
        for(j=0; j<(n-i); j++)
            if(arr[j]>arr[j+1])
            {
                temp=arr[j];
                arr[j]=arr[j+1];
                arr[j+1]=temp;
            }
    }
    cout<<"Sorted Array: ";
    for(i=0; i<n; i++)
        cout<<arr[i]<<"  ";
    return 0;
}

OUTPUT 1:

Enter the size of array: 3
Enter the array elements: 8 6 1
Sorted Array: 1  6  8

OUTPUT 2:

Enter the size of array: 5
Enter the array elements: 9 5 6 4 2
Sorted Array: 2  4  5  6  9

on Leave a Comment

Access Modifiers in C++

Access modifiers is necessary in object-oriented programming for data hiding. Access modifiers specifies who can access data and who can't access data. In class, we can set accessibility of instance member and static members by access modifiers.  

C++ has three access modifiers:

(1)  Public
(2)  Private
(3)  Protected

Public Access Modifier in C++

Public is keyword is c++ programming language. The variables and functions declared under public access modifier is accessible to everyone inside or outside of that class. Other class members can also access public members of another class. 

Public members can accessed by using (.) dot operator.

During inheritance, public members of base class accessible to derived class.

Programming example of public access modifier:

#include<iostream>
using namespace std;
class Rectangle
{
public:
    double l, b;
    double area();
    void setData(double, double);
};
void Rectangle:: setData(double L, double B)
{
    l = L;
    b = B;
}
double Rectangle:: area()
{
    return l*b;
}
int main()
{
    Rectangle R1;
    R1.setData(10.5, 5.0);
    cout<<"Area is "<<R1.area();
    return 0;
}

OUTPUT:

Area is 52.5

Private Access Modifier in C++

Private is keyword is c++ programming language. The variables and functions declared under private access modifier can be only accessible inside the class. Other class members can not access private members of another class. 

Objects of class can't access private members directly. But object can access private members by public functions of that class.

During inheritance, private members do not inherits.

Friend functions can access private members indirectly.

Programming example of private access modifier:

#include<iostream>
using namespace std;
class Rectangle
{
private:
    double l, b;
public:
    double area();
    void setData(double, double);
};
void Rectangle:: setData(double L, double B)
{
    l = L;
    b = B;
}
double Rectangle:: area()
{
    return l*b;
}
int main()
{
    Rectangle R1;
    R1.setData(10.5, 5.0);
    cout<<"Area is "<<R1.area();
    return 0;
}

OUTPUT:

Area is 52.5

Protected Access Modifier in C++

Protected is keyword is c++ programming language. The variables and functions declared under protected access modifier can be only accessible inside the class. Other class members can not access protected members of another class. 

Objects of class can't access protected members directly. But object can access protected members by public functions of that class.

During inheritance, protected member of base class accessible to derived class.

Friend functions can access protected members indirectly.

Programming example of protected access modifier:

#include<iostream>
using namespace std;
class Rectangle
{
protected:
    double l, b;
public:
    double area();
    void setData(double, double);
};
void Rectangle:: setData(double L, double B)
{
    l = L;
    b = B;
}
double Rectangle:: area()
{
    return l*b;
}
int main()
{
    Rectangle R1;
    R1.setData(10.5, 5.0);
    cout<<"Area is "<<R1.area();
    return 0;
}

OUTPUT:

Area is 52.5


on Leave a Comment

C++ Program to Find Transpose of a Matrix



This article contains C++ programming code to transpose a matrix.

C++ Program to Find Transpose of a Matrix

#include<iostream>
using namespace std;
int main()
{
        int a[3][3], i, j, t[3][3];
        cout<<"Enter elements of array (3*3): ";
        for(i=0; i<3; i++)
        {
                for(j=0; j<3; j++)
                {
                        cin>>a[i][j];
                }
        }
        for(i=0; i<3; i++)
        {
                for(j=0; j<3; j++)
                {
                        t[i][j]=a[j][i];
                }
        }
        cout<<"Transpose of the Matrix is :\n";
        for(i=0; i<3; i++)
        {
                for(j=0; j<3; j++)
                {
                        cout<<t[i][j]<<"\t";
                }
                cout<<"\n";
        }
        return 0;
}

OUTPUT:

Enter elements of array (3*3): 1
2
3
1
2
3
1
2
3
Transpose of the Matrix is :
1       1       1
2       2       2
3       3       3

See also: