0
What's wrong in this code?
#include<stdio.h> void main() { int a,b,sum,difference; int *c,*d; scanf("%d %d",&a,&b); printf("%d\n%d\n",a,b); if(difference>0) { sum = a+b; difference = a-b; c = ∑ d = &difference; printf("%d\n%d",*c,*d); } else { sum = a+b; difference = (-1)*(a-b); c = ∑ d = &difference; printf("%d\n%d",*c,*d); } return 0; } When i run it on C coding in android its working but when i run in on sololearn its gives negative value on difference whereas i want the absolute value of the difference. Please help.
7 ответов
+ 3
#include<stdio.h>
void main()
{
int a,b,sum,difference;
int *c,*d;
scanf("%d %d",&a,&b);
printf("%d\n%d\n",a,b);
difference=a-b;
if(difference>0)
{
sum = a+b;
difference = a-b;
c = ∑
d = &difference;
printf("%d\n%d",*c,*d);
}
else
{
sum = a+b;
difference = (-1)*(a-b);
c = ∑
d = &difference;
printf("%d\n%d",*c,*d);
}
return 0;
}
//this should work
+ 2
Maybe you just forgot to copy this short:
difference=a-b;
line???
+ 2
I liled to help
+ 1
ypu are using difference uninitialised... write :
difference=a-b before the if-clause
0
I did that too in C coding app but there it makes the code show the negative value and not the absolute.
0
Sorry i didn't see your code i was typing.
Thanx 😊
0
No i didn't write that.
Thanx.
I guess this is why i wasn't able to run it for all cases.😅