Thread: Hello new to to the forum and also to programmig.

  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
    4,183
    Take you have not yet learned to write functions.
    Is this correct?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #18
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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;
    }
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

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, 08: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