Question

Using C++ Write two `void` functions.  These functions each take two  integer parameters.  The functions accomplish the following tasks....

Using C++

Write two `void` functions.  These functions each take two  integer parameters.  The functions accomplish the following tasks.

The first function prints out the numbers from the first argument up to and including the number passed as the second argument counting by the first argument (i.e. if the arguments are 2 and 10,

the information below was given:

#include <iostream>

using namespace std;


// function definitions//


// main program

int main() {

  int firstNumber,secondNumber;

  char countType, doAgain;

  // we will do this until the user is done

  do {

    // collect the numbers from the user

    do {

      cout << "Enter a positive integer: ";

      cin >> firstNumber;

      if (firstNumber < 1) {

        cout << "Error! Invalid number." << endl;

      }

    } while (firstNumber < 1);

    do {

      cout << "Enter another positive integer: ";

      cin >> secondNumber;

      if (secondNumber < 1) {

        cout << "Error! Invalid number." << endl;

      }

    } while (secondNumber < 1);


    // ask for the type of sum

    do {

      cout << "What should I do with these two numbers?" << endl;

      cout << "  - (s)kip counting" << endl;

      cout << "  - (f)actor detection" << endl;

      cout << "Enter choice: ";

      cin >> countType;

      cout << endl;

      if (countType != 's' && countType != 'f') {

        cout << "Error! Invalid selection." << endl;

      }

    } while (countType != 's' && countType != 'f');

    // Make the function call

    switch (countType) {

      case 's':

        printSkipCount(firstNumber,secondNumber);

        break;

      case 'f':

        printFactor(firstNumber,secondNumber);

        break;

    }

    // should we do this again?

    cout << "Try Again? (y/n): ";

    cin >> doAgain;

  } while (doAgain == 'y');

   

  return 0;

   

}

it prints 2 4 6 8 10).

* The second function prints out a message stating if the first argument is a factor of the second argument.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PLEASE LOOK THE SCREENSHOT FOR BETTER UNDERSTANDING OF THE PROGRAM.

SOURCE CODE :

#include <iostream>

using namespace std;

// function definitions//

// printSkipCount FUNCTION

void printSkipCount(int firstNumber, int secondNumber)

{

    // looping from firstNumber to SecondNumber

    // were i is incremented by thr firstNumber in each iteration

    for(int i = firstNumber; i <=secondNumber; i += firstNumber)

    //printing the value of i

        cout << i << " ";

    cout << "\n\n";

}

// printFactor FUNCTION

void printFactor(int firstNumber, int secondNumber)

{

    // if the remainder when dividing the secondNumber by firstNumber

    // is zero then firstNumber is a factor of secondNumber

    if (secondNumber % firstNumber == 0)

    //printing the appropriate message

        cout << firstNumber << " is a factor of " << secondNumber;

    cout << "\n\n";

}


// main program

int main()

{

    int firstNumber, secondNumber;

    char countType, doAgain;

    // we will do this until the user is done

    do

    {

        // collect the numbers from the user

        do

        {

            cout << "Enter a positive integer: ";

            cin >> firstNumber;

            if (firstNumber < 1)

            {

                cout << "Error! Invalid number." << endl;

            }

        } while (firstNumber < 1);

        do

        {

            cout << "Enter another positive integer: ";

            cin >> secondNumber;

            if (secondNumber < 1)

            {

                cout << "Error! Invalid number." << endl;

            }

        } while (secondNumber < 1);

        // ask for the type of sum

        do

        {

            cout << "What should I do with these two numbers?" << endl;

            cout << "  - (s)kip counting" << endl;

            cout << "  - (f)actor detection" << endl;

            cout << "Enter choice: ";

            cin >> countType;

            cout << endl;

            if (countType != 's' && countType != 'f')

            {

                cout << "Error! Invalid selection." << endl;

            }

        } while (countType != 's' && countType != 'f');

        // Make the function call

        switch (countType)

        {

        case 's':

            printSkipCount(firstNumber, secondNumber);

            break;

        case 'f':

            printFactor(firstNumber, secondNumber);

            break;

        }

        // should we do this again?

        cout << "Try Again? (y/n): ";

        cin >> doAgain;

    } while (doAgain == 'y');

    return 0;

}

1 #include <iostream> using namespace std; 2 4 5 ~ m n N 000 6 7 // function definitions// // printSkipCount FUNCTION void pr

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 do { cout << Enter another positive integer: ; cin >> secondNumber

OUTPUT

C:\Users\Faheem\Documents\GitHub\HackerRank>cd c:\Users\Faheem\Documents\GitHub\HackerRank\factorchck\ && g++ skipcount.cpp

HOPE ANSWER THE QUESTION

COMMENT BELOW IF HAVING ANY DOUBTS

Add a comment
Know the answer?
Add Answer to:
Using C++ Write two `void` functions.  These functions each take two  integer parameters.  The functions accomplish the following tasks....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1...

    SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){    int num1,num2;    cout << "Enter two numbers "<< endl;    cout << "First :";    cin >> num1;    cout << "Second :";    cin >>num2;    int result=num1+num2;    cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result;   ...

  • #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int...

    #include <iostream> #include <conio.h> #include<limits> using namespace std; int main(){ char oparand, ch = 'Y'; int num1, num2, result; while(ch == 'Y'){ cout << "Enter first number: "; cin >> num1; while(1){//for handling invalid inputs if(cin.fail()){ cin.clear();//reseting the buffer cin.ignore(numeric_limits<streamsize>::max(),'\n');//empty the buffer cout<<"You have entered wrong input"<<endl; cout << "Enter first number: "; cin >> num1; } if(!cin.fail()) break; } cout << "Enter second number: "; cin >> num2; while(1){ if(cin.fail()){ cin.clear(); cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout<<"You have entered wrong input"<<endl; cout <<...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • This is for a C++ program: I'm almost done with this program, I just need to...

    This is for a C++ program: I'm almost done with this program, I just need to implement a bool function that will ask the user if they want to repeat the read_course function. I can't seem to get it right, the instructions suggest a do.. while loop in the main function. here is my code #include <iostream> #include <cstring> #include <cctype> using namespace std; const int SIZE = 100; void read_name(char first[], char last[]); void read_course(int & crn, char des[],...

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • c++ Write the following 2 functions and test them. Both of them need to be recursive...

    c++ Write the following 2 functions and test them. Both of them need to be recursive functions. int sum(int n); // recursive version to calculate the sum of 1 + 2 + ..... + n int str_length(char s[]; // Returns length of the string s[] and the null character, '0\', is not counted in the length). Example of program execution; Enter a positive integer: 10 (user input) The sum of 1+ 2+....+10 is: 55 Enter a sentence: Hello World! (user...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • CIS 22B Lab 3 Itty Bitty Airfreight (IBA) Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1...

    CIS 22B Lab 3 Itty Bitty Airfreight (IBA) Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Utilizing Lab 2.2 code Create your objects in the stack (not on the heap). Add a friend function, kilotopound, which will convert kilograms to pounds. Change your weight mutator to ask whether weight is input in kilograms or pounds. If it is kilograms, call the friend function kilotopound to convert it to pounds and return pounds.There are 2.2 pounds in one kilogram. Create an...

  • I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for.....

    I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for..while..do while,loops,pointer,address,continue,return,break. Create a C++ program which contain a function to ask user to enter user ID and password. The passwordmust contain at least 6 alphanumeric characters, 1 of the symbols !.@,#,$,%,^,&,* and 1 capital letter.The maximum allowable password is 20. Save the information. Test the program by entering the User ID and password. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT