So, my friend's program is supposed to find the Multiplicative of Number three then print them out with a comma between the number. And not for the last one.
int a;
int b;
cout<< "First Number = ";
cin>>a;
cout<< "Last Number = ";
cin>>b;
if(a<=b)
{
for(a;a<=b;a++)
{
if(a%3 == 0 && a%2 != 0)
{
cout<<a;
}
if(a<b && a%3==0 && a%2 != 0)
{
cout<< " , ";
}
else if(a==b)
{
cout<< ".";
}
}
}
And after i input
a = 1
b = 20
this is what i expected
3 , 9 , 15.
and this is what i actually get
3 , 9 , 15 , .
I would do something like this:
char const* sep = ""; // item separator (nothing to begin with)
for(a;a<=b;a++)
{
if(a%3 == 0 && a%2 != 0)
{
cout << sep << a; // output the separator before the next value
sep = ", "; // now change it to a comma
}
}
cout << "."; // finish with a full stop
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.