# C++ program to find the smallest number in the array using class

Submitted by Shubh Pachori, on August 13, 2022

## Problem statement

Given an array, we have to find the smallest number in the array using the class and object approach.

Example:

```Input:
array[0]:22
array[1]:43
array[2]:4
array[3]:30
array[4]:51

Output:
Smallest Number is 4
```

## C++ code to find the smallest number in the array using class and object approach

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

// create a class
class Array {
// private data member
private:
int array[5];

// public functions
public:

// putArray() function to get the value of the array
void putArray() {
for (int index = 0; index <= 4; index++) {
cout << "array[" << index << "]:";
cin >> array[index];
}
}

// smallest() function to find the
// smallest number in the array
int smallest() {
// let the first element of the array is min
int min = array[0];

// for loop to read the whole array from
// the second term to the last
for (int index = 1; index <= 4; index++) {

// if the value at the index is smaller than
// the min then the value
// will replace the value at the min
if (array[index] < min) {
min = array[index];
}
}

// returning the Smallest number
return min;
}
};

int main() {
// create a object
Array A;

// int type variable to store the
// Smallest number in it
int min;

// function is called by the object
// to store the array
A.putArray();

// smallest() function is called by the
// object to find the
// smallest value in the array
min = A.smallest();

cout << "Smallest Number is " << min;

return 0;
}
```

### Output

```array[0]:331
array[1]:3
array[2]:110
array[3]:11
array[4]:43
Smallest Number is 3
```

### Explanation

In the above code, we have created a class Array, one int type array data member array[5] to store the values, and public member functions puArray() and smallest() to store the given values in the array and to find the smallest one.

In the main() function, we are creating an object A of class Array, reading an integer value by the user of the array using the putArray() function, and finally calling the smallest() member function to find the smallest number in the given integer number in the array. The smallest() function contains the logic to find the smallest number in the given numbers and printing the result.

Comments and Discussions!

Student's Section
Subscribe

Copyright © 2024 www.includehelp.com. All rights reserved.