Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Tagove_SimplePuzzle
- //
- // Created by Raju Jangid on 20/08/16.
- //
- /**
- 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.
- 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:
- How to run program?
- Open command line interface
- $ g++ -std=c++11 -o main.cpp
- $ ./main
- Enter values to test.
- **/
- #include <iostream>
- using namespace std;
- int main(int argc, const char * argv[]) {
- int option = 1, count_extent = 0;
- do {
- int number_range_from, number_range_to, divisor;
- try {
- if(count_extent > 0){
- cout << "\n\nEnter Option:\n1. Extend\n2. Exit\n";
- cin >> option;
- if (option == 2) {
- continue;
- }
- }
- cout << "Enter number range from:\n";
- cin >> number_range_from;
- if(number_range_from <= 0){
- throw invalid_argument("Number range from must be positive integer and greater than 0.\n");
- }
- cout << "Enter number range to:\n";
- cin >> number_range_to;
- if(number_range_to <= 0){
- throw invalid_argument("Number range to must be positive integer and greater than 0.\n");
- }
- if(number_range_from > number_range_to){
- throw invalid_argument("Number range from value must be less than number range to.\n");
- }
- cout << "Enter divisor:\n";
- cin >> divisor;
- if(divisor <= 0){
- throw invalid_argument("Divisor must be positive integer and greater than 0.\n");
- }
- if(divisor > number_range_to){
- throw invalid_argument("Divisor value must be less than number range.\n");
- }
- int tmp_nr = (number_range_to) % (divisor);
- int i = number_range_to - tmp_nr;
- cout << "\n\nEvery multiple of "<< divisor << " that is between 1 and "<< number_range_to << " in reverse order:\n\n";
- while (i > 0) {
- cout << i << "\n";
- i = i - divisor;
- }
- count_extent++;
- } catch (exception& e) {
- cout << e.what();
- option = 2;
- }
- } while (option == 1);
- return 0;
- }
Add Comment
Please, Sign In to add comment