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:
Thank you for your reply.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;
- }