Sum and Difference of Two Numbers in C with Full Explanation

Learn how to solve the Sum and Difference of Two Numbers problem in C programming. Includes full code, step-by-step explanation, and beginner tips.

C programming is a foundational skill that every computer science student must master. At TruongDevs, we introduce a beginner-friendly but essential exercise that helps you understand how to work with basic data types, perform arithmetic operations, and manage input and output. This tutorial focuses on calculating the sum and difference of two integers and two floating-point numbers using the int and float types in C.

You will learn how to read multiple inputs using scanf with the correct format specifiers, perform operations using + and -, and format output using printf. For floating-point numbers, you will also see how to round results to one decimal place using the format %.1f.

C program to calculate the sum and difference of two integers and two float numbers using scanf and printf"
C program to calculate the sum and difference of two integers and two float numbers using scanf and printf

Objective

The fundamental data types in C are int, float and char. Today, we're discussing int and float data types.

The printf() function prints the given statement to the console. The syntax is printf("format string", argument_list);. In the function, if we are using an integer, character, string or float as argument, then in the format string we have to write %d (integer), %c (character), %s (string), %f (float) respectively.

The scanf() function reads the input data from the console. The syntax is scanf("format string", argument_list);. For ex: The scanf("%d", &number) statement reads integer number from the console and stores the given value in variable number.

To input two integers separated by a space on a single line, the command is scanf("%d %d", &n, &m), where n and m are the two integers.

Task

Your task is to take two numbers of int data type, two numbers of float data type as input and output their sum:

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

Input Format

The first line contains two integers.
The second line contains two floating point numbers.

Constraints

1 <= integer variables <= 10^4
1 <= float variables <= 10^4

Output Format

Print the sum and difference of both integers separated by a space on the first line, and the sum and difference of both float (scaled to 1 decimal place) separated by a space on the second line.

Sample Input

10 4
4.0 2.0

Sample Output

14 6
6.0 2.0

Hướng dẫn giải bài toán bằng tiếng Việt

Phân tích yêu cầu

Đây là một bài tập nhập môn C rất cơ bản nhưng cũng rất hữu ích để ôn lại cách khai báo biến, nhập dữ liệu và in ra kết quả. Cụ thể:

  • Dòng đầu tiên yêu cầu nhập 2 số nguyên.
  • Dòng thứ hai yêu cầu nhập 2 số thực (float).
  • Sau đó, tính tổng và hiệu của 2 số nguyên, rồi tính tổng và hiệu của 2 số thực.
  • Kết quả số thực phải in ra 1 chữ số thập phân.

Lời giải C chuẩn

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int a, b;
    float c, d;
    scanf("%d %d", &a, &b);
    scanf("%f %f", &c, &d);
    printf("%d %d\n", a + b, a - b);
    printf("%.1f %.1f", c + d, c - d);
    return 0;
}

Giải thích từng bước

  • Khai báo biến: a, b là int, c, d là float
  • Nhập dữ liệu: sử dụng scanf với định dạng %d %d và %f %f
  • Tính toán:
    • Tổng số nguyên: a + b
    • Hiệu số nguyên: a - b
    • Tổng số thực: c + d
    • Hiệu số thực: c - d
  • Xuất kết quả:
    • Sử dụng printf để in 2 kết quả trên mỗi dòng
    • Định dạng %.1f giúp in ra 1 chữ số sau dấu phẩy

Test Case minh họa

Input (Int) Sum Difference
10 4 14 6
20 5 25 15
Input (Float) Sum Difference
4.0 2.0 6.0 2.0
1.5 1.0 2.5 0.5

Mẹo từ TruongDevs

  • Luôn kiểm tra định dạng scanf có trùng khớp thứ tự dữ liệu không.
  • Khi in số thực, nhớ kiểm tra định dạng %.1f hoặc dùng round() nếu cần làm tròn thủ công.
  • In từng dòng kết quả riêng biệt để đúng format.

Mở rộng bài toán

Bạn có thể thử:

  • Nhập 2 số bất kỳ và xử lý dấu âm, số lớn nhỏ hơn nhau.
  • Sử dụng hàm phụ như intSumDiff() hoặc floatSumDiff() để rèn luyện kỹ năng viết hàm.
  • Tự viết hàm làm tròn số thập phân thay vì dùng %.1f.

Kết luận

Bài toán này là một ví dụ điển hình để rèn luyện kỹ năng cơ bản về thao tác số và định dạng xuất dữ liệu trong C. Với sinh viên học lập trình, đặc biệt là người mới học C, đây là bài tập không thể bỏ qua. Hãy luyện tập thật kỹ các thao tác này để sẵn sàng bước vào các bài toán xử lý logic và thuật toán nâng cao hơn.

Truy cập TruongDevs.com để khám phá thêm nhiều bài học lập trình chất lượng!

About the author

TruongDevs
Không phải bug nào cũng xấu, có bug giúp ta tỉnh ra

Post a Comment