Thread: Errors in smallest divisor program

  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
    4,183
    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.
    "...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. #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, 05: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, 04:50 PM
  4. smallest integer program, help please
    By STUDENT2 in forum C Programming
    Replies: 9
    Last Post: 09-06-2002, 03:48 PM
  5. problem with the divisor
    By yusiye in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 01:30 PM

Tags for this Thread