// Example Program 14 : While loop // Write a program that finds the largest // in a series of positive numbers entered // by the user. // When the user enters 0 or a negative number, // the program displays the largest number and // stops. // Walk through the program for some numbers. // Also, think about how to modify the program // to find the smallest. # include int main() { int largest, number; cout <<"Enter a number : "; cin >> number; cout << endl; largest = number; // Continue processing until number is // zero or negative while (number > 0) { if (largest < number) // Check and largest = number; // update cout <<"Enter a number : "; cin >> number; cout << endl; } cout <<"The largest number is "< int main() { long number; cout << "Enter a positive number : "; cin >> number; cout << endl; cout << "The number with its digits reversed is "; while (number) { cout << (number % 10); number = number / 10; } return 0; } // Example 16: while loop // Write a program that asks the user to enter // two integers, then calculates and displays their // greatest common divisor. #include int main() { int m, n, remainder; cout <<"Enter the first integer : "; cin >> m; cout <<"Enter the second integer : "; cin >> n; remainder = m % n; m = n; n = remainder; while (n) { remainder = m % n; m = n; n = remainder; } cout <<"Greatest common divisor: "< int main() { ifstream infile; ofstream outfile; char ch; infile.open("data.in"); if(!infile) {cout<<"Error";return 0;} outfile.open("data.out"); if(!outfile) {cout<<"Error";return 0;} // Read a charater infile.get(ch); while (!infile.eof()) { // check if it is a lower case // character if(ch >= 'a' && ch <= 'z') ch = ch - 'a' + 'A'; // write the character outfile << ch; // get the next character infile.get(ch); } infile.close(); outfile.close(); return 0; } // Example 18: Nested while loop // Write a program to display a tax table in the // following form. // Amount Tax Rate // ------------------------------------------------------ // 4.0 4.5% 5.0% 5.5% 6.0% 6.5% 7.0% // 100.00 // 200.00 // 300.00 // . // . // . // 1000.00 // ----------------------------------------------------- #include #include int main() { double tax_rate; double amount; cout.setf( ios::fixed, ios::floatfield ); cout.setf( ios::showpoint ); cout <<" Amount Tax Rate"< #include int main() { int perfect_number, divisor_number; int divisor_sum, remainder; int divisor, end_of_data , last_divisor , perfect; const int no_more_data = 0 , initial_sum = 1; const int first_divisor = 2; cout <<"Enter a possible perfect number or 0 to quit"<> perfect_number; end_of_data = ( perfect_number <= no_more_data); while (!end_of_data) { divisor_number = first_divisor; divisor_sum = initial_sum; last_divisor = (divisor_number > sqrt(perfect_number)); while (!last_divisor) { remainder = perfect_number % divisor_number; divisor = (remainder == 0); if (divisor) divisor_sum= divisor_sum + divisor_number + perfect_number/divisor_number; divisor_number = divisor_number + 1; last_divisor = (divisor_number > sqrt(perfect_number)); } perfect = (divisor_sum == perfect_number); if(perfect) cout <<"The number "<> perfect_number; end_of_data = (perfect_number <= no_more_data); } return 0; }