Guest User

Untitled

a guest
Dec 15th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // Tagove_SimplePuzzle
  4. //
  5. // Created by Raju Jangid on 20/08/16.
  6. //
  7.  
  8. /**
  9. Task: Design an efficient program that prints out, in reverse order, every multiple of 7 that is between 1 and 300. Extend the program to other multiples and number ranges. Write the program in any programming language of your choice.
  10. This program was written in C++ language and to compile on a BSDI BSD/OS with the g++ compiler. In order to compile it and get it ready for execution:
  11. How to run program?
  12.  
  13. Open command line interface
  14.  
  15. $ g++ -std=c++11 -o main.cpp
  16. $ ./main
  17. Enter values to test.
  18.  
  19. **/
  20.  
  21.  
  22.  
  23. #include <iostream>
  24.  
  25. using namespace std;
  26.  
  27. int main(int argc, const char * argv[]) {
  28.  
  29. int option = 1, count_extent = 0;
  30.  
  31.  
  32.  
  33. do {
  34. int number_range_from, number_range_to, divisor;
  35.  
  36.  
  37. try {
  38.  
  39.  
  40. if(count_extent > 0){
  41. cout << "\n\nEnter Option:\n1. Extend\n2. Exit\n";
  42. cin >> option;
  43. if (option == 2) {
  44. continue;
  45. }
  46. }
  47.  
  48. cout << "Enter number range from:\n";
  49. cin >> number_range_from;
  50.  
  51. if(number_range_from <= 0){
  52. throw invalid_argument("Number range from must be positive integer and greater than 0.\n");
  53. }
  54.  
  55. cout << "Enter number range to:\n";
  56. cin >> number_range_to;
  57.  
  58. if(number_range_to <= 0){
  59. throw invalid_argument("Number range to must be positive integer and greater than 0.\n");
  60. }
  61.  
  62. if(number_range_from > number_range_to){
  63. throw invalid_argument("Number range from value must be less than number range to.\n");
  64. }
  65.  
  66.  
  67.  
  68. cout << "Enter divisor:\n";
  69. cin >> divisor;
  70.  
  71. if(divisor <= 0){
  72. throw invalid_argument("Divisor must be positive integer and greater than 0.\n");
  73. }
  74.  
  75. if(divisor > number_range_to){
  76. throw invalid_argument("Divisor value must be less than number range.\n");
  77. }
  78.  
  79.  
  80. int tmp_nr = (number_range_to) % (divisor);
  81.  
  82. int i = number_range_to - tmp_nr;
  83.  
  84. cout << "\n\nEvery multiple of "<< divisor << " that is between 1 and "<< number_range_to << " in reverse order:\n\n";
  85.  
  86. while (i > 0) {
  87. cout << i << "\n";
  88.  
  89. i = i - divisor;
  90. }
  91.  
  92. count_extent++;
  93. } catch (exception& e) {
  94. cout << e.what();
  95. option = 2;
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. } while (option == 1);
  103.  
  104.  
  105.  
  106. return 0;
  107. }
Add Comment
Please, Sign In to add comment