Prime factor

This is a discussion on Prime factor within the C Programming forums, part of the General Programming Boards category; i am trying to make program that checks if the number is a prime or not: Code: int asal(int a,int ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    28

    Prime factor

    i am trying to make program that checks if the number is a prime or not:
    Code:
    int asal(int a,int b){
    	int x;
    	for(x=2;x<a;x++){
    		if(a%x!=0){return a;}
    		else {return b;}}}
    but when i do this asal(9,1)=>it returns 9 implying it a prime number
    i stared at the code for about 10 min.s couldnt find it so i said good people in c board could help me (i wish)

    note(In Turkish(asal)==In English(prime)(note(i am Turkish)))
    (i tried make it look like C programming

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Your loop never executes more than once -- in your if statement you return in either case.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Code:
    if(a%x!=0){return a;}
    You got your logic wrong. You want there to be a remainder in division.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by alperen1994 View Post
    but when i do this asal(9,1)=>it returns 9 implying it a prime number
    Of course. The first thing that happens is 9%2, which the answer is not zero so it returns a.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    28
    so how can i do it????
    (i am asking because i want learn new ways to make it)
    (i am in the computer olimpics this year,wish me luck)
    Last edited by alperen1994; 03-28-2009 at 11:55 AM.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by alperen1994 View Post
    (i am in the computer olimpics this year,wish me luck)
    You'll need it

    Probably you wanted == instead of !=, since if the number can be divided without a remainder (so modulus = 0), it cannot be prime.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    33
    first of all if you want it to be a program that interacts with the user you should add a "number" which is entered by the user..

    your for loop has a mistake, it should be ;(...;x<=(a-1);...)

    and a%x==0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 11:06 AM
  2. last prime factor
    By frango9000 in forum C Programming
    Replies: 7
    Last Post: 07-27-2006, 12:42 PM
  3. Replies: 3
    Last Post: 03-29-2005, 03:24 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Prime Factor Fxn
    By alpha in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2003, 10:44 AM

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