Tema 2: Tipos de datos y conversiones



1. Ingresar un número entero con scanf() y mostrarlo con printf():


                        #include < stdio.h>

                        int main (void)
                        {
                            //To declare variables.
                            int number;

                            //Start the program....
                            printf("\nPlease, write an integer number: ");
                            scanf("%d", &number);

                            printf("The number that has been introduced is: %d\n", number);
                            return 0;
                        }
                    

2. Ingresar un número real y mostrarlo:


                        #include < stdio.h>

                        int main (void)
                        {
                            //To declare variables.
                            float number;

                            //Start the program....
                            printf("\nPlease, write a float number: ");
                            scanf("%f", &number);

                            printf("The number that has been introduced is: %f\n", number);
                            return 0;
                        }
                    

3. Ingresar dos números enteros, dividirlos entre otra variable entera (división de enteros) y mostrar el resultado:


                        #include < stdio.h>

                        int main (void)
                        {
                            //To declare variables.
                            int num1; //Dividend.
                            int num2; //Dividing.
                            int result; //Quotient.

                            //Start the program....
                            printf("\nPlease, introduce the first integer number: ");
                            scanf("%d", &num1);
                            printf("Please, introduce the second integer number: ");
                            scanf("%d", &num2);

                            //Doing the operations....
                            result = num1 / num2;

                            //Printing the results....
                            printf("\n\nThe result of the division is: %d\n", result);
                            return 0;
                        }
                    

4. Ingresar dos números enteros, dividirlos entre una variable real y mostrar el resultado. ¿Sigue siendo una división de enteros, aunque definamos una variable float?:


                        #include < stdio.h>

                        int main (void)
                        {
                            //To declare variables.
                            int num1; //Dividend.
                            int num2; //Dividing.
                            float result; //Quotient.

                            //Start the program....
                            printf("\nPlease, introduce the first integer number: ");
                            scanf("%d", &num1);
                            printf("Please, introduce the second integer number: ");
                            scanf("%d", &num2);

                            //Doing the operations....
                            result = num1 / num2;

                            //Printing the results....
                            printf("\n\nThe float result of the division is: %f\n", result);
                            return 0;
                        }
                    

5. Ingresar dos números enteros, convertir uno de ellos a real ((float)int_no1), dividirlos entre otra variable real y mostrar el resultado:


                        #include < stdio.h>

                        int main (void)
                        {
                            //To declare variables.
                            int num1; //Dividend.
                            int num2; //Dividing.
                            float result; //Quotient.

                            //Start the program....
                            printf("\nPlease, introduce the first integer number: ");
                            scanf("%d", &num1);
                            printf("Please, introduce the second integer number: ");
                            scanf("%d", &num2);

                            //Doing the operations....
                            result = (float)num1 / (float)num2;

                            //Printing the results....
                            printf("\n\nThe float result of the division is: %f\n", result);
                            return 0;
                        }
                    

6. Ingresar una variable real y luego mostrarla como un valor entero (%d):


                        #include < stdio.h>

                        int main (void)
                        {
                            //To declare variables.
                            float num; //A float variable.

                            //Start the program....
                            printf("\nPlease, introduce the float number: ");
                            scanf("%f", &num);

                            //Doing the operations....
                            /* NO OPERATIONS */

                            //Printing the results....
                            printf("\n\nThe float result of the division is: %d\n", (int)num);
                            return 0;
                        }
                    

7. Ingresar un carácter y mostrarlo (%c):


                        #include < stdio.h>

                        int main (void)
                        {
                            //To declare variables.
                            char caracter; //A char variable.

                            //Start the program....
                            printf("\nPlease, introduce a character: ");
                            scanf("%c", &caracter);

                            //Doing the operations....
                            /* NO OPERATIONS */

                            //Printing the results....
                            printf("\n\nThe character is: %c\n", caracter);
                            return 0;
                        }
                    

8. Ingresar un carácter y mostrar su valor ASCII (%d):




                        #include < stdio.h>

                        int main (void)
                        {
                            //To declare variables.
                            char caracter; //A char variable.

                            //Start the program....
                            printf("\nPlease, introduce a character: ");
                            scanf("%c", &caracter);

                            //Doing the operations....
                            /* NO OPERATIONS */

                            //Printing the results....
                            printf("\n\nThe value of the character in its ASCII Table is: %d\n", caracter);
                            return 0;
                        }
                    

9. Introducir un número entero (a), un número real (b) y un carácter (c). Mostrar la variable (a) representada como un valor HEX, la (b) como un valor exponencial y la (c) como un valor de carácter ASCII y su respectivo código:


                        #include < stdio.h>

                        int main (void)
                        {
                            //To declare variables.
                            int a; //Integer number.
                            float b; //Float number.
                            char c; //Character.

                            //Start the program....
                            printf("\nPlease, introduce a integer number: ");
                            scanf("%d", &a);
                            printf("\nPlease, introduce a float number: ");
                            scanf("%f", &b);
                            printf("\nPlease, introduce a character: ");
                            scanf(" %c", &c); //Remind to leave a space because if not, you cannot introduce a char and go on to the next step without the character.

                            //Doing the operations....
                            /* NO OPERATIONS */

                            //Printing the results....
                            printf("\n\nThe value of the integer number in HEX is: %x\n", a);
                            printf("\n\nThe value of the float number in EXP is: %e\n", b);
                            printf("\n\nThe value of the character is: %d\n", c);
                            return 0;
                        }
                    

10. Crear un programa para mostrar los tamaños de las variables en bytes (operador sizeof):


                        #include < stdio.h>

                        void main()
                        {
                            //To declare variables.
                            int a, size; //a is a integer number and size is a integer number that measures the amount of bytes of each variable.
                            float b; //Float number.
                            char c; //Character.
                            double dbl; //Float double number.

                            //Start the program....
                            printf("\nPlease, introduce a integer number: ");
                            scanf("%d", &a);
                            printf("\nPlease, introduce a float number: ");
                            scanf("%f", &b);
                            printf("\nPlease, introduce a character: ");
                            scanf(" %c", &c); //Remind to leave a space because if not, you cannot introduce a char and go on to the next step without the character.
                            printf("\nPlease, introduce a float double number: ");
                            scanf("%lf", &dbl);

                            //Doing the 1� operation....
                            size = sizeof(a);
                            //Printing the result....
                            printf("\n\nThe amount of bytes of the integer number is: %d\n", size);

                            //Doing the 2� operation....
                            size = sizeof(b);
                            //Printing the result....
                            printf("\n\nThe amount of bytes of the float number is: %d\n", size);

                            //Doing the 3� operation....
                            size = sizeof(c);
                            //Printing the result....
                            printf("\n\nThe amount of bytes of the character is: %d\n", size);

                            //Doing the 4� operation....
                            size = sizeof(dbl);
                            //Printing the result....
                            printf("\n\nThe amount of bytes of the double float number is: %d\n", size);
                        }
                    

11. Calcular la cantidad necesaria de fertilizante artificial para una parcela en particular. Para ello, es necesario ingresar las dimensiones de la parcela (ancho y largo) en metros y con decimales. Luego, ingresar la cantidad de fertilizante por hectárea, con decimales. Por último, mostrar la cantidad necesaria de fertilizante para la parcela dada, con decimales:


                        #include < stdio.h>

                        #define HECTARES 10000 //10000 m^2 of hectares.
                        #define WeightOneTon 1000 //The weight of 1 ton is 1000 kg.

                        void main()
                        {
                            //To declare variables.
                            float w, leng, amount_fert, amount_plot, convert_kg; //w = the width of the plot; leng = the length of the plot;
                            //amount_fert = the amount of fertilizer (tons) that it is necessary per hectare (1 ha = 10000 m^2);
                            //amount_plot = the amount that the plot need; convert_kg = the amount in kg.

                            //Start the program....
                            printf("\nPlease, introduce the width of the plot (m): ");
                            scanf("%f", &w);
                            printf("\nPlease, introduce the length of the plot (m): ");
                            scanf("%f", &leng);
                            printf("\nPlease, introduce the amount of fertilizer (tons) that it is necessary per hectare: ");
                            scanf("%f", &amount_fert);

                            //Doing the operations....
                            amount_plot = ((w*leng)*amount_fert) / HECTARES;
                            convert_kg = amount_plot * WeightOneTon;

                            //Printing the result....
                            printf("\nThe amount that the plot need is: %g tons\n", amount_plot);
                            printf("The amount that the plot need is: %g kg\n", convert_kg);
                        }
                    

12. Realizar la conversión del tiempo de días:horas:minutos:segundos a segundos totales. Para ello, es necesario ingresar el número de días, horas, minutos y segundos y luego mostrar el número total de segundos:


                        #include < stdio.h>

                        #define HOURS_DAY 24 //Hours that 1 day has.
                        #define MIN_SEG 60 //Minutes that 1 hour has, or Seconds that 1 min has.

                        void main()
                        {
                            //To declare integer variables of the days, hours, minutes, seconds and the total seconds if you add together the first four.
                            int days, hours, minutes, seconds, total_seconds;

                            //Start the program....
                            printf("\nPlease, introduce the days that you need to introduce: ");
                            scanf("%d", &days);
                            printf("\nPlease, introduce the hours that you need to introduce: ");
                            scanf("%d", &hours);
                            printf("\nPlease, introduce the minutes that you need to introduce: ");
                            scanf("%d", &minutes);
                            printf("\nPlease, introduce the seconds that you need to introduce: ");
                            scanf("%d", &seconds);

                            //Print all of them like a stopwatch.
                            printf("\n\t STOPWATCH : %d:%d:%d:%d \n\n", days, hours, minutes, seconds);

                            //Doing the operations....
                            total_seconds = ((days*HOURS_DAY+hours)*MIN_SEG+minutes)*MIN_SEG+seconds;

                            //Print the total of seconds after adding the four parts of the stopwatch.
                            printf("\n\t STOPWATCH : %d seg \n\n", total_seconds);
                        }
                    

13. Ingresar un número real, mostrar su parte entera (conversión %d) y mostrar su parte decimal (conversión %g):


                        #include < stdio.h>

                        void main()
                        {
                            //To declare float variable for the number and the number only with decimals.
                            float num, decimal_num;

                            //Start the program....
                            printf("\nPlease, introduce a number with decimals: ");
                            scanf("%f", &num);

                            //Doing operations.....
                            decimal_num = num - (int)num;

                            //Print the integer part separtly of the decimal part.
                            printf("\nThe integer part of the introduced number is %d and ", (int)num);
                            printf("the decimal part of the introduced number is %g \n\n", decimal_num);
                        }
                    

14. Ingresar la letra "a" y mostrar su valor ASCII. Después, restar ese valor a 32 y mostrar el valor como un carácter ("%c"):


                        #include < stdio.h>

                        #define SUBTRACTING 32 //The const that must be used for subtracting.

                        void main()
                        {
                            //To declare a character variable that the user has to introduce.
                            //And to declare the integer variable that it represent the value of a character in ASCII.
                            char w = 'a';
                            int value_char;

                            //Start the program.....
                            printf("\nThe character that it is going to read is: %c\n", w);
                            printf("Its ASCII value is: %d\n", (int)w);

                            //Doing operations.....
                            printf("\n Doing the subtract of value of character 'a' - 32.... \n");
                            value_char = (int)w - SUBTRACTING;

                            //Print the integer part separtly of the decimal part.
                            printf("\nThe value of %d correspond with the character: %c\n\n", value_char, (char)value_char);
                        }
                    

15. Ingresar un carácter y mostrar el siguiente carácter de la tabla ASCII:


                        #include < stdio.h>

                        #define NEXT_CHAR 1 //The const that must be used for adding to the next character.

                        void main()
                        {
                            //To declare a character variable that the user has to introduce.
                            //And to declare the integer variable that it represent the value of a character in ASCII.
                            char w;
                            int value_char;

                            //Start the program.....
                            printf("\nPlease, introduce a character: ");
                            scanf("%c", &w);

                            //Doing operations.....
                            printf("\n Doing the add of going on to the next character.... \n");
                            value_char = (int)w + NEXT_CHAR;

                            //Print the integer part separtly of the decimal part.
                            printf("\nThe value of the next character: %d, ", value_char);
                            printf("from the value of the previous character that the user has introduced: %d, ", (int)w);
                            printf("correspond with the character: %c\n\n", (char)value_char);
                        }
                    

16. Calcular el tiempo de vuelo posible para un multirotor. Después, introducir el número de rotores, el consumo de corriente por rotor en mA (miliamperios), el número de baterías y la capacidad de la batería en mAh (miliamperios por hora). Calcular y mostrar el tiempo de vuelo posible en minutos:


                        #include < stdio.h>

                        #define MINUTES 60 //Const of the minutes that there is in 1 hour.

                        void main()
                        {
                            //To declare an integer variable of the rotor numbers, current consumption per rotor in mA, and the number of batteries.
                            //And to declare the float variable of the battery capacity in mAh.
                            int number_rotors, consum_rotor, number_batt; //In mA => the consum_rotor.
                            float batt_capacity, final_min; //In mAh the batt_capacity and the possible flight time in minutes.

                            //Start the program.....
                            printf("\nPlease, introduce the number of rotors that there are: ");
                            scanf("%d", &number_rotors);
                            printf("\nPlease, introduce the consumption per rotor (in mA): ");
                            scanf("%d", &consum_rotor);
                            printf("\nPlease, introduce the number of batteries that there are: ");
                            scanf("%d", &number_batt);
                            printf("\nPlease, introduce the battery capacity (in mAh): ");
                            scanf("%f", &batt_capacity);

                            //Doing operations.....
                            printf("\n\n\t\tDoing the Inversely Proportional Rule of Three....\n");
                            final_min = ((float)number_batt*batt_capacity*MINUTES)/((float)number_rotors*(float)consum_rotor);

                            //Print the integer part separtly of the decimal part.
                            printf("\n\nThe vehicle can fly for %g min.\n\n", final_min);
                        }
                    


Ubicación: Universidad de Novi Sad - Dr Zorana Đinđića 1, Novi Sad 21000, Vojvodina, Serbia

Teléfono: +381 21 485 2056

Correo electrónico: iro.ftn@uns.ac.rs



Copyright © 2023-2030 My Professional WEB - Todos los derechos reservados


Original text
Rate this translation
Your feedback will be used to help improve Google Translate