I am trying to create a program in C to find the k'th continuing not free square numbers.
For example if k=3 it will print 48,49,50.
However I'm constantly hitting this error:
[Error] invalid operands of types 'double' and 'double' to binary 'operator%'
The error is in this line: if (x % pow(j, 2)=0)
Here is my code:
#include <stdio.h>
#include <math.h>
#define K 6
int main()
{
int i,j,x;
while(i!=0)
{
for (x=4; x<=1000000000; x++)
for(j=2; j<=113; j++)
{
if (x % pow(j, 2)=0)
{
printf("%d",x);
}
}
}
}
A single equality sign in C is assigning the value form the right to the variable on the left. For comparison, use a dounle equality sign.
As soon as you have a bug you do not understand, instead of relying on the precedence of operators, add brackets
The % operator works on integers and pow returns a double, so you have to cast your result to an integer
Try replacing if (x % pow(j, 2)=0) by if ((x % (int) pow(j, 2))==0).
Related
That's my code -
main()
{
double x;
double y = pow(((1/3 + sin(x/2))(pow(x, 3) + 3)), 1/3);
printf("%f", y);
return 0;
}
I get an error in double y = pow((1/3 + sin(x/2))(pow(x, 3) + 3), 1/3);, it says that called object is not a function or function pointer. I don't get it though - (1/3 + sin(x/2))(pow(x, 3) + 3) is the first element of pow(x, y); that is the x I want to raise to y (1/3) power. Where lies the problem? I'm pretty new to c basic but I can't find the answer anywhere.
If you want to multiply, you need to use the * operator. You can't put parenthesized expressions adjacent to each other to denote multiplication.
(1/3 + sin(x/2))*(pow(x, 3) + 3)
That's because the return value of pow is a double, not a function. Maybe what you're trying to do is pass a call to pow as the second argument to the first pow.
#include <stdio.h>
#include <math.h> // For pow() function
int main() {
// Initialize with whatever value you want
double x = 100;
// Make sure to use an arithmetic operator
double y = pow(((1/3.0 + sin(x/2))*(pow(x, 3) + 3)), 1/3.0);
// Use right format specifier
printf("%lf", y);
return 0;
}
Don't forget to include math.h library.
Initialize x with a value.
Avoid using integer division 1/3, instead use 1/3.0 or 1.0/3.0 because 1/3 == 0.
Use an arithmetic operator (+, -, *, /) before middle pow() like pow(((1/3.0 + sin(x/2))+(pow(x, 3) + 3)), 1/3.0).
(Opt.) Use right format specifier for y which is %lf. But you may get away with %f.
Going to post the whole code so if there is something else you can help me.
The idea of the program is to know if the inserted number is multiple of 5, power of 10 or a perfect square (I'm not English, a bit unsure if this word is the one I'm looking for).
int main (void){
int i,b=0,rqi;
printf("Insert a number from 1 to 10000: ");
scanf("%d",i);
double rq=sqrt(i),p=log(i);
if(i%5==0){
printf("It's a multiple of 5\n");
b=1;
}
if(p%1==0){
int pi=p;
printf("It's a power of 10 (%d=10^%d)\n",i,pi);
b=1;
}
if(rq%1.0==0){
rqi=rq;
printf("It's a perfect square (%d = %d*%d)\n",i,rqi,rqi);
}
if(b==0){
printf("It doesn't satisfies any condition.\n");
}
return 0;
}
The errors I'm getting are
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat=]
scanf("%d",i);
^
error: invalid operands to binary % (have ‘double’ and ‘int’)
if(p%1==0)
^
invalid operands to binary % (have ‘double’ and ‘double’)
if(rq%1.0==0)
Also, I learned a bit of java and not C, is there a way to make a boolean or making an int 0 or 1 is the way to go?
scanf("%d",i);
Pass address of int .Write like this -
scanf("%d",&i);
% operator cannot be used on double or float . Remainder is just concerned for integer values .
Use fmod function for the above task to get remainder.
Like this you can do this and incorporate it in your if condition-
fmod(p,1.0) // gives remainder of p divided by 1.0
But probably don't compare double values using ==.
Seems like you should check fmod function.
Also, if you want to check whether a double is of integer's range or not, you can check if it's value is equal to it's floor's value or not, using floor fucntion.
if(double_num == floor(double_num)) return 1;
else return 0;
You are basically dealing with integers here. Floating-point arithmetic has its inaccuracies; it might be better to do the comparison with integers instead.
int is_square(unsigned int x)
{
int r = sqrt(x) + 0.5;
return (r*r == x);
}
Here, a (crudely) rounded integer root is calculated first. Then you check back whether squaring it gives you back your original number.
int is_power10(unsigned int x)
{
while (x && x % 10 == 0) x /= 10;
return (x == 1);
}
Strip off trailing zeros. If all that's left is a 1, you've got a power of 10.
I was trying to run a program but it shows an error as:
Invalid binary operator float to int
When I tried making it float it says:
Invalid binary operator float to float
The problem is with % operator And its operands.
Please tell me what to do?
#include <stdio.h>
int main()
{
float x,y;
scanf("%f%f",&x,&y);
float z=x%5.0f;
if(x<=y && z==0)
printf("%.2f",y-x-0.50);
else if (x>y || z!=0)
printf("%.2f",y);
return 0;
}
Modulus % only makes sense with integers because it is defined as the remainder from integer division. You can't do integer division with floats.
Modulus operator doesn't work with float. You probably want to use the fmod function:
http://www.cplusplus.com/reference/cmath/fmod/
Please note that % operator doesn't work with float. Instead you need to use fmod() for your requirement.
double fmod(double numerator, double denominator);
FMOD function is what you need:
#include <stdio.h>
int main(void){
float x,y;
double z;
scanf("%f%f",&x,&y);
z = fmod(x, 5.0f);
if(x<=y && z==0)
printf("%.2f",y-x-0.50);
else if (x>y || z!=0)
printf("%.2f",y);
return 0;
}
As #amdixon commented: "modulus operator % only applies between integers."
The operands of the % operator shall have integer type.
C11dr §6.5.5 2
In keeping with float arithmetic, suggest using fmodf() instead of % or fmod().
fmodf() computes the floating-point remainder of x/y.
// float z=x%5.0f;
float z = fmodf(x , 5.0f);
Notes:
The result of fmod() and family can be expected to be exact. Ref
The % is the remainder. Calling it the modulus operator overloads its meaning that often does not meet expectations when with a%b, either a or b is negative. See What's the difference between “mod” and “remainder”?
I am in an introductory C programming class. Our latest project has us writing code to tabulate x and sqrt(x) values from 1-10 with a 0.1 step using a while loop. When I try to do the 0.1 increment, however, nothing is added to the starting integer 1 and the program runs in an infinite loop. I'll post the code below. Other than it not doing the step, the program runs fine (and works with other increments like 1, etc.). How do I resolve this?
#include <stdio.h>
#include <math.h>
int main(void)
{
int x=1;
double sq_rt;
printf("Square Root Table: \n");
printf("Value of X Square Root of X\n");
while (x <= 10)
{
sq_rt = sqrt (x);
printf("%6i %20f \n", x, sq_rt);
x += 1e-1;
}
return 0;
}
An int type will only allow you to store whole numbers (i.e. -2, -1, 0, 1, 2 etc). To store numbers with a decimal point, you'll need a double precision (or double) type. Change the first line of main() to:
double x = 1.0;
If you try to add 1e-1 to an int, it will convert it to an int first - the type of x - which when truncated will end up being zero, so you'll never actually add anything to x.
The line in your program which reads
x += 1e-1;
is performing operations equivalent to
x = (int)(((double)x) + 0.1);
In other words, x is first converted to a double, then 0.1 is added to it, resulting in 1.1. This value is then converted to int, resulting in a value of 1, which is assigned to x.
The fix is to change the type of x to a floating point type such as float or double.
Share and enjoy.
the following code is a suggestion on how to perform the desired algorithm.
#include <stdio.h>
#include <math.h>
// define the magic numbers, don't embed them in the code
#define UPPER_LIMIT (10.0)
#define STEP_SIZE (0.1)
int main(void)
{
double x=1.0;
double sq_rt;
printf("Square Root Table: \n");
printf("Value of X Square Root of X\n");
// due to ambiguities in 'real' values,
// this loop will iterate approx. 90 times.
while( x < UPPER_LIMIT )
{
sq_rt = sqrt (x);
// display the two double values
// note: long float conversion values
// because the underlying numbers are double
// note: blanks for alignment with column headers
printf("%9.6lf %16.13lf \n", x, sq_rt);
// increase base value by increment of 0.1
x += STEP_SIZE;
} // end while
return 0;
} // end function: main
So I am trying to cast (3/17) as double. This is for an assignment so the professor wants it to be this way for some reason.
I am trying to cast it by doing the following:
(double)(3/17)
Actual code:
int assignment7()
{
#include <stdio.h>
#define PI 3.14
int a=0;
double Ny=0,y=0,z=0,x=0,amod2=0;
printf("Enter values for x,y,z and a(must be an odd number): ");
scanf("%lf%lf%lf%d",&x,&y,&z,&a);
amod2=a%2;
printf("%.2lf\n",test);
Ny=y / (double)(3/17) - z + x / amod2 + PI;
printf("%lf\n",Ny);
}
The problem is occurring on the second to last line where it is interpreting 3/17 as an int thus it would equal 0. y / 0
Professors exact instructions:
"General equation: y = y / (3/17) - z + x / (a % 2) + PI (recall: a is an integer; the 3 and 17 constants in the equation should be left as integers initially, but explicitly type-casted as floating-point values)"
(3/17) is equal to 0 because it is evaluated using integer arithmetic, and so you get a divide by zero, which is of course a run-time error. Change:
(double)(3/17)
to:
(3.0 / 17.0)
Note that the cast is redundant.
You have a division by zero as 3/17 is zero.
Instead use doubles to begin with: 3.0 / 17.0, then you don't even need the cast.