├── Functioin.c ├── Star Patterns ├── Rectangle.c ├── RightTriangle.c ├── InvertedRightTriangle.c ├── InvertedHollowRightTriangle.c ├── RhombusPattern.c ├── MirroredRhombusPattern.c ├── LeftTriangle.c ├── XPattern.c ├── RightHollowTriangle.c ├── HalfDiamond.c ├── InvertedPyramid.c ├── PyramidPattern.c ├── InvertedLeftTriangle.c ├── RectangleWithBlank.c ├── PlusPattern.c ├── HolloMirorRhombus.c ├── HollowRhombusPattern.c ├── LeftHollowTriangle.c ├── InvertedHollowPyramid.c ├── PyramidHollowPattern.c ├── InvertedHollowLeftTriangle.c ├── RectangleWithCross.c ├── Diamond.c ├── RightArrow.c └── LeftArrow.c ├── 1DArray.c ├── ForLoop.c ├── CalculateTheNthTerm.c └── Projects ├── Election Poll ├── voting.c └── election.h ├── Calculator.c ├── SnakeLadderGame.c └── TicTacToe.c /Functioin.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define max(a,b) (a>b?a:b) 4 | 5 | int max_of_four(int a,int b,int c,int d){ 6 | 7 | return max(a,max(b,max(c,d))); 8 | } 9 | 10 | int main() { 11 | 12 | int a, b, c, d; 13 | scanf("%d %d %d %d", &a, &b, &c, &d); 14 | int ans = max_of_four(a, b, c, d); 15 | printf("%d", ans); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Star Patterns/Rectangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the number of rows : "); 6 | scanf("%d",&n); 7 | for(int i=0;i 2 | 3 | int main() 4 | { 5 | int n; 6 | printf("Enter the number of rows : "); 7 | scanf("%d",&n); 8 | for(int i=1;i<=n;i++) 9 | { 10 | for(int j=1;j<=i;j++) 11 | { 12 | printf("* "); 13 | } 14 | printf("\n"); 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Star Patterns/InvertedRightTriangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n,m=1; 6 | printf("Enter the number of rows : "); 7 | scanf("%d",&n); 8 | for(int i=n;i>=1;i--) 9 | { 10 | for(int j=1;j<=i;j++) 11 | { 12 | printf("*"); 13 | } 14 | printf("\n"); 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /1DArray.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main() { 7 | int i,j,k,m, n; 8 | 9 | scanf("%d",&n); 10 | 11 | int a[n]; 12 | 13 | for(i=0;i 2 | int main() 3 | { 4 | int i,j,k; 5 | int a,b; 6 | char *ch[]={"null","one","two","three","four","five", 7 | "six","seven","eight","nine"}; 8 | scanf("%d%d",&a,&b); 9 | 10 | for(i=a;i<=b;i++) 11 | { 12 | if(i>9 && i%2) 13 | printf("odd\n"); 14 | else if(i>9 && i%2==0) 15 | printf("even\n"); 16 | else 17 | printf("%s\n",ch[i]); 18 | 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Star Patterns/InvertedHollowRightTriangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n,m=1; 6 | printf("Enter the number of rows : "); 7 | scanf("%d",&n); 8 | for(int i=n;i>=1;i--) 9 | { 10 | for(int j=1;j<=i;j++) 11 | { 12 | if(j==1 || j==i || i==n) 13 | printf("*"); 14 | else 15 | printf(" "); 16 | } 17 | printf("\n"); 18 | } 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Star Patterns/RhombusPattern.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | printf("Enter the number of rows : "); 7 | scanf("%d",&n); 8 | for(int i=n;i>=1;i--) 9 | { 10 | for(int j=1;j<=i-1;j++) 11 | { 12 | printf(" "); 13 | } 14 | for(int k=1;k<=n;k++) 15 | { 16 | printf("*"); 17 | } 18 | printf("\n"); 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Star Patterns/MirroredRhombusPattern.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the number of rows : "); 6 | scanf("%d",&n); 7 | for(int i=1;i<=n;i++) 8 | { 9 | for(int j=1;j 2 | 3 | int main() 4 | { 5 | int n,m=1; 6 | printf("Enter the number of rows : "); 7 | scanf("%d",&n); 8 | for(int i=n;i>=1;i--) 9 | { 10 | for(int j=1;j<=i-1;j++) 11 | { 12 | printf(" "); 13 | } 14 | for(int k=1;k<=m;k++) 15 | { 16 | printf("*"); 17 | } 18 | printf("\n"); 19 | m++; 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Star Patterns/XPattern.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n,m; 5 | printf("Enter the number : "); 6 | scanf("%d",&n); 7 | m=2*n-1; 8 | for(int i=1;i<=m;i++) 9 | { 10 | for(int j=1;j<=m;j++) 11 | { 12 | if(i==j || j==(m-i+1)) 13 | { 14 | printf("*"); 15 | } 16 | else 17 | { 18 | printf(" "); 19 | } 20 | } 21 | printf("\n"); 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Star Patterns/RightHollowTriangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n; 6 | printf("Enter the number of rows : "); 7 | scanf("%d",&n); 8 | for(int i=1;i<=n;i++) 9 | { 10 | for(int j=1;j<=i;j++) 11 | { 12 | if(j==1|| i==j || i==n ) 13 | { 14 | printf("*"); 15 | } 16 | else 17 | printf(" "); 18 | } 19 | printf("\n"); 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Star Patterns/HalfDiamond.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n,m=1; 6 | printf("Enter the number of columns : "); 7 | scanf("%d",&n); 8 | for(int i=1;i<=n;i++) 9 | { 10 | for(int j=1;j<=i;j++) 11 | { 12 | printf("*"); 13 | } 14 | printf("\n"); 15 | } 16 | for(int i=n-1;i>=1;i--) 17 | { 18 | for(int j=1;j<=i;j++) 19 | { 20 | printf("*"); 21 | } 22 | printf("\n"); 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /CalculateTheNthTerm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int find_nth_term(int n, int a, int b, int c) { 6 | if(n==1) return a; 7 | if(n==2) return b; 8 | if(n==3) return c; 9 | return find_nth_term(n-1,a,b,c)+find_nth_term(n-2,a,b,c)+find_nth_term(n-3,a,b,c); 10 | } 11 | 12 | int main() { 13 | int n, a, b, c; 14 | scanf("%d %d %d %d", &n, &a, &b, &c); 15 | int ans = find_nth_term(n, a, b, c); 16 | printf("%d", ans); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Star Patterns/InvertedPyramid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n,m=1; 6 | printf("Enter the number of rows : "); 7 | scanf("%d",&n); 8 | 9 | for(int i=n;i>=1;i--) 10 | { 11 | for(int j=1;j 2 | int main() 3 | { 4 | int n,m; 5 | printf("Enter the number of rows : "); 6 | scanf("%d",&n); 7 | m=n; 8 | for(int i=1;i<=n;i++) 9 | { 10 | for(int j=1;j<=m-1;j++) 11 | { 12 | printf(" "); 13 | } 14 | for(int k=1;k<=2*i-1;k++) 15 | { 16 | printf("*"); 17 | } 18 | m--; 19 | 20 | printf("\n"); 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Star Patterns/InvertedLeftTriangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n,m; 6 | printf("Enter the number of rows : "); 7 | scanf("%d",&n); 8 | m=n; 9 | for(int i=1;i<=n;i++) 10 | { 11 | for(int j=1;j 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the number of rows : "); 6 | scanf("%d",&n); 7 | for(int i=1;i<=n;i++) 8 | { 9 | for(int j=1;j<=n;j++) 10 | { 11 | if(i==1 ||i==n||j==1||j==n) 12 | { 13 | printf("*"); 14 | } 15 | else 16 | printf(" "); 17 | } 18 | printf("\n"); 19 | } 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Star Patterns/PlusPattern.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | int n; 5 | printf("Enter the odd number only : "); 6 | scanf("%d", &n); 7 | for(int i=1;i<=n;i++) 8 | { 9 | if(i==((n/2)+1)) 10 | { 11 | for(int j=1;j<=n;j++) 12 | { 13 | printf("+"); 14 | } 15 | 16 | } 17 | else 18 | { 19 | for(int j=1;j<=n/2;j++) 20 | { 21 | printf(" "); 22 | } 23 | printf("+"); 24 | } 25 | printf("\n"); 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Star Patterns/HolloMirorRhombus.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the number of rows : "); 6 | scanf("%d",&n); 7 | for(int i=1;i<=n;i++) 8 | { 9 | for(int j=1;j 2 | 3 | int main() 4 | { 5 | int n; 6 | printf("Enter the number of rows : "); 7 | scanf("%d",&n); 8 | for(int i=n;i>=1;i--) 9 | { 10 | for(int j=1;j<=i-1;j++) 11 | { 12 | printf(" "); 13 | } 14 | for(int k=1;k<=n;k++) 15 | { 16 | if(i==1 || i==n || k==1 || k==n) 17 | printf("*"); 18 | else 19 | printf(" "); 20 | } 21 | printf("\n"); 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Star Patterns/LeftHollowTriangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n,m=1; 6 | printf("Enter the number of rows : "); 7 | scanf("%d",&n); 8 | for(int i=n;i>=1;i--) 9 | { 10 | for(int j=1;j<=i-1;j++) 11 | { 12 | printf(" "); 13 | } 14 | for(int k=1;k<=m;k++) 15 | { 16 | if(k==1 || k==m || m==n) 17 | printf("*"); 18 | else 19 | printf(" "); 20 | } 21 | printf("\n"); 22 | m++; 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Star Patterns/InvertedHollowPyramid.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n,m=1; 6 | printf("Enter the number of rows : "); 7 | scanf("%d",&n); 8 | 9 | for(int i=n;i>=1;i--) 10 | { 11 | for(int j=1;j 2 | 3 | int main() 4 | { 5 | int n,m; 6 | printf("Enter the number of rows : "); 7 | scanf("%d",&n); 8 | m=n; 9 | for(int i=1;i<=n;i++) 10 | { 11 | for(int j=1;j<=m-1;j++) 12 | { 13 | printf(" "); 14 | } 15 | for(int k=1;k<=2*i-1;k++) 16 | { 17 | if(k==1 || k==2*i-1 || i==n) 18 | printf("*"); 19 | else 20 | printf(" "); 21 | } 22 | m--; 23 | 24 | printf("\n"); 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Star Patterns/InvertedHollowLeftTriangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n,m; 6 | printf("Enter the number of rows : "); 7 | scanf("%d",&n); 8 | m=n; 9 | for(int i=1;i<=n;i++) 10 | { 11 | for(int j=1;j 2 | int main() 3 | { 4 | int n; 5 | printf("Enter the number of rows : "); 6 | scanf("%d",&n); 7 | for(int i=1;i<=n;i++) 8 | { 9 | for(int j=1;j<=n;j++) 10 | { 11 | if(i==1 ||i==n||j==1||j==n-i+1||i==j||j==n) 12 | { 13 | printf("*"); 14 | } 15 | else 16 | { 17 | 18 | printf(" "); 19 | } 20 | 21 | } 22 | printf("\n"); 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Projects/Election Poll/voting.c: -------------------------------------------------------------------------------- 1 | #include"election.h" 2 | 3 | int main(){ 4 | while(1){ 5 | printf("\n\t\t\t 1.Student panel \n\t\t\t 2.Admin panel \n\t\t\t 3.Exit \n\t\t\t Option:"); 6 | char input; 7 | scanf(" %c",&input); 8 | 9 | switch(input){ 10 | case '1': 11 | studentPanel(); 12 | break; 13 | case '2': 14 | adminPanel(); 15 | break; 16 | case '3': 17 | return 0; 18 | default: 19 | printf("\nInvalid option"); 20 | getch(); 21 | } 22 | } 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Star Patterns/Diamond.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main(void) { 3 | int n; 4 | printf("Enter the number of rows\n"); 5 | scanf("%d",&n); 6 | int spaces=n-1; 7 | int stars=1; 8 | for(int i=1;i<=n;i++) 9 | { 10 | for(int j=1;j<=spaces;j++) 11 | { 12 | printf(" "); 13 | } 14 | for(int k=1;k<=stars;k++) 15 | { 16 | printf("*"); 17 | } 18 | if(spaces>i) 19 | { 20 | spaces=spaces-1; 21 | stars=stars+2; 22 | } 23 | if(spaces 2 | 3 | int main(void) { 4 | 5 | int n; 6 | printf("Enter the number of columns : "); 7 | scanf("%d",&n); 8 | //printing the upper part of the pattern.. 9 | for(int i=0;i 2 | 3 | int main(void) { 4 | 5 | int n; 6 | printf("Enter the number of columns : "); 7 | scanf("%d",&n); 8 | //printing the upper part of the pattern.. 9 | for(int i=1;i<=n;i++) 10 | { 11 | for(int j=1;j<=n-i;j++) 12 | { 13 | printf(" "); 14 | } 15 | for(int k=0;k<=n-i;k++) 16 | { 17 | printf("*"); 18 | } 19 | printf("\n"); 20 | } 21 | for(int i=1;i 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | float valueOne; 10 | float valueTwo; 11 | char operator; 12 | float answer; 13 | 14 | printf("Enter calculation:\n\n"); 15 | scanf("%f %c %f", &valueOne, &operator, & valueTwo); 16 | 17 | switch(operator) 18 | { 19 | case '/': answer = valueOne/valueTwo; 20 | break; 21 | case '*': answer = valueOne*valueTwo; 22 | break; 23 | case '+': answer = valueOne+valueTwo; 24 | break; 25 | case '-': answer = valueOne-valueTwo; 26 | break; 27 | case '^': answer = pow(valueOne,valueTwo); 28 | break; 29 | case ' ': answer = sqrt(valueTwo); 30 | break; 31 | default: goto fail; 32 | } 33 | printf("%.9g%c%.9g = %.6g\n\n",valueOne,operator, valueTwo, answer); 34 | goto exit; 35 | fail: 36 | printf("Fail.\n"); 37 | exit: 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Projects/SnakeLadderGame.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int rd() 4 | { 5 | int rem; 6 | A:rem=rand()%7; 7 | if(rem==0) 8 | goto A; 9 | else 10 | return rem; 11 | } 12 | void displaychart(int curp,char player[4]) 13 | { int i,j,t,c,sft=0,diceres,pos1,pos2; 14 | 15 | 16 | if(curp==100) 17 | { 18 | printf("*****Congratulations*****\n\n\nPlayer %s wins\n",player); 19 | scanf("%*s"); 20 | exit(0); 21 | } 22 | 23 | for(i=10;i>0;i--) 24 | { 25 | t=i-1; 26 | if((sft%2)==0) 27 | { 28 | c=0; 29 | for(j=10;j>=1;j--) 30 | { 31 | diceres=(i*j)+(t*c++); 32 | 33 | if(curp==diceres) 34 | printf("%s\t",player); 35 | else 36 | printf("%d\t",diceres); 37 | 38 | } 39 | sft++; 40 | } 41 | else 42 | { 43 | c=9; 44 | for(j=1;j<=10;j++) 45 | { 46 | diceres=(i*j)+(t*c--); 47 | 48 | if(curp==diceres) 49 | printf("%s\t",player); 50 | else 51 | printf("%d\t",diceres); 52 | } 53 | 54 | 55 | sft++; 56 | } 57 | printf("\n\n"); 58 | } 59 | 60 | 61 | 62 | printf("--------------------------------------------------------------------------\n"); 63 | } 64 | void main() 65 | { 66 | int i,dice,cur_pos1=0,cur_pos2=0; 67 | char ch; 68 | while(1) 69 | { printf(" ** SNAKE AND LADDER GAME** \n\n"); 70 | printf("Snakes:- 25 to 9,\t 65 to 40,\t 99 to 1.\nLadder:- 13 to 42,\t 60 to 83,\t 70 to 93.\n"); 71 | printf("Choose your option\n"); 72 | printf("1. Player 1 plays\n"); 73 | printf("2. Player 2 plays\n"); 74 | printf("3. Exit\n"); 75 | scanf("%s",&ch); 76 | 77 | switch(ch) 78 | { 79 | 80 | case '1':dice=rd(); 81 | system("cls"); 82 | printf("\t\t\t\tDice = %d\n\n",dice); 83 | if(dice==6) 84 | printf("Dice=6: You have earned a chance to play one more time.\n"); 85 | cur_pos1=dice+cur_pos1; 86 | if(cur_pos1<101){ 87 | if(cur_pos1==99) 88 | { 89 | displaychart(1,"$P1$");//snake 90 | } 91 | if(cur_pos1==65) 92 | { 93 | displaychart(40,"$P1$");//snake 94 | } 95 | if(cur_pos1==25) 96 | { 97 | displaychart(9,"$P1$");//snake 98 | } 99 | if(cur_pos1==70) 100 | { 101 | displaychart(93,"$P1$");//ladder 102 | } 103 | if(cur_pos1==60) 104 | { 105 | displaychart(83,"$P1$");//ladder 106 | } 107 | if(cur_pos1==13) 108 | { 109 | displaychart(42,"$P1$");//ladder 110 | } 111 | else{ 112 | displaychart(cur_pos1,"$P1$"); 113 | } 114 | 115 | } 116 | else{ 117 | cur_pos1=cur_pos1-dice; 118 | printf("Range exceeded of Player 1.\n"); 119 | displaychart(cur_pos1,"$P1$"); 120 | } 121 | printf("Player 2 position is %d\n",cur_pos2); 122 | 123 | break; 124 | case '2':dice=rd(); 125 | system("cls"); 126 | printf("\t\t\t\tDice = %d\n\n",dice); 127 | cur_pos2=dice+cur_pos2; 128 | if(cur_pos2<101){ 129 | if(cur_pos2==99) //snake 130 | { 131 | displaychart(1,"$P2$"); 132 | } 133 | if(cur_pos2==65) //snake 134 | { 135 | displaychart(40,"$P2$"); 136 | } 137 | if(cur_pos2==25) //snake 138 | { 139 | displaychart(9,"$P2$"); 140 | } 141 | if(cur_pos2==70) //ladder 142 | { 143 | displaychart(93,"$P2$"); 144 | } 145 | if(cur_pos2==60) //ladder 146 | { 147 | displaychart(83,"$P2$"); 148 | } 149 | if(cur_pos2==13) //ladder 150 | { 151 | displaychart(42,"$P2$"); 152 | } 153 | else{ 154 | displaychart(cur_pos2,"$P2$"); 155 | } 156 | } 157 | 158 | else{ 159 | cur_pos2=cur_pos2-dice; 160 | printf("Range exceeded of Player 2.\n"); 161 | displaychart(cur_pos2,"$P2$"); 162 | } 163 | printf("Player 1 position is %d\n",cur_pos1); 164 | break; 165 | case '3':exit(0); 166 | break; 167 | 168 | default:printf("Incorrect choice.Try Again\n"); 169 | 170 | } 171 | 172 | } 173 | 174 | } 175 | -------------------------------------------------------------------------------- /Projects/TicTacToe.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #ifdef _WIN32 6 | #define OS "nt" 7 | #elif __unix__ 8 | #define OS "posix" 9 | #endif 10 | #define TRUE 1 11 | #define FALSE 0 12 | 13 | void board(char x, char o, unsigned char *u1, unsigned char *u2, char *a); 14 | void rules(); 15 | int checkforwin(char *a); // Checks wether a player won 16 | bool decision(char *x, char *o, unsigned char *u1); // points player to mark X or mark 0 17 | int main() 18 | { 19 | FILE *p; 20 | char x,o; 21 | p=fopen("score.txt","a+"); 22 | fclose(p); 23 | char a[9]={'1','2','3','4','5','6','7','8','9'}; 24 | char u1[50],u2[50]; 25 | int player=1; 26 | int choice,score=-1; 27 | char symbol,re; 28 | char start,dec; 29 | int s; 30 | if (OS=="nt") 31 | system("color 09"); 32 | rules(); 33 | printf("\n\nType 1 to start the game:-\nType 2 to view leader board:-\n"); 34 | scanf("%d",&s); 35 | switch(s) { 36 | case 1: 37 | do { 38 | p=fopen("score.txt", "a+"); 39 | printf("\nEnter name of player1: "); 40 | scanf("%s",u1); 41 | fprintf(p,"\n%s",u1); 42 | printf("Enter name of player2: "); 43 | scanf("%s",u2); 44 | fprintf(p,"\t%s",u2); 45 | fclose(p); 46 | !strcmp(u1,u2) ? printf("Enter names of different players!\n\n") : FALSE; 47 | } while(!strcmp(u1,u2)); 48 | decision(&x, &o, u1); 49 | if (OS=="nt") 50 | system("color fc"); 51 | board(x,o, u1, u2, a); 52 | do { 53 | player=((player%2)?1:2); 54 | if(player==1) 55 | printf("%s Type any digit from 1-9 to fill your response:- ",u1); 56 | else 57 | printf("%s Type any digit from 1-9 to fill your response:- ",u2); 58 | scanf("%d",&choice); 59 | symbol=((player==1)?x:o); 60 | if(choice==1 && a[0]=='1') 61 | a[0]=symbol; 62 | else if(choice==2 && a[1]=='2') 63 | a[1]=symbol; 64 | else if(choice==3 && a[2]=='3') 65 | a[2]=symbol; 66 | else if(choice==4 && a[3]=='4') 67 | a[3]=symbol; 68 | else if(choice==5 && a[4]=='5') 69 | a[4]=symbol; 70 | else if(choice==6 && a[5]=='6') 71 | a[5]=symbol; 72 | else if(choice==7 && a[6]=='7') 73 | a[6]=symbol; 74 | else if(choice==8 && a[7]=='8') 75 | a[7]=symbol; 76 | else if(choice==9 && a[8]=='9') 77 | a[8]=symbol; 78 | else { 79 | printf("Wrong Selection\n"); 80 | player--; 81 | } 82 | score=checkforwin(a); 83 | player++; 84 | board(x, o, u1, u2, a); 85 | } while(score == -1); 86 | p=fopen("score.txt","a+"); 87 | if(score==1) { 88 | if(player==2){ 89 | printf("\n\nPlayer1 %s Wins!\n\n",u1); 90 | fprintf(p,"\t%s",u1); 91 | getchar(); 92 | } 93 | else { 94 | printf("\n\nPlayer2 %s Wins!\n\n",u2);fprintf(p,"\t%s",u2); 95 | getchar(); 96 | } 97 | fclose(p); 98 | } 99 | else { 100 | printf("\n\nGame Draws!\n\n");fprintf(p,"\t%s","DRAW"); 101 | getchar(); 102 | } 103 | break; 104 | case 2: 105 | if (OS=="nt") 106 | system("cls"); 107 | if (OS=="posix") 108 | system("clear"); 109 | printf("\n\n"); 110 | printf("\tLEADERBOARD\n\n"); 111 | char c; 112 | p=fopen("score.txt","r"); 113 | while((c=getc(p))!=EOF) { 114 | printf("%c",c); 115 | } 116 | fclose(p); 117 | getchar(); 118 | break; 119 | default: 120 | printf("\n\nShould have typed 1 to play the game!\nHope to see you back soon!\n\n"); 121 | getchar(); 122 | break; 123 | } 124 | } 125 | int checkforwin(char *a) 126 | { 127 | if(a[0]==a[1] && a[1]==a[2]) 128 | return 1; 129 | else if(a[3]==a[4] && a[4]==a[5]) 130 | return 1; 131 | else if(a[6]==a[7] && a[7]==a[8]) 132 | return 1; 133 | else if(a[0]==a[3] && a[3]==a[6]) 134 | return 1; 135 | else if(a[1]==a[4] && a[4]==a[7]) 136 | return 1; 137 | else if(a[2]==a[5] && a[5]==a[8]) 138 | return 1; 139 | else if(a[0]==a[4] && a[4]==a[8]) 140 | return 1; 141 | else if(a[2]==a[4] && a[4]==a[6]) 142 | return 1; 143 | else if(a[0]!='1' && a[1]!='2' && a[2]!='3' && a[3]!='4' && a[4]!='5' && a[5]!='6' && a[6]!='7' && a[7]!='8' && a[8]!='9') 144 | return 0; 145 | else 146 | return -1; 147 | } 148 | 149 | void board(char x, char o, unsigned char *u1, unsigned char *u2, char *a) 150 | { 151 | int i; 152 | if (OS=="nt") 153 | system("cls"); 154 | if (OS=="posix") 155 | system("clear"); 156 | printf("\tTic-Tac-Toe\n\n"); 157 | printf("\n\n"); 158 | printf("%s:- (%c)\n%s:- (%c)\n\n\n",u1,x,u2,o); 159 | 160 | printf(" %c | %c | %c\n",a[0],a[1],a[2]); 161 | printf(" | | \n"); 162 | printf("----|----|----\n"); 163 | printf(" | | \n"); 164 | printf(" %c | %c | %c\n",a[3],a[4],a[5]); 165 | printf(" | | \n"); 166 | printf("----|----|----\n"); 167 | printf(" %c | %c | %c\n",a[6],a[7],a[8]); 168 | printf(" | | \n"); 169 | } 170 | void rules() 171 | { 172 | char link; 173 | printf("\tTic-Tac-Toe\n\n"); 174 | printf("Welcome to the most played 2D game and a sort of fun using X and O\n\n"); 175 | printf("Rules:-\n"); 176 | printf("\n1:Each player will be entering the number to put respective X or O in the desired position"); 177 | printf("\n2:Player who gets a combination of 3 same characters either diagonal or horizontally or \n vertically will be declared as the winner"); 178 | printf("\n\nEnjoy the game! Be a Winner!\n\n"); 179 | printf("For more classifications press Y else type any other character:- "); 180 | scanf("%c",&link); 181 | if(link=='y' || link=='Y') 182 | { 183 | if (OS=="nt") 184 | system("start Play-Tic-Tac-Toe"); 185 | if (OS=="posix") 186 | system("firefox Play-Tic-Tac-Toe"); 187 | } 188 | 189 | } 190 | bool decision(char *x, char *o, unsigned char *u1) 191 | { 192 | char dec; 193 | printf("\n\n"); 194 | do { 195 | printf("Player1 %s choose the X or 0:",u1); 196 | dec=getchar(); 197 | scanf("%c", &dec); 198 | } while(dec!='X' && dec!='x' && dec!='0'); 199 | if (dec=='X' || dec=='x') { 200 | *x='X'; 201 | *o='0'; 202 | } 203 | else { 204 | *x='0'; 205 | *o='X'; 206 | } 207 | return 1; 208 | } 209 | -------------------------------------------------------------------------------- /Projects/Election Poll/election.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | struct currentValidID{ 7 | int year; 8 | char branch[6]; 9 | int totalVoters; 10 | }; 11 | typedef struct candidate{ 12 | int cid; 13 | char cname[20]; 14 | int votes; 15 | }CANDIDATE; 16 | 17 | //GLOBALS -------------------------------------------------------- 18 | struct currentValidID currentValidID; //stores current Valid user ID parameters 19 | CANDIDATE candidateArray[20]; //to store information all candidates 20 | int numberOfCandidates; //Total number of candidates standing for election 21 | char studentVotes[200]; //to store information of votes given by each student 22 | //---------------------------------------------------------------- 23 | 24 | //To extract year from userID -- For example, userID:2018btecs00064 year:2018 25 | int extractYear(char userID[15]) 26 | { 27 | int year=0; 28 | char tmp; 29 | for(int i=0;i<4;i++){ 30 | tmp=userID[i]; 31 | year=(year*10)+(tmp-48); 32 | } 33 | return year; 34 | } 35 | 36 | int extractRollNo(char userID[15]) 37 | { 38 | int rollno=0; 39 | char tmp; 40 | for(int i=9;i<14;i++){ 41 | tmp=userID[i]; 42 | rollno=(rollno*10)+(tmp-48); 43 | } 44 | return rollno; 45 | } 46 | 47 | //Will check whether the global branch code and inputed branch code is matching or not 48 | int checkBranchCode(char userID[15]) 49 | { 50 | char branchCode[6]; 51 | for(int i=4;i<9;i++){ 52 | branchCode[i-4]=userID[i]; 53 | } 54 | branchCode[5]='\0'; 55 | if(strcmp(branchCode,currentValidID.branch)==0) 56 | return 1; 57 | else 58 | return 0; 59 | } 60 | 61 | int authenticateAdmin(){ 62 | char username[15], password[6]; 63 | 64 | printf("\nEnter username: "); 65 | scanf("%s",username); 66 | if((strcmp(username,"Admin"))!=0) 67 | return 0; 68 | else 69 | { 70 | printf("Enter Password: "); 71 | int i=0; 72 | for(i=0;i<5;i++) 73 | { 74 | password[i]=getch(); 75 | printf("%c",'*'); 76 | } 77 | password[i]='\0'; 78 | if((strcmp(password,"admiN"))!=0) 79 | return 0; 80 | } 81 | return 1; 82 | } 83 | 84 | void banID(){ 85 | printf("\nCreating Banned.txt...\n"); 86 | FILE *fp=fopen("Banned.txt", "w"); 87 | if(fp==NULL){ 88 | printf("Error: Banned.txt not created.\n"); 89 | fclose(fp); 90 | return; 91 | } 92 | printf("Just Enter last roll no to ban\nPress 0 to exit... "); 93 | int input; 94 | while(1){ 95 | printf("\nEnter Number: "); 96 | scanf("%d",&input); 97 | if(input==0) 98 | break; 99 | studentVotes[input-1]='$'; 100 | fprintf(fp,"%d\n",input); 101 | } 102 | fclose(fp); 103 | printf("Created Successfully\n"); 104 | } 105 | 106 | void createCandidateFiles(){ 107 | printf("\nCreating candidate files...\n"); 108 | FILE *fp; 109 | char filename[20]; 110 | for(int i = 1;i <= numberOfCandidates; i++){ 111 | sprintf(filename,"candidate%d.txt",i); 112 | fp=fopen(filename,"w"); 113 | fprintf( 114 | fp,"0\n%s", 115 | candidateArray[i-1].cname 116 | ); 117 | fclose(fp); 118 | } 119 | printf("Created Files successfully\n"); 120 | } 121 | 122 | void deleteIllegalVote(char userID[15]) 123 | { 124 | FILE *fp,*fcp; 125 | char filename[20]; 126 | char line[20]; 127 | 128 | int location = extractRollNo(userID); 129 | sprintf(filename,"candidate%d.txt",candidateArray[studentVotes[location-1]-49].cid); 130 | candidateArray[studentVotes[location-1]-49].votes--; 131 | studentVotes[location-1]='0'; 132 | if ((fp = fopen(filename,"r")) == NULL) 133 | { 134 | printf("\nFile cannot be opened...Operation Failed"); 135 | return; 136 | } 137 | printf("\nDeleting in process...\n "); 138 | if ((fcp = fopen("tmp.txt","w")) == NULL) 139 | { 140 | printf("\nFile cannot be opened...Operation Failed"); 141 | return; 142 | } 143 | 144 | while (!feof(fp)) 145 | { 146 | fscanf(fp,"%s",line); 147 | fprintf(fcp,"%s\n",line); 148 | } 149 | fclose(fp); 150 | fclose(fcp); 151 | if ((fp = fopen(filename,"w")) == NULL) 152 | { 153 | printf("\nFile cannot be opened...Operation Failed"); 154 | return; 155 | } 156 | int numFromFile; 157 | char cnameFromFile[20]; 158 | fcp = fopen("tmp.txt","r"); 159 | fscanf(fcp,"%d",&numFromFile); 160 | fprintf(fp,"%d",numFromFile-1); 161 | fscanf(fcp,"%s",cnameFromFile); 162 | fprintf(fp,"\n%s",cnameFromFile); 163 | while(!feof(fcp)){ 164 | fscanf(fcp,"%d",&numFromFile); 165 | if(numFromFile!=location) 166 | fprintf(fp,"\n%d",numFromFile); 167 | } 168 | fclose(fp); 169 | fclose(fcp); 170 | remove("tmp.txt"); 171 | printf("\nVote deleted successfully\nPress any key to continue..."); 172 | getch(); 173 | } 174 | 175 | int getWinner(){ 176 | int maxV = -1; 177 | int winnerCid; 178 | for(int i = 0;i < numberOfCandidates; i++){ 179 | if(candidateArray[i].votes > maxV) { 180 | winnerCid = candidateArray[i].cid; 181 | maxV = candidateArray[i].votes; 182 | } 183 | else if(candidateArray[i].votes == maxV) { 184 | return -1; 185 | } 186 | } 187 | return winnerCid; 188 | } 189 | 190 | void initiateNewElection() 191 | { 192 | printf("\nNew Election Initiation:\n"); 193 | 194 | printf("\nElections for which Year: "); 195 | scanf("%d",¤tValidID.year); 196 | printf("Enter branch code:"); 197 | scanf("%s",currentValidID.branch); 198 | printf("Enter max roll no.:"); 199 | scanf("%d",¤tValidID.totalVoters); 200 | printf("Enter the no. of candidates:"); 201 | scanf("%d",&numberOfCandidates); 202 | 203 | for (int i = 0; i < currentValidID.totalVoters; i++) 204 | { 205 | studentVotes[i] = '0'; 206 | } 207 | 208 | for (int i = 0;i < numberOfCandidates; i++) 209 | { 210 | candidateArray[i].cid=i+1; 211 | printf("Enter name of candidate %d: ",i+1); 212 | scanf(" %s",candidateArray[i].cname); 213 | candidateArray[i].votes=0; 214 | } 215 | return; 216 | } 217 | 218 | void saveElectionInfoInFile(){ 219 | printf("Saving Election Info in File...\n"); 220 | FILE *fp = fopen("ElectionInfo.txt", "w"); 221 | if(fp==NULL) 222 | { 223 | printf("\nError in file creation\n"); 224 | fclose(fp); 225 | return; 226 | } 227 | fprintf( 228 | fp,"%d\n%s\n%d\n%d", 229 | currentValidID.year, 230 | currentValidID.branch, 231 | currentValidID.totalVoters, 232 | numberOfCandidates 233 | ); 234 | fclose(fp); 235 | printf("Saved Successfully : )"); 236 | } 237 | 238 | void loadElectionInfoFromFile() 239 | { 240 | FILE *f1,*f2,*f3; 241 | f1=fopen("ElectionInfo.txt","r"); 242 | if(f1==NULL) 243 | printf("Not Exist"); 244 | fscanf(f1,"%d",¤tValidID.year); 245 | fseek(f1,2,SEEK_CUR); 246 | fscanf(f1,"%s",currentValidID.branch); 247 | fseek(f1,2,SEEK_CUR); 248 | fscanf(f1,"%d",¤tValidID.totalVoters); 249 | fseek(f1,2,SEEK_CUR); 250 | fscanf(f1,"%d",&numberOfCandidates); 251 | fclose(f1); 252 | 253 | //load candidates info and student votes 254 | for (int i = 0; i < currentValidID.totalVoters; i++) 255 | { 256 | studentVotes[i] = '0'; 257 | } 258 | for(int i=1;i<=numberOfCandidates;i++) 259 | { 260 | int location; 261 | char filename[20]; 262 | sprintf(filename,"candidate%d.txt",i); 263 | f2=fopen(filename,"r+"); 264 | candidateArray[i-1].cid=i; 265 | fscanf(f2,"%d",&candidateArray[i-1].votes); 266 | fscanf(f2,"%s",candidateArray[i-1].cname); 267 | while(!feof(f2)){ 268 | fscanf(f2,"%d",&location); 269 | studentVotes[location-1] = i+48; 270 | } 271 | fclose(f2); 272 | } 273 | 274 | //load banned votes 275 | int location; 276 | f3=fopen("banned.txt","r+"); 277 | while(!feof(f3)){ 278 | fscanf(f3,"%d",&location); 279 | studentVotes[location-1] = '$'; 280 | } 281 | fclose(f3); 282 | 283 | } 284 | 285 | void adminPanel() 286 | { 287 | while(1){ 288 | 289 | if(authenticateAdmin()!=1){ 290 | printf("\n Wrong Username or Password \n"); 291 | break; 292 | } 293 | 294 | printf("\n\nLOGGED IN SUCCESSFULLY (Press Enter)"); 295 | getch(); 296 | 297 | while(1) 298 | { 299 | char inputID[15]; 300 | char input;char banInp; 301 | int WinnerCid, totalVotedNow=0; 302 | printf("\n1.New Election\n2.Continue Previous Election\n3.Delete Illegal Vote\n4.Ban User IDs\n5.Result\n6.Logout\nOption:"); 303 | scanf(" %c",&input); 304 | 305 | switch(input) 306 | { 307 | case '1': 308 | initiateNewElection(); 309 | saveElectionInfoInFile(); 310 | createCandidateFiles(); 311 | break; 312 | case '2': 313 | loadElectionInfoFromFile(); 314 | break; 315 | case '3': 316 | printf("\nEnter user ID to delete its vote: "); 317 | scanf("%s",inputID); 318 | deleteIllegalVote(inputID); 319 | break; 320 | case '4': 321 | printf("Do you want to ban particular ID's?\nPress 1 if yes or any other key to continue..."); 322 | scanf(" %c",&banInp); 323 | if(banInp=='1'){ 324 | banID(); 325 | } 326 | break; 327 | case '5': 328 | WinnerCid = getWinner(); 329 | if(WinnerCid != -1){ 330 | printf("\nWinner is %s with %d votes\n",candidateArray[WinnerCid-1].cname,candidateArray[WinnerCid-1].votes); 331 | } 332 | else{ 333 | printf("\nIts A TIE"); 334 | } 335 | printf("\nFull Result\n"); 336 | for(int i=0;i %d votes\n",candidateArray[i].cid,candidateArray[i].cname,candidateArray[i].votes); 339 | } 340 | printf("\nVoting Percentage: %d %%\n\n",(totalVotedNow*100)/currentValidID.totalVoters); 341 | break; 342 | case '6': 343 | return; 344 | default: 345 | printf("Invalid Option"); 346 | getch(); 347 | } 348 | 349 | } 350 | } 351 | 352 | }; 353 | 354 | 355 | int isValid(char userID[15]) 356 | { 357 | if(strlen(userID)!=14) 358 | return 0; 359 | 360 | int inputedYear=extractYear(userID); 361 | int inputedRollNo = extractRollNo(userID); 362 | 363 | if(inputedYear!=currentValidID.year || checkBranchCode(userID)!=1 || inputedRollNo>currentValidID.totalVoters) 364 | return 0; 365 | 366 | return 1; 367 | } 368 | 369 | int isVoted(char userID[15]) 370 | { 371 | int location=extractRollNo(userID); 372 | if(studentVotes[location-1]=='0') 373 | return 0; 374 | else 375 | return 1; 376 | } 377 | 378 | int isBanned(char userID[15]){ 379 | int location=extractRollNo(userID); 380 | if(studentVotes[location-1]=='$') 381 | return 1; 382 | else 383 | return 0; 384 | } 385 | 386 | void saveVote(char userID[15],char voteInput) 387 | { 388 | char filename[20]; 389 | sprintf(filename,"candidate%d.txt",voteInput-48); 390 | FILE *fp = fopen(filename,"r+"); 391 | int location=extractRollNo(userID); 392 | studentVotes[location-1]=voteInput; 393 | candidateArray[voteInput-49].votes++; 394 | fseek(fp, 0, SEEK_SET); 395 | fprintf(fp,"%d\n",candidateArray[voteInput-49].votes); 396 | fseek(fp, 0, SEEK_END); 397 | fprintf(fp,"\n%d",location); 398 | fclose(fp); 399 | } 400 | 401 | 402 | void studentPanel() 403 | { 404 | char userID[15]; 405 | char voteInput; 406 | while(1) 407 | { 408 | printf("\n\n To exit press 0"); 409 | printf("\n Enter user ID:"); 410 | scanf("%s",userID); 411 | if(strcmp(userID, "0")==0) 412 | return; 413 | if(isValid(userID)!=1) 414 | { 415 | printf("\n Invalid User ID(Press Enter)"); 416 | getch(); 417 | continue; 418 | } 419 | if(isBanned(userID)!=0) 420 | { 421 | printf("\nThis User ID is currently banned...\nContact Admin for the reason...(Press Enter to continue)"); 422 | getch(); 423 | continue; 424 | } 425 | if(isVoted(userID)!=0) 426 | { 427 | printf("\n Your PRN entered is already voted\n Contact Admin for furthur query"); 428 | getch(); 429 | continue; 430 | } 431 | printf("\n\n Candidates for election:"); 432 | for (int i = 0; i < numberOfCandidates; i++) 433 | { 434 | printf("\n %d. %s",i+1,candidateArray[i].cname); 435 | } 436 | printf("\n\n Your Vote(Enter Number):"); 437 | voteInput=getch(); 438 | printf("*"); 439 | if(voteInput-48 < 1 || voteInput-48 > numberOfCandidates) 440 | { 441 | printf("\nInvalid Vote\nTry Again..."); 442 | getch(); 443 | continue; 444 | } 445 | saveVote(userID,voteInput); 446 | printf("\n\nThanks for your precious vote(Press Enter)"); 447 | getch(); 448 | } 449 | }; --------------------------------------------------------------------------------