简体   繁体   中英

I can't figure out the problem with my code not showing expected results

Write a program that gets two positive single digit integers from the user. Then print all the numbers from 1 to 1,000 that are divisible by both of those integers. Print ten numbers per line and separate them by a tab. ('\t')

I have done a code to print numbers from 1 - 1000 that are divisible by two numbers, x and y.

#include <iostream> using namespace std; int main() {

int x, y;
cout << "Enter two positives single digit integers: ";
cin >> x >> y;
    for (int i = 1; i <= 1000; i++) {
        if ( i % x == 0 && i % y == 0)
            cout << i << '\t';
            if (i % 10 == 9)
            cout << endl;
        else i++;    
    }
    return 0; }

I'm not getting any results that I need for my code. Expected results:

Enter two positive single digit integers: 5 6
30  60  90  120 150 180 210 240 270 300
330 360 390 420 450 480 510 540 570 600
630 660 690 720 750 780 810 840 870 900
930 960 990

The 'i%10 ==9' should be a 0 and not a 9 since you want that after a number divisible by 10 (meaning that there is no remainder) you end the line. Second iterating i in the for loop is wrong since this is what a for loop initially does. This is just some bad basics, try rereading c++ introduction books or watching more tutorials.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM