Sum and Difference of Two Numbers hackerrank solution

    
        
        #include <stdio.h>

        int main() {
            int num1, num2;
            float float1, float2;
        
            scanf("%d %d", &num1, &num2);        // Read two integers
            scanf("%f %f", &float1, &float2);    // Read two floating-point numbers
        
            int sum_int = num1 + num2;           // Perform integer addition
            int diff_int = num1 - num2;          // Perform integer subtraction
        
            float sum_float = float1 + float2;   // Perform float addition
            float diff_float = float1 - float2;  // Perform float subtraction
        
            printf("%d %d\n", sum_int, diff_int);          // Print integer results
            printf("%.1f %.1f\n", sum_float, diff_float);  // Print float results
        
            return 0;
        }
        
    

Let's go through the solution step by step:

We include the header file to use the standard input/output functions.

In the main() function, we declare two integers num1 and num2 to store the input integers, and two floats float1 and float2 to store the input floating-point numbers.

We use the scanf() function to read input from the user:

scanf("%d %d", &num1, &num2) reads two integers and stores them in num1 and num2.

scanf("%f %f", &float1, &float2) reads two floating-point numbers and stores them in float1 and float2.

We perform the arithmetic operations on the input values:

sum_int = num1 + num2 calculates the sum of num1 and num2.

diff_int = num1 - num2 calculates the difference between num1 and num2.

sum_float = float1 + float2 calculates the sum of float1 and float2.

diff_float = float1 - float2 calculates the difference between float1 and float2.

Finally, we use the printf() function to print the results:

printf("%d %d\n", sum_int, diff_int) prints the integer results.

printf("%.1f %.1f\n", sum_float, diff_float) prints the float results with one decimal place of precision using the %.1f format specifier.

By providing the expected input and executing the code, you should see the desired output according to the problem statement on HackerRank.

Please note that this solution assumes valid input and does not perform any error handling for simplicity.