print a list of integers from 1 to N that are both divisible by 2 and 3.and after producing the list, count the numbers of integers found.

Dani AI

Generated

Numbers divisible by both 2 and 3 are exactly the multiples of 6. That means you don't need to test every number with two modulus operations — you can either (A) list multiples of 6 by stepping by 6, or (B) compute the count directly as floor(N / 6). Both are simpler and less error-prone than checking i % 2 == 0 && i % 3 == 0 for every i.

A concise, robust approach: validate the input, then either iterate i = 6; i <= N; i += 6 to print every match, or compute count = N / 6 (integer division) if you only need the quantity. Use a 64-bit integer for large N and handle N < 6 as a no-results case.

#include <iostream>

int main() {
    long long N;
    if (!(std::cin >> N)) return 0;
    if (N < 6) { std::cout << "Count: 0\n"; return 0; }

    long long count = 0;
    for (long long i = 6; i <= N; i += 6) {
        std::cout << i << '\n';
        ++count;
    }
    std::cout << "Count: " << count << '\n';
}

Practical notes: if you prefer the modulus method, be careful to compare to zero (e.g., i % 3 == 0), initialize your counter before use, and advance the loop variable each iteration — mistakes along those lines were the main issues in the earlier posts. correctly pointed out the variable mix-up, and 's suggestion to loop is sound; using the multiple-of-6 trick keeps the loop simple and fast.

Recommended Answers

All 10 Replies

You have to try first. Forum rules =)

But to get you started, you'll want to use a for loop. Inside your for loop you will do some math to figure if your number is divisible by either 2 or 3, and then have a counter variable for each number that passes the requirments in that iteration of the loop.

I just basically told you exactly what to do, so if you cant figure it out, ask your instructor for more help.

LOOP num from 1 to N
if(num%2==0 && num%3)
    {
     print num
     counter++
     }
end loop

I hope it will help you
Best Of Luck

how about this...

#include <iostream>

using namespace std;

int main()
{
    int n;
    int x = 1;
     cout << "enter the value for n : " << endl;
     cin >> n;

     if(x <= n)
     {
             if(n%2==0 && n%3)
             {
                       cout << n << endl;
                       x++;
             }
     }


     return 0;
     }
#include <iostream>

using namespace std;

int main()
{
    int n;
    int x = 1;
     cout << "enter the value for n : " << endl;
     cin >> n;
     
     if(x <= n)
     {
             if(n%2==0 && n%3)
             {
                       cout << n << endl;
                       x++;
             }
     }
                       
 
     return 0;
     }

Try to understand what happen in ur code

#include <iostream>

using namespace std;

int main()
{
    int n;
    int x = 1;
     cout << "enter the value for n : " << endl;
     cin >> n; // n is a specified number of input 
     
     if(x <= n)
     {
             if(n%2==0 && n%3) // u always check n logical error, it must be x
             {
                       cout << n << endl; // same problem here also x in place of n
                       x++;
             }
     }
                       
 
     return 0;
     }

I hope u understand
Best Of Luck

Remember you also need to loop the code, what your doing now is checking the number 1, once and then the program ends.

while(x <= n)
{
    if(x % 2 == 0 && x % 3 == 0)
       {
            cout << x << endl;
            count++;
       }

    x++;
}

This is basically what your looking for.

Sorry i don't understand what u try to say

while(x <= n) loop up to N
{
    if(x % 2 == 0 && x % 3 == 0) // it will check ur x (n >= x >= 1) if x is 
       {    // divisible by both 2 and 3 then it will print and increment the counter
            cout << x << endl;
            count++;
       }

    x++; //increase the value of by 1
}

so what that count for count++ which mean count = count + 1 am i right or wrong?? so in that case count i should declare the count as int count = 0??

YES, U Have declare the count as

int count=0;

Sorry i don't understand what u try to say

while(x <= n) loop up to N
{
    if(x % 2 == 0 && x % 3 == 0) // it will check ur x (n >= x >= 1) if x is 
       {    // divisible by both 2 and 3 then it will print and increment the counter
            cout << x << endl;
            count++;
       }

    x++; //increase the value of by 1
}

lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.