Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6673851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:40:36+00:00 2026-05-26T03:40:36+00:00

C++ question this time. I’m trying to store the product between two random numbers…

  • 0

C++ question this time.

I’m trying to store the product between two random numbers… It’s supposed to be asking what the product between two random numbers that generate based on srand(time(0)), and quitting after -1 is entered…

The following is my code:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

int multiplication()
{
    srand( time(0));
    int x = 0;

    while (x != -1)
    {
        int random_int;
        random_int = (rand()%10 * rand()%10);
        cout << "(Enter -1 to quit) \n";
        cout << "" << rand() % 10 << " multiplied by " << rand() % 10 <<"? \n";
        cin >> x;

        if(x == random_int)
        {
            cout << "you're right!" << endl;
        }
        else
        {
            cout << "you're wrong" << endl;
        }
    }
    return 0;
}

int main()
{
    multiplication();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:40:37+00:00Added an answer on May 26, 2026 at 3:40 am

    You shoud pay attention to operator precedence. Modulo operator % has the same precedence as multiplication *. Therefore when you write

    rand()%10 * rand()%10
    

    c++ will interpret that as

    ((rand()%10) * rand()) % 10
    

    in other words the last modulo is applied to the result of everything else.

    If you want to multiply two random numbers between 0 and 9 you should use instead

    (rand() % 10) * (rand() % 10)
    

    where the extra parenthesis ensure the correct computation sequence.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm re-asking this question but with a different framework this time. I have two
Easy question this time. I'm trying to test whether or not a string does
Hi Masters Of Web Development. I have a not that simple question this time.
Pretty simple question this time around. I have an application that communicates with another
A silly question, perhaps, but at this time of night, StackOverFlow is my only
This question was asked quite some time ago, and while it covers possible solutions
This question has been puzzling me for a long time now. I come from
This question is a result of a lunch-time conversation with a co-worker...I've read questions
This question has been bugging me for some time. I always picture launching my
This question has in the back of my mind for some time, sorry if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.