Eastern Mediterranean University ITEC113 Algorithms and Programming Techniques School of Computing and Technology Information Technology 2010- 2011 Fall Semester Instructors: Yeşim Kapsıl Çırak– Nazife Dimililer Some example solutions are given for each of the loop exercises below. Exercises on loops In the desired outputs shown below, the data entered from the keyboard is shown using a different and bigger font. Solve all questions using all types of loops. Draw a flowchart, write pseudocode and the C program for each solution. 1- Write a program that prompts the user to enter a number. Use a loop to print as many “I love to program” on screen as that number. Desired Output: Please enter a number: 4 I love to program. I love to program. I love to program. I love to program. PSEUDOCODE (USING FOR LOOP) DISPLAY “Please enter a number” INPUT num FOR counter =1 to num with increment=1 DISPLAY “I love to program” ENDFOR FLOWCHART START “Please enter a number” num Counterß 1 Counter ß counter+1 ? Counter<=num “I love to program” TRUE FALSE END C code #include <iostream.h> int main() { int counter; int num; cout<<"Please enter a number"; cin>>num; for(counter=1;counter<=num;counter=counter+1) { cout<<"I love to program\n"; } } 2- Write a program that asks the user to enter the beginning of a sequence (lower limit) and end of a sequence (upper limit). Print square of all numbers between these two limits. Please enter the lower limit: 4 Please enter the upper limit: 8 16 25 36 49 64 Pseudocode (using while loop) DISPLAY “Lower Limit” INPUT lower DISPLAY “Upper Limit” INPUT upper counter ß lowerlimit WHILE counter<=upperlimit DISPLAY counter*counter counter ß counter+1 ENDWHILE FLOWCHART START Please enter the lower limit lowerlimit Please enter the upper limit upperlimit counter ß lowerlimit ? counter<=upperlimit TRUE Counter*counter counter ß counter+1 FALSE END C code #include <iostream.h> int main() { int lower,upper,counter; cout<<"Please enter the lower limit "; cin>>lower; cout<<"Please enter the upper limit"; cin>>upper; counter=lower; while(counter<=upper) { cout<<counter*counter<<endl; counter=counter+1; } } 3- Write a program that asks the user to enter two numbers: the lower and upper limits of a sequence. Compute the summation, average, minimum and maximum of all numbers between these two limits using a loop. Please enter the lower limit: 4 Please enter the lower limit: 8 Summation is 30 Average is 7.5 Minimum is 4 Maximum is 8 Pseudocode DISPLAY “Please enter the lower limit” INPUT lower DISPLAY “Please enter the upperlimit” INPUT upper sum ß 0 counterß lower WHILE counter<=upper Sumßsum+counter ENDWHILE avgßsum/(upper-lower) DISPLAY ”Sum”,sum DISPLAY”Avg”, avg DISPLAY “Max”, upper DISPLAY “Min” ,lower C code #include <iostream.h> int main() { int lower,upper,counter; int sum; float avg; cout<<"Please enter the lower limit "; cin>>lower; cout<<"Please enter the upper limit"; cin>>upper; counter=lower; while(counter<=upper) { sum=sum+counter ; counter=counter+1; } avg=sum/(upper-lower); cout<<"The summation is "<<sum<<endl; cout<<"The average is "<<avg<<endl; cout<<"The maximum is "<<upper<<endl; cout<<"The minimum is "<<lower<<endl; } 4- Write a program that asks the user to enter 10 numbers and count the total number of these numbers that are greater than 50. Please enter number 1 :23 Please enter number 2 :15 Please enter number 3 : -15849 Please enter number 4 : 1 Please enter number 5 : 88 Please enter number 6 : 99 Please enter number 7 :71 Please enter number 8 : 45 Please enter number 9 : 9 Please enter number 10 :79 You have typed 4 numbers that are greater than 50. Pseudocode (UISNG WHILE) counter ß 1 large_counter ß 0 WHILE counter<=10 DISPLAY “Please enter number “, counter INPUT num IF num>50 large_counterß large_counter+1 ENDIF ENDWHILE DISPLAY “You have entered”, large_counter,”numbers that are greater than 50” Flowchart START counter ß 1 large_counter ß 0 ? counter<=10 TRUE “Please enter a number” num ? Num>=50 TRUE large_counter ß large_counter+1 FALSE counter ß counter+1 FALSE END C code #include <iostream.h> int main() { int counter,large_counter,num; counter=1; large_counter=0; while(counter<=10) { cout<<"Please enter number "<<counter<<":"; cin>>num; if(num>=50) { large_counter=large_counter+1; } counter=counter+1; } cout<<"You have entered "<<large_counter<<" numbers that are greater than 50."; }