Jump to content

Floating poing if statment

Go to solution Solved by mariushm,

You got the order wrong, the 

printf(" y is less than 10");

should be at the lowest if  ... 

 

edit: and yeah, use the correct data type... float for floating point numbers

 

also, I think you'd want to use >= in some places, because for example, in your code, if y is between 10.000 and 11, the number won't be tested. 

 

You could also use a FOR loop instead of ifs for this ... something like this: 

 

int counter;
int min;
int max;
for ( counter = 0;counter<6;counter++ ) {
  min = counter * 10;
  max = (counter+1) * 10;
  if (y>=min && y<max) {
	// syntax may be wrong here but you should get the point
    printf("y is equal or greater than %d and smaller than %d",min,max);
  }
}

 

or you could calculate the remainder of the division of y by 10, substract it, then divide by 10 ... something like this.

 

int remainder;
int value;

value = y;
remainder = value % 10;
value = (value-remainder) / 10; 

printf("y is equal or larger than %d and smaller than %d", value*10, (value+1)*10);

 

#include <stdio.h>
void main(void)
{
	int y;
	double x, a, b, c, result;

	printf("Enter value for y: \n");
	scanf_s("%lf", &y);
	getchar();

	if (y < 60 & y > 51)
	{
		printf(" y is less than 10");
	}
	else if (y <= 50 & y > 41) 
	{
		printf(" y is greater than 10 but less than 20");
	}
	else if (y < 40 & y>31) 
	{
		printf(" y is greater than 10 but less than 20");
	}
	else if (y < 30 & y>21)
	{
		printf(" y is greater than 10 but less than 20");
	}
	else if (y < 20 & y>11)
	{
		printf(" y is greater than 10 but less than 20");
	}
	else if (y < 10 & y> 0)
	{
		printf(" y is greater than 10 but less than 20");
	}

	printf("\n Enter value for X:");	//Gets vlaue for x
	scanf_s("%lf", &x);				//Reads in value for x
	getchar();						//Waits for input
	printf("Enter value for A:");	
	scanf_s("%lf", &a);				
	getchar();						
	printf("Enter value for B:");	
	scanf_s("%lf", &b);				
	getchar();
	printf("Enter value for C:");
	scanf_s("%lf", &c);
	getchar();

	result = (a * pow(x, 2)) + (b * x) + c;

	return(result);
}

I cant seem to get any of my if statements to print no matter what value I use. I am also aware i can do it incrementaly but that is not allowed in this case. The formula for result also does not work and I am just stuck. Any help appreciated.

Link to comment
Share on other sites

Link to post
Share on other sites

You got the order wrong, the 

printf(" y is less than 10");

should be at the lowest if  ... 

 

edit: and yeah, use the correct data type... float for floating point numbers

 

also, I think you'd want to use >= in some places, because for example, in your code, if y is between 10.000 and 11, the number won't be tested. 

 

You could also use a FOR loop instead of ifs for this ... something like this: 

 

int counter;
int min;
int max;
for ( counter = 0;counter<6;counter++ ) {
  min = counter * 10;
  max = (counter+1) * 10;
  if (y>=min && y<max) {
	// syntax may be wrong here but you should get the point
    printf("y is equal or greater than %d and smaller than %d",min,max);
  }
}

 

or you could calculate the remainder of the division of y by 10, substract it, then divide by 10 ... something like this.

 

int remainder;
int value;

value = y;
remainder = value % 10;
value = (value-remainder) / 10; 

printf("y is equal or larger than %d and smaller than %d", value*10, (value+1)*10);

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×