I can't get any compilation results.

Asked 1 years ago, Updated 1 years ago, 210 views

I would like to display, for example, a set of two sets of natural numbers that are given n or less and whose sum of divisors excluding myself is equal to each other, but nothing appears in the execution result.

Expected results:

$./a.out
Please enter a natural number of n: 5000
(220,284) → * The sum of the divisors excluding 220 is 284, whereas the sum of the divisors excluding 284 is 220*
(1184,1210)
(2620,2924)

Current state results:

$./a.out
Enter an integer n: 5000

In the program I created, the function sum was output with the sum of divisors excluding the natural number a, so there was no problem when I ran the compilation (of course the main function when checking the sum function is different from above).

n If there is a natural number equal to the sum of divisors excluding itself and their natural numbers, the two numbers should be programmed to be output in pairs, and if the sum of divisors excluding i itself and an and i are equal,

 for (i=1; i<n;i++)
    if(sum(i)==ans&sum(ans)==i)

I wrote that, but I'm asking because I can't run the program well and I can't think of what to do.
The sum function is correct, so where and how should I modify the yuai function?I look forward to hearing from you.

Current source code:

#include<stdio.h>

/* a function that calculates the sum of divisors excluding itself */
int sum(inta)
{
   inti,su;
   su = 0;
   for(i=1;i<a;i=i+1){
    if(a%i==0){
      su=su+i;
     }
   }
    return su;
}

void print_yuai(intn)
{
    inti,ans;
    an = 0;
    for (i=1; i<n;i++)
        if(sum(i)==ans&sum(ans)==i){
            printf("(%d,%d)\n", i,ans);
       }
}

int main (void)
{
    intx;

    printf("Enter an integer n:");
    scanf("%d", & x);
    print_yuai(x);

    return 0;
}

c

2022-09-30 21:51

1 Answers

First, let's learn how to use debugger.Question → While waiting for a response, you will see the error.

Tip 1: What is the value of sum at the start?
Tip 2: Does the value of sum change in the loop?

A certain super-large bulletin board site has a scroll area so that you can't see the answer right away.

Answer: The first value of sum is 0.And it has never been updated.Furthermore, since the sum of divisors is never 0, the first sum(i)==ans will never be established (that is, no results)

Do you want the result of the correction? First of all, let's try to correct it by ourselves.If you rely too much on others, you won't be able to do it.


2022-09-30 21:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.