• Write a program which finds the factorial of a number entered by the user. (check for all conditions) (Beginner).

void findfactor(const unsigned int number)
{
    unsigned int n = number;
    printf("The factors ot the input number are:\n");
    
    if (0 == n) {
        printf("%d\n", n);
        
        return;
    }
    for(unsigned int i = 1; i < n / i + 1; i++) {
        if (n % i == 0 ) {
            printf("%d\n", i);
            printf("%d\n", n / i);
        }
    }
}

You can see, that I don't store the factors. Acturally, I don't

know how to store the factors. The number of the factors are

unknown. I can't use fixed size array to store. What about list, stack

or dynamic array? Yeah, they could solve this problem. But if you

are in an interview, and you are not allowed to use these

encapsulated data structure, you may implement these at that time?

And it's a question for beginner, maybe I should use some beginner's

way.

  • Create a program which generates fibonacci series till a number 'n' where 'n' is entered by the user. For eg. if the user enters 10 then the output would be: 1 1 2 3 5 8 (beginner)

void generate(int a, int b)
{
    int total = a + b;
    if (total < n) { //n is the number inputed by user
        printf("total = %d\n", total);
        generate(b, total);
    }
}

Storing the data is a question. And if the question is "Create a

program which begin to generate fibonacci series untill a number 'n'

is entered by the user. Then output the series below n.", is it still for

beginner ?

:( Some basic questions still make me feel uneasy, the way is long

long...

>And it's a question for beginner, maybe I should use some beginner's way.
Maybe you should read the question more carefully. ;) The problem asks you to calculate a factorial (that is, N!), not the factors of a given number. You're trying to solve the wrong problem.

>Storing the data is a question.
Why?

>is it still for beginner ?
Yes, the Fibonacci series is very easy to calculate.

>And it's a question for beginner, maybe I should use some beginner's way.

Maybe you should read the question more carefully. ;) The problem asks you to calculate a factorial (that is, N!), not the factors of a given number. You're trying to solve the wrong problem.

:) Sorry, I misunderstood the question.

>Storing the data is a question.

Why?

Because I think I don't know the amount of the data, so I have to

use some dynamic array to store the data. Right?

>is it still for beginner ?
Yes, the Fibonacci series is very easy to calculate.

But I think it turns to be a multithreads question.

A thread generates Fibonacci serials.

A thread listen to the input.

And the two threads have to communicate in some way.

But I think it turns to be a multithreads question.

Threads are not for beginners. And a Fibonacci series is easy to calculate with a simple loop.

>Because I think I don't know the amount of the data, so I have to
>use some dynamic array to store the data. Right?
Wrong. Both factorials and Fibonacci numbers are a linear series. You can calculate them with a trivial loop. You're making things ridiculously complicated.

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.