Thursday, December 14, 2017

C++ Number of loops, Smallest and Biggest number without using for loop

#include <iostream>
using namespace std;

int main()
{

    int largest = 0;
    int smallest = 0;
    int counter = 0;
    int num;

    counter + 1;

    while (counter < 10)
    {

        cout << "Enter a number: ";
        cin >> num;

        if (counter == 0)
        {
            smallest = num; // this will put the first number in smallest
        }

        if (num > largest)
        {
            largest = num;
        }

        if (num < smallest)
        {
             smallest = num;
        }

    counter = counter + 1;

    }

    cout << "\nSmallest number: " << smallest << "\nLargest number: " << largest << "\nNumber of loops: " << counter << endl;

}

No comments:

Post a Comment