Sum and Difference of Two Numbers in C HackerRank

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

Sample Input

10 4
4.0 2.0
Code language: CSS (css)

Sample Output

14 6
6.0 2.0Code language: CSS (css)

Program:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a,b;
float x,y;
scanf("%d %d",&a,&b);
scanf("%f %f",&x,&y);
printf("%d %d",a+b,a-b);
printf("\n%.1f %.1f",x+y,x-y);
return 0;
}

Leave a Comment