├── Array Practice Questions.docx ├── Arrays Questions with Solutions.pdf ├── Arrays.pdf ├── CompetitiveProggramming ├── ChristmasTreePattern.c ├── Food_Chain.c ├── LeadGame.c ├── Red_Alert.c ├── Richie_Rich.c ├── circle_point.c ├── cricketmatch.c ├── eight_queens.c ├── pattern1.c ├── pattern2.c ├── pattern3.c └── round_Robin.c ├── Formatted input and output.pdf ├── Functions Practice Sheet.docx ├── IF-ELSE AND LOOPS NOTES.pdf ├── LOOP AND SWITCH QUESTIONS WITH ANSWERS.pdf ├── MCQ's on INPUT AND OUTPUT.pdf ├── OPERATORS QUESTIONS WITH ANSWERS.pdf ├── Operators notes.pdf ├── Output based uestions on if else and switch case.pdf ├── Pointers MCQ.pdf ├── Pointers in C.pdf ├── Practice sheet of Operators 2.pdf ├── Practice sheet of operators 1.docx ├── String all question.pdf ├── Strings.pdf ├── carrer 360 ├── Adding Fractions ├── Address of 1D ├── Anagram ├── Array operations ├── Automorpic number ├── Basic Game ├── Binary Search ├── Binary Tree Traversal ├── Complex Bitwise operations ├── DMA ├── Even Fabonacci Numbers ├── RockPaperScissors ├── Rusian Multiplication ├── ZigZac ├── camelcase ├── counting sort ├── diagonal difference ├── dice roll ├── digital root ├── dynamic2darrayusingpointer ├── dynamictwodarrayusingarrayofpointers ├── findingfirstmissingnaturalnumbber ├── heap sort ├── insertionsort ├── kth smallest number in array ├── lexicographicsorting ├── linear search ├── link list add del ├── link list all ├── linklist creation ├── magic number ├── mergesort ├── mirrornumber ├── p_queue ├── pattern spiral ├── replacing pi with 3.14 ├── selection sort ├── slicestring ├── sparcematrix ├── spiralmatrix ├── stack link list └── stack using link list ├── complete_square.c ├── decision control statements.pdf ├── dimond.c ├── full_pyramid.c ├── full_square_alphbet.c ├── full_square_with_number.c ├── functions.pdf ├── half_pyramid.c ├── holo_half_pyramid.c ├── holo_sqare_with_number.c ├── holo_square.c ├── holosquare_with_diagonal.c ├── if else questions_c language.pdf ├── inverted_full_pyramid.c └── zigzag.c /Array Practice Questions.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/Array Practice Questions.docx -------------------------------------------------------------------------------- /Arrays Questions with Solutions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/Arrays Questions with Solutions.pdf -------------------------------------------------------------------------------- /Arrays.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/Arrays.pdf -------------------------------------------------------------------------------- /CompetitiveProggramming/ChristmasTreePattern.c: -------------------------------------------------------------------------------- 1 | /* 2 | Christmas Tree Pattern 3 | input 3 4 | output 5 | * 6 | *** 7 | ***** 8 | ******* 9 | *** 10 | ***** 11 | * 12 | * 13 | We divide the problem into 3 parts: 14 | 1. Full triangle 15 | 2. Partial Triangle 16 | 3. Stand 17 | */ 18 | #include 19 | 20 | void fulltriangle(int row) 21 | { 22 | for(int i=1;i<=row;i++) 23 | { 24 | printf("%*s",row-i,""); 25 | for(int j=1;j<=(2*i-1);j++) 26 | { 27 | printf("*"); 28 | } 29 | printf("\n"); 30 | } 31 | } 32 | void partialtriangle(int row,int offset) 33 | { 34 | for(int i=1;i<=row;i++) 35 | { 36 | printf("%*s",offset,""); 37 | printf("%*s",row-i,""); 38 | for(int j=1;j<=(2*i+1);j++) 39 | { 40 | printf("*"); 41 | } 42 | printf("\n"); 43 | } 44 | } 45 | void stand(int row) 46 | { 47 | for(int i=1;i<=2;i++) 48 | { 49 | printf("%*s",row,""); 50 | printf("*\n"); 51 | } 52 | } 53 | int main(int argc, char const *argv[]) 54 | { 55 | int n,offset=1; 56 | scanf("%d",&n); 57 | fulltriangle(n+1); 58 | for(int j=n-1;j>=2;j--) 59 | { 60 | partialtriangle(j,offset); 61 | offset++; 62 | } 63 | stand(n); 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /CompetitiveProggramming/Food_Chain.c: -------------------------------------------------------------------------------- 1 | /* PROBLEM STATEMENT 2 | A great deal of energy is lost as metabolic heat when the organisms from one trophic level are consumed by the next level. 3 | Suppose in Chefland the energy reduces by a factor of K, i.e, if initially, the energy is X, then the transfer of energy to the next tropic level is ⌊XK⌋. This limits the length of foodchain which is defined to be the highest level receiving non-zero energy. 4 | E is the energy at the lowest tropic level. Given E and K for an ecosystem, find the maximum length of foodchain. 5 | Note: ⌊x⌋ denoted the floor function, and it returns the greatest integer that is less than or equal to x (i.e rounds down to the nearest integer). For example, ⌊1.4⌋=1, ⌊5⌋=5, ⌊−1.5⌋=−2, ⌊−3⌋=−3 , ⌊0⌋=0. 6 | Input Format 7 | First line will contain T, number of testcases. Then the testcases follow. 8 | Each testcase contains a single line of input, two integers E,K. 9 | Output Format 10 | For each testcase, output in a single line answer to the problem. 11 | Constraints 12 | 1≤T≤104 13 | 1≤E≤109 14 | 2≤K≤109 15 | Sample Input 1 16 | 3 17 | 5 3 18 | 6 7 19 | 10 2 20 | Sample Output 1 21 | 2 22 | 1 23 | 4 24 | Explanation 25 | TestCase 1: The energy at first level is 5 units. For the second level energy becomes ⌊53⌋=1 units. So the length of foodchain is 2 since from the next level onwards 0 units of energy will be received. 26 | TestCase 3: The energy at different levels is: 27 | Level 1- 10 units 28 | Level 2- ⌊102⌋=5 units 29 | Level 3- ⌊52⌋=2 units 30 | Level 4- ⌊22⌋=1 units 31 | Level 5- ⌊12⌋=0 units 32 | So the answer is 4, since it is the last level to receive non-zero energy. */ 33 | #include 34 | 35 | int main(void) { 36 | 37 | int t,e,k; 38 | int i; int j=0; 39 | scanf("%d",&t); 40 | int tc=t; int arr[t]; 41 | while(t--) 42 | { 43 | scanf("%d %d",&e,&k); 44 | int count=0; 45 | i=e; 46 | while(i>0) 47 | { 48 | count++; 49 | i=i/k; 50 | 51 | } 52 | arr[j]=count; 53 | j++; 54 | } 55 | 56 | for(j=0;j 38 | using namespace std; 39 | int main() 40 | { 41 | int n, lead=INT_MIN,p1=0,p2=0,winner=0; 42 | cin >> n; 43 | for (int i = 0; i < n; i++) 44 | { 45 | int a,b; 46 | cin>>a>>b; 47 | //Adding the scores of player1 and player2 so that we can store there cumulative score sum in p1 and p2 48 | p1+=a; 49 | p2+=b; 50 | //If the cumulative sum of p1 is greater than p2 51 | if(p1>p2){ 52 | //and their difference is greater than lead than the winner will be player 1 53 | if(p1-p2>lead){ 54 | lead=p1-p2; 55 | winner=1; 56 | } 57 | } 58 | 59 | else{ 60 | //if their difference is greater than lead than the winner will be player 2 61 | if(p2-p1>lead){ 62 | lead=p2-p1; 63 | winner=2; 64 | } 65 | } 66 | } 67 | cout<0, the water level of the city increases by Ai millimetres on the i-th day. 4 | If Ai=0, there is no rain on the i-th day. The water level of the city decreases by D millimetres on such a day. However, if the water level is less than D millimetres before the i-th day, then it becomes zero instead. 5 | There will be a red alert in the city if the water level becomes strictly greater than H millimetres on at least one of the N days. Determine if there will be a red alert. 6 | Input Format 7 | The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. 8 | The first line of each test case contains three space-separated integers N, D and H. 9 | The second line contains N space-separated integers A1,A2,…,AN. 10 | Output Format 11 | For each test case, print a single line containing the string "YES" if there will be a red alert or "NO" otherwise. 12 | Constraints 13 | 1≤T≤103 14 | 1≤N,D≤102 15 | 0≤Ai≤102 for each valid i 16 | 1≤H≤104 17 | Subtasks 18 | Subtask #1 (100 points): original constraints 19 | Sample Input 1 20 | 4 21 | 4 2 6 22 | 1 3 0 2 23 | 2 1 100 24 | 1 100 25 | 4 2 3 26 | 1 2 0 2 27 | 3 7 10 28 | 5 3 9 29 | Sample Output 1 30 | NO 31 | YES 32 | NO 33 | YES 34 | Explanation 35 | Example case 1: 36 | On the first day, the water level of the city increases to 1 millimtre. 37 | On the second day, the water level increases by 3 millimeters and becomes 1+3=4 millimtres. 38 | On the third day, there is no rain in the city, so the water level decreases by D=2 millimtres and becomes 4−2=2 millimtres. 39 | On the fourth day, the water level increases by 2 millimtres and becomes 2+2=4 millimtres. 40 | There will be no red alert in the city because the water level does not exceed H=6 millimtres on any of the four days. 41 | Example case 2: The water level of the city on the 2-nd day is equal to 101 millimtres, which is greater than H=100 millimtres, so there will be a red alert in the city. 42 | Example case 3: The water levels of the city on the four days are [1,3,1,3]. The water level is equal to H=3 millimtres on the second and fourth day, but it does not exceed the threshold. 43 | Example case 4: There will be a red alert in the city on the 3-rd day. */ 44 | 45 | // SOLUTION 46 | 47 | #include 48 | 49 | int main(void) { 50 | 51 | int t,n,d,h; 52 | 53 | scanf("%d",&t); 54 | // t represents the number of test cases 55 | 56 | while(t--) //the loop runs for t times 57 | { 58 | scanf("%d %d %d",&n,&d,&h); 59 | // n, d and h respectively stores the input values 60 | int sum=0;int val; int ret=0; 61 | for(int i=0;i0) 67 | { 68 | //if val( the level of water on ith day > 0 69 | sum+=val; // total level = previous(stored in sum itsef) + val; 70 | 71 | } 72 | if(val==0) 73 | { 74 | //if val( the level of water on ith day = 0 75 | sum=(sumh) //checks the level each day (level is calculated by carrying out arithmetic operations on variable val which is finally stored in variable sum 80 | { 81 | ret=1; //if red alert i.e. sum > h , the variable ret is initialised with value 1. 82 | break; 83 | } 84 | } 85 | if(ret==0) 86 | printf("NO\n"); 87 | else 88 | printf("YES\n"); 89 | // Time Complexity : O(N) 90 | // Space Complexity: 0(1) 91 | 92 | } 93 | 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /CompetitiveProggramming/Richie_Rich.c: -------------------------------------------------------------------------------- 1 | /* Chef aims to be the richest person in Chefland by his new restaurant franchise. Currently, his assets are worth A billion dollars and have no liabilities. He aims to increase his assets by X billion dollars per year. 2 | Also, all the richest people in Chefland are not planning to grow and maintain their current worth. 3 | To be the richest person in Chefland, he needs to be worth at least B billion dollars. How many years will it take Chef to reach his goal if his value increases by X billion dollars each year? 4 | Input 5 | The first line contains an integer T, the number of test cases. Then the test cases follow. 6 | Each test case contains a single line of input, three integers A, B, X. 7 | Output 8 | For each test case, output in a single line the answer to the problem. 9 | Constraints 10 | 1≤T≤21 000 11 | 100≤A 30 | 31 | int main(void) { 32 | 33 | int t,a,b,x; 34 | scanf("%d",&t); 35 | 36 | int i;int time[t-1]; 37 | for(i=0;i 7 | #include 8 | int main() 9 | { 10 | float h , k; 11 | printf("\n Enter the X , Y center co-ordinates of a circle : \n"); 12 | scanf("%f %f",&h ,&k); 13 | printf("\n Enter the radius :\n"); 14 | float radii; 15 | scanf("%f",&radii); 16 | float eqn; 17 | float x ,y; 18 | printf("\n Enter the X and Y co-ordinates of point A : \n"); 19 | scanf("%f %f",&x ,&y); 20 | float t=(h*h)+(k*k)-(radii*radii); 21 | eqn=(x*x)-(2*h*x)+(y*y)-(2*k*y)+t; 22 | printf("Equation of the circle : (x-%0.2f)^2 + (y-%0.2f)^2 -%0.2f \n",h,k,radii*radii); 23 | if(eqn<0) 24 | { 25 | printf("\n Inside the circle "); 26 | } 27 | else if(eqn>0) 28 | { 29 | printf("\n Outside the circle "); 30 | } 31 | else if(eqn==0) 32 | { 33 | printf("\n On the circumference of the circle .\n"); 34 | } 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /CompetitiveProggramming/cricketmatch.c: -------------------------------------------------------------------------------- 1 | /* PROBLEM STATEMENT 2 | In a season, each player has three statistics: runs, wickets, and catches. Given the season stats of two players A and B, denoted by R, W, and C respectively, 3 | the person who is better than the other in the most statistics is regarded as the better overall player. Tell who is better amongst A and B. It is known that in each statistic, 4 | the players have different values. 5 | Input Format 6 | The first line contains an integer T, the number of test cases. Then the test cases follow. 7 | Each test case contains two lines of input. 8 | The first line contains three integers R1, W1, C1, the stats for player A. 9 | The second line contains three integers R2, W2, C2, the stats for player B. 10 | Output Format 11 | For each test case, output in a single line "A" (without quotes) if player A is better than player B and "B" (without quotes) otherwise. 12 | Constraints 13 | 1≤T≤1000 14 | 0≤R1,R2≤500 15 | 0≤W1,W2≤20 16 | 0≤C1,C2≤20 17 | R1≠R2 18 | W1≠W2 19 | C1≠C2 20 | Sample Input 1 21 | 3 22 | 0 1 2 23 | 2 3 4 24 | 10 10 10 25 | 8 8 8 26 | 10 0 10 27 | 0 10 0 28 | Sample Output 1 29 | B 30 | A 31 | A 32 | Explanation 33 | Test Case 1: Player B is better than A in all 3 fields. 34 | Test Case 2: Player A is better than B in all 3 fields. 35 | Test Case 3: Player A is better than B in runs scored and number of catches. */ 36 | 37 | 38 | #include 39 | 40 | int main(void) { 41 | int i=0; 42 | int tc; 43 | // tc is to take input the number of test cases 44 | scanf("%d",&tc); 45 | int a[tc]; 46 | int t=tc; 47 | int r1,w1,c1,r2,w2,c2; 48 | while(tc--) 49 | { 50 | scanf("%d %d %d",&r1,&w1,&c1); //stores respective inputs for player A 51 | scanf("%d %d %d",&r2,&w2,&c2); //stores respective inputs for player B 52 | 53 | if(((w1>w2)&&(r1>r2)&&(c1>c2))||((w1>w2)&&(c1>c2))||((c1>c2)&&(r1>r2))||((r1>r2)&&(w1>w2))) 54 | { 55 | a[i]=1; //stores 1 in the array for results favouring player A 56 | } 57 | else 58 | { 59 | a[i]=2; //stores 1 in the array for results favouring player B 60 | } 61 | i++; 62 | } 63 | 64 | for(int j=0;j 6 | void solutionFinder(int tot_queens); 7 | void NQueenSolver(int queenPositions[], int tot_queens, int queenNum); 8 | int isSafe(int queenPositions[], int tot_queens, int choice, int queenNum); 9 | #define SAFE 1 10 | #define UNSAFE 0 11 | void printBoard(int queenPositions[], int tot_queens); 12 | int main() 13 | { 14 | solutionFinder(8); 15 | return 0; 16 | } 17 | void solutionFinder(int tot_queens) 18 | { 19 | int queenPositions[tot_queens]; 20 | int ind; 21 | for (ind = 0; ind < tot_queens; ind++) 22 | queenPositions[ind] = -1; //no queens placed at start 23 | NQueenSolver(queenPositions, tot_queens, 0); 24 | } 25 | void NQueenSolver(int queenPositions[], int tot_queens, int queenNum) 26 | { 27 | //choices are 0 to tot_queens-1 28 | int choice; 29 | if (tot_queens == queenNum) // 30 | { 31 | printBoard(queenPositions, tot_queens); 32 | getch(); 33 | return; 34 | } 35 | for (choice = 0; choice < tot_queens; choice++) 36 | { 37 | if (isSafe(queenPositions, tot_queens, choice, queenNum) == SAFE) 38 | { 39 | //do certain things 40 | queenPositions[queenNum] = choice; 41 | NQueenSolver(queenPositions, tot_queens, queenNum + 1); 42 | //undo 43 | queenPositions[queenNum] = -1; 44 | } 45 | } 46 | } 47 | int isSafe(int queenPositions[], int tot_queens, int choice, int queenNum) 48 | { 49 | int row, col; 50 | //left top 51 | for (row = queenNum - 1, col = choice - 1; row >= 0 && col >= 0; row--, col--) 52 | if (queenPositions[row] == col) 53 | return UNSAFE; 54 | //top 55 | for (row = queenNum - 1, col = choice; row >= 0; row--) 56 | if (queenPositions[row] == col) 57 | return UNSAFE; 58 | //right top 59 | for (row = queenNum - 1, col = choice + 1; row >= 0 && col < tot_queens; row--, col++) 60 | if (queenPositions[row] == col) 61 | return UNSAFE; 62 | return SAFE; 63 | } 64 | void printBoard(int queenPositions[], int tot_queens) 65 | { 66 | int row, col; 67 | for (printf("\n"), row = 0; row < tot_queens; row++, printf("\n")) 68 | { 69 | for (col = 0; col < tot_queens; col++) 70 | { 71 | if (queenPositions[row] == col) 72 | printf(" Q "); 73 | else 74 | printf(" - "); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /CompetitiveProggramming/pattern1.c: -------------------------------------------------------------------------------- 1 | /*WAP TO PRINT THE PATTERN AS GIVEN BELOW. 2 | INPUT: 4 3 | 1 2 3 4 3 2 1 4 | 5 6 7 7 6 5 5 | 8 9 9 8 6 | 10 10 7 | 8 9 9 8 8 | 5 6 7 7 6 5 9 | 1 2 3 4 3 2 1 10 | */ 11 | #include 12 | #include 13 | int main() 14 | { 15 | //clrscr(); 16 | int i,j,n,k=0,c; 17 | printf("ENTER THE VALUE OF N:\n"); 18 | scanf("%d",&n); 19 | for(i=1;i<=n;i++) 20 | { 21 | for(j=1;j<=n-i+1;j++) 22 | printf("%3d",++k); 23 | for(j=i-1;j>=1;j--) 24 | printf("%3c",' '); 25 | for(j=i-1;j>1;j--) 26 | printf("%3c",' '); 27 | c=k; 28 | if(i==1) 29 | c=c-1; 30 | for(j=1;j<=n-i+1&&c!=0;j++) 31 | printf("%3d",c--); 32 | printf("\n"); 33 | }//end of upper block 34 | int t=2; 35 | for(i=n-1;i>=1;i--) 36 | { 37 | k=k-t; 38 | t++; 39 | for(j=1;j<=n-i+1;j++) 40 | printf("%3d",k+j-1); 41 | for(j=i-1;j>=1;j--) 42 | printf("%3c",' '); 43 | for(j=i-1;j>1;j--) 44 | printf("%3c",' '); 45 | c=k+t-2; 46 | if(i==1) 47 | c=c-1; 48 | for(j=1;j<=n-i+1&&c!=0;j++) 49 | printf("%3d",c--); 50 | printf("\n"); 51 | }//end of lower block 52 | 53 | getch(); 54 | return 0; 55 | } 56 | /* 57 | ENTER THE VALUE OF N: 58 | 7 59 | 1 2 3 4 5 6 7 6 5 4 3 2 1 60 | 8 9 10 11 12 13 13 12 11 10 9 8 61 | 14 15 16 17 18 18 17 16 15 14 62 | 19 20 21 22 22 21 20 19 63 | 23 24 25 25 24 23 64 | 26 27 27 26 65 | 28 28 66 | 26 27 27 26 67 | 23 24 25 25 24 23 68 | 19 20 21 22 22 21 20 19 69 | 14 15 16 17 18 18 17 16 15 14 70 | 8 9 10 11 12 13 13 12 11 10 9 8 71 | 1 2 3 4 5 6 7 6 5 4 3 2 1 72 | -------------------------------- 73 | */ 74 | -------------------------------------------------------------------------------- /CompetitiveProggramming/pattern2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main(){ 4 | int i,j,k=10,a=30,l,m,t=5; 5 | for(i=1;i<=5;i++){ 6 | for(j=i-1;j>=1;j--) 7 | printf("\t"); 8 | for(l=5;l>=i;l--){ 9 | printf("%d\t",k); 10 | k += 10; 11 | } 12 | for(m=a-t+1;m<=a;m++){ 13 | if(m==a) 14 | printf("%d",m); 15 | else 16 | printf("%d0",m); 17 | } 18 | a -= t; 19 | t -= 1; 20 | printf("\n"); 21 | } 22 | } 23 | /* 24 | Output: 25 | 10 20 30 40 50 26027028029030 26 | 60 70 80 90 22023024025 27 | 100 110 120 19020021 28 | 130 140 17018 29 | 150 16 30 | */ 31 | -------------------------------------------------------------------------------- /CompetitiveProggramming/pattern3.c: -------------------------------------------------------------------------------- 1 | /*WAP TO GENERATE THE GIVEN PATTERN 2 | * * * * * * * * * * 3 | * * * * * * * * 4 | * * * * * * 5 | * * * * 6 | * * 7 | * * * * * 8 | * * * * 9 | * * * 10 | * * 11 | * 12 | */ 13 | #include 14 | int main() 15 | { 16 | int i,j,k,l,n; 17 | printf("Enter a number: "); 18 | scanf("%d",&n); 19 | for(i=n;i>=1;i--) 20 | { 21 | for(k=n-i;k>=1;k--) 22 | printf(" "); 23 | for(j=1;j<=i;j++) 24 | { 25 | if(j%2==1) 26 | printf("*"); 27 | else 28 | printf(" "); 29 | } 30 | for(l=i-1;l>=1;l--) 31 | { 32 | if(l%2==1) 33 | printf("*"); 34 | else 35 | printf(" "); 36 | } 37 | // 38 | for(j=n;j>=i;j--) 39 | printf(" "); 40 | for(j=n-i;j>=1;j--) 41 | printf(" "); 42 | for(j=1;j<=i;j++) 43 | { 44 | if(j%2==1) 45 | printf("*"); 46 | else 47 | printf(" "); 48 | } 49 | for(l=i-1;l>=1;l--) 50 | { 51 | if(l%2==1) 52 | printf("*"); 53 | else 54 | printf(" "); 55 | } 56 | 57 | printf("\n"); 58 | } 59 | 60 | for(i=n;i>=1;i--) 61 | { 62 | for(j=n;j>=1;j--) 63 | printf(" "); 64 | for(k=n-i;k>=1;k--) 65 | printf(" "); 66 | for(j=1;j<=i;j++) 67 | { 68 | if(j%2==1) 69 | printf("*"); 70 | else 71 | printf(" "); 72 | } 73 | for(l=i-1;l>=1;l--) 74 | { 75 | if(l%2==1) 76 | printf("*"); 77 | else 78 | printf(" "); 79 | } 80 | printf("\n"); 81 | } 82 | return 0; 83 | } 84 | /* 85 | 9 86 | * * * * * * * * * * * * * * * * * * 87 | * * * * * * * * * * * * * * * * 88 | * * * * * * * * * * * * * * 89 | * * * * * * * * * * * * 90 | * * * * * * * * * * 91 | * * * * * * * * 92 | * * * * * * 93 | * * * * 94 | * * 95 | * * * * * * * * * 96 | * * * * * * * * 97 | * * * * * * * 98 | * * * * * * 99 | * * * * * 100 | * * * * 101 | * * * 102 | * * 103 | * 104 | -------------------------------- 105 | */ 106 | -------------------------------------------------------------------------------- /CompetitiveProggramming/round_Robin.c: -------------------------------------------------------------------------------- 1 | /* 2 | Program to execute Round Robin algorithm to perform process scheduling. 3 | */ 4 | #include 5 | 6 | int main() 7 | { 8 | 9 | int p_id, n = 0, j, time, remaining, flag = 0, time_quantum; 10 | /* 11 | at = Ariving Time 12 | bt = Burst Time 13 | rt = Remaining Burst Time 14 | remaining = remaining processes 15 | p_id = process id 16 | */ 17 | int wait_time = 0, turnaround_time = 0, at[10], bt[10], rt[10]; 18 | printf("Enter Total number of Processes: "); 19 | scanf("%d", &n); 20 | remaining = n; 21 | for (p_id = 0; p_id < n; p_id++) 22 | { 23 | printf("Enter Arrival Time and Burst Time for Process process number %d :", p_id + 1); 24 | scanf("%d", &at[p_id]); 25 | scanf("%d", &bt[p_id]); 26 | rt[p_id] = bt[p_id]; 27 | } 28 | printf("Enter Time Quantum:\t"); 29 | scanf("%d", &time_quantum); 30 | printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n"); 31 | for (time = 0, p_id = 0; remaining != 0;) 32 | { 33 | if (rt[p_id] <= time_quantum && rt[p_id] > 0) 34 | { 35 | time += rt[p_id]; 36 | rt[p_id] = 0; 37 | flag = 1; 38 | } 39 | else if (rt[p_id] > 0) 40 | { 41 | rt[p_id] -= time_quantum; 42 | time += time_quantum; 43 | } 44 | if (rt[p_id] == 0 && flag == 1) 45 | { 46 | remaining--; 47 | printf("P[%d]\t|\t%d\t|\t%d\n", p_id + 1, time - at[p_id], time - at[p_id] - bt[p_id]); 48 | wait_time += time - at[p_id] - bt[p_id]; 49 | turnaround_time += time - at[p_id]; 50 | flag = 0; 51 | } 52 | if (p_id == n - 1) 53 | p_id = 0; 54 | else if (at[p_id + 1] <= time) 55 | p_id++; 56 | else 57 | p_id = 0; 58 | } 59 | printf("\nAverage Waiting Time= %f\n", wait_time * 1.0 / n); 60 | printf("Average Turnaround Time = %f", turnaround_time * 1.0 / n); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /Formatted input and output.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/Formatted input and output.pdf -------------------------------------------------------------------------------- /Functions Practice Sheet.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/Functions Practice Sheet.docx -------------------------------------------------------------------------------- /IF-ELSE AND LOOPS NOTES.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/IF-ELSE AND LOOPS NOTES.pdf -------------------------------------------------------------------------------- /LOOP AND SWITCH QUESTIONS WITH ANSWERS.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/LOOP AND SWITCH QUESTIONS WITH ANSWERS.pdf -------------------------------------------------------------------------------- /MCQ's on INPUT AND OUTPUT.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/MCQ's on INPUT AND OUTPUT.pdf -------------------------------------------------------------------------------- /OPERATORS QUESTIONS WITH ANSWERS.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/OPERATORS QUESTIONS WITH ANSWERS.pdf -------------------------------------------------------------------------------- /Operators notes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/Operators notes.pdf -------------------------------------------------------------------------------- /Output based uestions on if else and switch case.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/Output based uestions on if else and switch case.pdf -------------------------------------------------------------------------------- /Pointers MCQ.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/Pointers MCQ.pdf -------------------------------------------------------------------------------- /Pointers in C.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/Pointers in C.pdf -------------------------------------------------------------------------------- /Practice sheet of Operators 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/Practice sheet of Operators 2.pdf -------------------------------------------------------------------------------- /Practice sheet of operators 1.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/Practice sheet of operators 1.docx -------------------------------------------------------------------------------- /String all question.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/String all question.pdf -------------------------------------------------------------------------------- /Strings.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/Strings.pdf -------------------------------------------------------------------------------- /carrer 360/Adding Fractions: -------------------------------------------------------------------------------- 1 | #include 2 | int main(){ 3 | /* variable declaration */ 4 | int numerator1, numerator2, denominator1, denominator2, 5 | num_result, denom_result ; 6 | /* Read each fraction */ 7 | printf("Please provide the first numerator:\n"); 8 | scanf("%d",&numerator1); 9 | printf("Please provide the first denominator:\n"); 10 | scanf("%d",&denominator1); 11 | printf("Please provide the second numerator:\n"); 12 | scanf("%d",&numerator2); 13 | printf("Please provide the second denominator:\n"); 14 | scanf("%d",&denominator2); 15 | /*compare the demonators*/ 16 | if ( denominator1 == denominator2 ) { 17 | num_result = numerator1 + numerator2; 18 | denom_result = denominator1; /* or 2, they are equal */ 19 | } else { 20 | num_result = (numerator1 * denominator2) + (numerator2 * denominator1 ); 21 | denom_result = denominator1 * denominator2; 22 | } 23 | printf("The result of %d / %d + %d / %d is: %d / %d \n", numerator1,denominator1, numerator2, denominator2,num_result,denom_result); 24 | return 0; 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /carrer 360/Address of 1D: -------------------------------------------------------------------------------- 1 | //The & operator is used to get the address. 2 | //But in case of array the name of array itself returns its address. 3 | //In array the elements occupy consecutive address, 4 | //therefore incrementing it by 1 each time would give 5 | //the address of next element. 6 | 7 | #include 8 | int main() 9 | { 10 | int a[100],i,n,*add; 11 | 12 | printf("enter the size: "); 13 | scanf("%d",&n); 14 | 15 | printf("enter the numbers: \n"); 16 | for(i=0;i 2 | #include 3 | #include 4 | 5 | int checkAnagram(char *str1, char *str2); 6 | 7 | int main() 8 | { 9 | char str1[100], str2[100]; 10 | 11 | printf("Function : whether two given strings are anagram :"); 12 | printf("\nExample : pears and spare, stone and tones :"); 13 | 14 | printf(" Input the first String : "); 15 | fgets(str1, sizeof str1, stdin); 16 | printf(" Input the second String : "); 17 | fgets(str2, sizeof str2, stdin); 18 | 19 | if(checkAnagram(str1, str2) == 1) 20 | { 21 | str1[strlen(str1)-1] = '\0'; 22 | str2[strlen(str2)-1] = '\0'; 23 | printf(" %s and %s are Anagram.\n\n",str1,str2); 24 | } 25 | else 26 | { 27 | str1[strlen(str1)-1] = '\0'; 28 | str2[strlen(str2)-1] = '\0'; 29 | printf(" %s and %s are not Anagram.\n\n",str1,str2); 30 | } 31 | return 0; 32 | } 33 | 34 | 35 | //Function to check whether two passed strings are anagram or not 36 | 37 | int checkAnagram(char *str1, char *str2) 38 | { 39 | int str1ChrCtr[256] = {0}, str2ChrCtr[256] = {0}; 40 | int ctr; 41 | 42 | /* check the length of equality of Two Strings */ 43 | 44 | if(strlen(str1) != strlen(str2)) 45 | { 46 | return 0; 47 | } 48 | 49 | //count frequency of characters in str1 50 | 51 | for(ctr = 0; str1[ctr] != '\0'; ctr++) 52 | { 53 | str1ChrCtr[str1[ctr]]++; 54 | } 55 | 56 | //count frequency of characters in str2 57 | 58 | for(ctr = 0; str2[ctr] != '\0'; ctr++) 59 | { 60 | str2ChrCtr[str2[ctr]]++; 61 | } 62 | 63 | //compare character counts of both strings 64 | 65 | for(ctr = 0; ctr < 256; ctr++) 66 | { 67 | if(str1ChrCtr[ctr] != str2ChrCtr[ctr]) 68 | return 0; 69 | } 70 | return 1; 71 | } 72 | -------------------------------------------------------------------------------- /carrer 360/Array operations: -------------------------------------------------------------------------------- 1 | //ARRAY simple operations 2 | #include 3 | #include 4 | 5 | int arr[100],i,j,position,n,element,choice; 6 | 7 | void menu(); 8 | 9 | void insertion(){ 10 | printf("ENTER THE POSITION YOU WANT TO ENTER THE ELEMENT"); 11 | scanf("%d",&position); 12 | position=position-1; 13 | if(position>n-1){ 14 | printf("\n INVALID POSITION\n"); 15 | menu(); 16 | } 17 | printf("ENTER THE ELEMENT "); 18 | scanf("%d",&element); 19 | for(i=n;i>=position;i--){ 20 | arr[i+1]=arr[i]; 21 | } 22 | arr[position]=element; 23 | n=n+1; 24 | menu(); 25 | } 26 | 27 | void traversing(){ 28 | printf("\n THE ARRAY ENTERED IS:--\n"); 29 | for(i=0;in-1){ 40 | printf("\n INVALID POSITION\n"); 41 | menu(); 42 | } 43 | for(i=position;in){ 55 | printf("\n INVALID POSITION\n"); 56 | menu(); 57 | } 58 | printf("\n ENTER THE ELEMENT :-"); 59 | scanf("%d",&element); 60 | arr[position]=element; 61 | 62 | } 63 | 64 | void linear_search(){ 65 | printf("ENTER THE ELEMENT YOU WANT TO SEARCH\n"); 66 | scanf("%d",&element); 67 | for(i=0;i arr[j+1]){ 83 | arr[j]=arr[j]+arr[j+1]; 84 | arr[j+1]=arr[j]-arr[j+1]; 85 | arr[j]=arr[j]-arr[j+1]; 86 | } 87 | } 88 | } 89 | } 90 | void bubble_sort(){ 91 | bubble_sort_algo(); 92 | printf("\nTHE ARRAY HAS BEEN SORTED\n"); 93 | menu(); 94 | } 95 | 96 | void binary_search(){ 97 | printf("ENTER THE ELEMENT YOU WANT TO SEARCH\n"); 98 | scanf("%d",&element); 99 | int beg,mid,end,check; 100 | check=0; 101 | beg=0; 102 | end=n-1; 103 | bubble_sort_algo(); 104 | while(beg<=end){ 105 | mid=(beg+end)/2; 106 | if(arr[mid]==element){ 107 | printf("ELEMENT FOUND AT POSITION: %d, AFTER THE SORTING",mid+1); 108 | check=10; 109 | break; 110 | } 111 | else if(arr[mid]>element){ 112 | end = mid-1; 113 | } 114 | else if(arr[mid]arr[j]){ 128 | arr[i]=arr[i]+arr[j]; 129 | arr[j]=arr[i]-arr[j]; 130 | arr[i]=arr[i]-arr[j]; 131 | } 132 | } 133 | } 134 | printf("ARRAY IS SORTED"); 135 | menu(); 136 | } 137 | 138 | 139 | void menu(){ 140 | while(1){ 141 | printf("\n1. INSERTION\t 2. UPDATION\t 3. TRAVERSING\t 4. DELETION\t 5.EXIT\t 6. RECREATE THE ARRAY \n"); 142 | printf("7. LINEAR SEARCH\t 8. BUBBLE SORT\t 9. BINARY SEARCH\t 10. SELECTION SORT\n\n"); 143 | scanf("%d",&choice); 144 | switch(choice){ 145 | case 1: 146 | insertion(); 147 | break; 148 | case 2: 149 | updation(); 150 | break; 151 | case 3: 152 | traversing(); 153 | break; 154 | case 4: 155 | deletion(); 156 | break; 157 | case 5: 158 | exit(1); 159 | case 6: 160 | main(); 161 | break; 162 | case 7: 163 | linear_search(); 164 | break; 165 | case 8: 166 | bubble_sort(); 167 | break; 168 | case 9: 169 | binary_search(); 170 | break; 171 | case 10: 172 | selection_sort(); 173 | break; 174 | default: 175 | menu(); 176 | break; 177 | } 178 | } 179 | 180 | } 181 | 182 | int main(){ 183 | 184 | printf("ENTER THE ELEMENTS OF ARRAY"); 185 | i=0; 186 | while(1){ 187 | printf("\nENTER THE ELEMENT:-\n"); 188 | scanf("%d",arr+i); 189 | printf("\nDO YOU WANT TO ENTER ANOTHER ELEMENT PRESS ANY KEY ELSE PRESS 1\n"); 190 | scanf("%d",&choice); 191 | if(choice == 1){ 192 | n=i+1; 193 | menu(); 194 | } 195 | i++; 196 | } 197 | 198 | return 0; 199 | } 200 | -------------------------------------------------------------------------------- /carrer 360/Automorpic number: -------------------------------------------------------------------------------- 1 | /*An automorphic number is a number whose square has the same digits in the end as the number itself. 2 | For example, 25 is an automorphic number because the square of 25 is 625, which ends with 25. Similarly, 3 | 76 is an automorphic number because the square of 76 is 5776, which again ends with 76.*/ 4 | 5 | #include 6 | #include 7 | #include 8 | int main() { 9 | int num, sqr, temp, last; 10 | int n = 0; 11 | printf("Enter a number \n"); 12 | scanf("%d", & num); 13 | sqr = num * num; //calculating square of num 14 | temp = num; 15 | //Counting number of digits 16 | while (temp > 0) { 17 | n++; 18 | temp = temp / 10; 19 | } 20 | //Extracting last n digits 21 | int den = floor(pow(10, n)); 22 | last = sqr % den; 23 | if (last == num) 24 | printf("Automorphic number \n"); 25 | else 26 | printf("Not Automorphic \n"); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /carrer 360/Basic Game: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define MAX 10 7 | #define A "P" 8 | #define B "Q" 9 | int getRandom(int low, int high); 10 | int getValidInteger(int low, int high); 11 | unsigned int playerRoll(int low, int high); 12 | 13 | void seed(void) 14 | { 15 | srand(time(NULL)); 16 | } 17 | void space(unsigned int size) // create space 18 | { 19 | printf(" "); 20 | } 21 | char getDisplayType(unsigned int index, unsigned int playerPosition, char playerName) // check the index and return character 22 | { 23 | 24 | if (playerName != '#') 25 | { 26 | if (index == playerPosition) 27 | return playerName; 28 | 29 | if (index == 0) 30 | return ('C'); 31 | 32 | if (index % 3 == 0) 33 | if (index % 5 == 0) 34 | if (index % 7 == 0) 35 | return('G'); 36 | else return ('L'); 37 | else return ('W'); 38 | 39 | if (index % 5 == 0) 40 | if (index % 7 == 0) 41 | return('G'); 42 | else return ('L'); 43 | 44 | if (index % 7 == 0) { 45 | return('G'); 46 | } 47 | else return (' '); 48 | } 49 | 50 | if (playerName == '#') 51 | { 52 | 53 | if (index == 0) 54 | return ('C'); 55 | 56 | if (index % 3 == 0) { 57 | if (index % 5 == 0) { 58 | if (index % 7 == 0) { 59 | return('G'); 60 | } 61 | else return ('L'); 62 | } 63 | else return ('W'); 64 | } 65 | 66 | if (index % 5 == 0) { 67 | if (index % 7 == 0) { 68 | return('G'); 69 | } 70 | else return ('L'); 71 | } 72 | if (index % 7 == 0) { 73 | return('G'); 74 | } 75 | else 76 | return (' '); 77 | 78 | 79 | 80 | } 81 | } 82 | void firstLine(unsigned int size) // create the upper line of the square for the first and last line 83 | { 84 | int i; 85 | 86 | for (i = 0; i < size; i++) 87 | { 88 | printf(" ___ "); 89 | } 90 | printf("\n"); 91 | } 92 | 93 | void secondLine(unsigned int size, unsigned int i, unsigned int playerPosition, char playerName) //create the inside and return the type for the first line 94 | { 95 | int x, j; 96 | for (j = 0; j < size; j++) 97 | { 98 | x = j; 99 | printf("| %c |", getDisplayType(x, playerPosition, playerName)); 100 | } 101 | printf("\n"); 102 | } 103 | 104 | void secondLine2nd(unsigned int size, unsigned int i, unsigned int playerPosition, char playerName) { //create the inside and return the type for the last line 105 | int x, y, z; 106 | y = 3 * (size - 1); 107 | printf("| %c |", getDisplayType(y, playerPosition, playerName)); 108 | for (i = 0; i < size - 2; i++) 109 | { 110 | z = 2 * (size - 1) + ((size - 2) - i); 111 | printf("| %c |", getDisplayType(z, playerPosition, playerName)); 112 | } 113 | x = size + i; 114 | printf("| %c |", getDisplayType(x, playerPosition, playerName)); 115 | 116 | 117 | printf("\n"); 118 | } 119 | 120 | 121 | void thirdLine(unsigned int size) // create lower line for the first and last line 122 | { 123 | int i; 124 | 125 | for (i = 0; i < size; i++) 126 | { 127 | printf("|___|"); 128 | } 129 | printf("\n"); 130 | } 131 | void upperrow(unsigned int size) // create the upper line for the square of the row 132 | { 133 | int i; 134 | printf(" ___"); 135 | for (i = 0; i < size - 2; i++) { 136 | space(size); 137 | } 138 | printf(" ___"); 139 | printf("\n"); 140 | } 141 | 142 | void lowerrow(unsigned int size) //create the lower line for the square of the row 143 | { 144 | int i; 145 | printf("|___|"); 146 | for (i = 0; i < size - 2; i++) { 147 | space(size); 148 | } 149 | 150 | printf("|___|"); 151 | printf("\n"); 152 | } 153 | 154 | void middlerow(unsigned int size, unsigned int i, unsigned int playerPosition, char playerName) //create the inside and return the type for the square of the row 155 | { 156 | int q, b, p; 157 | b = i; 158 | q = 3 * (size - 1) + ((size - 1) - i); 159 | printf("| %c |", getDisplayType(q, playerPosition, playerName)); 160 | for (b = 0; b < size - 2; b++) { 161 | space(size); 162 | } 163 | p = size - 1 + i; 164 | printf("| %c |", getDisplayType(p, playerPosition, playerName)); 165 | printf("\n"); 166 | } 167 | 168 | 169 | char getValidCharacter(char a, char b) //make sure the character input is right// 170 | { 171 | char c, choice; 172 | do { 173 | scanf_s("%c", &choice); 174 | getchar(); 175 | if ((choice != 'p' && choice != 'q' && choice != 'r' && choice != 's')) 176 | { 177 | printf("Invalid input, please try again: "); 178 | } 179 | } while (choice != 'p' && choice != 'q' && choice != 'r' && choice != 's'); 180 | return choice; 181 | } 182 | 183 | int getValidInteger(int low, int high) //validate the input number// 184 | { 185 | int a; int choice; 186 | do { 187 | a = scanf_s("%d", &choice); 188 | if (choice high) 189 | { 190 | printf("Invalid input, please try again: "); 191 | } 192 | 193 | } while (choice < low || choice > high); 194 | return choice; 195 | } 196 | 197 | int getRandom(int low, int high) //get a rando number 198 | { 199 | int a; 200 | a = rand() % (high - low) + low; 201 | 202 | return a; 203 | } 204 | 205 | unsigned int playerRoll(int low, int high) //prompt and output the roll 206 | { 207 | int a = 1, b, c, d, e, choice; 208 | do { 209 | printf("\nyour turn, how many dice will you roll : "); 210 | scanf_s("%d", &choice); 211 | if (choice == 1) 212 | { 213 | b = getRandom(low, high); 214 | //printf("b: \n"); 215 | //scanf_s("%d", &b); 216 | printf("You rolled %d\n", b); 217 | printf("Advancing %d space\n", b); 218 | a = 0; 219 | return b; 220 | } 221 | else if (choice == 2) 222 | { 223 | c = getRandom(low, high); 224 | //printf("c: "); 225 | //scanf_s("%d", &c); 226 | d = getRandom(low, high); 227 | //printf("d: "); 228 | //scanf_s("%d", &d); 229 | b = c + d; 230 | printf("You rolled %d %d\n ", c, d); 231 | printf("Advancing %d space\n", b); 232 | a = 0; 233 | return b; 234 | } 235 | else if (choice == 3) { 236 | c = getRandom(low, high); 237 | d = getRandom(low, high); 238 | e = getRandom(low, high); 239 | b = c + d + e; 240 | printf("You rolled %d %d %d\n ", c, d, e); 241 | printf("Advancing %d space\n", b); 242 | a = 0; 243 | return b; 244 | } 245 | else 246 | printf("Try again,"); 247 | } while (a = 1); 248 | } 249 | 250 | void winPrize(int playerPrizes[], unsigned int* prizeCount) //do the winprize function 251 | { 252 | int i; 253 | unsigned int prize; 254 | prize = getRandom(10, 100); 255 | printf("%d\n", prize); 256 | if (*prizeCount < MAX) 257 | { 258 | playerPrizes[*prizeCount] = prize; 259 | printf("you won a prize of %d\n", prize); 260 | *prizeCount = *prizeCount + 1; 261 | } 262 | else 263 | printf("Your inventory is full \n"); 264 | } 265 | void winGrandPrize(int playerPrizes[], unsigned int* prizeCount) // do the win grand prize function 266 | { 267 | int i; 268 | unsigned int prize; 269 | prize = getRandom(100, 200); 270 | printf("%d\n", prize); 271 | if (*prizeCount < MAX) 272 | { 273 | playerPrizes[*prizeCount] = prize; 274 | printf("you won a grand prize of %d\n", prize); 275 | *prizeCount = *prizeCount + 1; 276 | } 277 | else 278 | printf("Your inventory is full "); 279 | } 280 | int loseItem(int playerPrizes[], unsigned int *prizeCount) // do the loseitem fuction 281 | { 282 | int i, j, k, r, ran = 2; 283 | 284 | if (*prizeCount == 0) 285 | { 286 | printf("Nothing happened,Move On\n"); 287 | } 288 | else 289 | { 290 | 291 | ran = getRandom(0, *prizeCount); 292 | playerPrizes[ran] = 0; 293 | *prizeCount = *prizeCount - 1; 294 | printf("you lost the prize"); 295 | for (i = ran - 1; i < MAX; i++) //arange the array in order 296 | for (j = i; j < MAX; j++) 297 | if (playerPrizes[i] == 0) 298 | { 299 | k = playerPrizes[i]; 300 | playerPrizes[i] = playerPrizes[j]; 301 | playerPrizes[j] = k; 302 | } 303 | 304 | } 305 | } 306 | 307 | 308 | void initPlayer(int *playerScore, int playerPrizes[], unsigned int *prizeCount, char *playerName, int *playerPosition) //do the initplayer function, set everything to 0 309 | { 310 | int i; 311 | playerPrizes[MAX] = 0; 312 | 313 | *playerScore = 0; 314 | printf("playerPrizes: %d\n", playerPrizes[MAX]); 315 | *prizeCount = 0; 316 | *playerPosition = 0; 317 | printf("Enter Player ID: "); 318 | scanf_s("%c", playerName); 319 | } 320 | 321 | 322 | 323 | 324 | void displayBoard(unsigned int size, unsigned int playerPosition, char playerName) //display the boardgame 325 | { 326 | 327 | int k, size1, loop; 328 | float loop2, playerPosition1, size2; 329 | //printf("player name in display board: %c\n", playerName); 330 | //printf("%d\n", r); 331 | //printf("%d", playerPosition); 332 | playerPosition1 = (float)playerPosition; 333 | size1 = (4 * (size - 1)); 334 | 335 | size2 = (float)size1; 336 | //printf("size2: %.2f\n", size2); 337 | // printf("playerPo1: %.2f\n", playerPosition1); 338 | //playerPosition2 = float size; 339 | //printf("playerPos: %d\n", playerPosition); 340 | loop2 = playerPosition1 / size2; 341 | 342 | loop = trunc(loop2); 343 | 344 | 345 | k = playerPosition - (4 * (size - 1))*loop; 346 | 347 | playerPosition = k; 348 | 349 | { 350 | int i = 0; 351 | if (size == 1) 352 | { 353 | printf(" ___ \n"); 354 | printf(" | ? | \n"); 355 | printf(" |___|"); 356 | printf("\n"); 357 | } 358 | else { 359 | 360 | for (i = 0; i < size - 1; i++) 361 | { 362 | 363 | if (i == 0) 364 | { 365 | firstLine(size); 366 | secondLine(size, i, playerPosition, playerName); 367 | thirdLine(size); 368 | } 369 | } 370 | for (i = 1; i < size - 1; i++) 371 | { 372 | upperrow(size); 373 | middlerow(size, i, playerPosition, playerName); 374 | lowerrow(size); 375 | } 376 | for (i = size - 2; i < size - 1; i++) 377 | { 378 | firstLine(size); 379 | secondLine2nd(size, i, playerPosition, playerName); 380 | thirdLine(size); 381 | } 382 | } 383 | } 384 | } 385 | 386 | int checkout(int *playerScore, int playerPrizes[], unsigned int* prizeCount) //do the checkout 387 | { 388 | int i; 389 | for (i = 0; i < *prizeCount; i++) 390 | *playerScore += playerPrizes[i]; 391 | *prizeCount = 0; 392 | printf("You check out for $%d score is now: $%d \n", *playerScore, *playerScore); 393 | if (*playerScore >= 200) 394 | { 395 | return 1; 396 | } 397 | else 398 | { 399 | return 0; 400 | } 401 | 402 | } 403 | 404 | 405 | void playGame(unsigned int size, int *playerScore, int playerPrizes[], unsigned int *prizeCount, char *playerName, int* playerPosition) //play the game 406 | { 407 | printf("playerName in playgame %c\n", *playerName); 408 | //printf("%d\n",*prizeCount); 409 | //printf("%d\n", *playerScore); 410 | int i, l = 1; 411 | while (l) 412 | { 413 | displayBoard(size, *playerPosition, *playerName); 414 | 415 | printf("Score: %d inventory (%d items): ", *playerScore, *prizeCount); 416 | for (i = 0; i < *prizeCount; i++) { 417 | printf("%d, ", playerPrizes[i]); 418 | 419 | } 420 | 421 | *playerPosition = *playerPosition + playerRoll(1, 6); 422 | if (*playerPosition >= 4 * (size - 1)) 423 | *playerPosition = *playerPosition - 4 * (size - 1); 424 | //printf("player position in display %d\n", *playerPosition); 425 | //printf("display type in play game: %c\n", getDisplayType(*playerPosition, *playerPosition, '#')); 426 | //displayBoard(boardSize, playerPosition, playerName); 427 | if (getDisplayType(*playerPosition, *playerPosition, '#') == 'G') 428 | { 429 | winGrandPrize(playerPrizes, prizeCount); 430 | } 431 | else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'W') 432 | { 433 | 434 | winPrize(playerPrizes, prizeCount); 435 | } 436 | else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'L') 437 | { 438 | 439 | loseItem(playerPrizes, prizeCount); 440 | } 441 | else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'C') 442 | { 443 | 444 | 445 | if (checkout(playerScore, playerPrizes, prizeCount) == 1) 446 | { 447 | printf("You Win\n"); 448 | l = 0; 449 | } 450 | } 451 | else 452 | printf("nothing happens, go again.\n"); 453 | } 454 | } 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | int main(void) 463 | { 464 | int i, l = 1; 465 | char a, choice; 466 | char c = '#'; 467 | int playerScore; 468 | int playerPrizes[MAX]; 469 | unsigned int prizeCount; 470 | char playerName; 471 | unsigned int size; 472 | unsigned int playerPosition; 473 | printf("Welcome to CHECKOUT\n"); 474 | while (l) { 475 | printf("Main Menu\n"); 476 | printf("p-(p)lay q-(q)uit r-inst(r)uctions s-HI(s)core: \n"); 477 | choice = getValidCharacter('P', 'Q'); 478 | if (choice == 'p') { 479 | printf("Number of players is 1\n"); 480 | initPlayer(&playerScore, playerPrizes, &prizeCount, &playerName, &playerPosition); 481 | 482 | printf("Enter board size: "); 483 | scanf_s("%d", &size); 484 | 485 | playGame(size, &playerScore, playerPrizes, &prizeCount, &playerName, &playerPosition); 486 | 487 | 488 | 489 | getchar(); 490 | 491 | } 492 | if (choice == 's') 493 | { 494 | printf("--\n"); 495 | printf(" \\ "); 496 | printf("_______\n"); 497 | printf(" \\++++++|\n"); 498 | printf(" \\=====|\n"); 499 | printf(" 0--- 0\n"); 500 | printf("HI SCORE: %d Player Name: %c \n", playerScore, playerName); 501 | } 502 | if (choice == 'q') 503 | { 504 | printf("dont go, I will miss you :("); 505 | l = 0; 506 | getchar(); 507 | } 508 | } 509 | } 510 | -------------------------------------------------------------------------------- /carrer 360/Binary Search: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* If x in into array return 0, else 1 */ 4 | /* 5 | * A function with 4 params: 6 | * int[] = array of elements 7 | * int = wanted number 8 | * int = start index 9 | * int = end index 10 | * 11 | * returns 0 or 1 12 | */ 13 | int binarySearch(int[], int, int, int); 14 | 15 | int main() { 16 | int arr[] = {5, 15, 24, 32, 56, 89}; 17 | /* check length of array */ 18 | int size_of_array = sizeof(arr) / sizeof(int); 19 | /* Check if 24 is into arr */ 20 | printf("%d\n", binarySearch(arr, 24, 0, size_of_array-1)); 21 | /* Check if 118 is into arr */ 22 | printf("%d\n", binarySearch(arr, 118, 0, size_of_array-1)); 23 | return 0; 24 | } 25 | 26 | int binarySearch(int array[], int number, int start, int end) { 27 | /* if start index is get end index, check if that element is equals wanter nmber */ 28 | if(start >= end) { 29 | return array[start] == number ? 0 : 1; 30 | } 31 | 32 | int tmp = (int) end / 2; 33 | /* divide array length in half */ 34 | /* if number is greater than element in half, do search by start to tmp 35 | * else search by tmp to end 36 | */ 37 | if(number == array[tmp]) { 38 | return 0; 39 | } else if(number > array[tmp]) { 40 | return binarySearch(array, number, start, tmp); 41 | } else { 42 | return binarySearch(array, number, tmp, end); 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /carrer 360/Binary Tree Traversal: -------------------------------------------------------------------------------- 1 | /* Tree Node Structure */ 2 | 3 | struct Node { 4 | int data; 5 | Node* left; 6 | Node* right; 7 | }; 8 | 9 | /* Inorder Traversal of Binary tree */ 10 | 11 | void inorderTraversal(Node* root) { 12 | if (root == NULL) 13 | return; 14 | /* recurring on left child */ 15 | inorderTraversal(root->left); 16 | 17 | /* printing the data of node */ 18 | cout << root->data << " "; 19 | 20 | /* recurring on right child */ 21 | inorderTraversal(root->right); 22 | } 23 | 24 | /* Preorder Traversal of Binary tree */ 25 | 26 | void preorderTraversal(Node* root) { 27 | if (root == NULL) 28 | return; 29 | /* printing the data of node */ 30 | cout << root->data << " "; 31 | 32 | /* recurring on left child */ 33 | preorderTraversal(root->left); 34 | 35 | /* recurring on right child */ 36 | preorderTraversal(root->right); 37 | } 38 | 39 | 40 | /* Postorder Traversal of Binary tree */ 41 | 42 | void postorderTraversal(Node* root) { 43 | if (root == NULL) 44 | return; 45 | /* recurring on left child */ 46 | postorderTraversal(root->left); 47 | 48 | /* recurring on right child */ 49 | postorderTraversal(root->right); 50 | 51 | /* printing the data of node */ 52 | cout << root->data << " "; 53 | } 54 | -------------------------------------------------------------------------------- /carrer 360/Complex Bitwise operations: -------------------------------------------------------------------------------- 1 | /* 2 | Function Description 3 | 4 | Complete the calculate_the_maximum function in the editor below. 5 | 6 | calculate_the_maximum has the following parameters: 7 | 8 | 1) int n: the highest number to consider 9 | 2) int k: the result of a comparison must be lower than this number to be considered 10 | 11 | Prints 12 | 13 | Print the maximum values for the and, or and xor comparisons, each on a separate line. 14 | 15 | Input Format 16 | The only line contains 2-space seperated integers n and k. 17 | The only line contains space-separated integers, and . 18 | 19 | Constraints 20 | 2<=n<=10pow3 21 | 2<=k<=n 22 | Sample Input 0 23 | 24 | 5 4 25 | Sample Output 0 26 | 27 | 2 28 | 3 29 | 3 30 | Explanation 0 31 | n=5 k=4 32 | S={1,2,3,4,5} 33 | All possible values of and are: 34 | value at i=1 and at j+1=2 AND =0 OR=3 XOR=3 35 | value at i=1 and at j+1=3 AND =1 OR=3 XOR=2 36 | value at i=1 and at j+1=4 AND =0 OR=5 XOR=5 37 | value at i=1 and at j+1=5 AND =1 OR=5 XOR=4 38 | value at i=2 and at j+1=3 AND =2 OR=3 XOR=1 39 | value at i=2 and at j+1=4 AND =0 OR=6 XOR=6 40 | value at i=2 and at j+1=5 AND =0 OR=7 XOR=7 41 | value at i=3 and at j+1=4 AND =0 OR=7 XOR=7 42 | value at i=3 and at j+1=5 AND =1 OR=7 XOR=6 43 | value at i=4 and at j+1=5 AND =4 OR=5 XOR=1 44 | 45 | The maximum possible value of i&j that is also <(k=4) is 2, so we print 2 on first line. 46 | 47 | The maximum possible value of i|j that is also <(k=4) is 3, so we print 3 on second line. 48 | 49 | The maximum possible value of i^j that is also <(k=4) is 3, so we print 3 on third line. 50 | 51 | */ 52 | 53 | #include 54 | #include 55 | #include 56 | #include 57 | //Complete the following function. 58 | 59 | 60 | void calculate_the_maximum(int x, int y) { 61 | int i,j; 62 | int sumand=0,sumand1=0,sumor=0,sumor1=0,sumxor=0,sumxor1=0; 63 | for (i=1;i<=x;i++) 64 | for (j=i;j 2 | #include 3 | struct course { 4 | int marks; 5 | char subject[30]; 6 | }; 7 | 8 | int main() { 9 | struct course *ptr; 10 | int noOfRecords; 11 | printf("Enter the number of records: "); 12 | scanf("%d", &noOfRecords); 13 | 14 | // Memory allocation for noOfRecords structures 15 | ptr = (struct course *)malloc(noOfRecords * sizeof(struct course)); 16 | for (int i = 0; i < noOfRecords; ++i) { 17 | printf("Enter subject and marks:\n"); 18 | scanf("%s %d", (ptr + i)->subject, &(ptr + i)->marks); 19 | } 20 | 21 | printf("Displaying Information:\n"); 22 | for (int i = 0; i < noOfRecords; ++i) { 23 | printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks); 24 | } 25 | 26 | free(ptr); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /carrer 360/Even Fabonacci Numbers: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int i;//loop counter 5 | int n;//max value 6 | int t1=1;//First term 7 | int t2=1;//Second term 8 | int nextTerm;//Next term 9 | int sum=0;//Summation of total even Fibonacci number 10 | int count=0;//counter to count the number of even Fibonacci numbers 11 | printf("Enter the max value up to which you want to print the values: "); 12 | scanf("%d",&n); 13 | printf("The listed even Fibonacci number are: "); 14 | printf("\nSlno.\t\tFibonacci number"); 15 | for(i=1;i<=n;i++) 16 | { 17 | if(t1>=1 && t1<=n) 18 | { 19 | nextTerm=t1+t2; 20 | t1=t2; 21 | t2=nextTerm; 22 | 23 | if(t1%2==0 && t1<=n) 24 | { 25 | sum=sum+t1; 26 | count++; 27 | printf("\n %d\t\t\t%d",count,t1); 28 | } 29 | } 30 | 31 | } 32 | printf("\n\nThe required sum of the total even Fibonacci number is: %u\n\n\n",sum); 33 | 34 | } 35 | -------------------------------------------------------------------------------- /carrer 360/RockPaperScissors: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int ask(){ 6 | int choice; 7 | puts("Choose one of the following:"); 8 | puts("1. Rock"); 9 | puts("2. Paper"); 10 | puts("3. Scissors"); 11 | scanf("%d", &choice); 12 | 13 | return choice; 14 | } 15 | 16 | char* choice_identify(int choice){ 17 | switch (choice) 18 | { 19 | case 1: 20 | return "Rock"; 21 | break; 22 | case 2: 23 | return "Paper"; 24 | break; 25 | case 3: 26 | return "Scissor"; 27 | break; 28 | default: 29 | puts("Invalid input!"); 30 | exit(0); 31 | break; 32 | } 33 | } 34 | 35 | int bot(){ 36 | srand(time(0)); 37 | int bot_choice = rand()%4; 38 | if (bot_choice==0){ 39 | return 1; 40 | } 41 | else{ 42 | return bot_choice; 43 | } 44 | } 45 | 46 | int main(int argc, char* argv[]){ 47 | int int_user_choice; 48 | int int_bot_choice; 49 | int rounds; 50 | char* char_bot_choice; 51 | char* char_user_choice; 52 | 53 | int user_points = 0; 54 | int bot_points = 0; 55 | 56 | puts("Welcome to RPC!"); 57 | puts("How many rounds do you want to play?"); 58 | scanf("%d", &rounds); 59 | 60 | do{ 61 | int_user_choice = ask(); 62 | char_user_choice = choice_identify(int_user_choice); 63 | printf("You have chosen %s\n", char_user_choice); 64 | int_bot_choice = bot(); 65 | char_bot_choice = choice_identify(int_bot_choice); 66 | printf("The bot have chosen %s\n", char_bot_choice); 67 | 68 | if (int_user_choice == int_bot_choice){ 69 | puts("Correct!"); 70 | user_points++; 71 | } 72 | else{ 73 | puts("Wrong :("); 74 | bot_points++; 75 | } 76 | rounds--; 77 | 78 | }while(rounds); 79 | 80 | if (bot_points>user_points){ 81 | puts("Bot won!"); 82 | puts("Better luck next time (: !"); 83 | } 84 | else{ 85 | puts("You won!"); 86 | puts("This proves that bots cannot rule the world!"); 87 | } 88 | 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /carrer 360/Rusian Multiplication: -------------------------------------------------------------------------------- 1 | /*Russian peasant multiplication is an interesting way to multiply 2 | numbers that uses a process of halving and doubling. Like standard 3 | multiplication and division*/ 4 | 5 | 6 | #include 7 | 8 | int main() 9 | { 10 | 11 | long long int a, b, k, l, sum=0, i=0; 12 | 13 | // input variables 14 | printf("ENTER TWO NUMBERS:\n"); 15 | scanf("%lld %lld", &a, &b); 16 | k=a; 17 | l=b; 18 | 19 | 20 | // working of algorithm 21 | while(a>0) 22 | { 23 | if(a%2==1){ 24 | sum = sum + b; 25 | } 26 | a = a>>1; 27 | b = b<<1; 28 | i++; 29 | } 30 | 31 | 32 | // output 33 | printf("PRODUCT OF %lld AND %lld IS = %lld \n",k,l,sum); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /carrer 360/ZigZac: -------------------------------------------------------------------------------- 1 | /* 2 | Q1. Given a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure. 3 | 4 | 5 | Example: 6 | 7 | Input: 8 | 1 2 3 9 | 4 5 6 10 | 7 8 9 11 | 12 | Output: 13 | 1 2 4 7 5 3 6 8 9 14 | */ 15 | 16 | #include 17 | #include 18 | #define C 3 19 | int min(int m,int n) 20 | { 21 | if (m>n) 22 | { 23 | return n; 24 | } 25 | else 26 | { 27 | return m; 28 | } 29 | } 30 | int max(int m,int n) 31 | { 32 | if (m>n) 33 | { 34 | return m; 35 | } 36 | else 37 | { 38 | return n; 39 | } 40 | } 41 | 42 | void zigZagMatrix(int arr[][C], int n, int m) 43 | { 44 | int row = 0, col = 0; 45 | bool row_inc = 0; 46 | // Print matrix of lower half zig-zag pattern 47 | int mn = min(m, n); 48 | for (int len = 1; len <= mn; ++len) { 49 | for (int i = 0; i < len; ++i) { 50 | printf("%d ", arr[row][col]); 51 | if (i + 1 == len) 52 | break; 53 | if (row_inc) 54 | ++row, --col; 55 | else 56 | --row, ++col; 57 | } 58 | 59 | if (len == mn) 60 | break; 61 | if (row_inc) 62 | ++row, row_inc = false; 63 | else 64 | ++col, row_inc = true; 65 | } 66 | if (row == 0) { 67 | if (col == m - 1) 68 | ++row; 69 | else 70 | ++col; 71 | row_inc = 1; 72 | } 73 | else { 74 | if (row == n - 1) 75 | ++col; 76 | else 77 | ++row; 78 | row_inc = 0; 79 | } 80 | // Print the next half zig-zag pattern 81 | int MAX = max(m, n) - 1; 82 | for (int len, diag = MAX; diag > 0; --diag) { 83 | 84 | if (diag > mn) 85 | len = mn; 86 | else 87 | len = diag; 88 | 89 | for (int i = 0; i < len; ++i) { 90 | printf("%d ",arr[row][col]); 91 | 92 | if (i + 1 == len) 93 | break; 94 | if (row_inc) 95 | ++row, --col; 96 | else 97 | ++col, --row; 98 | } 99 | 100 | if (row == 0 || col == m - 1) { 101 | if (col == m - 1) 102 | ++row; 103 | else 104 | ++col; 105 | 106 | row_inc = true; 107 | } 108 | 109 | else if (col == 0 || row == n - 1) { 110 | if (row == n - 1) 111 | ++col; 112 | else 113 | ++row; 114 | 115 | row_inc = false; 116 | } 117 | } 118 | } 119 | // Driver code 120 | int main() 121 | { 122 | int matrix[][3] = { { 1, 2, 3 }, 123 | { 4, 5, 6 }, 124 | { 7, 8, 9 } }; 125 | zigZagMatrix(matrix, 3, 3); 126 | 127 | return 0; 128 | } 129 | 130 | -------------------------------------------------------------------------------- /carrer 360/camelcase: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | int main() 7 | { 8 | char s[100],fs[100]; 9 | gets(s); 10 | /*int l=strlen(s); 11 | char fins[l-1]; 12 | int i=0; 13 | while(s[i]!='_') 14 | { 15 | k++; 16 | fins[i]=s[i]; 17 | i++; 18 | } 19 | fins[i]=toupper(s[k+1]); 20 | for(z=i+1;z65&&int(s[i]<90)) 30 | { 31 | fs[k++]='_'; 32 | fs[k++]=tolower(s[i]); 33 | } 34 | else 35 | { 36 | fs[k++]=s[i]; 37 | } 38 | } 39 | puts(fs); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /carrer 360/counting sort: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | long long* count_sort_naive(long long *arr, long n, long m) 5 | { 6 | long long *count_arr = malloc((m + 1) * sizeof(long long)); 7 | long long *sorted_arr = malloc(n * sizeof(long long)); 8 | 9 | long i_arr = 0; 10 | for (long i = 0; i <= m; ++i) { 11 | /* compute the frequency of the ith element */ 12 | for (long j = 0; j < n; ++j) { 13 | if (arr[j] == i) { 14 | count_arr[i] += 1; 15 | } 16 | } 17 | 18 | /* place the ith element count number of times */ 19 | for (long j = 0; j < count_arr[i]; ++j) { 20 | sorted_arr[i_arr] = i; 21 | i_arr += 1; 22 | } 23 | } 24 | free(arr); 25 | 26 | return sorted_arr; 27 | } 28 | 29 | int main() 30 | { 31 | /* Enter the size of the array */ 32 | long n = 0; 33 | printf("Enter the number of elements to be sorted: "); 34 | scanf("%ld", &n); 35 | 36 | /* Enter the range of the array [0 .. m] */ 37 | long m = 0; 38 | printf("Enter the maximum value of the numbers to be sorted: "); 39 | scanf("%ld", &m); 40 | 41 | /* Enter the values of the array */ 42 | printf("Enter the values to be sorted: "); 43 | long long *arr = malloc(n * sizeof(long long)); 44 | for (long i = 0; i < n; ++i) { 45 | scanf("%lld", &arr[i]); 46 | } 47 | 48 | arr = count_sort_naive(arr, n, m); 49 | 50 | printf("After sorting\n"); 51 | for (long i = 0; i < n; ++i) { 52 | printf("%lld ", arr[i]); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /carrer 360/diagonal difference: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | int n,i,j,sum1=0,sum2=0; //n denotes the number of rows and columns in the matrix arr. 6 | 7 | scanf("%d", &n); 8 | int arr[n][n]; 9 | for (i=0;i=-100 && arr[i][j]<=100) 16 | { 17 | if(i==j) 18 | { 19 | sum1+=arr[i][j]; 20 | } 21 | if(j==(n-1-i)) 22 | { 23 | sum2+=arr[i][j]; 24 | } 25 | } 26 | } 27 | } 28 | // This code part belongs to the absolute difference between the sums of the matrix's along two diagonals 29 | if((sum1-sum2)<0) 30 | { 31 | printf("%d", (-((sum1)-(sum2)))); 32 | } 33 | else 34 | { 35 | printf("%d", ((sum1)-(sum2))); 36 | } 37 | return 0; 38 | } 39 | 40 | /* 41 | Sample Input 42 | 43 | 3 44 | 11 2 4 45 | 4 5 6 46 | 10 8 -12 47 | Sample Output 48 | 49 | 15 50 | 51 | Explanation 52 | 53 | The primary diagonal is: 54 | 55 | 11 56 | 5 57 | -12 58 | Sum across the primary diagonal: 11 + 5 - 12 = 4 59 | 60 | The secondary diagonal is: 61 | 62 | 4 63 | 5 64 | 10 65 | Sum across the secondary diagonal: 4 + 5 + 10 = 19 66 | Difference: |4 - 19| = 15 67 | */ 68 | -------------------------------------------------------------------------------- /carrer 360/dice roll: -------------------------------------------------------------------------------- 1 | //Simulate a diceroll with an adjustable number of sides using the rand() function. 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | int Diceroll(int DiceSides) 8 | { 9 | srand(time(NULL)); 10 | int n = (DiceSides - 1) + 1; //n in this case is the range of the random int 11 | int DiceRoll = 1 + rand() % n; 12 | 13 | return DiceRoll; 14 | } 15 | 16 | int main(int argc, char argv) 17 | { 18 | int DiceSides; 19 | printf("Enter the number of sides your die has. \n"); 20 | scanf("%d",&DiceSides); 21 | 22 | int Dice = Diceroll(DiceSides); 23 | printf("you rolled a %d \n",Dice); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /carrer 360/digital root: -------------------------------------------------------------------------------- 1 | // Program to find the digital root of a number in C 2 | // The digital root of a number is the single digit that you get by adding 3 | // all of the digits of the original number together. If the result of that 4 | // is multiple digits, you add those digits together, repeating the process 5 | //until you get a single digit. That digit is the digital root of the original number. 6 | 7 | #include 8 | 9 | int main() { 10 | unsigned int number, temp, droot = 0; 11 | printf("Enter a positive number: "); 12 | scanf("%u", &number); 13 | temp = number; 14 | while(temp != 0) { 15 | int digit = temp % 10; 16 | droot += digit; 17 | temp /=10; 18 | if(temp == 0 && droot > 9) { 19 | temp = droot; 20 | droot = 0; 21 | } 22 | } 23 | printf("The digital root of %u is %u\n", number, droot); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /carrer 360/dynamic2darrayusingpointer: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int r = 3, c = 4; 6 | int * arr = (int * ) malloc(r * c * sizeof(int)); 7 | 8 | int i, j, count = 0; 9 | for (i = 0; i < r; i++) 10 | for (j = 0; j < c; j++) 11 | * 12 | (arr + i * c + j) = ++count; 13 | 14 | for (i = 0; i < r; i++) 15 | for (j = 0; j < c; j++) 16 | printf("%d ", *(arr + i * c + j)); 17 | 18 | /* Code for further processing and free the 19 | dynamically allocated memory */ 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /carrer 360/dynamictwodarrayusingarrayofpointers: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int r = 3, c = 4, i, j, count; 6 | 7 | int * arr[r]; 8 | for (i = 0; i < r; i++) 9 | arr[i] = (int * ) malloc(c * sizeof(int)); 10 | 11 | // Note that arr[i][j] is same as *(*(arr+i)+j) 12 | count = 0; 13 | for (i = 0; i < r; i++) 14 | for (j = 0; j < c; j++) 15 | arr[i][j] = ++count; // Or *(*(arr+i)+j) = ++count 16 | 17 | for (i = 0; i < r; i++) 18 | for (j = 0; j < c; j++) 19 | printf("%d ", arr[i][j]); 20 | 21 | /* Code for further processing and free the 22 | dynamically allocated memory */ 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /carrer 360/findingfirstmissingnaturalnumbber: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { int a,k=1,i,c,d,n,arr[10]; 5 | 6 | printf("Enter the no of elements:\n"); 7 | scanf("%d",&n); 8 | for(a=0;a 3 | 4 | void create(int []); 5 | void down_adjust(int [],int); 6 | 7 | void main() 8 | { 9 | int heap[30],n,i,last,temp; 10 | printf("Enter no. of elements:"); 11 | scanf("%d",&n); 12 | printf("\nEnter elements:"); 13 | for(i=1;i<=n;i++) 14 | scanf("%d",&heap[i]); 15 | 16 | //create a heap 17 | heap[0]=n; 18 | create(heap); 19 | 20 | //sorting 21 | while(heap[0] > 1) 22 | { 23 | //swap heap[1] and heap[last] 24 | last=heap[0]; 25 | temp=heap[1]; 26 | heap[1]=heap[last]; 27 | heap[last]=temp; 28 | heap[0]--; 29 | down_adjust(heap,1); 30 | } 31 | 32 | //print sorted data 33 | printf("\nArray after sorting:\n"); 34 | for(i=1;i<=n;i++) 35 | printf("%d ",heap[i]); 36 | } 37 | 38 | void create(int heap[]) 39 | { 40 | int i,n; 41 | n=heap[0]; //no. of elements 42 | for(i=n/2;i>=1;i--) 43 | down_adjust(heap,i); 44 | } 45 | 46 | void down_adjust(int heap[],int i) 47 | { 48 | int j,temp,n,flag=1; 49 | n=heap[0]; 50 | 51 | while(2*i<=n && flag==1) 52 | { 53 | j=2*i; //j points to left child 54 | if(j+1<=n && heap[j+1] > heap[j]) 55 | j=j+1; 56 | if(heap[i] > heap[j]) 57 | flag=0; 58 | else 59 | { 60 | temp=heap[i]; 61 | heap[i]=heap[j]; 62 | heap[j]=temp; 63 | i=j; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /carrer 360/insertionsort: -------------------------------------------------------------------------------- 1 | // Insertion sort ascending order 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | int n, array[1000], c, d, t; 8 | 9 | printf("Enter number of elements\n"); 10 | scanf("%d", &n); 11 | 12 | printf("Enter %d integers\n", n); 13 | 14 | for (c = 0; c < n; c++) 15 | scanf("%d", &array[c]); 16 | 17 | for (c = 1 ; c <= n - 1; c++) { 18 | d = c; 19 | 20 | while ( d > 0 && array[d-1] > array[d]) { 21 | t = array[d]; 22 | array[d] = array[d-1]; 23 | array[d-1] = t; 24 | 25 | d--; 26 | } 27 | } 28 | 29 | printf("Sorted list in ascending order:\n"); 30 | 31 | for (c = 0; c <= n - 1; c++) { 32 | printf("%d\n", array[c]); 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /carrer 360/kth smallest number in array: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void merge(int a[],int f,int m,int l) 4 | { 5 | int n1=m+1-f; 6 | int n2=l-m; 7 | int t1[n1],t2[n2]; 8 | for (int i = 0; it2[j]) 16 | { 17 | a[k]=t2[j]; 18 | j++; 19 | } 20 | else 21 | { 22 | a[k]=t1[i]; 23 | i++; 24 | } 25 | k++; 26 | } 27 | while(i 4 | #include 5 | void main() 6 | { 7 | char str[20][20], temp[20]; 8 | int n, i, j; 9 | printf("Enter the Number of Strings:\n"); 10 | scanf("%d", &n); 11 | 12 | // Getting strings input 13 | printf("Enter the Strings:\n"); 14 | for (i = 0; i < n; i++) 15 | { 16 | scanf("%s", str[i]); 17 | } 18 | 19 | // storing strings in the lexicographical order 20 | for (i = 0; i < n - 1; i++) 21 | { 22 | for (j = 0; j < n - 1 - i; j++) 23 | { 24 | if (strcmp(str[j], str[j + 1]) > 0) 25 | { 26 | // swapping strings if they are not in the lexicographical order 27 | strcpy(temp, str[j]); 28 | strcpy(str[j], str[j + 1]); 29 | strcpy(str[j + 1], temp); 30 | } 31 | } 32 | } 33 | printf("Strings in the Lexicographical Order is:\n"); 34 | for (i = 0; i < n; i++) 35 | { 36 | puts(str[i]); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /carrer 360/linear search: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n=0; 5 | int i,a[]; 6 | int x,c=0; 7 | printf("ENTER SIZE OF ARRAY AND ARRAY ELEMENTS\n"); 8 | scanf("%d",&n); 9 | for(i=0;i 7 | #include 8 | struct node 9 | { 10 | int data; 11 | struct node *next; 12 | }; 13 | struct node *head, *tail = NULL; 14 | void addNode(int data) 15 | { 16 | struct node *newNode=(struct node*)malloc(sizeof(struct node)); 17 | newNode->data=data; 18 | newNode->next=NULL; 19 | if(head==NULL) 20 | { 21 | head=newNode; 22 | tail=newNode; 23 | } 24 | else 25 | { 26 | tail->next = newNode; 27 | tail = newNode; 28 | } 29 | } 30 | void display() 31 | { 32 | struct node *current = head; // the node current will point to head 33 | if(head==NULL) 34 | { 35 | printf("List is empty\n"); 36 | return; 37 | } 38 | printf("\nNodes of singly linked list: \n"); 39 | while(current!= NULL) 40 | { 41 | printf("%d ", current->data); // each node is printed 42 | current = current->next; 43 | } 44 | printf("\n"); 45 | } 46 | void delete() 47 | { 48 | struct node *ptr,*ptr1; 49 | int item,i=0,flag,loc,j; 50 | ptr = head; 51 | if(ptr == NULL) 52 | { 53 | printf("\nEmpty List\n"); 54 | } 55 | else 56 | { 57 | printf("\nWhich integer do you want to delete?\n"); 58 | scanf("%d",&item); 59 | while (ptr!=NULL) 60 | { 61 | if(ptr->data == item) 62 | { 63 | printf("Integer found at node %d ",i+1); 64 | flag=0; 65 | loc=i; 66 | break; 67 | } 68 | else 69 | { 70 | flag=1; 71 | } 72 | i++; 73 | ptr = ptr -> next; 74 | } 75 | if(flag==1) 76 | { 77 | printf("Integer not found!\n"); 78 | } 79 | } 80 | ptr=head; 81 | for(j=0;jnext; 85 | if(ptr == NULL) 86 | { 87 | printf("\nDeletion is not possible!"); 88 | return; 89 | } 90 | } 91 | ptr1->next = ptr ->next; 92 | free(ptr); 93 | printf("\nDeleted node %d \n",loc+1); 94 | } 95 | int main() 96 | { 97 | int n,m,data; 98 | printf("How many nodes do you want to create?\n"); 99 | scanf("%d",&n); 100 | for(int i=0;i 7 | #include 8 | 9 | struct node 10 | { 11 | int info; 12 | struct node* link; 13 | }; 14 | 15 | typedef struct node node; 16 | 17 | node *head=NULL, *head2=NULL; 18 | 19 | //*head=NULL; 20 | //*head2=NULL; 21 | 22 | void Binsert(int); 23 | void Einsert(int,node*); 24 | void Minsert(int); 25 | void Display(node*); 26 | void Bdelete(); 27 | void Edelete(); 28 | void Mdelete(int); 29 | void DeleteLL(node*); 30 | //void copy(); 31 | //node* locate(node*, int); 32 | //void reverse(node*); 33 | //void swaplink(node*); 34 | //void len(node*); 35 | 36 | 37 | int main() 38 | { 39 | while(1) 40 | { 41 | int opt,ele; 42 | printf("\nEnter what you wanna do:\n1.Binsert\n2.Einsert\n3.Minsert\n4.Display\n5.Bdelete\n6.Edelete\n7.Mdelete\n8.Copy\n9.DeleteLL\n10.Reverse\n11.Quit\n"); 43 | scanf("%d",&opt); 44 | switch(opt) 45 | { 46 | case 1: 47 | { 48 | printf("\nEnter the element"); 49 | scanf("%d",&ele); 50 | Binsert(ele); 51 | break; 52 | } 53 | case 2: 54 | { 55 | printf("\nEnter the element"); 56 | scanf("%d",&ele); 57 | Einsert(ele,head); 58 | break; 59 | } 60 | case 3: 61 | { 62 | printf("\nEnter the element"); 63 | scanf("%d",&ele); 64 | Minsert(ele); 65 | break; 66 | } 67 | case 4: 68 | { 69 | printf("\nEnter the LL(1/2)"); 70 | scanf("%d",&ele); 71 | if(ele==1) 72 | Display(head); 73 | else 74 | Display(head2); 75 | break; 76 | } 77 | case 5: 78 | { 79 | Bdelete(); 80 | break; 81 | } 82 | case 6: 83 | { 84 | Edelete(); 85 | break; 86 | } 87 | case 7: 88 | { 89 | printf("\nEnter the element you wanna delete:"); 90 | scanf("%d",&ele); 91 | Mdelete(ele); 92 | break; 93 | } 94 | /*case 8: 95 | { 96 | copy(); 97 | break; 98 | } 99 | case 9: 100 | { 101 | printf("\nEnter the LL(1/2)"); 102 | scanf("%d",&ele); 103 | if(ele==1) 104 | DeleteLL(head); 105 | else 106 | DeleteLL(head2); 107 | break; 108 | } 109 | case 10: 110 | { 111 | reverse(head); 112 | break; 113 | } 114 | case 11: 115 | { 116 | swaplink(head); 117 | } 118 | 119 | case 12: 120 | { 121 | exit(0); 122 | }*/ 123 | } 124 | } 125 | return 0; 126 | } 127 | 128 | void Binsert(int ele) 129 | { 130 | // if (head==NULL) 131 | // return; 132 | node* p=head; 133 | node* temp=(node*)malloc(sizeof(node)); 134 | temp->info=ele; 135 | temp->link=head; 136 | head=temp; 137 | } 138 | 139 | void Einsert(int ele,node* temph) 140 | { 141 | node* temp=(node*)malloc(sizeof(node)); 142 | temp->info=ele; 143 | node* p; 144 | p=temph; 145 | while(p->link!=NULL) 146 | p=p->link; 147 | temp->link=NULL; 148 | p->link=temp; 149 | } 150 | 151 | void Minsert(int ele) 152 | { 153 | if(head==NULL) 154 | { 155 | Binsert(ele); 156 | return; 157 | } 158 | if (head->info>ele) 159 | { 160 | Binsert(ele); 161 | return; 162 | } 163 | node *p,*q,*temp=(node*)malloc(sizeof(node)); 164 | temp->info=ele; 165 | p=head; 166 | q=head->link; 167 | while(q!=NULL) //q->link!=null?????????????????????????????????????????? works?????????????????????????????????? 168 | { 169 | if(q->info>=ele) 170 | { 171 | p->link=temp; 172 | temp->link=q; 173 | return; 174 | } 175 | p=p->link; 176 | q=q->link; 177 | } 178 | Einsert(ele,head); 179 | } 180 | 181 | void Display(node* temph) 182 | { 183 | node* p=temph; 184 | while(p!=NULL) 185 | { 186 | printf("%d->",p->info); 187 | p=p->link; 188 | } 189 | printf("\n"); 190 | } 191 | 192 | void Bdelete() 193 | { 194 | node* p=head; 195 | head=head->link; 196 | free(p); 197 | } 198 | 199 | void Edelete() 200 | { 201 | node *p,*q; 202 | p=head; 203 | while(p->link->link!=NULL) 204 | p=p->link; 205 | q=p->link; 206 | p->link=NULL; 207 | free(q); 208 | } 209 | 210 | void Mdelete(int ele) 211 | { 212 | if (head->info==ele) 213 | { 214 | Bdelete(); 215 | return; 216 | } 217 | node *p=head,*q=head->link; 218 | while(q->link!=NULL) //q->link!=null?????????????????????????????????????????? doesnt work 219 | { 220 | if(q->info==ele) 221 | { 222 | p->link=q->link; 223 | free(q); 224 | return; 225 | } 226 | } 227 | } 228 | 229 | void DeleteLL(node* temph) 230 | { 231 | node *p=temph,*q=temph->link; 232 | while(q!=NULL) 233 | { 234 | free(p); 235 | p=q; 236 | q=q->link; 237 | } 238 | free(p); 239 | temph=NULL; 240 | } 241 | 242 | void copy() 243 | { 244 | DeleteLL(head2); 245 | node *p,*q; 246 | p=head; 247 | q=head2; 248 | while(p!=NULL) 249 | Einsert(p->info,head2); 250 | } 251 | 252 | node* locate(node* head, int ind) 253 | { 254 | node *p; 255 | int x; 256 | p= head->link; 257 | x=0; 258 | while(x!=ind||p!=NULL) 259 | { 260 | p=p->link; 261 | x++; 262 | } 263 | return p; 264 | } 265 | /* 266 | void reverse(node* head) 267 | { 268 | printf("111111111111111111111111111111111111"); 269 | node *x = head, *p, *q; 270 | int n=0, temp=0,i; 271 | for (i=0;x!=NULL;i++) 272 | n++; 273 | printf("222222222222222222222222222222222222"); 274 | for(i=0;iinfo; 280 | p->info=q->info; 281 | q->info = temp;//mind fucked 282 | } 283 | } 284 | 285 | 286 | void swaplink(node* head) 287 | { 288 | node *p,*q; 289 | q=head; 290 | int leng=len(head); 291 | int count=leng-1,i,n=0; 292 | for(i=0;ilink; 298 | } 299 | q->link=p; 300 | n++; 301 | } 302 | } 303 | 304 | int len(node* head) 305 | { 306 | node* p=head; 307 | int x=1; 308 | while (p->link!=NULL) 309 | { 310 | p=p->link; 311 | x++; 312 | } 313 | return x; 314 | } 315 | 316 | 317 | 318 | */ 319 | 320 | 321 | 322 | 323 | 324 | -------------------------------------------------------------------------------- /carrer 360/linklist creation: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int n; 4 | struct node 5 | { 6 | int data; 7 | struct node *info; 8 | }*start; 9 | void create(int n) 10 | { 11 | int i; 12 | struct node *temp,*p,*newnode; 13 | p=(struct node *)malloc(sizeof(struct node)); 14 | start=p; 15 | printf("enter the data\n"); 16 | scanf("%d",&p->data); 17 | p->info=NULL; 18 | temp=start; 19 | for(i=2;i<=n;i++) 20 | { 21 | newnode=(struct node *)malloc(sizeof(struct node)); 22 | printf("enter the data\n"); 23 | scanf("%d",&newnode->data); 24 | newnode->info=NULL; 25 | temp->info=newnode; 26 | temp=temp->info; 27 | } 28 | } 29 | void display() 30 | { 31 | struct node *temp; 32 | temp=start; 33 | while(temp!=NULL) 34 | { 35 | printf("%d",temp->data); 36 | temp=temp->info; 37 | } 38 | } 39 | int main() 40 | { 41 | printf("enter the no of node"); 42 | scanf("%d",&n); 43 | create(n); 44 | printf("\n"); 45 | display(); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /carrer 360/magic number: -------------------------------------------------------------------------------- 1 | //C program to check if a number is magic number or not 2 | //Magic Number:A number is said to be a magic number 3 | //if the product of the sum and the reverse number of the sum is the given number then the number is the magic number. 4 | //Ex:1729 is a magic number 5 | //Code: 6 | 7 | #include 8 | /* sum of digits of a number */ 9 | int sumOfDigits(int num) { 10 | int sum = 0; 11 | while (num > 0) { 12 | sum = sum + (num % 10); 13 | num = num / 10; 14 | } 15 | return sum; 16 | } 17 | 18 | /* returns reverse of a given number */ 19 | int reverse(int num) { 20 | int rev = 0; 21 | while (num > 0) { 22 | rev = (rev * 10) + (num % 10); 23 | num = num / 10; 24 | } 25 | return rev; 26 | } 27 | 28 | int main () { 29 | int num, sum, rev; 30 | 31 | printf("Enter the value for a number:"); 32 | scanf("%d", &num); 33 | 34 | /* find sum of digits by calling function */ 35 | sum = sumOfDigits(num); 36 | 37 | 38 | //if the value is single digit, then 39 | //the value and its reverse are same 40 | if (sum < 10) { 41 | if ((sum * sum) == num) { 42 | printf("%d is a magic number\n", num); 43 | } else { 44 | printf("%d is not a magic number\n", num); 45 | } 46 | return 0; 47 | } 48 | 49 | /* reverse of the given number */ 50 | rev = reverse(sum);//calling reverse function 51 | 52 | /* printing the outputs */ 53 | if ((sum * rev) == num) { 54 | printf("%d is a magic number\n", num); 55 | } else { 56 | printf("%d is not a magic number\n", num); 57 | } 58 | 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /carrer 360/mergesort: -------------------------------------------------------------------------------- 1 | // Merge sort Algorithm 2 | #include 3 | 4 | void mergesort(int a[], int i, int j); 5 | void merge(int a[], int i1, int j1, int i2, int j2); 6 | 7 | int main() { 8 | int a[30], n, i; 9 | printf("Enter no of elements:"); 10 | scanf("%d", & n); 11 | printf("Enter array elements:"); 12 | 13 | for (i = 0; i < n; i++) 14 | scanf("%d", & a[i]); 15 | 16 | mergesort(a, 0, n - 1); 17 | 18 | printf("\nSorted array is :"); 19 | for (i = 0; i < n; i++) 20 | printf("%d ", a[i]); 21 | 22 | return 0; 23 | } 24 | 25 | void mergesort(int a[], int i, int j) { 26 | int mid; 27 | 28 | if (i < j) { 29 | mid = (i + j) / 2; 30 | mergesort(a, i, mid); //left recursion 31 | mergesort(a, mid + 1, j); //right recursion 32 | merge(a, i, mid, mid + 1, j); //merging of two sorted sub-arrays 33 | } 34 | } 35 | 36 | void merge(int a[], int i1, int j1, int i2, int j2) { 37 | int temp[50]; //array used for merging 38 | int i, j, k; 39 | i = i1; //beginning of the first list 40 | j = i2; //beginning of the second list 41 | k = 0; 42 | 43 | while (i <= j1 && j <= j2) //while elements in both lists 44 | { 45 | if (a[i] < a[j]) 46 | temp[k++] = a[i++]; 47 | else 48 | temp[k++] = a[j++]; 49 | } 50 | 51 | while (i <= j1) //copy remaining elements of the first list 52 | temp[k++] = a[i++]; 53 | 54 | while (j <= j2) //copy remaining elements of the second list 55 | temp[k++] = a[j++]; 56 | 57 | //Transfer elements from temp[] back to a[] 58 | for (i = i1, j = 0; i <= j2; i++, j++) 59 | a[i] = temp[j]; 60 | } 61 | -------------------------------------------------------------------------------- /carrer 360/mirrornumber: -------------------------------------------------------------------------------- 1 | // Program to find if a number is mirror number or not 2 | #include 3 | #include 4 | int main() { 5 | int num, reverse1, reverse2, remainder1, remainder2, square, sqroot; 6 | reverse1 = 0; 7 | reverse2 = 0; 8 | /*If we don't initialize than without a initial value,reverse1 and 9 | reverse2 will contain garbage values so that run time error will occur. 10 | */ 11 | printf("Enter a number\n"); 12 | scanf("%d", & num); 13 | square = pow(num, 2); 14 | while (square != 0) { 15 | remainder1 = square % 10; 16 | reverse1 = reverse1 * 10 + remainder1; 17 | square = square / 10; 18 | 19 | } 20 | sqroot = sqrt(reverse1); 21 | 22 | while (sqroot != 0) { 23 | remainder2 = sqroot % 10; 24 | reverse2 = reverse2 * 10 + remainder2; 25 | sqroot = sqroot / 10; 26 | } 27 | 28 | if (reverse2 == num) 29 | printf("number is mirror"); 30 | else 31 | printf("Not a mirror number"); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /carrer 360/p_queue: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int CAPACITY = 10; 6 | int size; 7 | 8 | typedef struct task { 9 | int id; 10 | int priority; 11 | }task; 12 | 13 | int getLeftChildIndex(int i); 14 | int getRightChildIndex(int i); 15 | int getParentIndex(int i); 16 | bool hasLeftChild(int i); 17 | bool hasRightChild(int i); 18 | bool hasParent(int i); 19 | task leftChild(int i, task* taskList); 20 | task rightChild(int i, task* taskList); 21 | task parent(int i, task* taskList); 22 | void swap(int i, int j, task* taskList); 23 | void ensureExtraCapacity(task** taskList); 24 | task peek(task *taskList); 25 | task poll(task *taskList); 26 | void addTask(task t, task* taskList); 27 | void heapifyUp(task* taskList); 28 | void heapifyDown(task* taskList); 29 | 30 | 31 | int main() { 32 | task *taskList = malloc(sizeof(int)*CAPACITY); 33 | task task; 34 | int op; 35 | printf("\tIn this program the greater the priority value of the task, the higher is the priority of the task\n"); 36 | for(int i = 0; i < 110; ++i) 37 | printf("-"); 38 | printf("\n"); 39 | do { 40 | printf("1...Enter a new task\n"); 41 | printf("2...Remove highest priority task\n"); 42 | printf("3...Show highest priority task\n"); 43 | printf("4...Exit\n"); 44 | printf("Enter you option below:\n"); 45 | printf("-> "); scanf("%d", &op); 46 | printf("\n"); 47 | 48 | switch(op) { 49 | case 1: 50 | printf("Enter the task ID:\n"); 51 | printf("-> "); scanf("%d", &task.id); 52 | 53 | printf("Enter the task Priority:\n"); 54 | printf("-> "); scanf("%d", &task.priority); 55 | 56 | addTask(task, taskList); 57 | break; 58 | case 2: 59 | task = poll(taskList); 60 | printf("Element Processed is:\n"); 61 | printf("ID: %d\n", task.id); 62 | printf("Priority: %d\n", task.priority); 63 | break; 64 | case 3: 65 | task = peek(taskList); 66 | printf("Element Ready to be Processed first is:\n"); 67 | printf("ID: %d\n", task.id); 68 | printf("Priority: %d\n", task.priority); 69 | break; 70 | case 4: 71 | // nothing to see here... 72 | break; 73 | default: 74 | printf("Invalid option entered, please try again !\n\n"); 75 | break; 76 | } 77 | for(int i = 0; i < 100; ++i) 78 | printf("-"); 79 | printf("\n"); 80 | } while(op != 4); 81 | free(taskList); 82 | return 0; 83 | } 84 | 85 | void heapifyDown(task* taskList) { 86 | int index = 0; 87 | int greaterChildIndex = getLeftChildIndex(index); 88 | while(hasLeftChild(index)) { 89 | if(hasRightChild(index) && rightChild(index, taskList).priority > leftChild(index, taskList).priority) { 90 | greaterChildIndex = getRightChildIndex(index); 91 | } 92 | if(taskList[index].priority > taskList[greaterChildIndex].priority) { 93 | break; 94 | } 95 | else { 96 | swap(index, greaterChildIndex, taskList); 97 | } 98 | index = greaterChildIndex; 99 | } 100 | } 101 | 102 | void heapifyUp(task* taskList) { 103 | int index = size - 1; 104 | while(hasParent(index) && parent(index, taskList).priority < taskList[index].priority) { 105 | swap(getParentIndex(index), index, taskList); 106 | index = getParentIndex(index); 107 | } 108 | } 109 | 110 | void addTask(task t, task* taskList) { 111 | ensureExtraCapacity(&taskList); 112 | taskList[size] = t; 113 | ++size; 114 | heapifyUp(taskList); 115 | } 116 | 117 | task poll(task *taskList) { 118 | if(!size) { 119 | printf("Error !\n"); 120 | exit(0); 121 | } 122 | task t = taskList[0]; 123 | taskList[0] = taskList[size-1]; 124 | --size; 125 | heapifyDown(taskList); 126 | return t; 127 | } 128 | 129 | task peek(task *taskList) { 130 | if(!size) { 131 | printf("Error !\n"); 132 | exit(0); 133 | } 134 | return taskList[0]; 135 | } 136 | 137 | void ensureExtraCapacity(task** taskList) { 138 | if(size > 2*CAPACITY/3) 139 | *taskList = realloc(*taskList,CAPACITY*=2); 140 | } 141 | 142 | void swap(int i, int j, task* taskList) { 143 | task temp = taskList[i]; 144 | taskList[i] = taskList[j]; 145 | taskList[j] = temp; 146 | } 147 | 148 | task parent(int i, task* taskList) { 149 | return taskList[getParentIndex(i)]; 150 | } 151 | 152 | task rightChild(int i, task* taskList) { 153 | return taskList[getRightChildIndex(i)]; 154 | } 155 | 156 | task leftChild(int i, task* taskList) { 157 | return taskList[getLeftChildIndex(i)]; 158 | } 159 | 160 | bool hasParent(int i) { 161 | return getParentIndex(i) >= 0; 162 | } 163 | 164 | bool hasRightChild(int i) { 165 | return getRightChildIndex(i) < size; 166 | } 167 | 168 | bool hasLeftChild(int i) { 169 | return getLeftChildIndex(i) < size; 170 | } 171 | 172 | int getLeftChildIndex(int i) { 173 | return 2*i + 1; 174 | } 175 | 176 | int getRightChildIndex(int i) { 177 | return 2*i + 2; 178 | } 179 | 180 | int getParentIndex(int i) { 181 | return (i-1)/2; 182 | } 183 | -------------------------------------------------------------------------------- /carrer 360/pattern spiral: -------------------------------------------------------------------------------- 1 | 2 | input 7 3 | 4 | 7777777777777 5 | 7666666666667 6 | 7655555555567 7 | 7654444444567 8 | 7654333334567 9 | 7654322234567 10 | 7654321234567 11 | 7654322234567 12 | 7654333334567 13 | 7654444444567 14 | 7655555555567 15 | 7666666666667 16 | 7777777777777 17 | #include 18 | 19 | int main() 20 | { 21 | int i,n,j,k,l; 22 | scanf("%d",&n); 23 | i=n; 24 | while(i>=1) 25 | { 26 | j=n; 27 | while(j>=i) 28 | { 29 | printf("%d",j); 30 | j-=1; 31 | } 32 | k=1; 33 | while(k<(i*2)-1) 34 | { 35 | printf("%d",i); 36 | k+=1; 37 | } 38 | l=i+1; 39 | while(l<=n) 40 | { 41 | printf("%d",l); 42 | l+=1; 43 | 44 | } 45 | i-=1; 46 | printf("\n"); 47 | 48 | } 49 | i=2; 50 | while(i<=n) 51 | { 52 | j=n; 53 | while(j>=i) 54 | { 55 | printf("%d",j); 56 | j-=1; 57 | } 58 | k=1; 59 | while(k<(i*2)-1) 60 | { 61 | printf("%d",i); 62 | k+=1; 63 | } 64 | l=i+1; 65 | while(l<=n) 66 | { 67 | printf("%d",l); 68 | l+=1; 69 | } 70 | 71 | i+=1; 72 | printf("\n"); 73 | } 74 | return 0; 75 | } 76 | 77 | 78 | -------------------------------------------------------------------------------- /carrer 360/replacing pi with 3.14: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | char x[100],y[100]; 5 | int i,j,pos=0; 6 | gets(x); 7 | for (i=0;x[i]!='\0';i++) 8 | { 9 | if (x[i]=='p' && x[i+1]=='i') 10 | { 11 | pos=i; 12 | break; 13 | } 14 | else 15 | { 16 | y[i]=x[i]; 17 | } 18 | } 19 | y[pos]='3'; 20 | y[pos+1]='.'; 21 | y[pos+2]='1'; 22 | y[pos+3]='4'; 23 | for (j=pos+4,i=pos+2;x[i]!='\0';j++,i++) 24 | { 25 | y[j]=x[i]; 26 | } 27 | puts(y); 28 | return 0; 29 | } 30 | 31 | 32 | 33 | 34 | #include 35 | #include 36 | void replacep(char y[],int startindex) 37 | { 38 | if (y[startindex]=='\0' || y[startindex+1]=='\0') 39 | return; 40 | replacep(y,startindex+1); 41 | if (y[startindex] == 'p' && y[startindex + 1] == 'i') { 42 | for (int i = strlen(y); i >= startindex + 2; i--) { 43 | y[i + 2] = y[i]; 44 | } 45 | 46 | y[startindex] = '3'; 47 | y[startindex + 1] = '.'; 48 | y[startindex + 2] = '1'; 49 | y[startindex + 3] = '4'; 50 | 51 | } 52 | } 53 | void replacepi(char y[]) 54 | { 55 | replacep(y,0); 56 | } 57 | 58 | int main() 59 | { 60 | char x[]="pipimanoj"; 61 | replacepi(x); 62 | puts(x); 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /carrer 360/selection sort: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //function for swapping two numbers 4 | void swap(int * xp, int * yp) { 5 | int temp = * xp; 6 | * xp = * yp; 7 | * yp = temp; 8 | } 9 | 10 | void selectionSort(int arr[], int n) { 11 | int i, j, min_idx; 12 | 13 | // One by one move boundary of unsorted subarray 14 | for (i = 0; i < n - 1; i++) { 15 | // Find the minimum element in unsorted array 16 | min_idx = i; 17 | for (j = i + 1; j < n; j++) 18 | if (arr[j] < arr[min_idx]) 19 | min_idx = j; 20 | 21 | // Swap the found minimum element with the first element 22 | //so that the minimum element of unsorted subarray 23 | //comes to first position 24 | swap( & arr[min_idx], & arr[i]); 25 | } 26 | } 27 | 28 | int main() { 29 | 30 | int n, i; //n is size of array 31 | printf("Enter the number of elements "); 32 | scanf("%d",&n); 33 | int a[n]; //array of n integers to be sorted 34 | printf("Enter the elements "); 35 | for (i = 0; i < n; i++) 36 | scanf("%d",&a[i]); 37 | 38 | selectionSort(a, n); //calling the function to sort the array 39 | //printing sorted array 40 | for (i = 0; i < n; i++) 41 | printf("%d ", a[i]); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /carrer 360/slicestring: -------------------------------------------------------------------------------- 1 | #include 2 | void slice(char *st, int m, int n) 3 | { 4 | int i = 0; 5 | while ((m + i) < n) 6 | { 7 | st[i] = st[m + i]; 8 | i++; 9 | } 10 | st[i] = '\0'; 11 | } 12 | int main() 13 | { 14 | char st[] = "Hello"; 15 | slice(st, 1, 4); 16 | printf("%s", st); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /carrer 360/sparcematrix: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); 6 | int n, m, c, d, matrix[10][10]; 7 | int counter = 0; 8 | printf("\nEnter the number of rows and columns of the matrix \n\n"); 9 | scanf("%d%d",&m,&n); 10 | 11 | printf("\nEnter the %d elements of the matrix \n\n", m*n); 12 | for(c = 0; c < m; c++) // to iterate the rows 13 | { 14 | for(d = 0; d < n; d++) // to iterate the columns 15 | { 16 | scanf("%d", &matrix[c][d]); 17 | if(matrix[c][d] == 0) 18 | counter++; // same as counter=counter +1 19 | } 20 | } 21 | 22 | // printing the matrix 23 | printf("\n\nThe entered matrix is: \n\n"); 24 | for(c = 0; c < m; c++) // to iterate the rows 25 | { 26 | for(d = 0; d < n; d++) // to iterate the columns 27 | { 28 | printf("%d\t", matrix[c][d]); 29 | } 30 | printf("\n"); // to take the control to the next row 31 | } 32 | 33 | // checking if the matrix is sparse or not 34 | if(counter > (m*n)/2) 35 | printf("\n\nThe entered matrix is a sparse matrix\n\n"); 36 | else 37 | printf("\n\nThe entered matrix is not a sparse matrix\n\n"); 38 | 39 | printf("\n\n\t\t\tCoding is Fun !\n\n\n"); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /carrer 360/spiralmatrix: -------------------------------------------------------------------------------- 1 | #include 2 | #define Row 4 3 | #define Col 3 4 | void spiral_matrix(int r, int c, int arr[Row][Col]) 5 | { 6 | int i; 7 | int k = 0, l = 0; 8 | while (k < r && l < c) 9 | { 10 | for (i = l; i < c ; ++i) { 11 | printf("%d\t", arr[k][i]); 12 | } 13 | k++; 14 | 15 | for (i = k; i < r; ++i) { 16 | printf("%d\t", arr[i][c - 1]); 17 | } 18 | c--; 19 | if (k < r) { 20 | for (i = c - 1; i >= l; --i) { 21 | printf("%d\t", arr[r - 1][i]); 22 | } 23 | r--; 24 | } 25 | if (l < c) { 26 | for (i = r - 1 ; i >= k; --i) { 27 | printf("%d\t", arr[i][l]); 28 | } 29 | l++; 30 | } 31 | } 32 | } 33 | 34 | int main() 35 | { 36 | int a[Row][Col] = {{1, 2, 3}, {10, 20, 30}, {110, 220, 330}, {1100, 2200, 3300}}; 37 | 38 | spiral_matrix (Row, Col, a); 39 | return (0); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /carrer 360/stack link list: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define SIZE 11 4 | 5 | typedef struct node{ 6 | char data; 7 | struct node *link; 8 | }ctype, *LinkStack; 9 | 10 | void push(LinkStack * L, char elem); 11 | void pop(LinkStack *L); 12 | char top(LinkStack L); 13 | void display(LinkStack L); 14 | 15 | int main() { 16 | LinkStack A = NULL; 17 | int i; 18 | char word[SIZE] = {'P', 'R', 'O','G','R','A','M','M','I','N','G'}; 19 | char first; 20 | 21 | 22 | for (i = 0; i < SIZE; i++) { 23 | push(&A, word[i]); /* INSERTING THE CHARACTER IN LINK LIST*/ 24 | } 25 | 26 | 27 | printf("Example word:\n"); 28 | display(A); 29 | 30 | printf("\n\nAfter popping/deleting the first/top most element:\n"); 31 | pop(&A); 32 | display(A); 33 | 34 | 35 | first = top(A); 36 | printf("\n\nTop most element in the stack: %c\n", first); 37 | 38 | return 0; 39 | } 40 | 41 | /* PUSHING/INSERTING EACH CHARACTER TO THE STACK USING LINKLIST 42 | 43 | NOTE: 44 | Since this is a stack, I used the First in Last out order and we dont need to traverse, 45 | thats why the element is interchanged/reversed. 46 | */ 47 | 48 | void push(LinkStack * L, char elem) { 49 | LinkStack temp; 50 | temp = (LinkStack)malloc(sizeof(ctype)); 51 | 52 | if (temp != NULL) { 53 | temp->data = elem; 54 | temp->link = *L; 55 | *L = temp; 56 | } 57 | } 58 | 59 | 60 | /* DELETING/REMOVING THE TOP MOST ELEMENT IN THE STACK*/ 61 | void pop(LinkStack *L) { 62 | LinkStack temp; 63 | 64 | temp = *L; 65 | *L = temp->link; 66 | free(temp); 67 | } 68 | 69 | 70 | /* RETURNING THE TOP MOST ELEMENT IN THE STACK*/ 71 | char top(LinkStack L) { 72 | char elem; 73 | elem = L->data; 74 | 75 | return elem; 76 | } 77 | 78 | 79 | /* FOR DISPLAYING THE ELEMENTS */ 80 | void display(LinkStack L) { 81 | LinkStack trav; 82 | 83 | for (trav = L; trav != NULL; trav = trav->link) { 84 | printf("%c", trav->data); 85 | } 86 | } 87 | 88 | -------------------------------------------------------------------------------- /carrer 360/stack using link list: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node* link; 8 | } *top=NULL; 9 | void push(int x) 10 | { 11 | 12 | struct node *new; 13 | new=(struct node*)malloc(sizeof(struct node)); 14 | new->data=x; 15 | new->link=top; 16 | top=new; 17 | } 18 | void display() 19 | { 20 | 21 | struct node *temp; 22 | temp=top; 23 | 24 | if(top==NULL) 25 | { 26 | printf("Underflow"); 27 | } 28 | else 29 | { 30 | while(temp!=NULL) 31 | { 32 | 33 | printf("%d",temp->data); 34 | printf("\n"); 35 | temp=temp->link; 36 | } 37 | } 38 | } 39 | void pop() 40 | { 41 | 42 | struct node *temp; 43 | 44 | if(top==NULL) 45 | { 46 | printf("No element to deletes "); 47 | } 48 | else 49 | { 50 | temp=top; 51 | printf("Popped element is %d\t",temp->data); 52 | top=top->link; 53 | free(temp); 54 | } 55 | } 56 | 57 | int main() 58 | { 59 | 60 | 61 | int x; 62 | int ch; 63 | do{ 64 | printf("\t1.Push\t2.pop\t3.display\t4.exit\t"); 65 | scanf("%d",&ch); 66 | switch(ch) 67 | { 68 | case 1:printf(" Enter element\t"); 69 | scanf("%d",&x); 70 | push(x); 71 | break; 72 | case 2:pop(); 73 | break; 74 | case 3:display(); 75 | break; 76 | case 4:exit(0); 77 | } 78 | }while(ch!=0); 79 | getch(); 80 | } 81 | -------------------------------------------------------------------------------- /complete_square.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main(){ 3 | int i,j,a,b; 4 | printf("Please enter the number of rows: "); 5 | scanf("%d",&a); 6 | printf("Please enter the number of columns: "); 7 | scanf("%d",&b); 8 | for(i=1;i<=a;i++){ 9 | for(j=1;j<=b;j++){ 10 | printf("* "); 11 | } 12 | printf("\n"); 13 | } 14 | } -------------------------------------------------------------------------------- /decision control statements.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/decision control statements.pdf -------------------------------------------------------------------------------- /dimond.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int n, c, k; 5 | 6 | printf("Enter number of rows\n"); 7 | scanf("%d", &n); 8 | 9 | for (k = 1; k <= n; k++) 10 | { 11 | for (c = 1; c <= n-k; c++) 12 | printf(" "); 13 | 14 | for (c = 1; c <= 2*k-1; c++) 15 | printf("*"); 16 | 17 | printf("\n"); 18 | } 19 | 20 | for (k = 1; k <= n - 1; k++) 21 | { 22 | for (c = 1; c <= k; c++) 23 | printf(" "); 24 | 25 | for (c = 1 ; c <= 2*(n-k)-1; c++) 26 | printf("*"); 27 | 28 | printf("\n"); 29 | } 30 | 31 | 32 | } -------------------------------------------------------------------------------- /full_pyramid.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() { 3 | int i, space, rows, k = 0; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows);//Taking the incput for te rows 6 | for (i = 1; i <= rows; ++i, k = 0) {//iterating for the rows 7 | for (space = 1; space <= rows - i; ++space) { 8 | printf(" ");//for the spaces 9 | } 10 | while (k != 2 * i - 1) { 11 | printf("* ");// for the stars 12 | ++k; 13 | } 14 | printf("\n");//new line after each row 15 | } 16 | } -------------------------------------------------------------------------------- /full_square_alphbet.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main(){ 3 | int i,a,b; 4 | char j; 5 | printf("Please enter the number of rows: "); 6 | scanf("%d",&a); 7 | printf("Please enter the number of column: "); 8 | scanf("%d",&b); 9 | b = b+65; 10 | for(i=1;i<=a;i++){ 11 | for(j=65;j<=b;j++){ 12 | printf("%c ",j); 13 | } 14 | printf("\n"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /full_square_with_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main(){ 3 | int i,j,a,b; 4 | printf("Please enter the number of rows: "); 5 | scanf("%d",&a); 6 | printf("Please enter the number of columns: "); 7 | scanf("%d",&b); 8 | for(i=1;i<=a;i++){ 9 | for(j=1;j<=b;j++){ 10 | printf("%d ",j); 11 | } 12 | printf("\n"); 13 | } 14 | } -------------------------------------------------------------------------------- /functions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/functions.pdf -------------------------------------------------------------------------------- /half_pyramid.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main(){ 3 | int i,j,a,b; 4 | printf("Enter the number of rows: "); 5 | scanf("%d",&a); 6 | printf("Enter the number of columns: "); 7 | scanf("%d",&b); 8 | for(i=1;i<=a;i++){ 9 | for(j=1;j<=i;j++){ 10 | printf("%d ",j); 11 | } 12 | printf("\n"); 13 | } 14 | } -------------------------------------------------------------------------------- /holo_half_pyramid.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main(){ 3 | int i,j,a,b; 4 | printf("Enter the number of rows: "); 5 | scanf("%d",&a); 6 | printf("Enter the number of columns: "); 7 | scanf("%d",&b); 8 | for(i=1;i<=a;i++){ 9 | for(j=1;j<=i;j++){ 10 | if(i==a||j==1||i==j){ 11 | printf("* "); 12 | } 13 | else 14 | printf(" "); 15 | } 16 | printf("\n"); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /holo_sqare_with_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main(){ 3 | int i,j,a,b; 4 | printf("Enter the number of rows: "); 5 | scanf("%d",&a); 6 | printf("Enter the number of columns: "); 7 | scanf("%d",&b); 8 | for(i=1;i<=a;i++){ 9 | for(j=1;j<=b;j++){ 10 | if(i==1||i==a||j==b||j==1){ 11 | printf("%d ",j); 12 | } 13 | else 14 | printf(" "); 15 | } 16 | printf("\n"); 17 | } 18 | } -------------------------------------------------------------------------------- /holo_square.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main(){ 3 | int i,j,a,b; 4 | printf("Please enter the number of rows: "); 5 | scanf("%d",&a); 6 | printf("Please enter the number of columns: "); 7 | scanf("%d",&b); 8 | for(i=1;i<=a;i++){ 9 | for(j=1;j<=b;j++){ 10 | if(i==1||i==a||j==1||j==b){ 11 | printf("* "); 12 | } 13 | else 14 | printf(" "); 15 | } 16 | printf("\n"); 17 | } 18 | } -------------------------------------------------------------------------------- /holosquare_with_diagonal.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main(){ 3 | int i,j,a,b; 4 | printf("Enter the number of rows: "); 5 | scanf("%d",&a); 6 | printf("Enter the number of columns: "); 7 | scanf("%d",&b); 8 | for(i=1;i<=a;i++){ 9 | for(j=1;j<=b;j++){ 10 | if(i==1||i==a||j==1||j==b||j==i){ 11 | printf("* "); 12 | } 13 | else 14 | printf(" "); 15 | } 16 | printf("\n"); 17 | } 18 | } -------------------------------------------------------------------------------- /if else questions_c language.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrManoj001/CProgramming/fcec410683def169e534523b507a3bd2ad690f0b/if else questions_c language.pdf -------------------------------------------------------------------------------- /inverted_full_pyramid.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() { 3 | int rows, i, j, space; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows); 6 | for (i = rows; i >= 1; --i) { 7 | for (space = 0; space < rows - i; ++space) 8 | printf(" "); 9 | for (j = i; j <= 2 * i - 1; ++j) 10 | printf("* "); 11 | for (j = 0; j < i - 1; ++j) 12 | printf("* "); 13 | printf("\n"); 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /zigzag.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define C 3 4 | #define min(a, b) a>b?b:a 5 | #define max(a, b) a>b?a:b 6 | int main(){ 7 | int k = 3, l = 3; 8 | int mat[][3] = { 9 | { 10, 20, 30 }, 10 | { 40, 50, 60 }, 11 | { 70, 80, 90 } 12 | }; 13 | int row = 0, col = 0; 14 | bool flag = false; 15 | int i, j, len, diag; 16 | int MAX; 17 | int mn = min(k, l); //to check the minimum number and return that minimum number 18 | for ( len = 1; len <= mn; ++len) { 19 | for ( i = 0; i < len; ++i) { 20 | printf("%d ", mat[row][col]); //Printing the matrix in zigzag format 21 | if (i + 1 == len) 22 | break; 23 | if (flag) 24 | ++row, --col; 25 | else 26 | --row, ++col; 27 | } 28 | if (len == mn) 29 | break; 30 | if (flag) 31 | ++row, flag = false; 32 | else 33 | ++col, flag = true; 34 | } 35 | if (row == 0) { 36 | if (col == k - 1) 37 | ++row; 38 | else 39 | ++col; 40 | flag = 1; 41 | } else { 42 | if (row == l - 1) 43 | ++col; 44 | else 45 | ++row; 46 | flag = 0; 47 | } 48 | MAX = max(k, l) - 1; //To check the maximum element 49 | for ( len, diag = MAX; diag > 0; --diag) { //Loop to go diagonally. 50 | if (diag > mn) 51 | len = mn; 52 | else 53 | len = diag; 54 | for ( i = 0; i < len; ++i) { 55 | printf("%d ", mat[row][col]); 56 | if (i + 1 == len) 57 | break; 58 | if (flag) 59 | ++row, --col; 60 | else 61 | ++col, --row; 62 | } 63 | if (row == 0 || col == k - 1) { 64 | if (col == k - 1) 65 | ++row; 66 | else 67 | ++col; 68 | flag = true; 69 | } 70 | else if (col == 0 || row == l - 1) { 71 | if (row == l - 1) 72 | ++col; 73 | else 74 | ++row; 75 | flag = false; 76 | } 77 | } 78 | return 0; 79 | } --------------------------------------------------------------------------------