Hi i am writing a program to compute the smallest number divisible by each of the numbers 1 to 20. This is what i have so far but it does not cout anything. it just gives me a blank page. Could anyone please help

#include<iostream.h>
#include<math.h>



bool div(ull y)
{
    for(int x=1;x<=20;x++)
    if(y%x!=0)
    return false;
    return true;
}
int main()
{
    for(int x=1;;x++)
    if(div(x)){
    cout << x << endl;
    system("pause");
    }
}

Dani AI

Generated

The immediate problems in the posted attempts were mostly syntax and style: the token ull is not a built-in type (use unsigned long long or std::uint64_t), the function header in the follow-up was malformed (an extra int x; in the declaration causes the syntax errors), and the old header <iostream.h> should be replaced by the standard <iostream>. 's suggestion to declare loop variables was not wrong, but it did not address those root causes. 's higher-level hint is the key: a brute-force test of every integer is unnecessary.

A reliable approach is to compute the least common multiple (LCM) of 1..20. Two simple options:

  • Prime-factor method: for each prime p <= 20 take the largest p^k <= 20 and multiply them (2^4, 3^2, 5, 7, 11, 13, 17, 19), giving 232792560 (classic Project Euler problem 5).
  • Iterative LCM with gcd: compute lcm(a,b) = a / gcd(a,b) * b and fold over 2..20. This is compact, fast, and easy to code.

A small, correct C++ pattern (uses a portable gcd implementation and 64-bit integers):

#include <iostream>
#include <cstdint>

std::uint64_t gcd(std::uint64_t a, std::uint64_t b) {
    while (b) { std::uint64_t t = b; b = a % b; a = t; }
    return a;
}

std::uint64_t lcm(std::uint64_t a, std::uint64_t b) {
    return a / gcd(a, b) * b;
}

int main() {
    std::uint64_t r = 1;
    for (std::uint64_t i = 2; i <= 20; ++i) r = lcm(r, i);
    std::cout << r << '\n';
}

Notes: use standard headers (<iostream>, <cstdint>), avoid system("pause"), and pick a 64-bit type if the range might require larger LCMs (LCM(1..25) exceeds 32 bits). For reference on gcd in the standard library see std::gcd and the classic problem at Project Euler problem 5.

Recommended Answers

All 4 Replies

You need to declare x as an int like this: int x; or int x =(INPUT NUMBER HERE)
That should work. If not tell me.

Id also try: return 0;

Thanks i tried that and this is what it gave me.

#include<iostream.h>
#include<math.h>



bool div(ull x) // syntax error here and ull was not declared
int x;
{ // syntax error before '{'token
    for(int i=1;i<=20;i++) // syntax error before '<=' token and before '++" token
    if(x%i!=0)
    return false;
    return true;
}
int main()
{
    for(int i=1;;i++)
    if(div(i)){
    cout << i << endl;
    }
    system("pause");

}

This kind of question is designed to show you that a loop for all the numbers N=1 to N=LOTS [because the answer is big!], is not the way! Even if you got your div function you work, you could easily be spending a long time waiting for it to work.

[Note to teachers: don't set that as 1-20 since that is far too quick on todays hardware but 1-25 would be a better set, just doable in typical long int sizes [64bit] but not in int, and takes sufficient long but not stupidly long time that someone doing a brute force approach will not be able to do it]

To give you a large hint, a very similar question on a 1970's maths paper I saw as a kid. [Sorry I am old!!!]. Note that was before electronic calculators!! You need to think about primes and prime factors. For example, since the solution must divide by 7, and as 7 is prime, you only need to consider multiples of 7.

From that you should be able to figure out how to do it.

p.s. I have no idea what ull means, just delete it. You seem to have grabbed a C function from somewhere and dropped it into your C++ program.

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.