Write a program in C++ that prints the numbers from 1 to 100.?

Write a program in C++ that prints the numbers from 1 to 100. But for multiples of three

print “Knock” instead of the number and for multiples of five print “Out”. For numbers

which are multiples of both three and five print “KnockOut”.

1. Your program must contain at least one function used in a non-trivial way

2. The output should be neatly formatted

Next, allow the player to repeatedly input a number from 1 to 100 and the output is to be the

number, Knock, Out, or KnockOut according to the rules above. Keep track of how many

times the word KnockOut is printed and report the result when the player wants to quit the

game.

Your program should print the table of results and then repeat the KnockOut game until the

player is ready to quit.

3 Answers

Relevance
  • 9 years ago
    Favorite Answer

    include <cstdlib>

    #include <iostream>

    using namespace std;

    void wordCount();

    int main(int argc, char *argv[])

    {

    int num;

    char answer;

    do

    {

    cout << "Please enter a number between 1 and 100: ";

    cin >> num;

    if (num % 3 == 0 && num % 5 != 0)

    {

    cout << "Knock " << endl;

    }

    else if (num % 5 == 0 && num % 3 != 0)

    {

    cout << "Out " << endl;

    }

    else if (num % 3 == 0 && num % 5 == 0)

    {

    cout << "KnockOut " << endl;

    }

    else

    {

    cout << num << endl;

    }

    cout << "Would you like to play again? y or n: ";

    cin >> answer;

    cout << endl;

    }

    while (num >= 1 && num <= 100 && answer == 'y');

    system("PAUSE");

    return EXIT_SUCCESS;

    }

    void wordCount()

    {

    }

    This is a source file code that works it just doesn't keep track of how many times the word KnockOut is outputted or report and print the results. I was not able to come up with the coding for the function that counts how many times KnockOut is outputted but I did include the wordCount function in the source file code so you can try and figure that out.

    *note* I will continue working on the coding for the wordCount function I included in this source file code. If I am able to come with the coding for it before this question issubmitted into voting I will add to my answer. If I don't get it done before then I can e-mail it to you if you still need it.

    Source(s): Personal experience in C++ Personal experience with this C++ program
  • Anonymous
    5 years ago

    Shalom, This should be an easy program to write, in any language. What part are you having problems with? Here's your program, in BASIC. It requires one FOR LOOP and one IF statement: FOR x = 1 TO 100 .....IF x MOD 3 = 0 AND x MOD 5 = 0 THEN ..........PRINT "FitBit, "; .....ELSEIF x MOD 3 = 0 THEN ..........PRINT "Fit, "; .....ELSEIF x MOD 5 = 0 THEN ..........PRINT "Bit, "; .....ELSE ..........PRINT TRIM$(STR$(x));", "; .....END IF NEXT x Ignore the many periods. There are just there to indent the lines and make the code more readable, since stupid Yahoo automatically left justifies everything. I added, the commas to the print statement to make the final output a little more readable. Program Output: 1, 2, Fit, 4, Bit, Fit, 7, 8, Fit, Bit, 11, Fit, 13, 14, FitBit, 16, 17, Fit, 19, Bit, Fit, 22, 23, Fit, Bit, 26, Fit, 28, 29, FitBit, 31, 32, Fit, 34, Bit, Fit, 37, 38, Fit, Bit, 41, Fit, 43, 44, FitBit, 46, 47, Fit, 49, Bit, Fit, 52, 53, Fit, Bit, 56, Fit, 58, 59, FitBit, 61, 62, Fit, 64, Bit, Fit, 67, 68, Fit, Bit, 71, Fit, 73, 74, FitBit, 76, 77, Fit, 79, Bit, Fit, 82, 83, Fit, Bit, 86, Fit, 88, 89, FitBit, 91, 92, Fit, 94, Bit, Fit, 97, 98, Fit, Bit,

  • Phreak
    Lv 5
    9 years ago

    Try something along the lines as the following...

    #include <stdlib.h>

    #include <stdio.h>

    int num(int i)

    {

    if(!(i%3) || !(i%5))

    {

    if(!(i%3))printf("Knock");

    if(!(i%5))printf("Out");

    }

    else

    printf("%d",i);

    }

    int main(void)

    {

    int i, t = 0, q;

    for(i=1;i<=100;i++)

    {

    num(i);

    if(i!=100) printf(", ");

    }

    printf("\n\n");

    do{

    printf("Num? ");

    if(!(scanf("%d", &q)))

    q = 'y';

    if(q!='y')

    {

    if(!(q%15))t++;

    num(q);

    printf("\n\nQuit? y/n");

    q = scanf("%c ",&q);

    }

    printf("q: %c",q);

    }while(q=='n');

    }

    --- this is ***not a finished solution***, but should point you in the right general direction.

Still have questions? Get your answers by asking now.