How does the relational "is equal to" operator work [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
#include <stdio.h>
int main()
{
int x=3;
float y=3.0;
if(x==y)
printf("\n x and y are equal");
else
printf("\n x and y are not equal")
return 0;
}
The code prints "x and y are equal".
Please explain how did this happen.

When comparing an int variable to a float variable using ==, the int is converted to a float implicitly first, and then the comparison is made.
Hence, float(x) == y means 3.0f == 3.0f, which is true, that's why you it executes:
printf("\n x and y are equal");

Related

How to check if 3 numbers are a Pythagorean triple? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
Improve this question
How we could get three numbers without ordering and then check if they form a Pythagorean triple or not?
So, pythagorean(3, 4, 5) or pythagorean(5, 3, 4) will print/return true, while pythagorean(4, 3, 6) will print/return false.
You can use this algotrithm :
#include<stdio.h>
int main(){
long long int a, b, c ;
scanf("%llu %llu %llu", &a, &b, &c);
if (a*a==b*b+c*c || b*b==a*a+c*c || c*c==a*a+b*b)
{
printf("YES");
}
else
printf("NO");
return 0;
}
If you use the equation, a^2 + b^2 = c^2, c should be the largest number and the order of a and b should not matter. Just find the largest number, set that equal to c, and then set the other two values to a and b and check to see if the equality is true.

Relational operator and the precedence of operators [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I executed a code in the C language, However I am unable to understand its output.
#include <stdio.h>
int main()
{
int a=5;
int b= ++a + 0!=0;
printf("%d %d",++a, b);
return 0;
}
The output for the above program is
7 1
I am unable to understand why it is so.
Order of operations causes this to be treated as:
int b = (((++a) + 0) != 0);
Therefore:
int b = (6 != 0);
6 isn't 0, so that has a value of true aka 1.
int b = 1;

Convert to scientific notation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How do I first convert numbers that I'm adding to scientific notation in C program . Then add them together, and once I have my final answer, then convert it back to scientific notation and return it?
What's wrong with:
#include <stdio.h>
int main(void)
{
double a, b, c;
printf("Enter two numbers: ");
if (scanf("%lf %lf", &a, &b) != 2)
{
fprintf(stderr, "Expected two floating point numbers\n");
return 1;
}
c = a + b;
printf("%13.6e + %13.6e = %13.6e\n", a, b, c);
return 0;
}
Example run:
Enter two numbers: 13.456e34 +11.44225534e33
1.345600e+35 + 1.144226e+34 = 1.460023e+35

What is wrong with this code?? It is giving output as 0.0000? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
#include<stdio.h>
double sum_1(int n)
{
int i=1;
double s;
while(n>0)
{
s=s+i/(2*i+2);
i=i+2;
n--;
}
return s;
}
int main()
{
int n=5;
double s1;
printf("Enter n:\n");
scanf("%d",&n);
s1= sum_1(n);
printf("sum = %lf",s1);
return 0;
}
The problem is
s=s+i/(2*i+2);
in the first iteration, s is used uninitialized. Since this is a type which can have trap representation, and it's address is never taken, trying to use the uninitialized value here invokes undefined behavior.
That said, the grouping of the statement
s=s+i/(2*i+2);
is same as
s = s + ( i / (2*i+2) );
^^^^^^^^^^^^^---- integer division
so, it involves integer division, which is most likely what you don't want. You need to enforce floating point arithmetic, like
s=s+i/(float)(2*i+2);
Finally, for printing a double, %f is sufficient, %lf is not needed and has no effect.

how to handle a mathematical function in C language like MATLAB [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm looking for a way to create a "variable-function" in C language.
In MATLAB i'm able to create something like:
my_function = (#x) sin(x) + x^2 + x;
so that i'm able to evaluate it for any value of 'x' i like:
my_point = 3.09;
my_function(my_point);
is there anything like that for C language?
That's just a regular C function. The terminology would be: "A function with arguments"
double my_function(double x)
{
return sin(x) + x*x + x;
}
#include <stdio.h>
#include<math.h>
#define my_function(x) sin(x) + pow(x,2) + x
int main()
{
double my_point = 3.09;
printf("%lf",my_function(my_point));
return(0);
}

Resources