+ All Categories
Home > Education > Programas c angelwha

Programas c angelwha

Date post: 18-Aug-2015
Category:
Upload: angel-garcia
View: 66 times
Download: 36 times
Share this document with a friend
Popular Tags:
28
PROGRAMAS EN C MIGUEL ANGEL GARCIA WHA CURSO DE MAESTRIA DE ALGORITMOS 2015 Variables and types Exercise 8: first step. Write the C code that prints in the shell the sentence: hello word!. Use the printf function of the <stdio.h> library to print on the screen. In the shell, you will use the gcc compiler to produce an executable code (the instruction man gcc will print the manual of the gcc compiler). CODIGO: #include <stdio.h> #include <stdlib.h> /* Exercise 8: first step. Write the C code that prints in the shell the sentence: “hello word!”. Use the printf function of the <stdio.h> library to print on the screen. In the shell, you will use the gcc compiler to produce an executable code (the instruction man gcc will print the manual of the gcc compiler). */ int main (){ printf("Hello World\n"); system("pause"); return 0; }
Transcript
Page 1: Programas c angelwha

PROGRAMAS EN C MIGUEL ANGEL GARCIA WHA

CURSO DE MAESTRIA DE ALGORITMOS 2015

Variables and types Exercise 8: first step. Write the C code that prints in the shell the sentence: “hello word!”. Use the printf function of the <stdio.h> library to print on the screen. In the shell, you will use the gcc compiler to produce an executable code (the instruction man gcc will print the manual of the gcc compiler). CODIGO: #include <stdio.h> #include <stdlib.h> /* Exercise 8: first step. Write the C code that prints in the shell the sentence: “hello word!”. Use the printf function of the <stdio.h> library to print on the screen. In the shell, you will use the gcc compiler to produce an executable code (the instruction man gcc will print the manual of the gcc compiler). */ int main (){ printf("Hello World\n"); system("pause"); return 0; }

Page 2: Programas c angelwha

SALIDA DEL CODIGO:

Page 3: Programas c angelwha

Exercise 9: reading from the keyboard. Write a C code that asks the user to supply to integer numbers and then prints the two numbers and their sum on the shell. You will use the scanf function to read the two numbers. CODIGO: #include <stdio.h> #include <stdlib.h> /* Exercise 9: reading from the keyboard. Write a C code that asks the user to supply to integer numbers and then prints the two numbers and their sum on the shell. You will use the scanf function to read the two numbers. */ int main(){ printf("***Tarea Programa 9 Angel Wha***\n\n"); int n1=0,n2=0,suma=0; printf("Ingrese el 1 numero:\n"); scanf("%d",&n1); printf("Ingrese el 2 numero:\n"); scanf("%d",&n2); suma = n1+n2; printf("La suma de ambos numeros es:----->%d\n\n",suma); //scanf("%d",&suma); system("pause"); return 0; }

Page 4: Programas c angelwha

SALIDA DEL CODIGO:

Page 5: Programas c angelwha

Exercise 10: divisions Starting from the following declarations int a=2,b=3,c; float x=5.0,y; Predict the outcome of the operations y=a*b; c=a*b; y=a/b; c=a/b; y=a/b*x; c=a/b*x; y=a*x/b; c=a*x/b; Write the corresponding C code to check your predictions. You will use the printf command (<stdio.h>) to output the results on the screen. Note that you will have to use %i to print an integer and %f to print a float. CODIGO: #include <stdio.h> #include <stdlib.h> /* Exercise 10: divisions Starting from the following declarations int a=2,b=3,c; float x=5.0,y; Predict the outcome of the operations y=a*b; c=a*b; y=a/b; c=a/b; y=a/b*x; c=a/b*x; y=a*x/b; c=a*x/b; Write the corresponding C code to check your predictions. You will use the printf command (<stdio.h>) to output the results */ int main (){ printf("***Programa 3 Angel Wha***\n\n"); int a=2,b=3,c; float x=0.5,y;

Page 6: Programas c angelwha

//////////// y=a*b; printf("la multiplicacion de y=a*b es:%f\n\n",y); ///////////// c=a*b; printf("la multiplicacion de c=a*b es:%i\n\n",c); /////////// y=a/b; printf("la division de y=a/b es:%f\n\n",y); //////////// c=a/b; printf("la division de c=a/b es:%i\n\n",c); /////////// y=a/b*x; printf("la division entre la multiplicacion es de y=a/b*x es:%f\n\n",y); //////////// c=a/b*x; printf("la division entre la multiplicacion es de c=a/b*x es:%i\n\n",c); /////////// y=a*x/b; printf("la multiplicacion entre la division es de y=a*x/b es:%f\n\n",y); //////////// c=a*x/b; printf("la multiplicacion entre la division es de c=a*x/b es:%i\n\n",c); system("pause"); return 0; }

Page 7: Programas c angelwha

SALIDA DEL CODIGO:

Exercise 12: time conversion Given a time in seconds (integer), print to the screen the corresponding time in hours, minutes and seconds. The output will be formatted like: “XXXX seconds is equivalent to XX hours, XX minutes and XX seconds”. CODIGO: #include <stdio.h> #include <stdlib.h> /* Exercise 12: time conversion Given a time in seconds (integer), print to the screen the corresponding time in hours, minutes and seconds. The output will be formatted like: “XXXX seconds is equivalent to XX hours, XX minutes and XX seconds”. */ int main (){ printf("***PROGRAMA 4 ANGEL WHA***\n\n"); int hrs,mins,segs,segs2,a,b; printf("Ingresa los segundos para calcular la hora,minutos y segundos:\n");

Page 8: Programas c angelwha

scanf("%d",&segs); hrs = (int)(segs/3600); mins = (int)(segs-hrs*3600)/60; segs2 = segs - (hrs*3600+mins*60); printf("La hora,minutos y segundos es: %dhora %dminutos %dsegundos\n\n",hrs,mins,segs2); system("pause"); return 0; } SALIDA DEL CODIGO:

Exercise 13: constants and pi Write a C code that computes the surface of a disk given its radius (use scanf). The number pi will be declared as a constant in the file. In a second step, you will call the <math.h> library, which defines pi. Look where the library is located on the disk then find how pi is defined (use the UNIX command locate). Modify your code to use this library instead of the constant defined in the previous version.

Page 9: Programas c angelwha

CODIGO: #include <stdio.h> #include <stdlib.h> #include <math.h> const float pi = 33.14159265358979323846; /* Exercise 13: constants and pi Write a C code that computes the surface of a disk given its radius (use scanf). The number pi will be declared as a constant in the file. In a second step, you will call the <math.h> library, which defines pi. Look where the library is located on the disk then find how pi is defined (use the UNIX command locate). Modify your code to use this library instead of the constant defined in the previous version. */ int main(){ printf("***Programa Tarea 5 Angel Wha***\n\n"); printf("El pi es: %f\n",pi); //scanf("%c",&PI); system("pause"); return 0; }

Page 10: Programas c angelwha

SALIDA DEL CODIGO:

Exercise 14: switch Write a C code that switches the values of two variables A and B and prints the result on the screen. How many variables do you need? CODIGO: #include <stdio.h> #include <stdlib.h> /* Exercise 14: switch Write a C code that switches the values of two variables A and B and prints the result on the screen. How many variables do you need? */ int main (){ printf("Exercise 14: switch\n" "Write a C code that switches the values of two variables\n" "A and B and prints the result on the\n" "screen. How many variables do you need?\n\n"); //declaracion de variables /* int a,b,x,y;

Page 11: Programas c angelwha

a = 80; b =1800; x=100; y=3737; printf("Los valores principales de a y b son:------>%d %d\n\n", a,b); if(a && b){ a=0; b=0; x += a; y += b; printf("Los nuevos valores de a y b son:----->%d %d\n\n",x,y); } */ /* //// POR TECLADO int a,b,x,y; //a printf("Ingresa el valor de a:\n"); scanf("%d",&a); //b printf("Ingresa el valor de b:\n"); scanf("%d",&b); printf("Los valores principales de a y b son:------>%d %d\n\n", a,b); printf("//////////////////////////////////////////\n\n"); if(a && b){ a = 0, b=0; //x printf("Ingresa el nuevo valor de a:\n"); scanf("%d",&x); //y

Page 12: Programas c angelwha

printf("Ingresa el nuevo valor de b:\n"); scanf("%d",&y); x += a; y += b; printf("Los nuevos valores de a y b son:----->%d %d\n\n",x,y); } */ int a,b,x,y; //a printf("Ingresa el valor de a:\n"); scanf("%d",&a); //b printf("Ingresa el valor de b:\n"); scanf("%d",&b); printf("Los valores de a y b son:------>%d %d\n\n",b,a); system("pause"); return 0; } SALIDA DEL CODIGO:

Page 13: Programas c angelwha

Exercise 15: addition and memory Write a C code that sums 4 integer numbers (entered using scanf) using: 1) 5 variables (numbers are kept in variables) 2) 2 variables (no memory of the entries kept) CODIGO: #include <stdio.h> #include <stdlib.h> /* Exercise 15: addition and memory Write a C code that sums 4 integer numbers (entered using scanf) using: 1) 5 variables (numbers are kept in variables) 2) 2 variables (no memory of the entries kept) */ int main(){ printf("***Tarea Programa 7 Angel Wha***\n\n"); int n1=0,n2=0,n3=0,n4=0,suma=0; printf("Ingrese el numero 1:\n"); scanf("%d",&n1); printf("Ingrese el numero 2:\n"); scanf("%d",&n2); printf("Ingrese el numero 3:\n"); scanf("%d",&n3); printf("Ingrese el numero 4:\n"); scanf("%d",&n4); suma = n1+n2+n3+n4; printf("La suma de los 4 numeros es:-----> %d\n",suma); system("pause"); return 0; }

Page 14: Programas c angelwha

SALIDA DEL CODIGO:

Exercise 16: distance Write a C code that calculates the distance between two points whose coordinates in the [X,Y] plane are read from the keyboard as integer. CODIGO: #include <stdio.h> #include <stdlib.h> /* Exercise 16: distance Write a C code that calculates the distance between two points whose coordinates in the [X,Y] plane are read from the keyboard as integer. */ int main(){ printf("***Tarea Programa 8 Angel Wha***\n\n"); int x1=0,x2=0,x3=0,x4=0,distancia=0; printf("Ingrese el punto 1:\n"); scanf("%d",&x1); printf("Ingrese el punto 2:\n");

Page 15: Programas c angelwha

scanf("%d",&x2); printf("\n"); //La función sqrt retorna el resultado de la raíz cuadrada de x. distancia = sqrt((x1-x2)*(x1-x2)); printf("el valor de la distancia entre los dos puntos es:---->%d\n\n",distancia); system("pause"); return 0; } SALIDA DEL CODIGO:

Page 16: Programas c angelwha

Handling conditions Exercise 17: XOR Write the C code that corresponds to exercise 3 and test it. CODIGO: #include <stdio.h> #include <stdlib.h> /* Exercise 3: XOR Write the flowchart of the XOR logical gate defined by X Y X XOR Y 0 0 0 0 1 1 1 0 1 1 1 0 Exercise 17: XOR Write the C code that corresponds to exercise 3 and test it. */ int main(){ printf("Exercise 3: XOR\n" "Write the flowchart of the XOR logical gate defined by\n" "X Y X XOR Y\n" "0 0 0\n" "0 1 1\n" "1 0 1\n" "1 1 0\n" "Exercise 17: XOR\n" "Write the C code that corresponds to exercise 3 and test it.\n\n"); int x1,x2,x3,x4,x5,x6; system("pause"); return 0; }

Page 17: Programas c angelwha

SALIDA DEL CODIGO:

Exercise 19: maximum of three numbers Write a program that reads three integers (A, B and C) and prints the largest of the values using: 1) the if-else structure and a temporary variable 2) if-else structures without a temporary variable 3) conditional operations CODIGO: #include <stdio.h> #include <stdlib.h> /* Exercise 19: maximum of three numbers Write a program that reads three integers (A, B and C) and prints the largest of the values using: 1) the if-else structure and a temporary variable 2) if-else structures without a temporary variable 3) conditional operations */ int main () {

Page 18: Programas c angelwha

printf("***Programa Angel Wha***\n\n"); int n1,n2,n3,resultado; printf("Ingrese el 1 numero:\n"); scanf("%d",&n1); printf("Ingrese el 2 numero:\n"); scanf("%d",&n2); printf("Ingrese el 3 numero:\n"); scanf("%d",&n3); if(n1>n2 && n1>n3){ printf("El numero mayor es: numero1 %d\n",n1); }else{ if(n2>n1 && n2>n3){ printf("El numero mayor es: numero2 %d\n",n2); }else{ if(n3>n1 && n3>n2){ printf("El numero mayor es: numero3 %d\n",n3); } } } system("pause"); return 0; }

Page 19: Programas c angelwha

SALIDA DEL CODIGO:

Exercise 20: sign of a multiplication Write a C code that returns the sign of a multiplication of A and B without doing the multiplication. CODIGO: #include <stdio.h> #include <stdlib.h> /* Exercise 20: sign of a multiplication Write a C code that returns the sign of a multiplication of A and B without doing the multiplication. */ int main () { int a,b; int m=0; while ( printf("Escribe el numero1 y numero2:\n"), scanf("%d %d",&a,&b) ) { m=0;

Page 20: Programas c angelwha

while (a) { if (a&2) { if (a&1) m+=b+b+b; else m+=b+b; } else if(a&1) m+=b; a>>=2; b<<=2; //printf("%d,%d,%d\n",a,b,m); //para ver valores intermedios } printf("La multiplicacion de ambos numeros es:---->%d\n\n",m); system("pause"); return 0; } } SALIDA DEL CODIGO:

Page 21: Programas c angelwha

Exercise 21: parity Write the C code that returns the parity of a number. Define the proper mathematical test and the associated C condition. The number will be read from the keyboard using the scanf function of the <stdio.h> library. CODIGO: #include <stdio.h> #include <stdlib.h> int main(){ printf("Exercise 21: parity\n" "Write the C code that returns the parity of a number.\n" "Define the proper mathematical test and\n" "the associated C condition.\n" "The number will be read from the keyboard using the scanf\n" "function of the <stdio.h> library.\n\n"); int numero; printf("Ingresa el numero:\n"); scanf("%d",&numero); if(numero%2==0){ printf("Tu numero es par\n"); }else{ printf("Tu numero no es par lo cual es impar\n"); } system("pause"); return 0; }

Page 22: Programas c angelwha

SALIDA DEL CODIGO:

Exercise 22: sign of Using the if-else condition then the switch condition and finally the ternary operator, write a C code that prints the sign of an integer. Sign returns 1 for a positive number, -1 for a negative number and 0 if the value is 0. CODIGO: #include <stdio.h> #include <stdlib.h> int main(){ printf("Exercise 22: sign of\n" "Using the if-else condition then the switch condition\n" "and finally the ternary operator, write a C\n" "code that prints the sign of an integer.\n" "Sign returns 1 for a positive number, -1 for a negative\n" "number and 0 if the value is 0.\n\n"); float num; printf("Ingresa el numero:\n"); scanf("%f",&num);

Page 23: Programas c angelwha

if (num<=0) { if (num==0) printf("tu numero es un cero %.0f\n",num); else printf("tu numero es negativo %.2f\n",num); } else printf("tu numero es positivo %.2f\n",num); system("pause"); return 0; } SALIDA DEL CODIGO:

Page 24: Programas c angelwha

Using loops Exercise 24: prime numbers Write a program that tests if a positive integer is a prime number using a straightforward approach. You will assume that your number is smaller than 1000. CODIGO: #include <stdio.h> #include <stdlib.h> /* Exercise 24: prime numbers Write a program that tests if a positive integer is a prime number using a straightforward approach. You will assume that your number is smaller than 1000. */ int main (){ printf("***Programa 24 Tarea Angel Wha***\n\n"); int numero,i,x; x=0; /* x=0; Esta variable la vamos a usar para contar los divisores del número introducido, la inicializamos a 0*/ printf("Ingresa el numero:\n"); scanf("%d",&numero); for(i=1;i<=numero;i++){ if(numero%i==0){ x++; } } if(x==2){ printf("El numero es primo\n\n"); }else{

Page 25: Programas c angelwha

printf("El numero no es primo\n\n"); } system("pause"); return 0; } SALIDA DEL CODIGO:

Exercise 25: factorial Write a C code that computes the factorial of an integer n. When does the code fail (upper limit on n for the result to be correct) and why? CODIGO: #include<stdlib.h> #include<stdio.h> /* Exercise 25: factorial Write a C code that computes the factorial of an integer n. When does the code fail (upper limit on n for the result to be correct) and why? */ int main()

Page 26: Programas c angelwha

{ int num,fact=1,i; printf("Introduce un numero:\n"); scanf("%d",&num); for(i=2; i<=num; i++) { fact = fact * i; } printf("El factorial del numero que ingresastes es:------>%d\n",fact); system("pause"); return 0; } SALIDA DEL CODIGO:

Page 27: Programas c angelwha

Exercise 26: xn

Write the C code that computes xn using the three loop functions (for, while and do-while). We will consider n to be an integer. CODIGO: #include <stdio.h> #include <stdlib.h> /* Exercise 26: x n Write the C code that computes x n using the three loop functions (for, while and do-while). We will consider n to be an integer. */ int main () { int Base, Expo, i; float Potencia=1; printf ("Introduce el numero a elevar:\n"); scanf ("%d", &Base); printf ("Introduce la potencia del numero que ingresastes:\n"); scanf ("%d", &Expo); for (i=1; i<=abs(Expo); i++){ Potencia=Potencia*Base; } while(Expo<0){ Potencia=1./Potencia; } printf ("El numero resultante es----->: %.2f\n\n", Potencia); system ("pause"); return 0; }

Page 28: Programas c angelwha

SALIDA DEL CODIGO:


Recommended