I'm trying to make a program using do..while loop that asks user for two inputs. Then print from 1 to the first number the user entered or until the first multiple of the second number. However, print "Multiple of X" if the number is a multiple of the second number.
This is what I managed to do so far but It's not working how I expected it to lol. I would appreciate some guidance, thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int main() {
int val;
int mul;
int val2 = 1;
cout << " Enter Value: ";
cin >> val;
cout << " Enter Multiple: ";
cin >> mul;
do {
cout << val2;
val2++;
} while (val2 == val); {
cout << val;
}
}
#include <iostream>
int main()
{
std::cout << "enter two numbers: " ;
int first = 0 ;
int second = 0 ;
std::cin >> first >> second ;
// print "Multiple of X" if the first number is a multiple of the second number.
if( second != 0 && first%second == 0 ) std::cout << first << " is a multiple of " << second << '\n' ;
else // otherwise print from 1 to the first number the user entered
{
int n = 0 ;
do std::cout << ++n << '\n' ; while( n < first ) ; // using do..while loop
}
}
Sorry man, I mean like if the user inputs for example, first num: 18, second num 7,
It will stop once it finds a multiple of second num which is 7, so 14.
However, if the first num is 10 and second num is 6 it will print out 1 until 10 then ends
#include <iostream>
int main()
{
std::cout << "enter two numbers: " ;
int first = 0 ;
int second = 0 ;
std::cin >> first >> second ;
// print "Multiple of X" if the first number is a multiple of the second number.
if( second != 0 && first%second == 0 ) std::cout << first << " is a multiple of " << second << '\n' ;
else // otherwise print from 1 to the (first number the user entered or
// or second*2, whichever is lower)
{
int n = 0 ;
// print till maxval: either first or second*2, whichever is lower
constint maxval = second*2 > first ? first : second*2 ;
do std::cout << ++n << '\n' ; while( n < maxval ) ; // using do..while loop
}
}