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!
 
Ad

Advertisements

Ad

Advertisements

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);
}
 

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

Similar Threads


Top