Need help with a beginner program in X

Joined
Nov 19, 2021
Messages
2
Reaction score
0
Hello, I'm new to programming so I have a question about a program I'm trying to make in C. I'm supposed to make a program that prints 1) the number of numbers from 0 to MAXNUM that satisfy condition A, 2) the number of numbers from the same space that satisfy condition B and also 3) print all numbers that satisfy both conditions
I was able to do 1) and 2) but have no idea how to approach the 3d question and if anyone has any suggestions or can help me even by the smallest hint for how to approach this I'd really appreciate it!
 
Joined
Nov 19, 2021
Messages
2
Reaction score
0
i don't know if it makes sense but this was what I was asked to do and now i need to find how many and also print all the numbers that are both one-child and abundant

C:
#include <stdio.h>
#define MINNUM 0
#define MAXNUM 1000

int main()
{
        int n, j, i, tempn, ndigits, subseq, pow10, count, oc, divisor, sum, count2;
        count2 = 0;
        oc = 0;
        for (n = 1 ; n <= MAXNUM ; n++){
                sum = 1 ;
                for (divisor=2; divisor * divisor < n; divisor++) {
                        if(n%divisor==0){
                                sum += divisor + n / divisor;
                        }
                }
                if (divisor * divisor == n) {
                        sum += divisor;
                }
                if (sum >  n) {
                        count2++;
                }
        }
        for (n = MINNUM ; n <= MAXNUM ; n++){

                tempn = n;
                ndigits = 0;
                while (tempn != 0){
                        ndigits++;
                        tempn /= 10;
                }
                pow10 = 1;
                count = 0;
                for (j=1; j<= ndigits; j++) {
                        pow10 *= 10;
                        tempn = n;
                        for (i=1; i<=ndigits -j+1; i++){
                                subseq = tempn % pow10;
                                if (subseq % ndigits == 0) {
                                        count++;
                                        if (count == 1) {oc++; }
                                        if (count == 2) {oc--; }
                                }
                                tempn /= 10;
                        }
                }
        }
        printf("Found %d abundant numbers\nFound %d only-child numbers \n",count2, oc);
}
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
Sorry that I'm super late to the party! If this is still relevant... First, I'd split the two conditions into their own functions that just take one number to check. Next, I'd check them both in one loop. Then, it's easy to find the numbers that satisfy both conditions. Below, I've just printed out all of the numbers first, but you may want to create an integer array to store the numbers, then print them after the totals.

C:
#include <stdbool.h>
#include <stdio.h>

#define MINNUM 0
#define MAXNUM 1000

bool condition_a(int n)
{
        int sum = 1;
        int divisor = 0;
        for (divisor = 2; divisor * divisor < n; divisor++) {
                if (n % divisor == 0) {
                        sum += divisor + n / divisor;
                }
        }
        if (divisor * divisor == n) {
                sum += divisor;
        }
        return sum > n;
}
bool condition_b(int n)
{
        int tempn = n;
        int ndigits = 0;
        while (tempn != 0) {
                ndigits++;
                tempn /= 10;
        }
        int pow10 = 1;
        int count = 0;
        int oc = 0;
        for (int j = 1; j <= ndigits; j++) {
                pow10 *= 10;
                tempn = n;
                for (int i = 1; i <= ndigits - j + 1; i++) {
                        int subseq = tempn % pow10;
                        if (subseq % ndigits == 0) {
                                count++;
                                if (count == 1) {
                                        oc++;
                                }
                                if (count == 2) {
                                        oc--;
                                }
                        }
                        tempn /= 10;
                }
        }
        return oc != 0;
}
int main()
{
        int a_count = 0;
        int b_count = 0;
        for (int n = 1; n <= MAXNUM; n++) {
                bool a = condition_a(n);
                bool b = condition_b(n);
                if (a) {
                        a_count++;
                }
                if (b) {
                        b_count++;
                }
                if (a && b){
                        printf("%d fulfills both conditions\n", n);
                }
        }
        printf("Found %d abundant numbers\nFound %d only-child numbers \n", a_count, b_count);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top