Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • 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 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.

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 *

Related website

The Computer Science

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programmingoneonone | WordPress Theme by SuperbThemes