C Examples

download C Examples

of 31

  • date post

    15-Jan-2016
  • Category

    Documents

  • view

    216
  • download

    0

Embed Size (px)

description

C Examples

Transcript of C Examples

  • C

  • 1. , . . . . . . 2. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    1 2 ( ) 3 ( )

    3. . . . . . . . . . . . . . . . . . . . . . . . . . . .

    4. (Factorial) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6. 1-1/2+1/3-1/4+...+1/n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7. Fibonacci . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    8. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9. ASCII . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    12. Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    14. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (Bubble sort) (Insertion sort) (Selection sort)

    15. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    18. Taylor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20. . . . . . . . . . . . . . . . . . . . . . . . . . . 21. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    34

    7

    89910

    11121213

    1517

    20

    232526

    28293031

  • , . , (input data) , (output data). .

    C. .

    .

    , 10 100 .

    , .

    ,

    10 .

    #include #define size 10 // megethos pinakavoid main(){

    int pinakas[size],i;for(i=0;i

  • 10 , .

    1

    #include #define size 10void main(){

    int i,temp,pinakas[size];for (i=0;i

  • ( .)

    #include #define size 10void reverse(int pinakas[size]);void swap(int *x, int *y);void main(){

    int i,pinakas[size];for (i=0;i

  • #include #define size 10void reverse(int pinakas[size]);void main(){

    int i,pinakas[size];for (i=0;i

  • 10 .

    #include #define size 10void main(){

    int i,k=1,g=1,min,max,pinakas[size];for (i=0;i

  • void main(){

    int i,min,k=1,max,g=1,temp;printf("Doste ton 1o arithmo: ");scanf("%d", &temp);min=temp;max=temp;for (i=1;i

  • . ( , .. 1234321)

    #include #include void main(){

    char arithmos[128];int i,j,k;printf("Doste ton akeraio arithmo: ");scanf("%s", arithmos);k=strlen(arithmos);j=0;for (i=0;i

  • . ( Fibonacci .)

    #include #define size 100void main(){

    int i,x,pinakas[size];printf("Fores ektelesis Fibonacci: ");scanf("%d", &x);pinakas[0]=1;pinakas[1]=1;for(i=2;i

  • .

    #include < stdio.h >#define size 1000void main(){

    int a,i,j,k=0,c=0,pinakas[size];printf("Doste to diastima, mesa sto opoio\n");printf("thelete na vrethoun oi protoi arithmoi: 0-");scanf("%d", &a);for(i=2;i1;j--)if((i % j)==0) // To symvolo % einai o telestis

    c=c+1; // akeraiou ypolipou(mod)if(c==0){

    pinakas[k]=i;k=k+1;

    }c=0;

    }if(k==0)

    printf("\nDe vrethike kanenas protos sto diastima afto.\n");else{

    printf("\nVrethikan %d protoi arithmoi.\n", k);for(i=0;i

  • ASCII.

    #include #include void main(){

    int i,length; char string[128]; printf("Pliktrologiste mia leksi i frasi: "); gets(string); length=strlen(string); printf("\n"); for (i=0;i

  • 2 81 .

    #include void main(){

    int a,k,i,j;printf("Doste to plithos twn grammwn: ");scanf("%d", &a);j=1;k=(a-1)/2;while(k>0){

    for(i=0;i

  • void main(){

    int i,a,k;printf("Doste ton arithmo: ");scanf("%d",&a);for(i=0;i

  • void main(){

    int k,c,r;printf("Doste to mono plithos grammwn: ");scanf("%d", &k);for(r=0;r

  • c=0;k=0;for(i=0;i
  • #include #include void diafora();void main(){

    int a,a1,a2,a3,b,b1,b2,b3;char im1[10],im2[10];printf("Oi imerominies prepei na dinontai sti morfi 09/04/2001.\n");printf("\n");printf("Doste tin proti imerominia: ");scanf("%s", &im1);printf("Doste ti defteri imerominia: ");scanf("%s", &im2);a1=(im1[0]-48)*10+(im1[1]-48);a2=(im1[3]-48)*10+(im1[4]-48);a3=(im1[6]-48)*1000+(im1[7]-48)*100+(im1[8]-48)*10+(im1[9]-48);a=a1+(a2*30.42)+(a3*365.25);b1=(im2[0]-48)*10+(im2[1]-48);b2=(im2[3]-48)*10+(im2[4]-48);b3=(im2[6]-48)*1000+(im2[7]-48)*100+(im2[8]-48)*10+(im2[9]-48);b=b1+(b2*30.42)+(b3*365.25);if(a==b)

    printf("Oi dio imerominies einai idies.\n");else{

    if(a>b){

    printf("H proti imerominia einai metagenesteri tis defteris,\n");diafora(&a,&b);

    }else{

    printf("H defteri imerominia einai metagenesteri tis protis,\n");diafora(&b,&a);

    }}

    }

    void diafora(x,y)int *x, *y;{

    int diaf,a,b,c;diaf=*x-*y;c=diaf/365.25;b=(diaf-(c*365.25))/30.42;

  • a=diaf-((c*365.25)+(b*30.42));printf("kata %d meres, %d mines kai %d xronia.\n", a, b, c);

    }

    strings 48 ASCII . 30.42 . 365.25 , .

    , .

    #include #include void diafora();void main(){

    int a,a1,a2,b,b1,b2;char wra1[5],wra2[5];printf("Oi wres prepei na dinontai sti morfi 21:06 kai 04:15.\n");printf("\n");printf("Doste tin proti wra: ");scanf("%s", &wra1);printf("Doste ti defteri wra: ");scanf("%s", &wra2);a1=(wra1[0]-48)*10+(wra1[1]-48);a2=(wra1[3]-48)*10+(wra1[4]-48);a=(a1*60)+a2;b1=(wra2[0]-48)*10+(wra2[1]-48);b2=(wra2[3]-48)*10+(wra2[4]-48);b=(b1*60)+b2;

    if(a==b)printf("\nOi dio wres einai idies.\n");

    else{

    if(a>b){

    printf("\nApo tis %s ews tis %s mesolavoun ", wra2, wra1);diafora(&a,&b);

  • }else{

    printf("\nApo tis %s ews tis %s mesolavoun ", wra1, wra2);diafora(&b,&a);

    }}

    }

    void diafora(x,y)int *x, *y;{

    int diaf,a,b;diaf=*x-*y;a=diaf/60;b=diaf-(a*60);printf("%d wres kai %d lepta.\n", a, b);

    }

    48 ASCII .

    (Bubble sort)

    . .

    #include

  • #define size 10void swap(int *a1,int *b1);void main(){

    int pinakas[size],i,j;for(i=0;i

  • for (i=0;i
  • {int i,j,k,min,pinakas[size];for (i=0;i
  • char epitheto[25];};struct birthdaterec{

    int mera;int minas;int xronos;

    };struct lessonsrec{

    char mathima[40];float vathmos;

    };struct student{

    int mitroo;struct namerec onoma; char patronymo[20];char sex;struct birthdaterec imgenisis;char tmima[40];struct lessonsrec mathimata[50];int counter;

    };

    struct student pinakas[size];int g,plithos=0;void emfanisi(int *t);

    void main(){

    int c=0;char ch[3],temp[5];struct student foititis;

    printf("\n- EISAGWGH STOIXEIWN FOITHTH -\n");printf("\nArithmos mitroou: ");scanf("%d", &foititis.mitroo);printf("\n** ONOMA **");printf("\nMikro: ");scanf("%s", &foititis.onoma.mikro);printf("Epitheto: ");scanf("%s", &foititis.onoma.epitheto);printf("\nOnoma patera: ");scanf("%s", &foititis.patronymo);printf("\nSex (M)ale/(F)emale: ");scanf("%s", &foititis.sex);printf("\n** HMEROMHNIA GENNHSHS **\n");

  • printf("\nMera: ");scanf("%d", &foititis.imgenisis.mera);printf("Minas: ");scanf("%d", &foititis.imgenisis.minas);printf("Xronos: ");scanf("%d", &foititis.imgenisis.xronos);printf("\n** TMHMA **");printf("\nTmima: ");scanf("%s", &foititis.tmima);printf("\n** VATHMOLOGIA **\n");printf("Gia na diakopsete tin eisagwgi mathimatwn,\n");printf("pliktrologiste 'telos'.\n");do{

    printf("\nTitlos mathimatos: ");scanf("%s", &foititis.mathimata[c].mathima);strcpy(temp,foititis.mathimata[c].mathima);if(strcmp(temp,"telos")==0)

    printf("- Telos eisagwgis dedomenwn. -\n");else{

    printf("Vathmos: ");scanf("%f", &foititis.mathimata[c].vathmos);c++;

    }}while((c

  • pinakas[*t].mitroo, pinakas[*t].onoma.mikro, pinakas[*t].onoma.epitheto,pinakas[*t].patronymo, pinakas[*t].sex, pinakas[*t].imgenisis.mera,pinakas[*t].imgenisis.minas, pinakas[*t].imgenisis.xronos);printf("\nTmima: %s", pinakas[*t].tmima);

    printf("\n** MATHIMATA **\n");for(i=0;i

  • scanf("%d", &pinakas[i]);}printf("\nDoste to stoixeio pou anazitate: ");scanf("%d", &x);i=0;do{

    if(pinakas[i]==x){

    flag=1;printf("Vrethike sti thesi %d tou pinaka.\n\n", i+1);

    }else

    i++;}while((flag==0)&&(i

  • printf("\nDoste to stoixeio pou anazitate: ");scanf("%d", &x);while((first
  • printf("Fores ektelesis Taylor: ");scanf("%d", &n);a=-1.0;sum=x;do{

    i++; // oi ekthetes kai oi paranomastes einai protoi arithmoifor(j=i-1;j>1;j--)

    if((i % j)==0)c=c+1;

    if(c==0){

    fores++;sum=sum+(dynami(&i)/paragontiko(&i)*a);a=a*(-1.0);

    }c=0;

    }while(fores

  • #include #include void main(){

    float a,b,c;double t,s,e;printf("Doste to mikos tis protis pleyras: ");scanf("%f", &a);printf("Doste to mikos tis defteris ple