Question

In: Computer Science

1. a) Write a C program to calculate |A-B|. You should type in A and B...

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.

Solutions

Expert Solution

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 :


Related Solutions

(Write a program in C++) A local instructor wants you to write a program to calculate...
(Write a program in C++) A local instructor wants you to write a program to calculate the average score made on exams by her students. For simplicity, she always has only 12 students in each course she teaches. She teaches multiple subjects so she would like to enter the name of the exam. She wants the program to also determine the highest and lowest scores and the number of students who passed and failed the exam. A score of 60...
Using C++ Write a program to calculate the amount a customer should pay in a checkout...
Using C++ Write a program to calculate the amount a customer should pay in a checkout counter for the purchases in a bagel shop. The products sold are bagels, cream cheese, and coffee. Use the pseudo code discussed in the class to write the program. Make reasonable assumptions about the prices of bagel, cream cheese, and coffee. Declare prices of bagel, cream cheese, and coffee as constants.
Using C++ write a program to calculate the amount a customer should pay in a self...
Using C++ write a program to calculate the amount a customer should pay in a self checkout counter for his purchases in a bagel shop. The products sold are everything bagels, garlic bagels, blueberry bagels, cream cheese, and coffee. Using "doWhile loops" write the program to display the menu of the Bagel shop. Make the user buy multiple items of different choices. Finally display the total amount for the purchases. Below is my current code to work with. Please add...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
For this lab, you will write a C++ program that will calculate the matrix inverse of...
For this lab, you will write a C++ program that will calculate the matrix inverse of a matrix no bigger than 10x10. I will guarantee that the matrix will be invertible and that you will not have a divide by 0 problem. For this program, you are required to use the modified Gaussian elimination algorithm. Your program should ask for the size (number of rows only) of a matrix. It will then read the matrix, calculate the inverse, and print...
You should write a small C++ program that performs the following tasks: First, your program should...
You should write a small C++ program that performs the following tasks: First, your program should read in a set of 5 integers from “standard in” (remember cin). The numbers that you read in will be either zero or positive. If at least one of the five numbers entered is non-zero then your program should calculate the sum of the numbers and report that back to the user using “standard output” (remember cout). Then your program should be ready to...
WRITE A C++ PROGRAM: QUESTION 1 Assume you have a hobby to collect a particular type...
WRITE A C++ PROGRAM: QUESTION 1 Assume you have a hobby to collect a particular type of items. Specify the category to which the items you collect belong to ____________________________ [2 points] Define a struct with the name the same as the category that you filled in the blank above [4 points] The struct must have at least two members corresponding to properties relevant to the items in the category you specified. The struct members must have names meaningful to...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT