We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Sum and Difference of Two Numbers
Sum and Difference of Two Numbers
Sort by
recency
|
590 Discussions
|
Please Login in order to post a comment
include
int main() { int a,b; float c,d; // printf ("enter first integer digit"); scanf ("%d",&a); // printf ("enter second integer digit"); scanf ("%d",&b); // printf ("enter first float digit"); scanf ("%f",&c); // printf ("enter first float digit"); scanf ("%f",&d); int sum = a+b; int differnce= a-b; printf ("%d %d " , sum, differnce);
float add= c+d; float subtract= c-d; printf ("%.1f %.1f " , add,subtract);
}
Your task is to take two numbers of int data type, two numbers of float data type as input and output their sum:
Declare variables: two of type int and two of type float. Read lines of input from stdin (according to the sequence given in the 'Input Format' section below) and initialize your variables. Use the and operator to perform the following operations: Print the sum and difference of two int variable on a new line. Print the sum and difference of two float variable rounded to one decimal place on a new line.
Here is solution - HackerRank sum and difference of two numbers problem solution
Maybe some rewording on the Task would be nice. I added in some if statements to check for the higher number and substract that from the lower one to get their (positive) difference, but the task isnt't about the difference but about the result of a substraction of float 2 from float 1.