Factorial and Prime Problem using functions

This is a discussion on Factorial and Prime Problem using functions within the C++ Programming forums, part of the General Programming Boards category; Can any correct this programming please need help on this 1..... cant really get this one....thank you Code: #include<stdio.h> int ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Exclamation Factorial and Prime Problem using functions

    Can any correct this programming please need help on this 1..... cant really get this one....thank you




    Code:
    #include<stdio.h>
    int Fact(int);
    int Prime(int);
    
    int main()
    {
    printf("Factorial %d\n", Fact(3));
    printf("Prime %d", Prime(3));
    
    Return 0;
    }
    int Fact(int n)
    {
         int i;
         for(i=1;i<=n;i++)
    {
         i*n;
    }
    
    return i;
    }
    int Prime(int n)
    {
         int i;
         for(i=1;i<=n;i++)
    {
         n/=n;
    }
    return i;
    }
    }
    it made me go nosebleed over my shirt of thinking...btw im still new in Programming

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    int Fact(int n) {
         int i;
         for(i=1;i<=n;i++) {
             i*n; // this doesn't do anything just multiplys i by n and discards the result
        }
        return i;  
    }
    try like this
    Code:
    int Fact(int n) {
         int i; ret = 1;
         for(i=1;i<=n;i++) {
             ret*=i; 
        }
        return ret;  
    }
    Kurt

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    hmmm does it give the results of 6 cause the factorial of 3 is like 6:Answer"3x2x1" but output should be 6..... but do you have clue on the Prime part it took the hell out of me T.T

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The prime part doesn't make any sense (you just keep dividing the number by itself).

    Instead you should perform trial divisions with values up to the number you are testing (seeing if number is divisible with any number less than it or not - using modulus).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime Factorization
    By jack_carver in forum C Programming
    Replies: 0
    Last Post: 07-02-2009, 08:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21