coder18s,blogspot.com

Powered by Blogger.

3. Sum and Difference of Two Numbers Hackerrank Solution

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:
  4. Print the sum and difference of two int variable on a new line.
  5. 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

Sample Output

1        4        6
6.0              2.0
     Program Code
#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\n",a+b,a-b);
    printf("%.1f %.1f",x+y,x-y);
    
    
    return 0;
}