121k views
0 votes
Write a program that implements the algorithm to find the smallest number in a list of numbers. The program should continue to read numbers from the keyboard until a value of 0 (zero) is entered. It should then print the smallest number found and exit.

1 Answer

3 votes

Answer:

#include <bits/stdc++.h>

using namespace std;

int main() {

int a[10000];//array of ten thousand capacity.

int n=0,min=INT_MAX;//decalring n and min variables.

do{

cin>>a[n];

}while(a[n++]!=0); //using do while loop to take input.

for(int i=0;i<n-1;i++)//looping to n-2th index because 0 is at n-1th index.

{

if(a[i]<min)

{

min=a[i];

}

}

cout<<"The minimum element is "<<min<<endl;//printing the minimum.

return 0;

}

Input:-

5 2 6 78 32 31 21 6 45 2 1 0

Output:-

The minimum element is 1

Step-by-step explanation:

I have created an array of capacity 10000.Taking input till 0 is entered. Now the 0 is at the n-1 th index so we have to consider elements upto n-2th elements.Using for loop to iterate upto n-2 th index and finding the minimum among those elements.

User Ruben Trancoso
by
5.6k points