Followers

Monday, April 10, 2023

Program To Find The Minimum Number In An Array

 

Program:

 #include <iostream>    
  using namespace std;    
  int main() {    
   int arr[5] = {1, 2, 3, 4, 5}, min_num=arr[0];    
   for(int i=0; i<5;i++){   
       if(arr[i]>min_num){   
         min_num=arr[i];   
       }   
    }   
    cout<<"The minimum number in the array [1, 2, 3, 4, 5] is "<<min_num<<endl;   
   return 0;   
  }   
Output:

Working Of The Program:

The program works as follows:
1. This code is a C++ program that finds the minimum element in an array of integers.
2. The program initializes an array arr of size 5 with the values {1, 2, 3, 4, 5}.
3. The program also initializes a variable min_num to the first element of the array, which is 1.
4. The program then enters a for loop that iterates over each element of the array, using the loop counter variable i.
5. Inside the loop, the program checks if the current element of the array arr[i] is less than the current value of min_num.
6. If the current element is less than min_num, the program updates the value of min_num to be the current element arr[i].
7. After the loop completes, the program outputs the minimum element of the array to the console using cout.
8. The output message includes the phrase "The minimum number in the array", followed by the array elements in square brackets and the calculated minimum value.
9. Finally, the program terminates with a return value of 0.

1 comment:

Program to Find the Length of a String

Program: #include <iostream> using namespace std; int main() { string a; cout<<"Enter the string...