Last active
February 23, 2016 20:46
-
-
Save aziflaj/15d62c561e8ba8e9bb51 to your computer and use it in GitHub Desktop.
Some small C programs written as homeworks
This file contains 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
/* | |
* wap | |
* kalendari per 1 muaj | |
* perdoruesi jep vitin dhe muajin | |
*/ | |
#include <stdio.h> | |
int main(int argc,char **argv) { | |
char* s_muaji[] = {"Janar","Shkurt","Mars","Prill","Maj","Qershor","Korrik","Gusht","Shtator","Tetor","Nentor","Dhjetor"}; | |
int viti,muaji,shekulli,v_shek; | |
int nr_dit,fillimi,i; | |
int count; | |
int flag = 7; /* flag per te kontrolluar nqs eshte arritur e diela e pare e muajit */ | |
if (argc != 3) { | |
printf("Perdorimi: calendar viti muaji\n"); | |
return 1; | |
} | |
/* lexo vitin */ | |
viti = atoi(argv[1]); | |
/* percaktojme shekullin dhe vitin e shekullit qe i perket ky vit */ | |
shekulli = (int) viti/100; | |
v_shek = viti % 100; | |
/* lexo muajin */ | |
muaji = atoi(argv[2]); | |
printf("Kalendari per muajin %s te vitit %d\n\n",s_muaji[muaji-1],viti); | |
/* percaktojme numrin e diteve */ | |
switch(muaji) { | |
case 1: /* janar */ | |
case 3: /* mars */ | |
case 5: /* maj */ | |
case 7: /* korrik */ | |
case 8: /* gusht */ | |
case 10: /* tetor */ | |
case 12: /* dhjetor */ | |
nr_dit = 31; | |
break; | |
case 2: /* shkurti ka 29 dite ne vit te brishte, 28 ne vite te tjera */ | |
if((viti%4==0 && viti%100 != 0) || (viti%400==0)) /* vit i brishte */ | |
nr_dit=29; | |
else nr_dit=28; | |
break; | |
default: | |
nr_dit=30; | |
} | |
/* rregullim per shkak te formules: Janari eshte muaji 13 dhe Shkurti muaji 14 */ | |
if(muaji<3) muaji+=12; | |
/* gjejme cfare dite e javes eshte dita e pare e muajit */ | |
fillimi = (1 + 26*(muaji+1)/10 + v_shek + v_shek/4 + shekulli/4 + 5*shekulli)%7; | |
/* rregullim per shkak te formules: Java fillon me te shtunen */ | |
switch(fillimi) { | |
case 0: /* e shtune */ | |
case 1: /* e diel */ | |
fillimi+=5; | |
break; | |
default: | |
fillimi-=2; | |
} | |
/* printo header */ | |
printf(" H M M E P S D\n"); | |
printf("---------------------\n"); | |
flag -= fillimi; | |
/* printo hapesirat per te zhvendosur ne te djathte ditet e javes */ | |
for (i=0;i<fillimi;i++) printf("%3c",' '); | |
for (i=1,count=i;i<=nr_dit;i++,count++) { | |
printf("%3d",i); | |
if (count==flag) { | |
count=0; | |
printf("\n"); | |
flag = 7; | |
} | |
} | |
return 0; | |
} |
This file contains 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
/* 10. Palindrome string */ | |
#include <stdio.h> | |
#include <string.h> | |
int palindrome(char* s) { | |
int i=0,l=strlen(s); | |
int upto=l/2+1; | |
for (;i<upto;i++) | |
if (s[i]!=s[l-i-1]) return 0; | |
return 1; | |
} | |
int main(void) { | |
char *str; | |
printf("Test if this is palindrome: "); | |
gets(str); | |
palindrome(str) ? printf("%s is palindrome\n",str) : printf("%s is not palindrome\n",str); | |
return 0; | |
} |
This file contains 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
/* 30. Power of 2 */ | |
#include <stdio.h> | |
int power2(int n) { | |
do { | |
if (n%2==0) { | |
n/=2; | |
power2(n); | |
} | |
else return 0; | |
} while (n>2); | |
return 1; | |
} | |
int main(void) { | |
int p; | |
printf("p="); | |
scanf("%d",&p); | |
power2(p) ? printf("%d is power of 2",p) : printf("%d is not power of 2",p); | |
return 0; | |
} |
This file contains 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
/* 15. String concatenate */ | |
#include <stdio.h> | |
void concatenate(char *s1,char *s2) { | |
sprintf(s1,"%s%s",s1,s2); | |
} | |
int main(void) { | |
char *s1,*s2; | |
printf("s1: "); | |
gets(s1); | |
printf("s2: "); | |
gets(s2); | |
concatenate(s1,s2); | |
printf("%s",s1); | |
return 0; | |
} |
This file contains 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
/* 14. String compare */ | |
#include <stdio.h> | |
#include <string.h> | |
int equal_str(char *s1,char *s2) { | |
int l1=strlen(s1); | |
int l2=strlen(s2); | |
int i; | |
/* if string dont have the same length, they are not equal */ | |
if (l1!=l2) return 0; | |
for (i=0;i<l1;i++) | |
if (s1[i]!=s2[i]) return 0; | |
return 1; | |
} | |
int main(void) { | |
char *s1,*s2; | |
printf("s1: "); | |
gets(s1); | |
printf("s2: "); | |
gets(s2); | |
equal_str(s1,s2) ? printf("both strings are equal\n") : printf("strings are not equal"); | |
return 0; | |
} |
This file contains 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
/* 3. Swap with temp */ | |
#include <stdio.h> | |
void swap(int* a,int* b) { | |
int temp; | |
temp=*a; | |
*a=*b; | |
*b=temp; | |
} | |
int main(void) { | |
int n1,n2; | |
printf("n1="); | |
scanf("%d",&n1); | |
printf("n2="); | |
scanf("%d",&n2); | |
swap(&n1,&n2); | |
printf("n1=%d n2=%d",n1,n2); | |
return 0; | |
} |
This file contains 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
/* 5. Swap with xor */ | |
#include <stdio.h> | |
void swap(int *a,int *b) { | |
if (a!=b) { | |
*a ^= *b; | |
*b ^= *a; | |
*a ^= *b; | |
} | |
} | |
int main(void) { | |
int a,b; | |
printf("a="); scanf("%d",&a); | |
printf("b="); scanf("%d",&b); | |
swap(&a,&b); | |
printf("a=%d, b=%d",a,b); | |
return 0; | |
} |
This file contains 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
/* 4. Swap without temp */ | |
#include <stdio.h> | |
void swap(int *a,int *b) { | |
*a-=*b; | |
*b+=*a; | |
*a=(*b - *a); | |
} | |
int main(void) { | |
int a,b; | |
printf("a="); scanf("%d",&a); | |
printf("b="); scanf("%d",&b); | |
swap(&a,&b); | |
printf("a=%d, b=%d",a,b); | |
return 0; | |
} |
This file contains 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
#include <stdio.h> | |
#include <time.h> | |
int main(void) { | |
time_t start,cont; | |
int seconds,counter=0; | |
double d; | |
printf("Enter time to delay: "); | |
scanf("%d",&seconds); | |
printf("Time started\n"); | |
printf("%d ",counter); | |
time(&start); | |
do { | |
time(&cont); | |
d=difftime(cont,start); | |
if (d==1.0) { | |
counter++; | |
printf("%d ",counter); | |
time(&start); | |
} | |
else continue; | |
} while(counter<seconds); | |
printf("\nCounting ended"); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment