Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • 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 Number solution in C

YASH PAL, 12 July 202412 July 2024

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.

hackerrank sum and differenc of two number problem solution in c
Sum and Difference of Two Numbers

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.

Sum and difference of two numbers

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 Programming Tutorials HackerRank cHackerRank

Post navigation

Previous post
Next post

Leave a Reply

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

  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programmingoneonone | WordPress Theme by SuperbThemes