In: Computer Science
1.
a) Write a C program to calculate |A-B|. You should type in A and B when running the program.
b) Write a C program using a function to calculate (A-B)^(1/2)
-If A > B, the result will be (A-B)^(1/2)
-If A < B,the result will be (B-A)^(1/2)i
-You should type in A and B when running the program.
1) Code :
#include <stdio.h>
int main()
{ int a,b,s;
printf("Enter A and B \n"); //take input A and B from user
scanf("%d %d", &a,&b);
s=a-b; //find subtraction of inputs
if(s<0){
s= s *(-1); // make result positive if it was negative
}
printf("|a-b|= %d", s); // print result.
return 0;
}
Sample output :
2) Code :
#include <stdio.h>
#include <math.h>
int main()
{ double a,b,sub,sroot;
printf("Enter A and B \n"); //take input A and B from user
scanf("%lf %lf", &a,&b);
if(a>b){
sub= a-b; // find difference
sroot= sqrt(sub); // find square root.
printf("(a-b)^0.5= %f\n", sroot);
}
else{
sub= b-a; // find difference
sroot= sqrt(sub); // find square root
printf("(b-a)^0.5= %f\n", sroot);
}
return 0;
}
Sample output :