Thread: Comparing Int with double???

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    Comparing Int with double???

    Hi all would appreciate some help or general advice on changing the following code so as to stop whenever i>sqrt(n). My problem is that n is a integer input from a user so how do I calculate a sqrt of it?

    Thanks in advance

    Andrew

    Code:
    /*Is n a prime?*/
    
    main()
    {
    int i,m,n;
    m=0;
    scanf("%d", &n);
    for (i=2;i<n;i++){
          if (n%i==0)
          m=1;
    }
    if (m==0)
        printf("%d is prime",n);
    else
        printf("%d is not prime",n);
    return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    use double sqrt(double) in math.h

    If you want to use integers, cast them:

    int Number;
    int Answer = (int)sqrt((double)Number);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. 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
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM