Thread: C language

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    C language


    can any tell me,how to code a program of find a prime no. b/w a range using while loop.???
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Code:
    #include<stdio.h>
    
    int main() {
      int i = 1;
      char*message = "enter 2";
      puts(message);
      while(getchar() != '2')
        if (! (i++ % 9))
          puts(message);
      puts("found a prime!");
      return 0;
    }
    If this program isn't quite right for you it may be that I ignored the incomprehensible characters in you problem statement "no. b/w a range".
    Last edited by b49P23TIvg; January 7th, 2014 at 03:04 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by b49P23TIvg
    If this program isn't quite right for you it may be that I ignored the incomprehensible characters in you problem statement "no. b/w a range".
    "no. b/w" = number between

    But b49P23TIvg is absolutely correct, how hard would it have been to type that instead of abbreviations perhaps incomprehensible in certain cultures and to those for whom English (or Latin in fact) may not be a first language?

    Apart from that the requirement is unclear; just one prime in the entire range or all? Is the range fixed or entered by the user?

    Apart from that the etiquette of this forum is that you demonstrate a fair attempt before assistance is given. What have you got so far. You need an algorithm - what algorithm are you proposing? Even a naive exhaustive (x / n) * n == x test may be acceptable for a sufficiently small range.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    is there any fault in this given code ???


    #include<stdio.h>
    #include<conio.h>
    void main()
    { int x,z,y=2;
    //x is lower limit and z is upper limit of range
    printf("enter the no.");
    scanf("%d%d",&x&z);
    while(x<=z)
    {
    while(y<=x)
    {
    if(x%y==0)
    {
    break;
    }
    y++;
    if(x==y)
    printf("prime no.=%d \n",x);

    }
    x++;
    }
    getch();
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0
    is this code right for the code which i asked ??
    #include<stdio.h>
    #include<conio.h>
    void main()
    { int x,z,y=2;
    //x is lower limit and z is upper limit of range
    printf("enter the no.");
    scanf("%d%d",&x&z);
    while(x<=z)
    {
    while(y<=x)
    {
    if(x%y==0)
    {
    break;
    }
    y++;
    if(x==y)
    printf("prime no.=%d \n",x);

    }
    x++;
    }
    getch();
    }
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480

    Oh yes that's correct, do hand it in immediately.


    adorable!

    scanf("%d%d",&x&z);


    Compile your program.
    In your compiler trust.
    Gather your courage.
    Use your compiler.

    Be sure to turn on warning flags.
    with gcc in unix

    gcc -Wall -o primes primes.c


    Use this website to find prime numbers.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo