HackerRank Sum and Difference of Two Numbers solution in C YASH PAL, 12 July 202430 August 2025 HackerRank Sum and Difference of Two Numbers solution in C – In this tutorial, we are going to solve the Hackerrank Sum and Difference of Two Numbers problem and write a c program to for this problem.ObjectiveThe fundamental data types in c are int, float and char. Today, we’re discussing int and float data types.The printf() function prints the given statement to the console. The syntax is printf("format string",argument_list);. In the function, if we are using an integer, character, string or float as argument, then in the format string we have to write %d (integer), %c (character), %s (string), %f (float) respectively.The scanf() function reads the input data from the console. The syntax is scanf("format string",argument_list);. For ex: The scanf("%d",&number) statement reads integer number from the console and stores the given value in variable number.To input two integers separated by a space on a single line, the command is scanf("%d %d", &n, &m), where n and m are the two integers.TaskYour 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.Input FormatThe first line contains two integers.The second line contains two floating point numbers.Constraints integer variables float variables Output FormatPrint the sum and difference of both integers separated by a space on the first line, and the sum and difference of both float (scaled to decimal place) separated by a space on the second line.Using the problem we are going to learn about the integer and float variable type in c programming and how to calculate the sum and difference of integer and float variables.Sum and Difference of Two Numbers SolutionHackerRank Sum and Difference of Two Numbers solution in C.#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int a,b,sum=0,sub=0; float c,d,s=0,su=0; scanf("%d%d",&a,&b); sum= a+b; sub=a-b; printf("%d %d\n",sum,sub); scanf("%f%f",&c,&d); s=c+d; su=c-d; printf("%0.1f %0.1f",s,su); return 0; }ExplanationIn this above-written program first, we have included the necessary header files that we are going to use in our program. and then we have declared a main() function. in the main function first, we declared two variables a, b of type int that will hold the user input values and sum, and sub integer variables that will hold the calculated sum and subtraction value of a and b.after that, we have declared two float variables c and d to store the floating values given by the user and two variables s and su to hold the calculated value.So first we stored the sum of a and b in the sum variable and the subtraction value of a and b in the sub variable and then using the printf() function we printed the value of the sum and sub variable on the output screen.After that, we calculated the sum and subtraction of values c and d and stored them in the s and su variables using the printf() function we printed the value of the s and su variables on the output screen.How to round the float variable value to one decimal place in c To round the float variable value to one decimal we need to use the syntax 0.1f C Solutions Hackerrank Problems Solutions cHackerRank