Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Sum and Difference of Two Numbers solution in C

YASH PAL, 12 July 202416 January 2026

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.

Objective

The 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.

Task

Your task is to take two numbers of int data type, two numbers of float data type as input and output their sum:

  1. Declare  variables: two of type int and two of type float.
  2. Read  lines of input from stdin (according to the sequence given in the ‘Input Format’ section below) and initialize your  variables.
  3. 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 Format

The first line contains two integers.
The second line contains two floating point numbers.

Constraints

  •  integer variables 
  •  float variables 

Output Format

Print 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.

HackerRank sum and difference of two numbers solution in c
Sum and Difference of Two Numbers Solution

HackerRank 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;
}

Explanation

In 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

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Hello world in c solution
Playing with characters problem solution
sum and difference of two numbers problem solution
functions in c problem solution
pointers in c problem solution
Conditional statements in c problem solution
For loop in c solution
Sum of Digits of a five-digit number problem solution
Bitwise operators problem solution
Printing pattern using loops problem solution
1D Arrays in c problem solution
Array Reversal problem solution
Printing Tokens problem solution
Digit Frequency problem solution
Dynamic Array in c problem solution
Calculate the Nth term problem solution
Students Marks sum problem solution
Sorting Array of strings problem solution
Permutations of strings problem solution
Variadic functions in c problem solution
Querying the documents problem solution
Boxes through a tunnel problem solution
Small Triangles, Large Triangles problem solution
post-transition problem solution
Structuring the document problem solution

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes