Using a for statement to find the smallest of several integers. - C++ Statement

C++ examples for Statement:for

Description

Using a for statement to find the smallest of several integers.

Demo Code

#include <iostream>

int main(int argc, const char *argv[]) {
    int counter = 0;
    int smallest = 0;
    int current = 0;

    std::cout << "Enter a list of space separated integers to determine the smallest"
        << "first entry is number of separate entries: " << std::endl;
    std::cin >> counter;// w w w  .  j a  v a 2 s  .co m

    // set current smallest
    smallest = counter;

    for (int i = 0; i < counter; i++) {
        std::cin >> current;

        if (current < smallest) smallest = current;
    }

    std::cout << "Smallest: " << smallest << std::endl;
    return 0;
}

Result


Related Tutorials