Introducción
Las operaciones aritméticas como la adición, la sustracción, la multiplicación y la división son operaciones fundamentales en la programación. En este laboratorio, te mostraremos cómo escribir un programa en C para realizar operaciones aritméticas básicas y cómo el lenguaje C maneja el casteo de tipos.
Configura la estructura básica
Antes de comenzar, asegúrate de tener instalado un compilador C en tu máquina. Abre tu editor de texto y crea un nuevo archivo llamado "main.c" en el directorio ~/project/.
Comencemos incluyendo el archivo de encabezado stdio.h y escribiendo la función principal:
#include<stdio.h>
int main()
{
return 0;
}
Pide la entrada del usuario
Pide al usuario que ingrese dos enteros utilizando la función scanf(). Declara las variables para almacenar estos enteros como int a y int b.
#include<stdio.h>
int main()
{
int a, b;
printf("Enter two integers: ");
scanf("%d%d", &a, &b);
return 0;
}
Realizar operaciones aritméticas básicas sin casteo de tipos
Ahora, vamos a realizar operaciones aritméticas básicas sin casteo de tipos. Declara variables para almacenar los resultados de las operaciones aritméticas como add, subtract, multiply y divide.
#include<stdio.h>
int main()
{
int a, b, add, subtract, multiply;
float divide;
printf("Enter two integers: ");
scanf("%d%d", &a, &b);
add = a + b;
subtract = a - b;
multiply = a * b;
divide = a / b;
printf("Addition of the numbers = %d\n", add);
printf("Subtraction of 2nd number from 1st = %d\n", subtract);
printf("Multiplication of the numbers = %d\n", multiply);
printf("Dividing 1st number from 2nd = %f\n", divide);
return 0;
}
Realizar operaciones aritméticas básicas con casteo de tipos
El lenguaje C maneja el casteo de tipos implícitamente. Sin embargo, también podemos manejarlo explícitamente en nuestros programas. Escribamos un programa en C que realice operaciones aritméticas básicas con casteo de tipos.
Declara variables para almacenar los resultados de las operaciones aritméticas como add, subtract, multiply, divide y remainder.
#include<stdio.h>
int main()
{
int a, b, add, subtract, multiply, remainder;
float divide;
printf("Enter two integers: ");
scanf("%d%d", &a, &b);
add = a + b;
subtract = a - b;
multiply = a * b;
divide = a / (float)b;
remainder = a % b;
printf("Addition of the numbers = %d\n", add);
printf("Subtraction of 2nd number from 1st = %d\n", subtract);
printf("Multiplication of the numbers = %d\n", multiply);
printf("Dividing 1st number from 2nd = %f\n", divide);
printf("Remainder on Dividing 1st number by 2nd is %d\n", remainder);
return 0;
}
Compila y ejecuta el programa
Guarda el archivo main.c. Abre tu terminal en el directorio ~/project/ donde guardaste tu archivo y compila el programa usando el siguiente comando:
gcc -o main main.c
Esto creará un archivo ejecutable llamado main. Ejecuta el programa usando el siguiente comando:
./main
Prueba el programa
Prueba el programa ingresando dos enteros y comprueba si el programa está realizando las operaciones aritméticas como se esperaba.
Código completo
#include<stdio.h>
int main()
{
int a, b, add, subtract, multiply, remainder;
float divide;
printf("Enter two integers: ");
scanf("%d%d", &a, &b);
add = a + b;
subtract = a - b;
multiply = a * b;
divide = a / (float)b;
remainder = a % b;
printf("Addition of the numbers = %d\n", add);
printf("Subtraction of 2nd number from 1st = %d\n", subtract);
printf("Multiplication of the numbers = %d\n", multiply);
printf("Dividing 1st number from 2nd = %f\n", divide);
printf("Remainder on Dividing 1st number by 2nd is %d\n", remainder);
return 0;
}
Resumen
En este laboratorio, hemos aprendido cómo escribir un programa en C para realizar operaciones aritméticas básicas y cómo el lenguaje C maneja el casteo de tipos. Hemos demostrado cómo realizar operaciones aritméticas básicas con y sin casteo de tipos. Finalmente, hemos compilado y ejecutado el programa para probar sus funcionalidades.



