Ejercicios de Programacion C

Post on 08-Nov-2014

163 views 4 download

Tags:

Transcript of Ejercicios de Programacion C

1

sistema educativo universitario aztecaInstituto de estudios superiores “sor Juana Inés de la cruz”

plantel Tapilula

EJERCICIOS DE PROGRAMACIÓN EN LENGUAJE “C”

/*CALCULA EL DIA QUE LE CORRESPONDE UNA FECHA*/

DOCENTE: LSC CONCEPCIÓN LAGUNAS RODRÍGUEZ

1

sistema educativo universitario aztecaInstituto de estudios superiores “sor Juana Inés de la cruz”

plantel Tapilula

EJERCICIOS DE PROGRAMACIÓN EN LENGUAJE “C”

/*CALCULA EL DIA QUE LE CORRESPONDE UNA FECHA*/#include <stdio.h>#include<conio.h> #include <stdlib.h> #include<dos.h> #include <string.h> main () { int mes, dt, r, dia; char nmes [15] ,ndia [10]; clrscr () gotoxy (10,5); printf (“indicar el número del mes:”);scanf(“%d”,&mes);gotoxy (10,6);printf(“indicar el dia:”); scanf(“%d”,&dia); switch(mes)

{ case 1:{strcpy(nmes,”enero”); dt=0;break;} case 2:{strcpy(nmes, “febrero”); dt=31;break} case 3:{strcpy(nmes, “marzo” ); dt=59; break;} case 4:{strcpy(nmes, “abril “ ); dt=90;break;} case 5:{strcpy(nmes, “mayo” ); dt=190:break;} case 6:{strcpy(nmes, “junio”); dt=151;break;} case 7:{strcpy(nmes, “julio”); dt=181;break;}case 8:{strcpy(nmes, “agosto”); dt=212;break;}case 9:{strcpy(nmes, “septiembre”); dt=243;break;} case10:{strcpy(nmes, “octubre”); dt=273;break;}case 11:{strcpy(nmes, “noviembre”): dt=304;break;}case 12:{strcpy(nmes , “diciembre”) dt=334;break;}

}r= (dt+dia) %7);switch(r){ case 0: {strcpy(ndia, “martes”); break;}

case 1: {strcpy(ndia, “miercoles”); break;}case 2: {strcpy(ndia, “jueves”); break;}case 3: {strcpy(ndia, “viernes”); break;}case 4: {strcpy(ndia, “sabado”); break;}case 5: {strcpy(ndia, “domingo”); break;}case 6: {strcpy(ndia, “lunes”); break;}

}printf(“\n %-15s: %s”, nmes, ndia);gotoxy(10,22); printf(“pulsar cualquier tecla para continuar”);getch();}

DOCENTE: LSC CONCEPCIÓN LAGUNAS RODRÍGUEZ

1

sistema educativo universitario aztecaInstituto de estudios superiores “sor Juana Inés de la cruz”

plantel Tapilula

EJERCICIOS DE PROGRAMACIÓN EN LENGUAJE “C”

/* IDENTIFICA SI ES NUMERO PRIMO O NO */

#include <stdio.h>#include<conio.h> #include <stdlib.h> #include<dos.h> #include <string.h> main () {

int c, x, r;clscr ();gotoxy (28,5); printf (“introduce el numero”)scanf(“%d”, &x);c= 2;r= 1;while ( (c<x) && (r!=0) )

{r= x%c;if (r!=0)c= c+1;

}if(r!=0){

gotoxy(20,15);printf(“es un numero primo”);

}else{

gotoxy(20,15);printf(“ no es un numero primo”);

}getch();}

DOCENTE: LSC CONCEPCIÓN LAGUNAS RODRÍGUEZ

1

sistema educativo universitario aztecaInstituto de estudios superiores “sor Juana Inés de la cruz”

plantel Tapilula

EJERCICIOS DE PROGRAMACIÓN EN LENGUAJE “C”

CALCULO DE PAGO

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<dos.h>#include<string.h>main(){float p,c,t;clrscr();gotoxy(10,8);printf("indica el valor de p");scanf("%f",&p);gotoxy(10,9);printf("indica el valor de c");scanf("%f",&c);if(c<11)t=(p*.95)*c;elseif(c<21)t=(p*.90)*c;elseif(c<36)t=(p*.85)*c;elset=(p*.75)*c;printf("\n total a pagar %f",t);gotoxy(18,36);printf("pulsa una tecla");getch();}

DOCENTE: LSC CONCEPCIÓN LAGUNAS RODRÍGUEZ

for(f=1;f<=4;f=f+1){gotoxy(c*5,f*2);printf("%d",x[f][c]);}}getch();}

1

sistema educativo universitario aztecaInstituto de estudios superiores “sor Juana Inés de la cruz”

plantel Tapilula

EJERCICIOS DE PROGRAMACIÓN EN LENGUAJE “C”

MEMORAMA, IMPRIME 2 VECES UN MISMO NUMERO#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<dos.h>#include<string.h>int x[5][5];main(){int i, b, f, c, y, fil, col;clrscr();srand(time(NULL));for(i=1;i<=6;i=i+1){b=1;while(b==1){y=rand()%51;b=0;for(f=1;f<=4;f=f+1){for(c=1;c<=3;c=c+1){if(y==x[f][c])b=1;}}{do{fil=(rand()%4)+1;col=(rand()%3)+1;}while(x[fil][col]!=0);x[fil][col]=y;do{fil=(rand()%4)+1;col=(rand()%3)+1;}while(x[fil][col]!=0);x[fil][col]=y;}

DOCENTE: LSC CONCEPCIÓN LAGUNAS RODRÍGUEZ

1

sistema educativo universitario aztecaInstituto de estudios superiores “sor Juana Inés de la cruz”

plantel Tapilula

EJERCICIOS DE PROGRAMACIÓN EN LENGUAJE “C”

CARRERA

DOCENTE: LSC CONCEPCIÓN LAGUNAS RODRÍGUEZ

1

sistema educativo universitario aztecaInstituto de estudios superiores “sor Juana Inés de la cruz”

plantel Tapilula

EJERCICIOS DE PROGRAMACIÓN EN LENGUAJE “C”

CARRERA

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<dos.h>#include<string.h>main(){int x,Co,C1,C2,Io,I1,I2,H;clrscr();srand(time(NULL));Co=1;C1=1;C2=1;Io=1;I1=1;I2=1;H=0;gotoxy(Co,5);printf("M");gotoxy(C1,10);printf("C");gotoxy(C2,15);printf("y");while(H==0){X=rand()%3;switch(X){case0:gotoxy(Co,5);printf(" ");Co=Co+Io;delay(15);gotoxy(Co,5);printf("MO");if(Co==70)

Io=-1;if(Co==1)H=1;break;case1:gotoxy(C1,10);printf(" ");C1=C1+I1;delay(15);gotoxy(C1,10);printf("C1");if(C1==70)I1=-1;if(C1==1)H=1;break;case2:gotoxy(C2,15);printf(" ");C2=C2+I2;delay(15);gotoxy(C2,15);printf("Y2");if(C2==70)I2=-1;if(C2==1)H=1;break;}}gotoxy(30,24);printf("el ganador es %d",X);getch();}

PROGRAMA QUE DARÁ LA FECHA DEL ÚLTIMO DOMINGO DE TODOS LOS MESES

DOCENTE: LSC CONCEPCIÓN LAGUNAS RODRÍGUEZ

1

sistema educativo universitario aztecaInstituto de estudios superiores “sor Juana Inés de la cruz”

plantel Tapilula

EJERCICIOS DE PROGRAMACIÓN EN LENGUAJE “C”

PROGRAMA QUE DARÁ LA FECHA DEL ÚLTIMO DOMINGO DE TODOS LOS MESES

DOCENTE: LSC CONCEPCIÓN LAGUNAS RODRÍGUEZ

Mes=1

f=tm-rr;

gotoxy(5,mes+1);printf("mes=%s",names);gotoxy(30,mes+1);printf("ultimo domingo=%d",f);

mes=mes+1; }

gotoxy(15,20);printf("pulsar cualquier tecla");getche();

}

1

sistema educativo universitario aztecaInstituto de estudios superiores “sor Juana Inés de la cruz”

plantel Tapilula

EJERCICIOS DE PROGRAMACIÓN EN LENGUAJE “C”

#include<stdio.h>#include<conio.h>gg#include<stdlib.h>#include<dos.h>#include<string.h>

main();

{Int mes,f,tm,r,td,rr;char nmes[15];clrscr();mes=1;While(mes<=12) {Swich(mes){

Case 1:{strcpy (nmes,"Enero"); tm=31; td=31; break;}Case 2:{strcpy (nmes,"Febrero"); tm=28; td=59; break;} Case 3:{strcpy (nmes,"Marzo"); tm=31; td=90; break;} Case 4:{strcpy (nmes,"Abril"); tm=30; td=120; break;} Case 5:{strcpy (nmes,"Mayo"); tm=31; td=151; break;} Case 6:{strcpy (nmes,"Junio"); tm=30; td=181; break;} Case 7:{strcpy (nmes,"Julio"); tm=31; td=212; break;} Case 8:{strcpy (nmes,"Agosto"); tm=31; td=243; break;} Case 9:{strcpy (nmes,"Septiembre"); tm=30; td=273; break;} Case 10:{strcpy (nmes,"Octubre"); tm=31; td=304; break;} Case 11:{strcpy (nmes,"Noviembre"); tm=31; td=334; break;} Case 12:{strcpy (nmes,"Diciembre"); tm=31; td=365; break;}

}

r= td%7; Switch(r); {

Case 0:{rr=2; break;} Case 1:{rr=3; break;} Case 2:{rr=4; break;} Case 3:{rr=5; break;} Case 4:{rr=6; break;} Case 5:{rr=0; break;} Case 6:{rr=1; break;}

}

DOCENTE: LSC CONCEPCIÓN LAGUNAS RODRÍGUEZ

1

sistema educativo universitario aztecaInstituto de estudios superiores “sor Juana Inés de la cruz”

plantel Tapilula

EJERCICIOS DE PROGRAMACIÓN EN LENGUAJE “C”

PRUEBA DE CADENA

DOCENTE: LSC CONCEPCIÓN LAGUNAS RODRÍGUEZ

1

sistema educativo universitario aztecaInstituto de estudios superiores “sor Juana Inés de la cruz”

plantel Tapilula

EJERCICIOS DE PROGRAMACIÓN EN LENGUAJE “C”

PRUEBA DE CADENA

#include<stdio.h>#include<string.h>#include<dos.h>#include<conio.h>

char m[13][20]={"mes no correcto","enero","febrero" ,"marzo","abril","mayo","junio","julio","agosto","septiembre","octubre","noviembre","diciembre"};

main()

{intmes,longi,carac;clrscr();

for(mes=1;mes<=!2;mes++){gotoxy(10,mes);printf("%s",m[mes]);}

for (mes=1; mes<=12;mes++) {longi=strlen(m[mes]);for(carac=0;carac<longi;carac++){

gotoxy("25+mes*4,carac+1")Printf("%c",m[mes][carac]);delay(30);

} }

gotoxy(60,20);printf("oprimir una tecla");getche();

}

DOCENTE: LSC CONCEPCIÓN LAGUNAS RODRÍGUEZ

1

sistema educativo universitario aztecaInstituto de estudios superiores “sor Juana Inés de la cruz”

plantel Tapilula

EJERCICIOS DE PROGRAMACIÓN EN LENGUAJE “C”

CONTEO DE CAMPANAS

DOCENTE: LSC CONCEPCIÓN LAGUNAS RODRÍGUEZ

INICIOINICIO

H2, M1

H2, M2

H2, M2

H2, M1

H2, M2

H2, M2

Ca=Em=0

cH=0

Ca=Em=0

cH=0

ME<M5ME<M5

ME=Me+1

M1=M1+1

ME=Me+1

M1=M1+1

M1

15 30 45 60

M1

15 30 45 60

CQ=CQ+1CQ=CQ+1 Cm=CM+1Cm=CM+1 CQ=CQ+1CQ=CQ+1CH=Ch+1CH=Ch+1

CQ

CM CH

Ch

CQ

CM CH

Ch

FINFIN

1

sistema educativo universitario aztecaInstituto de estudios superiores “sor Juana Inés de la cruz”

plantel Tapilula

EJERCICIOS DE PROGRAMACIÓN EN LENGUAJE “C”

CONTEO DE CAMPANAS

# include<stdio.h># include<conio.h># include<stdlib.h># include<dos.h># include<string.h>Main(){Int CQ,CM,CH, ME, H1, M1, MS, H2, M2;Clrscr();CQ=0;CM=0;CH=0;gotoxy(15,8);printf(“hora de llegada”);scanf(“%d”,&H1);gotoxy(15,9);printf(“minuto de llegada”);scanf(“%d”,&M1)gotoxy(15,10);printf(“hora de retirarse”);scanf(“%d”,&H2);gotoxy(15,11);printf(“minuto de retirarse”);scanf(“%d”,M2);ME=(H1*60)+M1;MS=(H2*60)+M2;While(ME<Ms){ME=ME+1;MS=M1+1;Switch(M1){

Case 15:CQ=CQ+1; break;Case 30:CM=CM+1;breakCase 45: CQ=CQ+1;breakCase 60: CH=CH+1;M1=0;Break;}}gotoxy(10,15);printf(“campanadas de cuartos”=”%d”,CQ);gotoxy(10,17);printf(“campanadas de media=”%d”,CQ);gotoxy(10,15);printf(“campanadas de cuartos”=”%d”,CH);getch();}

DOCENTE: LSC CONCEPCIÓN LAGUNAS RODRÍGUEZ

DA LA FECHA DEL ULTIMO DOMINGO DEL MES DEL AÑO 2011

DA LA FECHA DEL ULTIMO DOMINGO DEL MES DEL AÑO 2011# include<stdio.h># include<conio.h>

tm=total de días de cada mes

td=días transcurridos

r=residuo

rr=días retrocedido

f=fecha del ultimo domingo

# include<stdlib.h># include<dos.h># include<string.h>Main(){Int mes,f,tm,r,td,rr;Char nmes[15];Clrscr();Gotoxy(15,5); printf(“numero de mes”);Scanf(“%d”,&mes);Switch(mes){Case1: {strcpy (nmes,”enero”); tm=31; td=31; break;} Case1: {strcpy (nmes,”febrero”); tm=28; td=59; break;} Case1: {strcpy (nmes,”marzo”); tm=31; td=90; break;} Case1: {strcpy (nmes,”abril”); tm=30; td=120; break;} Case1: {strcpy (nmes,”mayo”); tm=31; td=151; break;} Case1: {strcpy (nmes,”junio”); tm=30; td=181; break;} Case1: {strcpy (nmes,”julio”); tm=31; td=212; break;} Case1: {strcpy (nmes,”agosto”); tm=31; td=243; break;} Case1: {strcpy (nmes,”septiembre”); tm=30; td=273; break;} Case1: {strcpy (nmes,”octubre”); tm=31; td=304; break;} Case1: {strcpy (nmes,”noviembre”); tm=31; td=334; break;} Case1: {strcpy (nmes,”diciembre”); tm=31; td=365; break;} {r=td%7;switch®{Case 0: {rr=2; break;}Case 0: {rr=3; break;}Case 0: {rr=4; break;}Case 0: {rr=5; break;}Case 0: {rr=6; break;}Case 0: {rr=20; break;}Case 0: {rr=1; break;}}f=tm-rr;printf(“\n fecha del ultimo domingo del mes=%d”,f);gotoxy(15,20); printf(“pulsar cualquier tecla”);getch()}

/*GENERA 20 VALAS, HASTA QUE LLEGUEN A 70*/

# include <stdio.h># include <conio.h># include <stdlib.h>

# include <stdio.h>

# include <conio.h>

# include <stdlib.h>

# include <dos.h>

# include <string.h>

Main()

{

Int f,c;

Clrscr();

F=1;

C=80;

While(f<20)

{

Printf(“\n*=%d,*”);

F=f+1;

}

Getch();

}

# include <dos.h># include <string.h>

Main(){Int c[20],I,x;Clrscr();Srand(time(NULL));

For(i=1; i<=20; i=i+1){C[i]=1;Gotoxy(c[i],i); printf(“0”);}X=1;

While(c[x]<70){ X=(rand()%20)+1; Gotoxy(c[x],x); printf(“”); C[x]=c[x]+1; Delay(9); Gotoxy(c[x],x); printf(“%d”,x);}

Gotoxy(10,5); printf(“fila ganadora=%d”,x);Getch();}

# include <stdio.h>

# include <stdlib.h>

# include <dos.h>

# include <string.h>

Main()

{

Int f,c;

Clrscr();

F=20;

C=80;

While(f>0)

{

Printf(“\n*=%d,*”);

c=c-1;

}

Getch();

}

*/DISTINGUE SI EL NUMERO ES PAR O IMPAR/*

#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <dos.h>#include <string.h>Main(){Int C,CP,CI,N,X,R;Clrscr();C=0;CP=0;CI=0;Gotxy(10,5);Printf(“INTRODUCIR EL NUMERO DE DATOS”);

Scanf(“%d”,&N);While(C<N){Gotoxy(10,6+c);Printf(“INTRODUCIR LA CANTIDAD”);Scanf(“%d”,&X);R=X%2;If(R!=0){Textcolor(13);Gotoxy(60,6+C);cprintf(“NUMERO IMPAR”);CI=CI+1;}Else {Textcolor(13);Gotoxy(60,6+C);cprintf(“NUMERO PAR”);CP=CP+1;}C=C+1;}Gotoxy(10,20);Printf (“TOTAL DE NUMEROS IMPARES INTRODUCIDOS= %d”,CP);Gotoxy(10,21);Printf(“TOTAL DE NUMEROS IMPARES INTRODUCIDOS= %d”,CI);Getch();}

/*INTRODUCE DATOS, IMPRIME LA SUMA DE CADA FILA Y COLUMNA GENERANDO DATOS DOS VECES*/

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

Int x[4][3];Main(){

Int c,f,s;Clrscr();

For(f=1;f<=4;f=f+1){S=0;For(c=1;c<=3;c=c+1)

#include<stdio.h> #include<conio.h> #include<dos.h> #include<stdlib.h> #include<string.h> main() { int i,A[11]={0,1,2,3,4,5,6,7,8,9,10},x; clrscr(); for(i=1; i<=10; i=i+1) { gotoxy(5,i);printf("%d",A[i]); } x=A[10]; for(i=9; i>=1; i=i-1) { A[i+1]=A[i]; } A[1]=x; for(i=1; i<=10; i=i+1) { gotoxy(20,i);printf("%d",A[1]); } getch(); }

{Gotoxy(c*5,f*2);Scanf(“%d”,&x[f][c]);S=S+x[f][c];}Gotoxy(25,f*2);Printf(“%d”s);}Delay(20);Clrscr();For(c=1;c<=4;c=c+1){S=0;For(f=1;f<=3;f=f+1){Gotoxy(c*5,f*2);Scanf(“%d”,&x[c][f]);S=S+x[c][f];}Gotoxy(c*5,10);printf(“%d”,s);}Getch();}

ROTACIÓN DE LOS NÚMEROS DEL 1 AL 10

#include<stdio.h> #include<conio.h> #include<dos.h> #include<stdlib.h> #include<string.h> main() { int cf,c,cl,f; clrscr(); cf=80; f=1; while(f<=20) { c=80; while(c>=(cf-20)) { gotoxy(c,f);cprintf("$"); delay(30); c=c-1; } f=f+1; cf=cf+1; } getch();}

PIRAMIDE #3

#include<conio.h> #include<dos.h> #include<stdlib.h> #include<string.h> main() { int nf,cf,ci,f,c; clrscr(); nf=24; f=1; cf=41; ci=41; while(f<=nf) { c=ci; while(c<=cf) { textcolor(c); gotoxy(c,f);printf("&"); delay(30); c=c+1; } f=f+1; ci=ci-1;cf=cf+1;} getch();}

PIRAMIDE #4

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<dos.h>#include<string.h>main(){intf,nf,c,cf;clrscr();

cf=1;f=1;nf=24;while (f<=nf){c=1;while (c<=cf)

{textcolor(c);gotoxy(c,f);printf(“myc 16”);delay(30);c=c+1;

}cf=cf+1f=f+1;}

getche();}

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<dos.h>#include<string.h>main(){intf,nf,c,cf;clrscr();

nf=24;cf=nf;f=1;

while (f<=nf){c=1;while (c<=cf)

{textcolor(c);gotoxy(c,f);printf(“myc 16”);delay(30);c=c+1;

}cf=cf-1f=f+1;}

getche();}

/* GENERA DATOS DE 1 AL 100, PERMITE 40 DATOS QUE SE ALMACENA EN EL ARREGLO X */

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<dos.h>#include<string.h>main(){int H,E,D,X[40],i,c,f;clrscr();srand(time(NULL));H=0;while (H<40)

{E=0;While(E==0){E=1;D=(rand()%101)+1;for(i=0;i<40;i=i+1){if(D==X[i])E=0;}}X[H]=D;H=H+1;}C=10;F=1;for(i=0;i<40;i=i+1){gotoxy(C,F);printf(“%d”,X[i]);F=F+1;if(F==21){

C=C+10;F=1;}}getch();}

IMPRIME ASRTERISCOS INICIANDO C=1 A F=20

IMPRIME ASTERISCOS INICIANDO C=1 A F=1

#include<stdio.h>#include<stdlib.h>#include<conio.h> main (){ int c,cf,f; clrscr() c=1: f=1: while(c<80) { gotoxy(c,f):printf("*");c=c+1; }

#include<dos.h>#include<string.h>#include<stdio.h> main (){ int c,f; f=20: c=1: while(f>0) { printf("\n*=%d,*"); f=f-1; } getch(); }

getch(); }

IMPRIME LA NUMERACION DEL 1-200

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<dos.h>

#include<string.h>

main ()

{

int c,f,i;

clrscr();

c=5:

f=1:

while(f>0)

for(i=1; i<=200; i=i+1)

{

gotoxy(c,f);printf("%d",i);

f=f+1;

if(f==21)

}

c=c+5;

f=1;

}

}

getch();

}

IMPRIME EL SALUDO Y EL VALOR DE X Y Y

#include<stdio.h>#include<stdlib.h>#include<conio.h>#include<string.h>#include<dos.h>

main(){int a,b,x,y;clrscr():gotoxy(5,6);printf(“introducir el valor de a:”);scanf(“%d”,&a);gotoxy(5,7);printf(“introducir el valor de b:”);scanf(“%d”,&b);if(a>b){x=1;printf(“x=%d”,x);}else{y=4;printf(”y=%d”,y);}gotoxy(10,8);printf(“hola”);getch();}

SUBE Y BAJA EL SIMBOLO DIAGONALMENTE

#include<stdio.h>#include<stdlib.h>#include<conio.h>#include<string.h>#include<dos.h>

main(){int c,ic,f,tf;clrscr();c=1;ic=1;f=10;tf=1;textcolor(30);gotoxy(c,f);printf(“myc16”);while(!kbhit()){delay(5);textcolor(30);gotoxy(c,f);printf(“m”);if(f==1)tf=1;if(f==22)tf=-1;if(c==80)ic=-1;if(c==1)ic=1;c=c+ic;f=f+tf;delay(5);textcolor(30);gotoxy(c,f);printf(“c”);}getch();}