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
Programmingoneonone
HackerRank sum and difference of two numbers solution in c

HackerRank Sum and Difference of Two Numbers solution in C

YASH PAL, 12 July 202416 June 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 or write a program to for this problem. In this problem, we need to declare two variables of integer type and two variables of float type. from the first line of input, we need to read two values of integer and in the second line we need to read the two values of float type using the + and – operators we need to print the sum and difference of two integer variables on a new line and sum and difference of float variables rounded to one decimal place to the new 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.

Below is the explanation and solutions

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

Solution in C Programming

#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

Next problem solution – HackerRank Functions in C problem solution

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 *

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
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes