Write a C program which will take 30 numbers from the keyboard and display the min of these numbers . By using
•for loop
•while loop
•do while loop
#include <iostream>
using namespace std;
int main()
{
const int n = 30;
int a[n];
cout << "Enter array: ";
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
int min = a[0];
for (int i = 1; i < n; i++)
{
if (a[i] < min)
{
min = a[i];
}
}
cout << "The min of these numbers by using for loop: " << min << "\n";
int i = 1;
min = a[0];
while (i < n)
{
if (a[i] < min)
{
min = a[i];
}
i++;
}
cout << "The min of these numbers by using while loop: " << min << "\n";
i = 1;
min = a[0];
do
{
if (a[i] < min)
{
min = a[i];
}
i++;
} while (i < n);
cout << "The min of these numbers by using while loop: " << min << "\n";
return 0;
}
Comments
Leave a comment