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 = &sum; d = &difference; printf("%d\n%d",*c,*d); } else { sum = a+b; difference = (-1)*(a-b); c = &sum; 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.

28th Apr 2020, 1:35 PM
Unnati Mishra
Unnati Mishra - avatar
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 = &sum; d = &difference; printf("%d\n%d",*c,*d); } else { sum = a+b; difference = (-1)*(a-b); c = &sum; d = &difference; printf("%d\n%d",*c,*d); } return 0; } //this should work
28th Apr 2020, 1:52 PM
Alexander Thiem
Alexander Thiem - avatar
+ 2
Maybe you just forgot to copy this short: difference=a-b; line???
28th Apr 2020, 1:54 PM
Alexander Thiem
Alexander Thiem - avatar
+ 2
I liled to help
28th Apr 2020, 1:57 PM
Alexander Thiem
Alexander Thiem - avatar
+ 1
ypu are using difference uninitialised... write : difference=a-b before the if-clause
28th Apr 2020, 1:51 PM
Alexander Thiem
Alexander Thiem - avatar
0
I did that too in C coding app but there it makes the code show the negative value and not the absolute.
28th Apr 2020, 1:53 PM
Unnati Mishra
Unnati Mishra - avatar
0
Sorry i didn't see your code i was typing. Thanx 😊
28th Apr 2020, 1:53 PM
Unnati Mishra
Unnati Mishra - avatar
0
No i didn't write that. Thanx. I guess this is why i wasn't able to run it for all cases.😅
28th Apr 2020, 1:56 PM
Unnati Mishra
Unnati Mishra - avatar