Summary: This simple C program reads two integers and two floating-point numbers from the user, then computes and displays their sums and differences. It demonstrates formatted I/O and type-specific arithmetic.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int a, b;
float x, y;
// Read two integers
scanf("%d %d", &a, &b);
// Read two floats
scanf("%f %f", &x, &y);
// Print sum and difference of integers
printf("%d %d\n", a + b, a - b);
// Print sum and difference of floats (rounded to 1 decimal place)
printf("%.1f %.1f\n", x + y, x - y);
return 0;
}
Variable Types:
The program uses int
for integer values and float
for decimal values, demonstrating basic data types in C.
User Input:
The scanf()
function is used to read values from the standard input. %d
reads integers and %f
reads floats.
Arithmetic Operations:
Basic arithmetic operators +
(addition) and -
(subtraction) are used to calculate the sum and difference of both integer and float variables.
Formatted Output:
The printf()
function is used to print results. For float values, %.1f
is used to limit output to 1 decimal place.
Precision Handling:
Using %.1f
ensures consistent float formatting which is essential in many real-world applications like billing or reporting.
Header Files:
Although not all included headers are needed here (string.h
, math.h
, stdlib.h
), stdio.h
is essential for input/output functions like scanf
and printf
.