Excluding Numbers divisible by 2 or by 3 – CSKC
Sign In

Search

Solved Problem! #CPP#2094    siteicon   siteicon  

Problem Statement - Excluding Numbers divisible by 2 or by 3

Write the output of the following program. Assume that required header files are included.

int main()
{
  for(int i=1;i<=30;++i)
  {
    if(i%2==0 || i%3==0) continue;
    else cout<<i<<" ";
  }
  return 0 ;
}

TC++ #2094

 

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Solution


Notes

for (int i=1;i<=30;++i) //This loop will work from 1 to 30 (both inclusive)
if(i%2==0 || i%3==0) continue;
This line will eliminate numbers which are either divisible by 2 or by 3, for e.g. numbers like 2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 26, 27, 28 and 30 are eliminated as the loop will continue with the increment of index again.
else cout<<i<<” “;
This will print the numbers which have not been eliminated.


Share

Created: 1-Jan-2018 22:45 | Updated: 22-Mar-2026 20:17|