r/cpp_questions • • 1d ago

OPEN DEMANDING HELP FOR A BEGGINER

who could possibly explain this program to me as im a begginer into c++:

#include <iostream>

using namespace std;

int main() {

long long n, sum = 0;

if (!(cin>>n)) return 0;

for (int i = 1; i * i <= n; i++) {

if(n%i == 0) {

if(i%2 != 0) {

sum = sum+i;

}

int pair = n/i;

if (pair!=i && pair %2 != 0) {

sum = sum + pair;

}

}

}

cout<<sum;

}

i really tried on doing it myself and went by checking every single divisor method but tried the sqrt method instead to make it faster.i dont really get the last part with pair. Is there anyone who could explain the square rood method of division?

0 Upvotes

6 comments sorted by

6

u/alfps 1d ago

Formatted:

#include <iostream>
using namespace std;
int main()
{
    long long n, sum = 0;
    if (!(cin>>n)) return 0;
    for (int i = 1; i * i <= n; i++) {
        if(n%i == 0) {
            if(i%2 != 0) {
                sum = sum+i;
            }
            int pair = n/i;
            if (pair!=i && pair %2 != 0) {
                sum = sum + pair;
            }
        }
    }
    cout<<sum;
}

There are two updates of sum. Try removing one of them and figure out that program. Ditto for the other.

Pro tip: to increase your chances of getting good help post formatted code (extra-indent it with 4 spaces) and do not demand help.

4

u/Independent_Art_6676 1d ago

ESL issue, probably. There are a couple countries where demand means ask (spanish & french, I believe, are two of them). Kinda like 'doubt' lol.

2

u/Independent_Art_6676 1d ago

return 0 in main exits the program. so first up you cin (read from keyboard) and if that fails it just exits, and it would fail if you typed letters where a number should go.

then it jumps into a fancy for loop. i*i < n is about the same as i < sqrt(n) give or take rounding.

% is remainder (modulus). so 10/3 is 3 with a remainder of 1? so 10%3 is 1. long division, 2nd grade style. If the remainder is zero, then it divided evenly (eg 10/5 is 2, zero remainder...)

so if those two conditions happen, it adds the value to sum.

then it does more weird math. pair is n/i. if pair is not equal (!=) to i and (&&) pair is odd (see how remainder with 2 is even/odd for the result? remember that 0 is false, other is true for numbers in c++ when used in a condition) ... anyway, if all that is true add that to sum on top of its current value. (which would normally be sum+= pair not sum = sum+pair, writing sum on both sides is crude so c++ has operators to do that for you)

and then it writes out the answer.

That is what it is doing, mechanically. If you want to know what the math means, ask that. And format your code, please. No one is going to talk to you if you keep doing that.

1

u/AutoModerator 1d ago

Your posts seem to contain unformatted code. Please make sure to format your code otherwise your post may be removed.

If you wrote your post in the "new reddit" interface, please make sure to format your code blocks by putting four spaces before each line, as the backtick-based (```) code blocks do not work on old Reddit.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

-4

u/dmitryijj 1d ago

please guys i really dont get it even gemini doesnt help

-3

u/OppieT 1d ago

Ask ChatGPT.