Hello new to to the forum and also to programmig.

This is a discussion on Hello new to to the forum and also to programmig. within the C Programming forums, part of the General Programming Boards category; Originally Posted by stahta01 Write code to find out if a number is prime. Until you can do that you ...

  1. #16
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Quote Originally Posted by stahta01 View Post
    Write code to find out if a number is prime.
    Until you can do that you do not have a chance at doing this assignment.
    After writing a function to determine if a number is prime; than take this code and create a new function (possibly called factor) to do this assignment.

    Tim S.

    Here is a code to see if a number is prime

    Code:
    #include <stdio.h>
    
    int main() {
    
    
        int N, a, b;
    
    
        printf("What number do you want to test?\n");
        scanf("%d",&N);
    
    
        for(a=2; a<N; a++) {
            if(N % a == 0) {
                printf("%d is divisble by %d.\n", N, a);
                break;
            }
        }
    
    
        if(a == N) //the loop ran completely and never 'broke'
            printf("%d is a prime!\n",N);
    
    
        return 0;

  2. #17
    Registered User
    Join Date
    May 2009
    Posts
    2,478
    Take you have not yet learned to write functions.
    Is this correct?

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  3. #18
    Registered User
    Join Date
    May 2009
    Posts
    2,478
    I did a untested change to your code; you might be able to figure out what else to change.

    Code:
    #include <stdio.h>
    
    int main() {
    
    
        int N, a, b=1;
    
    
        printf("What number do you want to test?\n");
        scanf("%d",&N);
    
    
        for(a=2; a<N; a++) {
            if(N % a == 0) {
                b = N/a;
                printf("%d is divisble by %d.\n", N, a);
                printf("%d is divisble by %d.\n", N, b);
            }
        }
    
    
        if(b == 1) 
            printf("%d is a prime!\n",N);
    
    
        return 0;
    }
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. General forum question - if in wrong forum...
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 05:00 AM
  2. Having a second forum pop-up
    By 7smurfs in forum C# Programming
    Replies: 0
    Last Post: 03-11-2005, 07:10 PM
  3. programmig help!
    By dean in forum C++ Programming
    Replies: 7
    Last Post: 06-03-2004, 02:25 PM
  4. Replies: 7
    Last Post: 09-08-2002, 02:20 PM

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