prime numbering

This is a discussion on prime numbering within the C Programming forums, part of the General Programming Boards category; i am trying to make a program that checks if the number is a prime number: Code: #include <stdio.h> int ...

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

    prime numbering

    i am trying to make a program that checks if the number is a prime number:
    Code:
    #include <stdio.h>
    
    int prime( int a )
    
    int main(){
    	int b;
    	printf("type in the  number:")
    	scanf("%d",&b);
    	if (prime( b )==b){printf("of course");}
    	else {printf("nope=no hope");}
    	getchar();
    	return 0;}
    
    int prime( int a ){
    	int x;
    	for(x=2;x<a;x++){
    		if(!(a%x==0)){return a;}}}
    but when i run it after typing in the number it quits
    pleasse help!!!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    what your prime function should return if the number is prime and if it is not?

    And where is the second return?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    sorry but it really hard for me to read and consequently understand your code

    please see this.
    C Coding Standards

    Code:
    int testprime(int no)
    {
        int i;
        for(i=2;i<no;i++)
        {
            if((no % i) == 0)
            {
                return 0;            
            }
        }
        return 1;
    }
    so if 1 is returned then number is prime.
    Last edited by creeping death; 03-26-2009 at 11:35 AM.

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. prime number program with function
    By mackieinva in forum C Programming
    Replies: 17
    Last Post: 09-20-2007, 08:36 AM
  3. prime numbers, counters, help!
    By vege^ in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2003, 03:32 PM
  4. Prime Wonder
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-07-2002, 11:49 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 04:56 PM

Tags for this Thread


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