/* This program would serve to demostrate while and d-while loops. */ #include using namespace std; int main() { /* 1. Loop to calculate the sum of the first 10 natural numbers. * We need to add 1 + 2 + 3 + 4 + ... + 10 * We accumulate the sum in a variable. That is we sum stuff one addition at a time. * Calculate 0 + 1 first. Then add that sum to 2, then add that sum to 3 and so on. * At each step, we check if the value we are adding has reached 10. If it has passed 10, we stop. */ int i = 1, sum = 0; // i is conventionally used for counting variables. while (i <= 10) { sum = sum + i; i++; // This moves to the next i at each step } cout<< "The sum of the first 10 natural numbers is "<< sum << endl; /* 2. Loop to print numbers from 20 through 1 in descending order. * We need to print 20 19 18 ... 2 1 * We only need a counting variable here. * This is to demonstrate that loops can count backwards. */ i = 20; // reusing the old counting variable. // using a do - while loop here do { cout<< i << " "; i--; }while (i>=1); cout<>ans; // This is to avoid printng "No, this is Patrick!! when the user says no if(ans == 'Y') { cout<<"No, this is Patrick!!"<>ans; while( ans == 'Y') { cout<<"No, this is Patrick!!"<>ans; } cout<> num; while (i<=num) { fact = fact * i; i++; } cout <<"The factorial is "<< fact<