Errors in smallest divisor program

This is a discussion on Errors in smallest divisor program within the C Programming forums, part of the General Programming Boards category; Dear All, I have written a program to find the smallest divisor of an input number. The code gives me ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Errors in smallest divisor program

    Dear All,


    I have written a program to find the smallest divisor of an input number. The code gives me an error in lines 19 and 23. The error is invalid operands to binary %. Please help me to troubleshoot, thank you. Below is my code:

    Code:
     
    
    
    • #include<stdio.h>
    • #include<stdlib.h>
    • #include<math.h>
    • int main()
    • {
    • int n,r,sdivisor;
    • double d,sqroot,comp;
    • printf("Enter the number whose smallest divisor has to be found");
    • scanf("%d",&n);
    • if(n%2==0)
    • {
    • printf("the smallest divisor is 2");
    • }
    • else
    • {
    • sqroot=sqrt(n);
    • d=3;
    • }
    • comp=n%d;
    • while(comp!=0 & d<sqroot)
    • {
    • d=d+2;
    • comp=n%d;
    • }
    • if(comp==0)
    • {
    • sdivisor=d;
    • }
    • else
    • {
    • sdivisor=1;
    • }
    • system("pause");
    • return 0;
    • }
    Thank you for your reply.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,764
    Code:
    comp=n%d;
    The "%" operator is not for floating point types; d is a double.

    Read the directions on how to post code.
    http://cboard.cprogramming.com/c-pro...ead-first.html

    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. #3
    Registered User
    Join Date
    May 2012
    Location
    Kathmandu
    Posts
    4

    Wink

    You have declared variable comp as double. But, in line 19 and 23, n%d returns integer value. When you tried to assign integer value to double using code comp=n%d, it shows error. Source: C operators

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Thank you for your reply. Problem solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The greatest common divisor (GCD) help..
    By damha in forum C Programming
    Replies: 4
    Last Post: 04-09-2011, 06:18 AM
  2. Divisor Algorithm
    By tytelizgal in forum C Programming
    Replies: 6
    Last Post: 12-03-2008, 10:58 PM
  3. Greatest common divisor
    By wiz23 in forum C++ Programming
    Replies: 5
    Last Post: 04-13-2005, 05:50 PM
  4. smallest integer program, help please
    By STUDENT2 in forum C Programming
    Replies: 9
    Last Post: 09-06-2002, 04:48 PM
  5. problem with the divisor
    By yusiye in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 02:30 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