write a program that displays numbers from 1 to 100 like this
*if number is divisible by 3 then it displays aaa
*if number is divisible by 5 then it displays bbb
*if number is divisible by 3 and 5 then it displays ccc
*else display number
*each number must be displayed on single line
How can i do this in c++? so far I've written this but it doesn't work as expected
for(int i=1;i<=100;i++)
{
if(i%3==0)
cout<<"aaa"<<endl;
if(i%5==0)
cout<<"bbb"<<endl;
if(i%3==0 && i%5==0)
cout<<"ccc"<<endl;
else
{
cout<<i<<endl;
}
}
If you want to use some original approach then you can write the program the following way.:)
#include <iostream>
int main()
{
for ( int i = 1; i <= 100; i++ )
{
unsigned mask = ( i % 3 == 0 ) + ( ( i % 5 == 0 ) << 1 );
switch ( mask )
{
case 1:
std::cout << "aaa";
break;
case 2:
std::cout << "bbb";
break;
case 3:
std::cout << "ccc";
break;
default:
std::cout << i;
break;
}
std::cout << std::endl;
}
return 0;
}
For example if to use the range [1, 15] instead of [1,100] then the output will be
1
2
aaa
4
bbb
aaa
7
8
aaa
bbb
11
aaa
13
14
ccc
Or you can even introduce an enumeration for readability. For example
#include <iostream>
int main()
{
enum { NONE = 0, AAA = 1, BBB = 2, CCC = AAA + BBB };
for ( int i = 1; i <= 100; i++ )
{
// AAA + BBB
unsigned mask = ( i % 3 == 0 ) + ( ( i % 5 == 0 ) << 1 );
switch ( mask )
{
case AAA:
std::cout << "aaa";
break;
case BBB:
std::cout << "bbb";
break;
case CCC:
std::cout << "ccc";
break;
default: // NONE
std::cout << i;
break;
}
std::cout << std::endl;
}
return 0;
}
If it is a school assignment then I assure you that nobody will present the solution I showed. You will be the only who will present such a solution.:)
the second and third "if" should be "else if", otherwise they are disconnected if-structures, meaning the if statements are seen as separate bits of code that do not know about eachother - each if is evaluated every time. So also the last if is evaluated every time, meaning that the last "else" will be done more often than you expected and print the number every time
furthermore, the third if should be the first one, to avoid multiple letter prints (the aaa bbb ccc stuff)
I think this should do what you expect:
for(int i=1;i<=100;i++)
{
if(i%3==0 && i%5==0)
cout<<"ccc"<<endl;
else if(i%3==0)
cout<<"aaa"<<endl;
else if(i%5==0)
cout<<"bbb"<<endl;
else
{
cout<<i<<endl;
}
}
If there are many choices and we only choose one, we often write if-else
nested statements:
if(i%3==0 && i%5==0)
cout<<"ccc"<<endl;
else if(i%3==0)
cout<<"aaa"<<endl;
else if(i%5==0)
cout<<"bbb"<<endl;
else
cout<<i<<endl;
If you can't figure out what's else if
statement, that's combined with else
and if
:
if(i%3==0 && i%5==0)
{
cout<<"ccc"<<endl;
}
else
{
if(i%3==0)
{
cout<<"aaa"<<endl;
}
else
{
if(i%5==0)
{
cout<<"bbb"<<endl;
}
else
{
cout<<i<<endl;
}
}
}
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.