Last active
September 5, 2015 20:13
-
-
Save joaquini/bc0b1cab2ecdb4e75e2e to your computer and use it in GitHub Desktop.
Practico A de Programacion en C para OC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//ORGANIZACION DE COMPUTADORAS | |
//PRACTICO DE PROGRAMACION A | |
//EJERCICIO 1 | |
int main (){ | |
int i; | |
int sumat = 0; | |
int product = 1; | |
for (i = 1; i<=10; i++){ | |
sumat += (i * 2); | |
product *= (i * 2 - 1); | |
} | |
printf("Resultado sumatoria: %d\n", sumat); | |
printf("Resultado productoria: %d\n", product); | |
return 0; | |
} | |
//EJERCICIO 2 | |
int main (){ | |
int input; | |
printf("Ingrese un numero: "); | |
scanf("%d", &input); | |
//the above statement says ==> read %d (integer) type of data into &input address | |
int i; | |
int sumat = 0; | |
for (i = 1; i <= input; i++){ | |
sumat += (i); | |
} | |
printf("El resultado de la sumatoria desde 1 hasta %d es %d\n", input, sumat); | |
return 0; | |
} | |
//EJERCICIO 3A | |
int main (){ | |
float input; | |
printf("BIENVENIDO AL CONVERSOR CELSIUS A FAHRENHEIT \n\n\n"); | |
printf("Ingrese un numero en Celsius: "); | |
scanf("%f", &input); | |
//the above statement says ==> read %f (float) type of data into &input address | |
float result = input * 9/5 + 32; | |
printf("%.1f°C equivalen a %.1f°F", input, result); | |
return 0; | |
} | |
//EJERCICIO 3B | |
int main (){ | |
float input; | |
printf("Bienvenido a conversor de Yardas a Metros \n\n\n"); | |
printf("Ingrese un numero en yardas: "); | |
scanf("%f", &input); | |
//the above statement says ==> read %f (float) type of data into &input address | |
float result = input / 1.0936; | |
printf("%.2f yardas equivalen a %.2f metros", input, result); | |
return 0; | |
} | |
//EJERCICIO 3C | |
int main (){ | |
float input; | |
printf("Bienvenido a conversor de MPH a Km/h \n\n\n"); | |
printf("Ingrese un numero en MPH: "); | |
scanf("%f", &input); | |
//the above statement says ==> read %f (float) type of data into &input address | |
float result = input * 1.6; | |
printf("%.2f MPH equivalen a %.2f Km/h", input, result); | |
return 0; | |
} | |
//EJERCICIO 3D | |
int main (){ | |
float input; | |
printf("Bienvenido a conversor de litros cada 100km a millas por galeon \n\n\n"); | |
printf("Ingrese un numero de litros cada 100km: "); | |
scanf("%f", &input); | |
//the above statement says ==> read %f (float) type of data into &input address | |
float result = input * 6.2137 / 3.7854; | |
printf("%.2f litros cada 100km equivalen a %.2f millas por galeon", input, result); | |
return 0; | |
} | |
//EJERCICIO 4 | |
float celsiusToFahrenheit(float c); | |
float yardsToMeters(float y); | |
float mphToKph(float mph); | |
float litres100ToMpg(float litres); | |
int main (){ | |
int inputOpcion; | |
float input; | |
printf("Bienvenido al Conversor \n\n"); | |
printf("1 - Celsius a Fahrenheit\n"); | |
printf("2 - Yardas a metros\n"); | |
printf("3 - MPH a km/h\n"); | |
printf("4 - L/100km a MPG\n"); | |
printf("Ingresa el numero de la opcion deseada y presiona Enter:\n"); | |
scanf("%d", &inputOpcion); | |
switch(inputOpcion){ | |
case 1: | |
printf("Ingrese un numero en Celsius: "); | |
scanf("%f", &input); | |
printf("\n%.1f°C equivalen a %.1f°F \n", input, celsiusToFahrenheit(input)); | |
break; | |
case 2: | |
printf("Ingrese un numero en Yardas: "); | |
scanf("%f", &input); | |
printf("%.2f yardas equivalen a %.2f metros", input, celsiusToFahrenheit(input)); | |
break; | |
case 3: | |
printf("Ingrese un numero en MPH: "); | |
scanf("%f", &input); | |
printf("%.2f MPH equivalen a %.2f Km/h", input, celsiusToFahrenheit(input)); | |
break; | |
case 4: | |
printf("Ingrese un numero de litros cada 100km: "); | |
scanf("%f", &input); | |
printf("%.2f litros cada 100km equivalen a %.2f millas por galeon", input, celsiusToFahrenheit(input)); | |
break; | |
} | |
return 0; | |
} | |
float celsiusToFahrenheit(float c){ | |
return c * 9/5 + 32; | |
} | |
float yardsToMeters(float y){ | |
return y / 1.0936; | |
} | |
float mphToKph(float mph){ | |
return mph * 1.6; | |
} | |
float litres100ToMpg(float litres){ | |
return litres * 6.2137 / 3.7854; | |
} | |
//EJERCICIO 5 | |
float sumar(float a, float b); | |
float restar(float a, float b); | |
float multiplicar(float a, float b); | |
float dividir(float a, float b); | |
int main (){ | |
int inputOpcion; | |
float input1, input2; | |
printf("Bienvenido a la calculadora! \n\n"); | |
printf("1 - Sumar\n"); | |
printf("2 - Restar\n"); | |
printf("3 - Multiplicar\n"); | |
printf("4 - Dividir\n"); | |
printf("Ingresa el numero de la operacion deseada y presiona Enter:\n"); | |
scanf("%d", &inputOpcion); | |
printf("Ingresa dos numeros separados por un espacio:\n"); | |
scanf("%f %f", &input1, &input2); | |
switch(inputOpcion){ | |
case 1: | |
printf("\n %.2f + %.2f = %.2f \n", input1, input2, sumar(input1, input2)); | |
break; | |
case 2: | |
printf("\n %.2f - %.2f = %.2f \n", input1, input2, restar(input1, input2)); | |
break; | |
case 3: | |
printf("\n %.2f * %.2f = %.2f \n", input1, input2, multiplicar(input1, input2)); | |
break; | |
case 4: | |
printf("\n %.2f / %.2f = %.2f \n", input1, input2, dividir(input1, input2)); | |
break; | |
} | |
return 0; | |
} | |
float sumar(float a, float b){ | |
return a + b; | |
} | |
float restar(float a, float b){ | |
return a - b; | |
} | |
float multiplicar(float a, float b){ | |
return a * b; | |
} | |
float dividir(float a, float b){ | |
return a / b; | |
} | |
//EJERCICIO 6 | |
void imprimirTabla(int num); | |
void imprimirLinea(int i, int num); | |
int multiplicar(int a, int b); | |
int main (){ | |
int input; | |
printf("Bienvenido al generador de tablas de multiplicar! \n\n"); | |
printf("Ingresa un numero entero y presiona Enter:\n"); | |
scanf("%d", &input); | |
imprimirTabla(input); | |
return 0; | |
} | |
void imprimirTabla(int num){ | |
printf("Tabla del %d", num); | |
int i; | |
for (i = 0; i <= 20; i++){ | |
imprimirLinea(i, num); | |
} | |
} | |
void imprimirLinea(int i, int num){ | |
printf("%d x %d = %d\n", num, i, multiplicar(i, num)); | |
} | |
int multiplicar (int a, int b){ | |
return a * b; | |
} | |
//EJERCICIO 7 | |
int main (){ | |
int cantidadDeseada; | |
printf("Bienvenido a generador de promedios! \n\n"); | |
printf("Decime por favor cuantos numeros vas a ingresar: \n"); | |
scanf("%d", &cantidadDeseada); | |
printf("Escribi los %d numeros, separados por espacios. Al finalizar presiona Enter! \n", cantidadDeseada); | |
int cantLeidos; | |
long actualLeido = 0; | |
long sumaLeidos = 0; | |
long mayorLeido = 0; | |
long menorLeido = 0; | |
scanf("%ld", &actualLeido); | |
mayorLeido = actualLeido; | |
menorLeido = actualLeido; | |
sumaLeidos += actualLeido; | |
for (cantLeidos = 1; cantLeidos < cantidadDeseada; cantLeidos++){ | |
scanf("%ld", &actualLeido); | |
sumaLeidos += actualLeido; | |
if (actualLeido < menorLeido) | |
menorLeido = actualLeido; | |
if (actualLeido > mayorLeido) | |
mayorLeido = actualLeido; | |
} | |
float promedio = sumaLeidos/cantidadDeseada; | |
printf("Menor: %ld - Mayor: %ld - Promedio: %.2f", menorLeido, mayorLeido, promedio); | |
return 0; | |
} | |
//EJERCICIO 8 | |
int convertirBinarioADecimal(int binario); | |
int expo(int base, int exp); | |
int main () | |
{ | |
int numBinario; | |
printf("BIENVENIDO A CONVERSOR BINARIO A DECIMAL! \n\n"); | |
printf("Numero en binario: "); | |
scanf("%ld", &numBinario); | |
printf("\n\nEl numero binario %d representa al numero decimal %d", numBinario, convertirBinarioADecimal(numBinario)); | |
return 0; | |
} | |
int convertirBinarioADecimal(int binario){ | |
int pos = 0; | |
int numLeido; | |
int suma = 0; | |
while (binario > 0){ | |
numLeido = binario % 10; | |
binario = binario / 10; | |
suma += numLeido * expo(2, pos++); | |
} | |
return suma; | |
}; | |
int expo(int base, int exp){ | |
if (exp == 0) | |
return 1; | |
else return base * expo(base, exp-1); | |
} | |
//EJERCICIO 9 | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
char nrohexa[10]; | |
printf("Ingrese numero en hexadecimal: "); | |
scanf("%s",nrohexa); | |
int i, nrodecimal=0; | |
for(i=0;(nrohexa[i]!='\n' & nrohexa[i]!='.');i++) { | |
if(nrohexa[i]>='0' && nrohexa[i]<='9') | |
nrodecimal=nrodecimal*16+(nrohexa[i]-'0'); | |
else if (nrohexa[i]=='a' || nrohexa[i]=='A') nrodecimal=nrodecimal*16+10; | |
else if (nrohexa[i]=='b' || nrohexa[i]=='B') nrodecimal=nrodecimal*16+11; | |
else if (nrohexa[i]=='c' || nrohexa[i]=='C') nrodecimal=nrodecimal*16+12; | |
else if (nrohexa[i]=='d' || nrohexa[i]=='D') nrodecimal=nrodecimal*16+13; | |
else if (nrohexa[i]=='e' || nrohexa[i]=='E') nrodecimal=nrodecimal*16+14; | |
else if (nrohexa[i]=='f' || nrohexa[i]=='F') nrodecimal=nrodecimal*16+15; | |
} | |
printf("\nEl numero hexadecimal %s en decimal es: %d",nrohexa,nrodecimal); | |
return 0; | |
} | |
//EJERCICIO 10A | |
#include <stdio.h> | |
int contarCifras(int input); | |
int main () | |
{ | |
printf("Bienvenido a contador recursivo de digitos!\n"); | |
printf("Ingrese un numero y presione enter: "); | |
int input; | |
scanf("%d", &input); | |
printf("El numero %d tiene %d cifras!", input, contarCifras(input)); | |
} | |
int contarCifras(int input){ | |
if (input < 10) | |
return 1; | |
else return 1 + contarCifras(input / 10); | |
} | |
//EJERCICIO 10B | |
#include <stdio.h> | |
int contarApariciones(int digito, int numero); | |
int main () | |
{ | |
printf("Bienvenido a contador recursivo de digitos en un numero!\n"); | |
printf("Ingrese un numero y presione enter: "); | |
int numero; | |
scanf("%d", &numero); | |
int digito; | |
printf("Ingrese un digito y presione enter: "); | |
scanf("%d", &digito); | |
printf("El digito %d aparece %d veces en el numero %d!", digito, contarApariciones(digito, numero), numero); | |
return 0; | |
} | |
int contarApariciones(int digito, int numero){ | |
if (numero < 10){ | |
if (numero == digito) | |
return 1; | |
else | |
return 0; | |
} | |
int ultimoDigito = numero % 10; | |
if (ultimoDigito == digito) | |
return 1 + contarApariciones(digito, numero/10); | |
else | |
return contarApariciones(digito, numero/10); | |
} | |
//EJERCICIO 10C | |
#include <stdio.h> | |
int invertir(int numero, int cant); | |
int expo (int base, int exp); | |
int cantDigitos (int numero); | |
int main() | |
{ | |
int numero; | |
printf("Bienvenido a inversor recursivo de numeros!\n\n"); | |
printf("Ingrese un numero: "); | |
scanf("%d", &numero); | |
printf("El numero %d invertido es %d", numero, invertir(numero, cantDigitos(numero) - 1)); | |
return 0; | |
} | |
int invertir (int numero, int cant){ | |
if (numero < 10) | |
return numero; //Ya esta invertido | |
else{ | |
int ultimoDigito = numero % 10; | |
return ultimoDigito * expo(10, cant) + invertir(numero / 10, --cant); | |
} | |
} | |
int expo (int base, int exp){ | |
if (exp == 0) | |
return 1; | |
else return base * expo(base, exp - 1); | |
} | |
int cantDigitos (int numero){ | |
if (numero < 10) | |
return 1; | |
else return 1 + cantDigitos(numero /10); | |
} | |
//EJERCICIO 10D | |
#include <stdio.h> | |
int contarParEnImpar(int numero, int pos); | |
int esPar(int numero); | |
int cantDigitos(int numero); | |
int main() | |
{ | |
int numero; | |
printf("Bienvenido a contador de digitos pares en posiciones impares!\n\n"); | |
printf("Ingrese un numero: "); | |
scanf("%d", &numero); | |
printf("El numero %d tiene %d digitos pares en posiciones impares", numero, contarParEnImpar(numero, cantDigitos(numero)+1)); | |
return 0; | |
} | |
int contarParEnImpar(int numero, int pos){ | |
int ultimoDigito = numero % 10; | |
if (numero < 10) { | |
if (esPar(numero)) | |
return 0; | |
else | |
return 1; | |
} | |
else { | |
if (esPar(ultimoDigito) && !esPar(pos)) | |
return 1 + contarParEnImpar(numero/10, pos--); | |
else | |
contarParEnImpar(numero/10, pos--); | |
} | |
} | |
int esPar(int numero){ | |
if (numero % 2 == 0) | |
return 0; | |
else | |
return 1; | |
} | |
int cantDigitos(int numero){ | |
if (numero < 10) | |
return 1; | |
else | |
return 1 + cantDigitos(numero/10); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment