├── ARRAY.c ├── Adding_Fractions.c ├── Addition.c ├── Address of 1-D array ├── AllTempScalesConv.c ├── Anagram-Program-in-C ├── AreaAndCircumference.c ├── Area_of_Circle.c ├── Area_of_Square.c ├── Area_of_Triangle.c ├── ArmstrongNumber.c ├── Automorphic_number.c ├── BackgroundThreadSorter.c ├── BasicArithmatic.c ├── BasicGame.c ├── Binary to decimal ├── Binary-Tree-Traversals ├── BinarySearch.c ├── BitwiseAndOperator.c ├── BitwiseComplementOperator.c ├── BitwiseLeftshiftOperator.c ├── BitwiseOddOrEven.c ├── BubbleSort.c ├── Buffer Overflow for Kids.md ├── CalcUsingSwitchCase.c ├── CalculateSimpleInterest.c ├── CelciusToKelvinConv.c ├── Changingbase.c ├── CheckCharacterType.c ├── CommonElementsInTwoArrays.c ├── Convert an array to reduced form.c ├── DailyWageCalc.c ├── DayNameUsingSwitchCase.c ├── Decimal to Binary ├── DecimalToBaseN.c ├── DecimalToBinary.c ├── DecimalToHexadecimalViceVersa.c ├── DecrementOperator.c ├── Diagonal-Difference.c ├── DiceRoll.c ├── DigitalRoot.c ├── DisplayLinuxEnvirmentVariables.c ├── Division.c ├── DynamicMemoryAllocation ├── DynamicTwoDArrayUsingArrayOfPointer.c ├── DynamicTwoDArrayUsingOnePointer.c ├── EmployeeGrade.c ├── EncryptDecryptXOR.c ├── Even Fibonacci numbers ├── FIND_EVEN_ AND_DIVISIBLE_BY_3.c ├── Factorial.c ├── FahrenheitToCelciusConv.c ├── FibonacciGeneration.c ├── FindAsciiValue.c ├── FindRemainder.c ├── GotoStatementEvenOrOdd.c ├── Gross_Salary.c ├── HaystackAndNeedle_SubString.c ├── HelloWorld.c ├── IncrementOperator.c ├── Insertionsort.c ├── LICENSE ├── Largest.c ├── LeapYearTernaryOperator.c ├── LenghtOfString(without using strlen).cpp ├── Lexicographic_Sorting.c ├── LinearCongruentialGenerator.c ├── Linked List Creation ├── LinkedLists.c ├── Linked_List.c ├── Log_Conversion.c ├── LowercaseToUppercase.c ├── MagicNumbers.c ├── Mergesort.c ├── MirrorNumber.c ├── MostFrequentWordInString.c ├── NaiveModularInverse.c ├── NestedIfLeapYear.c ├── NestedifGreatestInteger.c ├── NumberPattern.c ├── Number_guessing_game ├── Number_to_Character.c ├── Palindrome.c ├── PalindromeNumber.c ├── PascalTriangle.c ├── Pattern Combos ├── Pattern1.c ├── PerfectNumber.c ├── Pointers.c ├── Polynomial_linklist.c ├── Position of First'n'Second maximum element in Array without sorting ├── Position_of_First'n'Second_max_elements_without_sorting_array ├── Prime.c ├── PrimeByEratosthenes.c ├── Prime_No.c ├── Quicksort.c ├── README.md ├── Recursion.c ├── RecursiveFactorial.c ├── RelationalOperators.c ├── ReverseNumber.c ├── ReverseNumber2.c ├── RussianPeasantMultiplication.c ├── Screenshot.PNG ├── SegmentationFaultorBusErrorDemo.c ├── SelectionSort.c ├── Separate.c ├── SimpleArithmeticAverage ├── SimpleEMICalculator.c ├── SimpleInterestCalculator.c ├── SimpleMultiplicationTable.c ├── Simple_Interest.c ├── SizeofOperator.c ├── Slicestring ├── SparseMatrix_017.c ├── SpiralMatrix.c ├── SquareRoot.c ├── Stack - Linked List.c ├── Star_Pattern.c ├── StringLength.c ├── StringReverse.c ├── Structure.c ├── StudentMarksPercentage.c ├── Subtraction.c ├── SumUsingThreads.c ├── SwapByRefandCopy.c ├── SwapIntegers.c ├── SwapIntegersWithout3rdVariable(Arithmatic).c ├── SwapValueUsingThirdVariable.c ├── SwapValueWithoutUsingThirdVariable.c ├── Swap_1.c ├── Swap_2.c ├── Swapping(without using extra variable).cpp ├── Temperature.c ├── TemperatureSwitch.c ├── TernaryOperator.c ├── Trif.c ├── UppercaseToLowercase.c ├── VowelorConsonant.c ├── alphabetTriangle.cpp ├── binary_to_octal.c ├── camelcase ├── check functon is even or odd in c programming ├── closestpowerof2.c ├── combine_calculator.c ├── counting_sort.c ├── dynamicMemoryAllocation.c ├── endian.c ├── fastModuloExponentiation.c ├── finding the first missing natural number ├── gcd.c ├── getPIDs.c ├── heap sort.c ├── increment_number.c ├── ip_address.c ├── isInputLeapYear.c ├── kth smallest no in an array.c ├── lambda_in_c.c ├── large_factorial.c ├── last-digit-fibonacci.c ├── lcm.c ├── linearsearch.c ├── linked_list.c ├── linkedlist.cpp ├── multiply.c ├── nCrCalculatorLargeNumbers.c ├── oddandeven.c ├── omang_fibonacci.c ├── pattern.c ├── priority_queue.c ├── simple_interest.c ├── stack_using_linklist ├── swappingwithoutthirdvariable.c ├── to find average & sum of a,b,c in one function ├── transposeOfMatrix.c └── volume_cone_sphere.c /ARRAY.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Adding_Fractions.c: -------------------------------------------------------------------------------- 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 | 29 | -------------------------------------------------------------------------------- /Addition.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { int a,b,c; 4 | printf("Enter two Numbers : "); 5 | scanf("%d %d",&a,&b); 6 | c=a+b; 7 | printf("The Sum is %d",c); 8 | } 9 | -------------------------------------------------------------------------------- /Address of 1-D array: -------------------------------------------------------------------------------- 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 | 4 | // Function that performs the conversion 5 | double convertTemp(double initValue, int initScale, int finalScale){ 6 | double finalValue; 7 | switch(initScale){ 8 | // Celsius 9 | case 1: 10 | // Celsius to Kelvin 11 | if(finalScale == 1){ 12 | finalValue = initValue + 273.15; 13 | } 14 | // Celsius to Fahrenheit 15 | else if(finalScale == 2){ 16 | finalValue = (initValue * 9 / 5) + 32; 17 | } 18 | break; 19 | case 2: 20 | // Kelvin to Celsius 21 | if(finalScale == 1){ 22 | finalValue = initValue - 273.15; 23 | } 24 | // Kelvin to Fahrenheit 25 | else if(finalScale == 2){ 26 | finalValue = ((initValue - 273.15) * 9 / 5) + 32; 27 | } 28 | break; 29 | case 3: 30 | // Fahrenheit to Celsius 31 | if(finalScale == 1){ 32 | finalValue = (initValue - 32) * 5 / 9; 33 | } 34 | // Fahrenheit to Kelvin 35 | else if(finalScale == 2){ 36 | finalValue = ((initValue - 32) * 5 / 9) + 273,15; 37 | } 38 | break; 39 | 40 | } 41 | return finalValue; 42 | } 43 | 44 | int main(){ 45 | int option; 46 | double initialValue, finalValue; 47 | while(1){ 48 | // main menu 49 | printf("\n0 - Exit\n"); 50 | printf("1 - Convert from Celsius to Kelvin\n"); 51 | printf("2 - Convert from Celsius to Fahrenheit\n"); 52 | printf("3 - Convert from Kelvin to Fahrenheit\n"); 53 | printf("4 - Convert from Kelvin to Celsius\n"); 54 | printf("5 - Convert from Fahrenheit to Celsius\n"); 55 | printf("6 - Convert from Fahrenheit to Kelvin\n"); 56 | 57 | printf("Select a number: "); 58 | scanf("%d",&option); 59 | if(!option){ 60 | printf("Ending program\n"); 61 | return 0; 62 | } 63 | 64 | printf("Please enter the initial value: "); 65 | scanf("%lf",&initialValue); 66 | 67 | switch(option){ 68 | case 1: 69 | finalValue = convertTemp(initialValue,1,1); 70 | printf("Valor em Kelvin: %.2lf",finalValue); 71 | break; 72 | case 2: 73 | finalValue = convertTemp(initialValue,1,2); 74 | printf("Valor em Fahrenheit: %.2lf",finalValue); 75 | break; 76 | case 3: 77 | finalValue = convertTemp(initialValue,2,1); 78 | printf("Valor em Celsius: %.2lf",finalValue); 79 | break; 80 | case 4: 81 | finalValue = convertTemp(initialValue,2,2); 82 | printf("Valor em Fahrenheit: %.2lf",finalValue); 83 | break; 84 | case 5: 85 | finalValue = convertTemp(initialValue,3,1); 86 | printf("Valor em Celsius: %.2lf",finalValue); 87 | break; 88 | case 6: 89 | finalValue = convertTemp(initialValue,3,1); 90 | printf("Valor em Kelvin: %.2lf",finalValue); 91 | break; 92 | 93 | } 94 | printf("\n"); 95 | } 96 | 97 | return 0; 98 | } -------------------------------------------------------------------------------- /Anagram-Program-in-C: -------------------------------------------------------------------------------- 1 | #include 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 | -------------------------------------------------------------------------------- /AreaAndCircumference.c: -------------------------------------------------------------------------------- 1 | // To calculate Area and Circumference of a circle 2 | 3 | #include 4 | int main() 5 | { 6 | float r, a ,c; 7 | printf("enter radius :\n"); 8 | scanf("%f", &r); 9 | a = 3.14*r*r; 10 | c = 2*3.14*r; 11 | printf("Area = %f\n circumference = %f\n",a,c); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Area_of_Circle.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { float a,r; 4 | printf("Enter the Radius : "); 5 | scanf("%f",&r); 6 | a=3.14*r*r; 7 | printf("Area is %f",a); 8 | } 9 | -------------------------------------------------------------------------------- /Area_of_Square.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { float a,r; 4 | printf("Enter the Radius : "); 5 | scanf("%f",&s); 6 | a=s*s; 7 | printf("Area is %f",a); 8 | } 9 | -------------------------------------------------------------------------------- /Area_of_Triangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { float a,b,h; 4 | printf("Enter the base : "); 5 | scanf("%f",&b); 6 | printf("Enter the height : "); 7 | scanf("%f",&h); 8 | a=0.5*b*h; 9 | printf("Area is %f",a); 10 | } 11 | -------------------------------------------------------------------------------- /ArmstrongNumber.c: -------------------------------------------------------------------------------- 1 | //To check whether the given number is Armstrong number or not 2 | //Armstrong number: An n -digit number equal to the sum of the nth powers of its digits. 3 | //Example: (1^3) + (5^3) + (3^3)= 153 4 | 5 | #include 6 | #include 7 | 8 | void main() { 9 | int number, sum = 0, rem = 0, nthPower = 0, digits = 0, temp; 10 | printf("Enter a number"); 11 | scanf("%d", & number); 12 | temp = number; 13 | //to calculate the number of digits in the number 14 | while (number != 0) { 15 | number = number / 10; 16 | digits++; 17 | } 18 | number = temp; 19 | //to get the nth power of each digit and add it to the sum 20 | while (number != 0) { 21 | rem = number % 10; 22 | nthPower = pow(rem, digits); 23 | sum = sum + nthPower; 24 | number = number / 10; 25 | } 26 | //to check if obtained sum is equal to the original number 27 | if (sum == temp) 28 | printf("The given number is an Armstrong number"); 29 | else 30 | printf("The given number is not an Armstrong number"); 31 | } 32 | -------------------------------------------------------------------------------- /Automorphic_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main() { 5 | int num, sqr, temp, last; 6 | int n = 0; 7 | printf("Enter a number \n"); 8 | scanf("%d", & num); 9 | sqr = num * num; //calculating square of num 10 | temp = num; 11 | //Counting number of digits 12 | while (temp > 0) { 13 | n++; 14 | temp = temp / 10; 15 | } 16 | //Extracting last n digits 17 | int den = floor(pow(10, n)); 18 | last = sqr % den; 19 | if (last == num) 20 | printf("Automorphic number \n"); 21 | else 22 | printf("Not Automorphic \n"); 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /BackgroundThreadSorter.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This program is an example of using a background thread to sort a fixed size array 3 | * asynchronously. The array itself can be modified by adding new elements. The array 4 | * acts like a circular buffer when we cross the size of the array i.e the index goes like this: 5 | * 0,1,2,3,4,0,1,2,3,4,.....(For an array of size 5). 6 | * This program demonstrates usage of pthreads, mutexes, qsort(in built C sorting function). 7 | * The array used is an array of structures. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define RECT_ARRAY_SIZE 5 16 | 17 | /* 18 | * Rectangle structure has length and breadth. 19 | */ 20 | struct rect { 21 | int length; 22 | int breadth; 23 | }; 24 | 25 | /* 26 | * Array of rectangle structs to be sorted. 27 | */ 28 | struct rect r[RECT_ARRAY_SIZE]; 29 | /* 30 | * mutex lock to serialize the array of rectangle structs 31 | */ 32 | pthread_mutex_t lock; 33 | 34 | /* 35 | * The comparator to compare the 2 rectangle structures. 36 | * Comparison is based on area. 37 | */ 38 | int area_comp(const void *r1, const void *r2) 39 | { 40 | int area1, area2; 41 | 42 | struct rect r1_t = *(const struct rect *)r1; 43 | struct rect r2_t = *(const struct rect *)r2; 44 | 45 | area1 = r1_t.length * r1_t.breadth; 46 | area2 = r2_t.length * r2_t.breadth; 47 | 48 | return area1 > area2 ? 1 : 0; 49 | } 50 | 51 | /* 52 | * Function of the background pthread. 53 | * This function sorts the array of rectangle structs 54 | * every 10 seconds and prints out the output. 55 | */ 56 | void *sorter(void *p) 57 | { 58 | for(;;) { 59 | pthread_mutex_lock(&lock); 60 | qsort(r, RECT_ARRAY_SIZE, sizeof(struct rect), area_comp); 61 | printf("The rect array is\n"); 62 | for (int i = 0; i < RECT_ARRAY_SIZE; i++) { 63 | printf("%d %d\n", r[i].length, r[i].breadth); 64 | } 65 | pthread_mutex_unlock(&lock); 66 | sleep(10); 67 | printf("\n"); 68 | } 69 | } 70 | 71 | int main(void) 72 | { 73 | /* 74 | * Our background thread to sort the array 75 | * of rectangle structs 76 | */ 77 | pthread_t thread; 78 | int index = 0; 79 | 80 | /* 81 | * First initialise the rectangle struct to some values 82 | */ 83 | r[0].length = 10; 84 | r[0].breadth = 2; 85 | 86 | r[1].length = 34; 87 | r[1].breadth = 23; 88 | 89 | r[2].length = 32; 90 | r[2].breadth = 3; 91 | 92 | r[3].length = 2; 93 | r[3].breadth = 45; 94 | 95 | r[4].length = 2; 96 | r[4].breadth = 12; 97 | 98 | /* 99 | * Init the mutexes and the pthread 100 | */ 101 | pthread_mutex_init(&lock, NULL); 102 | pthread_create(&thread, NULL, &sorter, (void*)NULL); 103 | 104 | /* 105 | * The main thread allows the user to input data i.e length 106 | * and breadth of the users rectangle. The length and breadth inputted 107 | * are stored in a structure which is placed into the rectangle struct array. 108 | * The rectangle struct array is of fixed size. The size is defined by 109 | * RECT_ARRAY_SIZE. The array acts like a circular buffer. 110 | */ 111 | for (;;) { 112 | struct rect temp_rect; 113 | int length, breadth; 114 | 115 | scanf("%d %d", &length, &breadth); 116 | 117 | temp_rect.length = length; 118 | temp_rect.breadth = breadth; 119 | 120 | pthread_mutex_lock(&lock); 121 | r[(index++)%RECT_ARRAY_SIZE] = temp_rect; 122 | pthread_mutex_unlock(&lock); 123 | } 124 | 125 | return 0; 126 | } 127 | -------------------------------------------------------------------------------- /BasicArithmatic.c: -------------------------------------------------------------------------------- 1 | // A simple arithmetic operation on two integers 2 | 3 | #include 4 | 5 | int main() { 6 | int number1, number2, addition, subtraction, multiplication, division, modulo; 7 | printf("Enter two numbers :\n"); 8 | scanf("%d%d", & number1, & number2); 9 | addition = number1 + number2; 10 | subtraction = number1 - number2; 11 | multiplication = number1 * number2; 12 | division = number1 / number2; 13 | modulo = number1 % number2; 14 | printf("Addition of number 1 and number 2 : %d\n", addition); 15 | printf("Subtraction of number 1 and number 2 : %d\n", subtraction); 16 | printf("Multiplication of number 1 and number 2 : %d\n", multiplication); 17 | printf("Division of number 1 and number 2 : %d\n", division); 18 | printf("Modulo of number 1 and number 2 : %d\n", modulo); 19 | return 0; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /BasicGame.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Binary to decimal: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | long long int a; 7 | cout<<"enter the no in binary\n"; 8 | cin>>a; 9 | int b=0,j=0; 10 | int c; 11 | while(a!=0) 12 | { 13 | c=a%10; 14 | b=b+c*pow(2,j); 15 | j++; 16 | a=a/10; 17 | } 18 | cout<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 | -------------------------------------------------------------------------------- /BinarySearch.c: -------------------------------------------------------------------------------- 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 | 47 | -------------------------------------------------------------------------------- /BitwiseAndOperator.c: -------------------------------------------------------------------------------- 1 | // Bitwise and operator 2 | 3 | #include 4 | int main() 5 | { 6 | int a=14, b= 7, c; 7 | c =a&b; 8 | printf("Value of c = %d\n",c); 9 | return 0; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /BitwiseComplementOperator.c: -------------------------------------------------------------------------------- 1 | // Bitwise complement operator 2 | 3 | #include 4 | int main() 5 | { 6 | int a=14, b; 7 | b = ~a; 8 | printf("Value of c = %d\n",b); 9 | return 0; 10 | 11 | } 12 | -------------------------------------------------------------------------------- /BitwiseLeftshiftOperator.c: -------------------------------------------------------------------------------- 1 | // Bitwise left shift operator 2 | 3 | #include 4 | int main() 5 | { 6 | int a=7, b=2,c; 7 | c = a< 5 | 6 | int main(){ 7 | 8 | int num, mask = 0x1; 9 | 10 | printf("Enter a number: "); 11 | scanf("%d", &num); 12 | 13 | num &= mask; 14 | 15 | if ( 0 == num) 16 | printf("Even Number\n"); 17 | else 18 | printf("Odd Number\n"); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /BubbleSort.c: -------------------------------------------------------------------------------- 1 | // Bubble sort code 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | int array[100], n, c, d, swap; 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 = 0 ; c < n - 1; c++) 18 | { 19 | for (d = 0 ; d < n - c - 1; d++) 20 | { 21 | if (array[d] > array[d+1]) /* For decreasing order use < */ 22 | { 23 | swap = array[d]; 24 | array[d] = array[d+1]; 25 | array[d+1] = swap; 26 | } 27 | } 28 | } 29 | 30 | printf("Sorted list in ascending order:\n"); 31 | 32 | for (c = 0; c < n; c++) 33 | printf("%d\n", array[c]); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Buffer Overflow for Kids.md: -------------------------------------------------------------------------------- 1 | # Buffer Overflow in C 2 | 3 | # What is a buffer? 4 | A buffer, in terms of a program in execution, can be thought of as a region of computer’s main memory that has certain boundaries in context with the program variable that references this memory. 5 | ### Example Code : 6 | **char buff[10];** 7 | 8 | The above line will declare an array of 10 characters in C. Here buff[0] is the left boundary and buff[9] is the right boundary of the buffer. 9 | 10 | # What is Buffer Overflow? 11 | ![Example of Buffer Overflow](https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Art/stringcopy.gif) 12 | 13 | Now that we have seen the idea of a buffer, we can talk about what is meant by buffer overflow. 14 | A buffer is said to be overflown when the data (meant to be written into memory buffer) gets written past the left or the right boundary of the buffer. This way the data gets written to a portion of memory which does not belong to the program variable that references the buffer. 15 | 16 | **Note** : Though in the above case we have taken the example of a character array as a buffer, we can have buffer overflow with different types of data structures as well such as stack, linked lists, etc if we violate their boundary conditions. 17 | 18 | ### Example Code : 19 | **char buff[10];** 20 | **buff[10] = 'a';** 21 | 22 | The effect of the above code will be that most likely during runtime, your code will throw a Segmentation Fault or Stack Smashing Error. 23 | This is due to buffer overflow. 24 | 25 | **Similarly,** 26 | 27 | **char buff[10];** 28 | **buff[-1] = 'a';** 29 | 30 | This code will also throw the same kind of array index violation errors. 31 | 32 | In general accessing a memory location that has not been assigned for your program can lead to buffer overflow. 33 | Hence, de-referencing dangling pointers, filling array or string with more elements than it is defined to, etc can all lead to a buffer overflow and hence segfault. 34 | 35 | **More Examples :** 36 | 1. ***char a[10] = "This string is longer than the buffer can hold";*** 37 | 38 | 2. ***int \*a = (int \*)malloc(10 \* sizeof(int)) ;*** 39 | ***a[20] = 100 ;*** 40 | 41 | ![Example of Buffer Overflow](http://www.cs.fsu.edu/~baker/opsys/notes/graphics/bufferoverflow2.gif) 42 | ## Why does it matter to us? 43 | 44 | Buffer overflows are one of the most common and also one of the most notorious errors in programming as they can lead to overwriting of memory and also unexpected program crashes. 45 | 46 | The 'Segmentation Fault' error is the most common type of error associated with buffer overflow. 47 | 48 | There can also be cases where buffer overflow does not give errors immediately. But what happens in these cases is that the program will secretly overwrite neighboring memory allocated to other programs/same program and eventually will lead to a crash. 49 | 50 | Example of a segfault crash :
51 | 52 | ![enter image description here](https://qph.fs.quoracdn.net/main-qimg-669ba1b3d021390ed98c70ed29e71d62.webp) 53 | 54 | **Special Note for C Programmers :** 55 | C Programmers need to be extra careful when writing code to take care of buffer overflow because many C functions do have the feature of boundary checking and hence the slightest mistake can lead to a segfault easily. 56 | Also since it allows pointer and low level memory manipulation, it is the programmers duty to check for buffer overflow. 57 | 58 | **Thank You !** 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /CalcUsingSwitchCase.c: -------------------------------------------------------------------------------- 1 | // Very tiny and stupid Calculator using switch case 2 | //Answer will be 0 in case 4 if quotients value is in points. 3 | //For instance, 5/3 will be shown as 0. This is because we have declared c as integer and not float. 4 | 5 | #include 6 | int main() { 7 | int a, b, c; 8 | char ch; 9 | printf("enter two number\n"); 10 | scanf("%d%d", & a, & b); 11 | fflush(stdin); 12 | //flush the input buffer. You might use this function if you think that there might be 13 | //some data in input buffer which can create problems for you while taking user inputs from stdin. 14 | printf("enter your choice\n"); 15 | printf("1. enter 1 for addition\n"); 16 | printf("2. enter 2 for subtraction\n"); 17 | printf("3. enter 3 for multiplication\n"); 18 | printf("4. Enter 4 for division\n"); 19 | printf("5. enter 5 for modulo division\n"); 20 | scanf("%c", & ch); 21 | switch (ch) { 22 | case '1': 23 | c = a + b; 24 | break; 25 | case '2': 26 | c = a - b; 27 | break; 28 | case '3': 29 | c = a * b; 30 | break; 31 | case '4': 32 | c = a / b; 33 | break; 34 | case '5': 35 | c = a % b; //Modulus division only works with integers. 36 | break; 37 | default: 38 | printf("wrong choice"); 39 | } 40 | printf("calculated value=%d", c); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /CalculateSimpleInterest.c: -------------------------------------------------------------------------------- 1 | #include 2 | main() 3 | { 4 | int principleAmount; 5 | int numberOfYears; 6 | int rateOfInterest; 7 | int SimpleInterest; 8 | printf("enter principleAmount\n",principleAmount); 9 | scanf("%d",&principleAmount); 10 | printf("enter number of year\n",numberOfYears); 11 | scanf("%d",&numberOfYears); 12 | printf("enter rate of interest\n",rateOfInterest); 13 | scanf("%d",&rateOfInterest); 14 | 15 | SimpleInterest=(principleAmount*numberOfYears*rateOfInterest)/100; 16 | printf("Simple interest is %d",SimpleInterest); 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /CelciusToKelvinConv.c: -------------------------------------------------------------------------------- 1 | // Celsius to Kelvin converter 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | double c, k; 8 | printf("Enter the desired temperature in Celcius:\n"); 9 | scanf("%c", &c); 10 | k = c + 273.15; 11 | printf("Converted value: %f K\n", k); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Changingbase.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include //used because of predefined power function 3 | 4 | int main() 5 | { 6 | long long int n,x,y,b,j=-1,c,d,e,a=0,k=-1,f=0; 7 | 8 | printf("This program converts the no. from one base to another\n"); 9 | 10 | printf("enter the no. you want to convert\n"); 11 | scanf("%lld",&n); 12 | 13 | printf("enter the base of no.\n"); 14 | scanf("%lld",&x); 15 | 16 | printf("enter the base to which you want to convert\n"); 17 | scanf("%lld",&y); 18 | 19 | while(n>0) 20 | { 21 | b=n%10; 22 | n=n/10; 23 | j++; 24 | d=b*pow(x,j); 25 | a=d+a;//converted the no. to base 10 26 | } 27 | 28 | while(a>0) 29 | { 30 | c=a%y; 31 | a=a/y; 32 | k++; 33 | e=c*pow(10,k); 34 | f=e+f;//converted the no. to base user wanted 35 | } 36 | 37 | printf("%lld\n",f); 38 | return 0; 39 | } -------------------------------------------------------------------------------- /CheckCharacterType.c: -------------------------------------------------------------------------------- 1 | // Program to check type of input character 2 | 3 | 4 | #include 5 | int main() { 6 | char ch; 7 | printf("Enter a character\n"); 8 | scanf("%c", & ch); 9 | if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122)) 10 | printf("Entered character is an alphabet"); 11 | else if (ch >= 48 && ch <= 57) 12 | printf("Entered character is a digit"); 13 | else 14 | printf("Entered character is a special symbol"); 15 | return 0; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /CommonElementsInTwoArrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int i,j,n; 5 | printf("enter the number of elements in an arrays\n"); 6 | scanf("%d",&n); 7 | int a[n],b[n]; 8 | printf("enter the elements of 1st array\n"); 9 | for(i=0;i 2 | int main() 3 | { 4 | int num;scanf("%d",&num); 5 | while(num--) 6 | { 7 | int n;scanf("%d",&n); 8 | int a[n],t[n],tmp,k=0; 9 | for(int i=0;it[j+1]) 18 | { 19 | tmp=t[j]; 20 | t[j]=t[j+1]; 21 | t[j+1]=tmp; 22 | } 23 | } 24 | for(int i=0;i 15 | int main(){ 16 | int hour, amount; 17 | printf("Enter number of duty hours\n"); 18 | scanf("%d", &hour); 19 | if (hour >= 1 && hour <= 8) 20 | amount = 100; 21 | else if (hour >= 9 && hour <= 12) 22 | amount = 100 + (hour - 8)*20; 23 | else if (hour >= 13 && hour <= 16) 24 | amount = 180 + (hour - 12)*40; 25 | else if (hour >= 17 && hour <= 20) 26 | amount = 340 + (hour - 16)*60; 27 | else if (hour >= 21 && hour <= 24) 28 | amount = 580 + (hour - 20)*80; 29 | printf("Amount incurred by worker : %d", amount); 30 | return 0; 31 | 32 | } 33 | -------------------------------------------------------------------------------- /DayNameUsingSwitchCase.c: -------------------------------------------------------------------------------- 1 | // Program to display the day name using switch case 2 | #include 3 | int main(){ 4 | int day; 5 | printf("Enter the day number\n"); 6 | scanf("%d", &day); 7 | 8 | switch(day) 9 | { 10 | case 1 : 11 | printf("Day is Monday"); 12 | break; 13 | case 2 : 14 | printf("Day is Tuesday"); 15 | break; 16 | case 3 : 17 | printf("Day is Wednesday"); 18 | break; 19 | case 4 : 20 | printf("Day is Thrusday"); 21 | break; 22 | case 5 : 23 | printf("Day is Friday"); 24 | break; 25 | case 6 : 26 | printf("Day is Saturday"); 27 | break; 28 | case 7 : 29 | printf("Day is Sunday"); 30 | break; 31 | default: 32 | printf("Wrong choice"); 33 | } 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Decimal to Binary: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main() 5 | { 6 | int a,s; 7 | cout<<"enter a decimal no"; 8 | int b; 9 | cin>>b; 10 | int c,d,e[100],count=0,i=0; 11 | while(b!=1) 12 | { 13 | c=b%2; 14 | b=b/2; 15 | e[i]=c; 16 | count++; 17 | i++; 18 | } 19 | e[i]=1; 20 | 21 | int j; 22 | for(j=count;j>=0;j--) 23 | { 24 | cout< 4 | 5 | void printDigit(int, int); 6 | 7 | int main(){ 8 | 9 | int input, base; 10 | 11 | // Getting user input 12 | printf("Enter number: "); 13 | scanf("%d", &input); 14 | printf("Enter base: "); 15 | scanf("%d", &base); 16 | 17 | printf("%d to base %d : ", input, base); 18 | 19 | printDigit(input, base); 20 | 21 | return 0; 22 | } 23 | 24 | // This is a reccursive function that prints digit by digit 25 | void printDigit(int num, int base){ 26 | if(num > 0){ 27 | // getting current digit to be printed 28 | int digit = num % base; 29 | num = num - digit; 30 | num = num / base; 31 | // Call to function to print next digit 32 | printDigit(num, base); 33 | // If the current digit is more than 9 (eg: 10=A, 11=B) 34 | if(digit > 9){ 35 | printf("%c", 55 + digit); 36 | } 37 | // Else print the digit directly 38 | else { 39 | printf("%d", digit); 40 | } 41 | } else { 42 | return; 43 | } 44 | } -------------------------------------------------------------------------------- /DecimalToBinary.c: -------------------------------------------------------------------------------- 1 | // Convert base10(decimal) values to base2(binary) 2 | 3 | #include 4 | 5 | void PrintBits(int power, int num){ 6 | // firstOne is a flag that stops "0" being printed before a "1" is printed 7 | int firstOne = 0; 8 | 9 | // Conversion loop 10 | while (power > 0) { 11 | if (num >= power) { 12 | firstOne = 1; 13 | printf("1"); 14 | num %= power; 15 | } else if (firstOne == 1) { 16 | printf("0"); 17 | } 18 | power /= 2; 19 | } 20 | printf("\n"); 21 | } 22 | 23 | int main() { 24 | 25 | int num = 0; 26 | int power = 1; 27 | 28 | // Getting input 29 | printf("Please enter a numeric value: "); 30 | scanf("%d", & num); 31 | printf("%d represented in binary is: ", num); 32 | 33 | // Determine highest order of magnitude needed for the base2 conversion 34 | while (power < num) { 35 | power *= 2; 36 | } 37 | 38 | // Edge case in event of 0 being entered 39 | if (num == 0) { 40 | printf("0\n"); 41 | } else { 42 | PrintBits(power, num); 43 | } 44 | return (0); 45 | } 46 | -------------------------------------------------------------------------------- /DecimalToHexadecimalViceVersa.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | void decimal_hex(int n, char hex[]); 5 | int hex_decimal(char hex[]); 6 | int main() 7 | { 8 | char hex[20]; 9 | int n,c; 10 | printf("**----Program to Convert Decimal and Hexadeciaml Vice Versa----***\n\n"); 11 | printf("Choose Your Choice: \n"); 12 | printf("1.Decimal to Hexadecimal:\n"); 13 | printf("2.Hexadecimal to Decimal:\n\n"); 14 | scanf("%d",&c); 15 | if (c==1) 16 | { 17 | printf("Enter decimal number: "); 18 | scanf("%d",&n); 19 | decimal_hex(n,hex); 20 | printf("Hexadecimal number: %s",hex); 21 | } 22 | if (c==2) 23 | { 24 | printf("Enter hexadecimal number: "); 25 | scanf("%s",hex); 26 | printf("Decimal number: %d",hex_decimal(hex)); 27 | } 28 | return 0; 29 | } 30 | 31 | void decimal_hex(int n, char hex[]) 32 | { 33 | int i=0,rem; 34 | while (n!=0) 35 | { 36 | rem=n%16; 37 | switch(rem) 38 | { 39 | case 10: 40 | hex[i]='A'; 41 | break; 42 | case 11: 43 | hex[i]='B'; 44 | break; 45 | case 12: 46 | hex[i]='C'; 47 | break; 48 | case 13: 49 | hex[i]='D'; 50 | break; 51 | case 14: 52 | hex[i]='E'; 53 | break; 54 | case 15: 55 | hex[i]='F'; 56 | break; 57 | default: 58 | hex[i]=rem+'0'; 59 | break; 60 | } 61 | ++i; 62 | n/=16; 63 | } 64 | hex[i]='\0'; 65 | strrev(hex); 66 | } 67 | 68 | int hex_decimal(char hex[]) 69 | { 70 | int i, length, sum=0; 71 | for(length=0; hex[length]!='\0'; ++length); 72 | for(i=0; hex[i]!='\0'; ++i, --length) 73 | { 74 | if(hex[i]>='0' && hex[i]<='9') 75 | sum+=(hex[i]-'0')*pow(16,length-1); 76 | if(hex[i]>='A' && hex[i]<='F') 77 | sum+=(hex[i]-55)*pow(16,length-1); 78 | if(hex[i]>='a' && hex[i]<='f') 79 | sum+=(hex[i]-87)*pow(16,length-1); 80 | } 81 | return sum; 82 | } 83 | 84 | -------------------------------------------------------------------------------- /DecrementOperator.c: -------------------------------------------------------------------------------- 1 | // Decrement operator 2 | 3 | #include 4 | int main() 5 | { 6 | int a= 5, b, c; 7 | b = a--; 8 | c = a--; 9 | 10 | printf("Value of b = %d\n Value of c = %d\n", b,c); 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Diagonal-Difference.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /DiceRoll.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /DigitalRoot.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /DisplayLinuxEnvirmentVariables.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char **argv, char **environ){ 4 | int i = -1; 5 | 6 | while (environ[++i]) 7 | printf("%s\n",environ[i]); 8 | } 9 | -------------------------------------------------------------------------------- /Division.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { int a,b,c; 4 | printf("Enter two Numbers : "); 5 | scanf("%d %d",&a,&b); 6 | c=a/b; 7 | printf("Division is %d",c); 8 | } 9 | -------------------------------------------------------------------------------- /DynamicMemoryAllocation: -------------------------------------------------------------------------------- 1 | #include 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 | -------------------------------------------------------------------------------- /DynamicTwoDArrayUsingArrayOfPointer.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /DynamicTwoDArrayUsingOnePointer.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /EmployeeGrade.c: -------------------------------------------------------------------------------- 1 | // Program to find employee grade 2 | // Given TA : 5% , DA : 7.5% , HRA : 10% 3 | 4 | #include 5 | int main(){ 6 | float BaseSalary, TA, DA, HRA, GrossSalary; 7 | 8 | printf("Enter basic salary of employee\n"); 9 | scanf("%f", &BaseSalary); 10 | 11 | TA = 0.05 * BaseSalary; 12 | DA = 0.075 * BaseSalary; 13 | HRA = 0.1 * BaseSalary; 14 | GrossSalary = BaseSalary + TA + DA + HRA; 15 | 16 | if (GrossSalary >= 100000) 17 | printf("A grade employee"); 18 | else if (GrossSalary >= 75000 && GrossSalary < 100000) 19 | printf("B grade employee"); 20 | else if (GrossSalary >=50000 && GrossSalary < 75000) 21 | printf("C grade employee"); 22 | else if (GrossSalary >= 20000 && GrossSalary < 50000) 23 | printf("D grade employee"); 24 | else 25 | printf("E grade employee"); 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /EncryptDecryptXOR.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | char XORkey[12] = {'F','P','k','k','Y','P','l','p','V','P','L','z'}; 5 | void encryptDecrypt(); 6 | 7 | int main() { 8 | char sampleString[] = " This contains highly sensitive message\n" \ 9 | " coordinates : 23.445, 34.443\n" \ 10 | " All further messages MUST be send via\n" \ 11 | " XOR encryption only - Long Live Revolution!!\n" ; 12 | 13 | 14 | printf("\nEncrypted String :\n"); 15 | encryptDecrypt(sampleString); 16 | 17 | printf("\nDecyrpted String :\n"); 18 | encryptDecrypt(sampleString); 19 | 20 | return 0; 21 | } 22 | 23 | void encryptDecrypt(char inputString[]) { 24 | int i = 0; 25 | 26 | int len = strlen(inputString); 27 | for (i = 0; i < len; i++) { 28 | inputString[i] = inputString[i] ^ XORkey[i % (sizeof(XORkey)/sizeof(char))]; 29 | printf("%c", inputString[i]); 30 | } 31 | 32 | } 33 | 34 | -------------------------------------------------------------------------------- /Even Fibonacci 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 | -------------------------------------------------------------------------------- /FIND_EVEN_ AND_DIVISIBLE_BY_3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define MAX 5 3 | 4 | int main() 5 | { 6 | int i,j = 0,num[MAX]; 7 | printf("Enter numbers in the range of 1 to 9 \n"); 8 | for(i = 0; i 2 | int main() 3 | { 4 | int n, i; 5 | unsigned long long factorial = 1; 6 | //unsigned long long is the same as unsigned long long int. 7 | //Its size is platform-dependent, but guaranteed by the C standard (ISO C99) to be at least 64 bits. 8 | 9 | printf("Enter a number: "); 10 | scanf("%d",&n); 11 | 12 | // Show error if number is less than 0 13 | if (n < 0) 14 | printf("Error! Factorial of a negative number doesn't exist."); 15 | 16 | else 17 | { 18 | for(i=1; i<=n; ++i) 19 | { 20 | factorial *= i; // factorial = factorial*i; 21 | } 22 | printf("Factorial of %d = %llu", n, factorial); 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /FahrenheitToCelciusConv.c: -------------------------------------------------------------------------------- 1 | // Fahrenheit to celcius temp converter 2 | 3 | #include 4 | int main() 5 | { 6 | float c, f; 7 | printf("Enter temp in fahrenheit :\n"); 8 | scanf("%f", &f); 9 | c = (f-32)*5/9; 10 | printf("Temp in celcius is : %f",c); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /FibonacciGeneration.c: -------------------------------------------------------------------------------- 1 | // Fibonacci Series using Recursion 2 | #include 3 | 4 | // fib function with argument n to generate nth Fibanocci Number 5 | int fib(int n) 6 | { 7 | // Base case defined 8 | if (n <= 1){ 9 | return n; 10 | } 11 | 12 | // Recursive Calls to fib function 13 | return fib(n - 1) + fib(n - 2); 14 | } 15 | 16 | int main() 17 | { 18 | // Sample input 19 | int n = 11; 20 | 21 | // Printing the nth Fibanocci Number 22 | printf("%d", fib(n)); 23 | } -------------------------------------------------------------------------------- /FindAsciiValue.c: -------------------------------------------------------------------------------- 1 | // Program to find ASCII value 2 | #include 3 | int main() 4 | { 5 | char c; 6 | printf("Enter a character: "); 7 | 8 | // Reads character input from the user 9 | scanf("%c", &c); 10 | 11 | // %d displays the integer value of a character 12 | // %c displays the actual character 13 | printf("ASCII value of %c = %d", c, c); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /FindRemainder.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main () 3 | { 4 | int t, A, B; 5 | int rem = 0; 6 | scanf ("%d", &t); 7 | 8 | while (t--) 9 | { 10 | 11 | scanf ("%d%d",&A,&B); 12 | rem = A % B; 13 | printf("%d\n", rem); 14 | } 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /GotoStatementEvenOrOdd.c: -------------------------------------------------------------------------------- 1 | // Program to input an integer number and check whether it is 2 | //even or odd and positive or negative using goto statements 3 | 4 | #include 5 | int main() { 6 | 7 | int n; 8 | 9 | printf("enter a number\n"); 10 | scanf("%d", & n); 11 | if (n % 2 == 0 && n > 0) 12 | goto a; 13 | else if (n % 2 != 0 && n < 0) 14 | goto b; 15 | else if (n % 2 == 0 && n < 0) 16 | goto c; 17 | else if (n % 2 != 0 && n > 0) 18 | goto d; 19 | else 20 | goto e; 21 | a: 22 | printf("number is even and positive"); 23 | goto stop; 24 | b: 25 | printf("number is odd and negative"); 26 | goto stop; 27 | c: 28 | printf("number is even and negative"); 29 | goto stop; 30 | d: 31 | printf("number is odd and positive"); 32 | goto stop; 33 | e: 34 | printf("special number"); 35 | goto stop; 36 | stop: 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Gross_Salary.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { float agp,b,da,gs; 4 | printf("Enter the Basic Salary : \n"); 5 | scanf("%f",&b); 6 | printf("Enter the AGP : \n"); 7 | scanf("%f",&agp); 8 | printf("Enter the DA in Percentage : \n"); 9 | scanf("%f",&da); 10 | gs=(b+agp)*(1+(da/100)); 11 | printf("Gross Salary is %f",gs); 12 | } 13 | -------------------------------------------------------------------------------- /HaystackAndNeedle_SubString.c: -------------------------------------------------------------------------------- 1 | // Function that takes a string "haystack" and a string "needle" 2 | // and checks if needle appears in haystack 3 | 4 | #include 5 | #include 6 | 7 | int SubString(char *haystack, char *needle ){ 8 | int bigIndex = 0; 9 | int littleIndex = 0; 10 | 11 | // Iterate through Haystack until a match is found 12 | // then iterate through both strings until the end of needle 13 | // or a difference is found 14 | while (haystack[bigIndex] != '\0'){ 15 | while (haystack[bigIndex + littleIndex] == needle[littleIndex]){ 16 | if (needle[littleIndex + 1] == '\0') 17 | return (1); 18 | littleIndex++; 19 | } 20 | littleIndex = 0;\ 21 | bigIndex++; 22 | } 23 | return (0); 24 | } 25 | 26 | int main(){ 27 | char needle[100]; 28 | char haystack[100]; 29 | 30 | // Getting input 31 | printf("Please enter your Haystack string (< 100 characters): "); 32 | scanf("%s", haystack); 33 | printf("Please enter your Needle string (< 100 characters): "); 34 | scanf("%s", needle); 35 | 36 | if (SubString(haystack, needle)) 37 | printf("Needle was found in Haystack!\n"); 38 | else 39 | printf("Needle not found in Haystack\n"); 40 | return (0); 41 | } 42 | -------------------------------------------------------------------------------- /HelloWorld.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | 5 | printf("Hello, World!"); 6 | return 0; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /IncrementOperator.c: -------------------------------------------------------------------------------- 1 | // Increment operator 2 | 3 | #include 4 | int main() 5 | { 6 | int a= 5, b, c; 7 | b = a++ + ++a; //Right to left 8 | c = ++a + a++; //Right to left 9 | 10 | printf("Value of b = %d\n Value of c = %d\n", b,c); 11 | return 0; 12 | } 13 | 14 | -------------------------------------------------------------------------------- /Insertionsort.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 gourav thakur 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Largest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | int size, i, max; 7 | int *array; 8 | printf("Enter number of numbers: "); 9 | scanf("%d", &size); 10 | //creating an array of entered size 11 | array = malloc(size * sizeof(int)); 12 | printf("Enter %d numbers: \n", size); 13 | //accepting each number and adding them in the array 14 | for(i=0; i max) 23 | { 24 | max = array[i]; 25 | } 26 | } 27 | //printing the integer max 28 | printf("The largest number is %d\n", max); 29 | } 30 | -------------------------------------------------------------------------------- /LeapYearTernaryOperator.c: -------------------------------------------------------------------------------- 1 | // Program to check for leap year using ternary operator 2 | #include 3 | int main() 4 | { 5 | int y; 6 | printf("enter year for checking leap year or not\n"); 7 | scanf("%d",&y); 8 | //Using ternary operator 9 | (((y%100!=0)&&(y%4==0))||(y%400==0))?printf("%d is leap year",y):printf("%d is not a leap year",y); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /LenghtOfString(without using strlen).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | slength(char name[30]); 4 | void main() 5 | { 6 | int len; 7 | char name[30]; 8 | printf("Enter any string\n"); 9 | gets(name); 10 | len=slength(name); 11 | printf("lenght of '%s' is %d\n",name,len); 12 | getch(); 13 | } 14 | slength(char name[30]) 15 | { 16 | int i=0,count=0; 17 | while(name[i]!='\0') 18 | { 19 | i++; 20 | count++; 21 | } 22 | return count; 23 | } 24 | -------------------------------------------------------------------------------- /Lexicographic_Sorting.c: -------------------------------------------------------------------------------- 1 | // Lexicographic sorting is the way of sorting words based on the alphabetical order of their component letters. 2 | 3 | #include 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 | -------------------------------------------------------------------------------- /LinearCongruentialGenerator.c: -------------------------------------------------------------------------------- 1 | // A basic implementation of a linear congruential pseudorandom number generator in C 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | struct lcg_rand { 8 | unsigned int modulus; 9 | unsigned int multiplier; 10 | unsigned int increment; 11 | unsigned int seed; 12 | unsigned int last; 13 | }; 14 | 15 | struct lcg_rand new_generator(unsigned int modulus, unsigned int multiplier, unsigned int increment) { 16 | struct lcg_rand random; 17 | random.modulus = modulus; 18 | random.multiplier = multiplier; 19 | random.increment = increment; 20 | random.seed = time(NULL); 21 | random.last = random.seed; 22 | return random; 23 | } 24 | 25 | int random(struct lcg_rand * random) { 26 | int number = ((random->multiplier * random->last) + random->increment) % random->modulus; 27 | random->last = number; 28 | return number; 29 | } 30 | 31 | int main() { 32 | struct lcg_rand rng = new_generator(pow(2, 31), 1103515245, 12345); 33 | for (int i = 0; i < 10; i++) { 34 | printf("%d\n", random( & rng)); 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Linked List 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 | -------------------------------------------------------------------------------- /LinkedLists.c: -------------------------------------------------------------------------------- 1 | //Normal LL with all fns 2 | //kinda incomplete - work in progress 3 | //TO ADD - fn to reverse the LL 4 | 5 | 6 | #include 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 | -------------------------------------------------------------------------------- /Linked_List.c: -------------------------------------------------------------------------------- 1 | /*Write a C program that uses functions to perform the following: 2 | a) Create a singly linked list of integers 3 | b) Delete a given integer from the above linked list 4 | c) Display the contents of the above list after deletion 5 | CODE:-*/ 6 | #include 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 2 | #include 3 | 4 | int main(int argc, const char * argv[], value) 5 | { 6 | /* Define temporary variables */ 7 | double value; 8 | double result; 9 | 10 | /* Calculate the log of the value */ 11 | result = log(value); 12 | 13 | /* Display the result of the calculation */ 14 | printf("The Natural Logarithm of %f is %f\n", value, result); 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /LowercaseToUppercase.c: -------------------------------------------------------------------------------- 1 | // Lowercase character to Uppercase conversion 2 | 3 | #include 4 | int main () 5 | { 6 | char ch; 7 | int no; 8 | printf("Enter a lowercase character :\n"); 9 | scanf("%c", &ch); 10 | no = ch-32; 11 | printf("Letter in capital : %c", no); 12 | return 0; 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /MagicNumbers.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Mergesort.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /MirrorNumber.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /MostFrequentWordInString.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | cout << "Value of inputs in your array?" << endl; 9 | int arraysize; 10 | cin >> arraysize; 11 | 12 | cout << "Input array elements:" << endl; 13 | 14 | string array[arraysize]; 15 | 16 | for(int i=0; i> array[i]; 19 | } 20 | 21 | int most_occur = 1; 22 | 23 | for(int i=0; imost_occur) 34 | { 35 | most_occur = times_occur; 36 | } 37 | 38 | } 39 | 40 | if(most_occur>1) 41 | { 42 | cout << "Most occuring: "; 43 | for(int i=0; i 2 | 3 | int naiveModInv(int x, int y) 4 | { 5 | // If there is an immediate value reduction 6 | x %= y; 7 | 8 | for(int i = 1; i < y; i++) 9 | if((x * i) % y == 1) 10 | return i; 11 | } 12 | 13 | int main() 14 | { 15 | printf("Enter a and m: "); 16 | int a, m; 17 | scanf("%d%d", &a, &m); 18 | 19 | int ans = naiveModInv(a, m); 20 | printf("%d\n", ans); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /NestedIfLeapYear.c: -------------------------------------------------------------------------------- 1 | // Leap year check using nested if statements 2 | 3 | #include 4 | #include 5 | void main() 6 | { 7 | int y; 8 | printf("Enter a year:"); 9 | scanf("%d",&y); 10 | if(y%4==0) 11 | { 12 | if(y%100==0) //In case of century 13 | { 14 | if(y%400==0) 15 | { 16 | printf("\n%d is a leap year",y); 17 | exit(0); 18 | } 19 | else 20 | { 21 | printf("\n%d is not a leap year",y); 22 | exit(0); 23 | } 24 | } 25 | else 26 | printf("\n%d is a leap year",y); 27 | } 28 | else 29 | printf("\n%d is not a leap year",y); 30 | 31 | } 32 | -------------------------------------------------------------------------------- /NestedifGreatestInteger.c: -------------------------------------------------------------------------------- 1 | // Greatest number using nested if 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | int a,b,c; 8 | printf("enter three integer number\n"); 9 | scanf("%d%d%d",&a,&b,&c); 10 | if(a>b) 11 | if(a>c) 12 | printf("a is greatest"); 13 | else 14 | printf("c is greatest"); 15 | else 16 | if(b>c) 17 | printf("b is greatest"); 18 | else 19 | printf("c is greatest"); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /NumberPattern.c: -------------------------------------------------------------------------------- 1 | /*Prints the pattern 2 | 1 3 | 2 3 4 | 4 5 6 5 | 7 8 9 10 6 | . 7 | */ 8 | #include 9 | int main() 10 | { 11 | int i, j, rows ,num=1; 12 | 13 | printf("Enter number of rows: "); 14 | scanf("%d",&rows); 15 | 16 | for(i=1; i<=rows; i++) 17 | { 18 | for(j=1; j<=i; j++) 19 | { 20 | printf("%3d ",num); 21 | num++; 22 | } 23 | printf("\n"); 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Number_guessing_game: -------------------------------------------------------------------------------- 1 | // Number guessing Game 2 | 3 | #include 4 | int main() 5 | { 6 | int secretNumber = 6; 7 | int guess; 8 | int guessCount = 0; 9 | int guessLimit = 5; 10 | int outOfGuesses = 0; 11 | 12 | while(guess != secretNumber && outOfGuesses == 0) 13 | { 14 | if(guessCount < guessLimit) 15 | { 16 | printf("Enter a number :"); 17 | scanf("%d",&guess); 18 | guessCount++; 19 | } 20 | else 21 | { 22 | outOfGuesses = 1; 23 | } 24 | } 25 | if(outOfGuesses == 1) 26 | { 27 | printf("\n Out Of Guesses "); 28 | } 29 | else 30 | { 31 | printf("\n You won!"); 32 | } 33 | return 0; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /Number_to_Character.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | //This program convert numbers into their corresponding characters 5 | 6 | void convertNumbertoChar(long int n); 7 | 8 | int main(){ 9 | long int n; 10 | printf("Please Enter the Number = "); 11 | scanf("%ld",&n); 12 | convertNumbertoChar(n); /* It's important to break your code in bloks, 13 | So let's make a function that solve our problem*/ 14 | } 15 | 16 | void convertNumbertoChar(long int n){ 17 | long int r,sum = 0; 18 | while(n > 0){ // This loop change the position of number so we can print in the righ order 19 | r = (n % 10); // Decompose the number 20 | sum = (sum * 10) + r; // To compose the number 21 | n = n / 10; // n decrement 22 | } 23 | n = sum; 24 | while(n > 0){ // Now we can print the characters 25 | r = n % 10; // Selecting one number to print 26 | switch(r){ // Print the corresponding character for each case 27 | case 1: 28 | printf("one "); 29 | break; 30 | case 2: 31 | printf("two "); 32 | break; 33 | case 3: 34 | printf("three "); 35 | break; 36 | case 4: 37 | printf("four "); 38 | break; 39 | case 5: 40 | printf("five "); 41 | break; 42 | case 6: 43 | printf("six "); 44 | break; 45 | case 7: 46 | printf("seven "); 47 | break; 48 | case 8: 49 | printf("eight "); 50 | break; 51 | case 9: 52 | printf("nine "); 53 | break; 54 | case 0: 55 | printf("zero "); 56 | break; 57 | default: 58 | printf("undefined"); 59 | break; 60 | } 61 | n = n / 10; //Decrementing n, ensuring the condition to the loop break 62 | } 63 | printf("\n"); 64 | } -------------------------------------------------------------------------------- /Palindrome.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | long long int max=1e6 +5; 4 | int main() 5 | { 6 | //Defining a string to take input of the number 7 | char number[max]; 8 | 9 | long long int len,i,count=0; 10 | 11 | //Taking the inputin form of a string 12 | printf("Enter a Number: "); 13 | scanf("%s",number); 14 | 15 | //Finding the number of digits by calculating the lenght of the string 16 | len=strlen(number); 17 | 18 | //Checking if the number is a palindrome or not 19 | for(i=0;i 2 | int main() 3 | { 4 | 5 | 6 | /* A no. is said to be palindrome if the number when reversed equals to the same no. eg: 121* 7 | 8 | int n, reversedInteger = 0, remainder, originalInteger; 9 | printf("Enter an integer: "); 10 | scanf("%d", &n); 11 | originalInteger = n; 12 | 13 | // reversed integer is stored in variable 14 | 15 | while( n!=0 ) 16 | { 17 | remainder = n%10; 18 | reversedInteger = reversedInteger*10 + remainder; 19 | n /= 10; 20 | } 21 | 22 | // palindrome if orignalInteger and reversedInteger are equal 23 | 24 | if (originalInteger == reversedInteger) 25 | printf("%d is a palindrome.", originalInteger); 26 | else 27 | printf("%d is not a palindrome.", originalInteger); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /PascalTriangle.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | long long int r,k,j,i,coeff; 5 | // input nuber of rows 6 | scanf("%lld",&r); 7 | 8 | 9 | for(i=0;i 4 | void main() 5 | { 6 | int i,j,k; 7 | int t=0, temp=1; 8 | //Problem:1------------------------------------1 9 | printf("Problem 1\n"); 10 | for (i=0; i<5; i++) 11 | { 12 | for (j=0; j<5; j++) 13 | { 14 | printf("*"); 15 | } 16 | printf("\n"); 17 | } 18 | printf("\n\n"); 19 | //Problem:2-------------------------------------2 20 | printf("Problem 2\n"); 21 | for (i=1; i<=5; i++) 22 | { 23 | for (j=5; j>=i; j--) 24 | { 25 | printf(" "); 26 | } 27 | for (k=1; k<=i; k++) 28 | { 29 | printf("*"); 30 | } 31 | printf("\n"); 32 | } 33 | printf("\n\n"); 34 | //Problem:3--------------------------------------3 35 | printf("Problem 3\n"); 36 | for (i=0; i<5; i++) 37 | { 38 | for (j=0; j<=i; j++) 39 | { 40 | printf("*"); 41 | } 42 | printf("\n"); 43 | } 44 | printf("\n\n"); 45 | //Problem:4-------------------------------------4 46 | printf("Problem 4\n"); 47 | for (i=5; i>=1; i--) 48 | { 49 | for (k=temp; k>=0; k--) 50 | { 51 | printf(" "); 52 | } 53 | for (j=i; j>=1; j--) 54 | { 55 | printf("*"); 56 | } 57 | temp = temp + 1; 58 | printf("\n"); 59 | } 60 | printf("\n\n"); 61 | //Problem:5--------------------------------------5 62 | printf("Problem 5\n"); 63 | for (i=5; i>=1; i--) 64 | { 65 | for (j=1; j<=i; j++) 66 | { 67 | printf("*"); 68 | } 69 | printf("\n"); 70 | } 71 | printf("\n\n"); 72 | //Problem:6--------------------------------------6 73 | printf("Problem 6\n"); 74 | for (i=1; i<=5; i++) 75 | {printf(" "); 76 | for (k=t; k<5; k++) 77 | { 78 | printf(" "); 79 | } 80 | for (j=0; j< i; j++) 81 | { 82 | printf(" * "); 83 | t = t + 1; 84 | } 85 | printf("\n"); 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /Pattern1.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | 5 | /** Program to print the following pattern: 6 | 7 | * 8 | ** 9 | *** 10 | **** 11 | ***** 12 | **/ 13 | int i,j,k;//Declaration of variable 14 | for(i=1;i<=5;i++) 15 | { 16 | for(j=1;j<=i;j++) 17 | { 18 | printf("*"); 19 | } 20 | printf("\n"); 21 | } 22 | 23 | /** Program to print the following pattern: 24 | 25 | 1 26 | 123 27 | 12345 28 | 1234567 29 | 123456789 30 | 1234567 31 | 12345 32 | 123 33 | 1 34 | 35 | **/ 36 | for (i=1;i<=5;i++) 37 | { 38 | for (j=i;j<5;j++) 39 | { 40 | printf(" "); 41 | } 42 | for (k=1;k<(i*2);k++) 43 | { 44 | printf("%d",k); 45 | } 46 | printf("\n"); 47 | } 48 | 49 | for (i=4;i>=1;i--) 50 | { 51 | for (j=5;j>i;j--) 52 | { 53 | printf(" "); 54 | } 55 | for (k=1;k<(i*2);k++) 56 | { 57 | printf("%d",k); 58 | } 59 | printf("\n"); 60 | } 61 | 62 | 63 | 64 | return 0; 65 | } 66 | 67 | -------------------------------------------------------------------------------- /PerfectNumber.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main () 4 | { 5 | int n,i,sum=0; 6 | printf("enter n\n"); 7 | scanf("%d",&n); 8 | for (i=1;i 9 | 10 | int main(void) { 11 | int * pc; /* the '*' is used to make pointer */ 12 | int c; 13 | 14 | c = 22; 15 | printf("Address of c: %p\n", (void * ) & c); 16 | printf("Value of c: %d\n\n", c); 17 | 18 | pc = & c; 19 | printf("Address of c: %p\n", (void * ) pc); 20 | printf("Content of c: %d\n\n", * pc); 21 | 22 | c = 11; 23 | printf("Address of c: %p\n", (void * ) pc); 24 | printf("Content of c: %d\n\n", * pc); 25 | 26 | * pc = 2; 27 | printf("Address of c: %p\n", (void * ) & c); 28 | printf("Value of c: %d\n\n", c); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Polynomial_linklist.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | struct Node { //node structure for polynomial 5 | int coeff; 6 | int exp; //exponent 7 | struct Node *next; //pointing to next node 8 | } *poly = NULL; //type pointer polynomial 9 | void create(void) 10 | { //creating polyno mial 11 | struct Node *t, *last = NULL; //temporary pointer, last pointer 12 | int num, i; 13 | printf("Enter number of terms"); 14 | scanf("%d", &num); 15 | printf("Enter each term with coeff and exp\n"); 16 | for (i = 0; i < num; i++) { //loop 17 | t = (struct Node *) malloc(sizeof(struct Node)); //create new node 18 | scanf("%d%d", &t->coeff, &t->exp); //reading 2 data 19 | t->next = NULL; //linking each node into linklist 20 | if (poly == NULL) { //first node check 21 | poly = last = t; 22 | } else { 23 | last->next = t; 24 | last = t; 25 | } 26 | } 27 | } 28 | 29 | void Display(struct Node *p) 30 | { 31 | while (p) { 32 | printf("%dx%d +", p->coeff, p->exp); //printing node 33 | p = p->next; //shifting node 34 | } 35 | printf("\n"); 36 | } 37 | 38 | long Eval(struct Node *p, int x) 39 | { //evalution 40 | long val = 0; 41 | while (p) { //scanning through polynomial 42 | val += p->coeff * pow(x, p->exp); 43 | p = p->next; 44 | } 45 | return val; 46 | } 47 | 48 | // TODO insert 49 | // TODO delete 50 | 51 | int main(void) 52 | { 53 | create(); 54 | Display(poly); 55 | printf("%ld\n", Eval(poly, 1)); 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Position of First'n'Second maximum element in Array without sorting: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int size; 5 | int i; 6 | int arr[100]; 7 | 8 | //initializing the array 9 | printf("Enter the size of the array:"); 10 | scanf("%d",&size); 11 | for(i=0;ifmax) 34 | { 35 | smax=fmax; 36 | fmax=arr[i]; 37 | spos=fpos; 38 | fpos=i+1; 39 | } 40 | else if(arr[i]>smax && arr[i]!=fmax) 41 | { 42 | smax=arr[i]; 43 | spos=i+1; 44 | } 45 | } 46 | 47 | printf("First max element :%d at position:%d\n",fmax,fpos); 48 | printf("Second max element:%d at position:%d\n",smax,spos); 49 | 50 | } 51 | -------------------------------------------------------------------------------- /Position_of_First'n'Second_max_elements_without_sorting_array: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int size; 5 | int i; 6 | int arr[100]; 7 | printf("Enter the size of the array:"); 8 | scanf("%d",&size); 9 | for(i=0;ifmax) 28 | { 29 | smax=fmax; 30 | fmax=arr[i]; 31 | spos=fpos; 32 | fpos=i+1; 33 | } 34 | else if(arr[i]>smax && arr[i]!=fmax) 35 | { 36 | smax=arr[i]; 37 | spos=i+1; 38 | } 39 | } 40 | 41 | printf("First max element :%d at position:%d\n",fmax,fpos); 42 | printf("Second max element:%d at position:%d\n",smax,spos); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Prime.c: -------------------------------------------------------------------------------- 1 | //A c program to print prime numbers from 1 to n 2 | 3 | #include 4 | #include 5 | void main(){ 6 | int num, i, j,check; 7 | printf("Enter number: "); 8 | scanf("%d",&num); 9 | printf("Prime numbers are:\n"); 10 | //traversing all numbers from 2 to entered integer num 11 | for(i=2; i 2 | #include 3 | 4 | #define limit 100 /*size of integers array*/ 5 | 6 | int main(){ 7 | unsigned long long int i,j; 8 | int *primes; //pointer created for prime 9 | int z = 1; 10 | 11 | primes = malloc(sizeof(int) * limit); //allocating 100 spaces in memory with the starting address to prime pointer 12 | 13 | for (i = 2;i < limit; i++) //initializing the 100 memory spaces 14 | primes[i] = 1; 15 | 16 | for (i = 2;i < limit; i++) //Filling the array with prime numbers as per the method proposed by seive of eratosthenes 17 | if (primes[i]) 18 | for (j = i;i * j < limit; j++) 19 | primes[i * j] = 0; 20 | 21 | printf("\nPrime numbers in range 1 to 100 are: \n"); 22 | 23 | for (i = 2;i < limit; i++) //Printing the prime numbers in the range from 1 to 100 24 | if (primes[i]) 25 | printf("%d\n", i); 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Prime_No.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int i,n,c=0; 5 | printf("Enter a Number : "); 6 | scanf("%d",&n); 7 | for(i=2;i 3 | 4 | void quick_sort(int[], int, int); 5 | int partition(int[], int, int); 6 | 7 | int main() { 8 | int a[50], n, i; 9 | printf("How many elements?"); 10 | scanf("%d", & n); 11 | printf("\nEnter array elements:"); 12 | 13 | for (i = 0; i < n; i++) 14 | scanf("%d", & a[i]); 15 | 16 | quick_sort(a, 0, n - 1); 17 | printf("\nArray after sorting:"); 18 | 19 | for (i = 0; i < n; i++) 20 | printf("%d ", a[i]); 21 | 22 | return 0; 23 | } 24 | 25 | void quick_sort(int a[], int l, int u) { 26 | int j; 27 | if (l < u) { 28 | j = partition(a, l, u); 29 | quick_sort(a, l, j - 1); 30 | quick_sort(a, j + 1, u); 31 | } 32 | } 33 | 34 | int partition(int a[], int l, int u) { 35 | int v, i, j, temp; 36 | v = a[l]; 37 | i = l; 38 | j = u + 1; 39 | 40 | do { 41 | do 42 | i++; 43 | 44 | while (a[i] < v && i <= u); 45 | 46 | do 47 | j--; 48 | while (v < a[j]); 49 | 50 | if (i < j) { 51 | temp = a[i]; 52 | a[i] = a[j]; 53 | a[j] = temp; 54 | } 55 | } while (i < j); 56 | 57 | a[l] = a[j]; 58 | a[j] = v; 59 | 60 | return (j); 61 | } 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | [Visit my website to know more about me : gouravthakur.com](https://gouravthakur.com) 3 | # Beginners C Program Examples 4 | ## :octocat: Fork and :eight_pointed_black_star: star this repo 5 | Simple, short and sweet beginners friendly C language programs 6 | 7 | These program are written in codeblocks ide for windows. These programs are not very sophisticated as these are beginners friendly and have many bugs. Anyone who is new to c language can practice these examples. 8 | 9 | - Only programs written in c language will be merged. 10 | - Beautify/Format your code before making a PR. Poorly stuctured code with inconsistent spacing and bad variable name will not be merged. 11 | - Use this tool to beautify your code : https://codebeautify.org/c-formatter-beautifier 12 | - Make sure your program works after beautifying it. 13 | - #include< stdio.h > is wrong and you must remove whitespaces. #include is correct. 14 | - Please check your spellings before making a PR 15 | - Comment code properly. 16 | 17 | ![alt text](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Screenshot.PNG) 18 | 19 | # List of programs 20 | - [Hello World!](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/HelloWorld.c) 21 | - [Area and circumference](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/AreaAndCircumference.c) 22 | - [Basic Arithmatic](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/BasicArithmatic.c) 23 | - [Fahrenheit To Celcius](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/FahrenheitToCelciusConv.c) 24 | - [Lowercase To Uppercase](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/LowercaseToUppercase.c) 25 | - [Uppercase To Lowercase](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/UppercaseToLowercase.c) 26 | - [Simple Interest Calculator](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SimpleInterestCalculator.c) 27 | - [Student Marks Percentage](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/StudentMarksPercentage.c) 28 | - [Swap Values Using Third Variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapValueUsingThirdVariable.c) 29 | - [Swap Value Without Using Third Variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapValueWithoutUsingThirdVariable.c) 30 | - [Relational Operators in C](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/RelationalOperators.c) 31 | - [Ternary Operator](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/TernaryOperator.c) 32 | - [Leap Year Using Ternary Operator](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/LeapYearTernaryOperator.c) 33 | - [Sizeof Operator](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SizeofOperator.c) 34 | - [Find ASCII Value](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/FindAsciiValue.c) 35 | - [Reverse Number](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/ReverseNumber.c) 36 | - [Reverse Number 2](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/ReverseNumber2.c) 37 | - [Bitwise Left Shift Operator](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/BitwiseLeftshiftOperator.c) 38 | - [Bitwise Complement Operator](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/BitwiseComplementOperator.c) 39 | - [Bitwise AND Operator](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/BitwiseAndOperator.c) 40 | - [Bitwise Odd or Even](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/BitwiseOddOrEven.c) 41 | - [Increment Operator](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/IncrementOperator.c) 42 | - [Decrement Operator](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DecrementOperator.c) 43 | - [Nested If Leap Year](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/NestedIfLeapYear.c) 44 | - [Nested if Greatest Integer](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/NestedifGreatestInteger.c) 45 | - [Number to Character](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Number-to-Character.c) 46 | - [Check Character Type](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/CheckCharacterType.c) 47 | - [Employee Grade](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/EmployeeGrade.c) 48 | - [Daily Wage Calc](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DailyWageCalc.c) 49 | - [Day Name Using Switch Case](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DayNameUsingSwitchCase.c) 50 | - [Vowel or Consonant](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/VowelorConsonant.c) 51 | - [Calculator using switch case](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/CalcUsingSwitchCase.c) 52 | - [Bubble Sort](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/BubbleSort.c) 53 | - [Insertion Sort](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Insertionsort.c) 54 | - [Merge Sort](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Mergesort.c) 55 | - [Quick Sort](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Quicksort.c) 56 | - [Goto statement](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/GotoStatementEvenOrOdd.c) 57 | - [Mirror Number](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/MirrorNumber.c) 58 | - [Dice roll with Adjustable sides](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DiceRoll.c) 59 | - [Dynamic 2D Array Using One Pointer](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DynamicTwoDArrayUsingOnePointer.c) 60 | - [Dyanamic 2D Array using Array of Pointer](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DynamicTwoDArrayUsingArrayOfPointer.c) 61 | - [Digital Root of a Number](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DigitalRoot.c) 62 | - [Swap By Reference Vs Swap By Copy](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapByRefandCopy.c) 63 | - [Display Linux Environment Variables](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DisplayLinuxEnvirmentVariables.c) 64 | - [Factorial](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Factorial.c) 65 | - [Get String Length](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/StringLength.c) 66 | - [Decimal To Binary](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DecimalToBinary.c) 67 | - [Haystack and Needle (SubString)](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/HaystackAndNeedle_SubString.c) 68 | - [Pointers in C](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Pointers.c) 69 | - [Binary Search](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/BinarySearch.c) 70 | - [Recursion](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Recursion.c) 71 | - [Segmentation Fault or Bus Error Demo](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SegmentationFaultorBusErrorDemo.c) 72 | - [Structure](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Structure.c) 73 | - [Swapping 2 Numbers Without a Third Variable or ^](https://github.com/geetanjaliaich/beginners-C-program-examples/blob/FactorialEratosthenes/SwapIntegersWithout3rdVariable(Arithmatic).c) 74 | - [Print 100 Prime numbers using Seive of Eratosthenes](https://github.com/geetanjaliaich/beginners-C-program-examples/blob/FactorialEratosthenes/PrimeByEratosthenes.c) 75 | - [Palindrome Number](https://github.com/geetanjaliaich/beginners-C-program-examples/blob/FactorialEratosthenes/PalindromeNumber.c) 76 | - [Temperature conversion](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/AllTempScalesConv.c) 77 | - [Alphabet triangle](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/alphabetTriangle.cpp) 78 | - [Armstrong Number](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/ArmstrongNumber.c) 79 | - [Background Thread Sorter](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/BackgroundThreadSorter.c) 80 | - [Basic Game](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/BasicGame.c) 81 | - [Celsius to Kelvin](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/CelciusToKelvinConv.c) 82 | - [Calculator](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/combine_calculator.c) 83 | - [Common elements in two array](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/CommonElementsInTwoArrays.c) 84 | - [Decimal to base n conversion](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DecimalToBaseN.c) 85 | - [Two-way decimal to hexadecimal](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DecimalToHexadecimalViceVersa.c) 86 | - [Encrypt and Decrypt XOR](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/EncryptDecryptXOR.c) 87 | - [Fibonnaci Generation](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/FibonacciGeneration.c) 88 | - [Calculate remainder](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/FindRemainder.c) 89 | - [Check leap year](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/isInputLeapYear.c) 90 | - [Find largest number](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Largest.c) 91 | - [Check odd or even](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/oddandeven.c) 92 | - [Print pattern](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Pattern1.c) 93 | - [Polynomial linklist](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Polynomial_linklist.c) 94 | - [Print prime numbers](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Prime.c) 95 | - [Factorial of a number with recursion](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/RecursiveFactorial.c) 96 | - [Simple EMI Calculator](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SimpleEMICalculator.c) 97 | - [Simple multiplication table](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SimpleMultiplicationTable.c) 98 | - [Square root](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SquareRoot.c) 99 | - [Stack implemenation of linklist](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Stack%20-%20Linked%20List.c) 100 | - [Swap integers without 3rd variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapIntegers.c) 101 | - [Swap value without third variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapValueWithoutUsingThirdVariable.c) 102 | - [Identify machine is big-endian or little-endian](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/endian.c) 103 | - [Perform Selection Sort](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SelectionSort.c) 104 | - [Calculate Factorial upto input of 5000](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/large_factorial.c) 105 | - [To check if a matrix is a sparse matrix or not](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SparseMatrix_017.c) 106 | - [To calculate the Least Common Multiple](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/lcm.c) 107 | - [Lambda in C](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/lambda_in_c.c) 108 | # Contributing 109 | This is a personal learning project for me. 110 | 111 | Please feel free to fork this repo. Pull request to submit more programs. 112 | -------------------------------------------------------------------------------- /Recursion.c: -------------------------------------------------------------------------------- 1 | // Sum of natural numbers using recursion 2 | 3 | #include 4 | int sum(int n); 5 | 6 | int main() { 7 | int number, result; 8 | 9 | printf("Enter a positive integer: "); 10 | scanf("%d", & number); 11 | 12 | result = sum(number); 13 | 14 | printf("sum = %d", result); 15 | return 0; 16 | } 17 | 18 | int sum(int num) { 19 | if (num != 0) 20 | return num + sum(num - 1); // sum() function calls itself 21 | else 22 | return num; 23 | } 24 | -------------------------------------------------------------------------------- /RecursiveFactorial.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int factorial(int num) { 4 | if (num == 1) 5 | return 1; 6 | else 7 | return num * factorial(num - 1); 8 | } 9 | 10 | int main() { 11 | int number; 12 | 13 | printf("Type a positive number: "); 14 | scanf("%d", & number); 15 | 16 | printf("%d! is equal to %d", number, factorial(number)); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /RelationalOperators.c: -------------------------------------------------------------------------------- 1 | // Relational operators in c language 2 | 3 | #include 4 | int main() 5 | { 6 | int a, b; 7 | printf("Enter two numbers a and b respectively\n"); 8 | scanf("%d%d",&a,&b); 9 | // Greater than operator 10 | if (a>b) 11 | printf("a is greater than b\n"); 12 | else 13 | printf("a is smaller than b\n"); 14 | 15 | // Greater than equal to 16 | if (a>=b) 17 | printf("a is greater than equal to b\n"); 18 | else 19 | printf("a is not greater than equal to b\n"); 20 | 21 | //Less than 22 | if (a 4 | 5 | int main() 6 | { 7 | int n, reverse = 0; 8 | 9 | printf("Enter a number to reverse\n"); 10 | scanf("%d", &n); 11 | 12 | while (n != 0) 13 | { 14 | reverse = reverse * 10; 15 | reverse = reverse + n%10; 16 | n = n/10; 17 | } 18 | 19 | printf("Reverse of entered number is = %d\n", reverse); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /ReverseNumber2.c: -------------------------------------------------------------------------------- 1 | // Reverse of a number - 2nd program 2 | #include 3 | int main() { 4 | int num, reverse = 0, remainder; 5 | printf("Enter a number\n"); 6 | scanf("%d", & num); 7 | while (num != 0) { 8 | remainder = num % 10; 9 | reverse = reverse * 10 + remainder; 10 | num = num / 10; 11 | } 12 | printf("Reverse of the number is : %d\n", reverse); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /RussianPeasantMultiplication.c: -------------------------------------------------------------------------------- 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 | } -------------------------------------------------------------------------------- /Screenshot.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gouravthakur39/beginners-C-program-examples/373d27c131e35bacdbf5c500f79fd234c6d4ec9b/Screenshot.PNG -------------------------------------------------------------------------------- /SegmentationFaultorBusErrorDemo.c: -------------------------------------------------------------------------------- 1 | // This program will show a segmentation or bus error because we tried to change string literals 2 | // A segmentation fault (aka segfault) is a common condition that causes programs to crash; 3 | // they are often associated with a file named core . Segfaults are caused by a program trying to 4 | // read or write an illegal memory location. 5 | 6 | #include 7 | int 8 | main () 9 | { 10 | 11 | char *cards = "JQK"; 12 | 13 | char a_card = cards[2]; 14 | 15 | cards[2] = cards[1]; 16 | 17 | cards[1] = cards[0]; 18 | 19 | cards[0] = cards[2]; 20 | 21 | cards[2] = cards[1]; 22 | 23 | cards[1] = a_card; 24 | 25 | puts (cards); 26 | 27 | return 0; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /SelectionSort.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /Separate.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { float a,c; 4 | int b; 5 | printf("Enter the Number : "); 6 | scanf("%f",&a); 7 | b=a/1; 8 | c=a-b; 9 | printf("Integer Part is : %d\n",b); 10 | printf("Decimal Part is : %f",c); 11 | } 12 | -------------------------------------------------------------------------------- /SimpleArithmeticAverage: -------------------------------------------------------------------------------- 1 | //A simple code that calculates a student's arithmetic average over 3 grades. 2 | 3 | #include 4 | 5 | int main(void) { 6 | 7 | float grade1, grade2, grade3, average; 8 | 9 | //Grades input 10 | printf("Type de first grade: "); 11 | scanf("%f", & grade1); 12 | 13 | printf("Type the second grade : "); 14 | scanf("%f", & grade2); 15 | 16 | printf("Type the third grade: "); 17 | scanf("%f", & grade3); 18 | 19 | //Processing 20 | average = (grade1 + grade2 + grade3) / 3; 21 | 22 | //Output 23 | printf("Student Average = %.1f\n", average); 24 | 25 | system("pause"); 26 | return 0; 27 | -------------------------------------------------------------------------------- /SimpleEMICalculator.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | float principal, rate, time, emi; 7 | 8 | printf("Enter principal amount: "); 9 | scanf("%f",&principal); 10 | 11 | printf("Enter rate of interest: "); 12 | scanf("%f",&rate); 13 | 14 | printf("Enter time in years: "); 15 | scanf("%f",&time); 16 | 17 | rate=rate/(12*100); /*one month interest*/ 18 | time=time*12; /*one month period*/ 19 | 20 | emi= (principal*rate*pow(1+rate,time))/(pow(1+rate,time)-1); 21 | 22 | printf("Monthly EMI is= %f\n",emi); 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /SimpleInterestCalculator.c: -------------------------------------------------------------------------------- 1 | //Program to calculate simple interest 2 | 3 | 4 | #include 5 | int main() 6 | { 7 | float PrincipleAmount,Rate,Time, SimpleInterest; 8 | printf("Enter Principal Amount, Rate of interest and Time Respectively\n"); 9 | scanf("%f%f%f", &PrincipleAmount, &Rate, &Time); 10 | SimpleInterest = (PrincipleAmount * Rate * Time)/ 100; 11 | printf("Simple Interest is :%f",SimpleInterest); 12 | return 0; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /SimpleMultiplicationTable.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int num, i = 1; 6 | 7 | printf("Enter a number to calculate the multiplication table up to 10:\n"); 8 | 9 | scanf("%d", &num); 10 | 11 | for(i = 1; i <= 10; i++){ 12 | printf("%d x %d = %d\n", i, num, num * i); 13 | } 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Simple_Interest.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { float p,r,t,s; 4 | printf("Enter the principal : "); 5 | scanf("%f",&p); 6 | printf("Enter the rate : "); 7 | scanf("%f",&r); 8 | printf("Enter the time : "); 9 | scanf("%f",&t); 10 | s=(p*r*t)/100; 11 | printf("SI is %f",s); 12 | } 13 | -------------------------------------------------------------------------------- /SizeofOperator.c: -------------------------------------------------------------------------------- 1 | // sizeof() operator in c is used to calculate the size of a variable 2 | // used inside the program and return the size in integer in the form 3 | // of memory bytes 4 | 5 | #include 6 | int main() 7 | { 8 | int a = 5; 9 | char ch = 'X'; // Capital X 10 | printf("Size of a : %d Bytes\n" ,sizeof(a)); 11 | printf("Size of X : %d Bytes\n",sizeof(ch)); 12 | return 0; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /SparseMatrix_017.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /SpiralMatrix.c: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /SquareRoot.c: -------------------------------------------------------------------------------- 1 | /* Program to find the square root of a number 2 | * - the current implementation uses the Newton-Raphson method 3 | * - mathematical explanation can be found online, and requires basic calculus knowledge 4 | */ 5 | 6 | #include 7 | #include // for 'fabs' - returns unsigned absolute value 8 | 9 | const double MAX_ERROR = 1e-7; // equivalent to 10^-7 -> accurate upto 7 decimal places 10 | // can be set according to need or even taken in as input 11 | 12 | double squareRoot(int x) 13 | { 14 | double r = 1; // initial guess for the root 15 | while (fabs(r*r - x) > MAX_ERROR) 16 | { 17 | r = (r + x/r) / 2; 18 | // value of 'r' moves closer and closer to the actual root value 19 | } 20 | 21 | return r; 22 | } 23 | 24 | int main() 25 | { 26 | // the number for which we expect to compute the root 27 | int num; 28 | scanf("%d", &num); 29 | 30 | printf("%lf \n", squareRoot(num)); 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Stack - Linked List.c: -------------------------------------------------------------------------------- 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 | 89 | -------------------------------------------------------------------------------- /Star_Pattern.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int n, i,j; 6 | 7 | printf("Enter number of rows : "); 8 | scanf("%d" , &n); 9 | 10 | for(i=1;i<=n;i++){ 11 | for(j=1;j<=i;j++) 12 | printf("* "); 13 | printf("\n"); 14 | } 15 | return 0; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /StringLength.c: -------------------------------------------------------------------------------- 1 | // Implementation of the strlen function (for standard C strings) 2 | 3 | #include 4 | 5 | int stringLength(char * word) { 6 | int count = 0; 7 | 8 | // Increment our counter until NULL TERMINATOR is found 9 | while (word[count] != '\0') { 10 | count++; 11 | } 12 | return (count); 13 | } 14 | 15 | int main() { 16 | int index = 0; 17 | char word[100]; 18 | 19 | // Gets single word as input 20 | printf("Please enter a word to get its length (< 100 characters): "); 21 | scanf("%s", word); 22 | 23 | // Prints output of stringLength() 24 | printf("\"%s\" has a length of %d\n", word, stringLength(word)); 25 | 26 | return (0); 27 | } -------------------------------------------------------------------------------- /StringReverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | int i, j, k; 6 | char str[100]; 7 | char rev[100]; 8 | printf("Enter a string:\t"); 9 | scanf("%s", str); 10 | printf("The original string is %s\n", str); 11 | for(i = 0; str[i] != '\0'; i++); 12 | { 13 | k = i-1; 14 | } 15 | for(j = 0; j <= i-1; j++) 16 | { 17 | rev[j] = str[k]; 18 | k--; 19 | } 20 | printf("The reverse string is %s\n", rev); 21 | getch(); 22 | } 23 | -------------------------------------------------------------------------------- /Structure.c: -------------------------------------------------------------------------------- 1 | #include 2 | struct student 3 | { 4 | char name[50]; 5 | int roll; 6 | float marks; 7 | } s; 8 | 9 | int main() 10 | { 11 | printf("Enter information:\n"); 12 | 13 | printf("Enter name: "); 14 | scanf("%s", s.name); 15 | 16 | printf("Enter roll number: "); 17 | scanf("%d", &s.roll); 18 | 19 | printf("Enter marks: "); 20 | scanf("%f", &s.marks); 21 | 22 | 23 | printf("Displaying Information:\n"); 24 | 25 | printf("Name: "); 26 | puts(s.name); 27 | 28 | printf("Roll number: %d\n",s.roll); 29 | 30 | printf("Marks: %.1f\n", s.marks); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /StudentMarksPercentage.c: -------------------------------------------------------------------------------- 1 | // Evaluate total, average and percentage of a student 2 | 3 | #include 4 | int main() 5 | { 6 | int a,b,c,d,e; 7 | float total,average,percentage; 8 | printf("Enter marks of 5 subjects : \n"); 9 | scanf("%d%d%d%d%d",&a,&b,&c,&d,&e); 10 | total = a+b+c+d+e; 11 | average = total/5; 12 | percentage = (total/500)*100; 13 | printf("Total marks = %f\n", total); 14 | printf("Average marks = %f\n",average ); 15 | printf("Net percentage = %f\n",percentage); 16 | return 0; 17 | 18 | } 19 | -------------------------------------------------------------------------------- /Subtraction.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { int a,b,c; 4 | printf("Enter two Numbers : "); 5 | scanf("%d %d",&a,&b); 6 | c=a-b; 7 | printf("Subtraction is %d",c); 8 | } 9 | -------------------------------------------------------------------------------- /SumUsingThreads.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | pthread_t tids[3]; //creating three threads using pthread library 5 | int numbers[10] = {1,2,3,4,5,6,7,8,9,10}; //decleration of numbers array with 10 elements 6 | int num[2]={0,0}; //decleration of num array with 2 elements 7 | 8 | //dividing firt five numbers into one array 9 | void* first5(void* arg){ 10 | int ans = 0; 11 | for(int i=0;i<5;i++){ 12 | ans += numbers[i]; 13 | } 14 | printf("%d\n",ans); 15 | num[0] = ans; 16 | } 17 | 18 | diving second five numbers into another array 19 | void* second5(void* arg){ 20 | int ans = 0; 21 | for(int i=5;i<10;i++){ 22 | ans+=numbers[i]; 23 | } 24 | printf("%d\n",ans); 25 | num[1]=ans; 26 | } 27 | 28 | //funtion to calculate sum 29 | void* sum(void* arg){ 30 | pthread_join(tids[0],NULL); 31 | pthread_join(tids[1],NULL); 32 | int ans = num[0]+num[1]; 33 | printf("%d\n",ans); 34 | } 35 | 36 | int main (void){ 37 | 38 | pthread_create(&tids[0],NULL,first5,(void*)NULL); //creating one thread to calculate sum of first five numbers 39 | pthread_create(&tids[1],NULL,second5,(void*)NULL); //creating second thread to calculate sum of last five numbers 40 | pthread_create(&tids[2],NULL,sum,(void*)NULL); //creating another thread to to take sum of the above 41 | pthread_join(tids[2],NULL); // thread 3 will wait until thread 1 and 2 finish executing 42 | printf("Main Ended\n"); 43 | 44 | return 0; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /SwapByRefandCopy.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Swap_ref creates a temporary variable temp and dereffrences the address that was sent to it(this is done to get the actual 4 | // value of the int in the memory space). the dereffrencing allows us to change the actual values. 5 | void swap_ref(int* a, int* b){ 6 | int temp = *a; 7 | 8 | *a = *b; 9 | *b = temp; 10 | } 11 | 12 | // Swap however does not use addresses and reffrences and only alters the ints within it's own scope 13 | void swap(int a, int b) 14 | { 15 | int temp = a; 16 | a = b; 17 | b = temp; 18 | } 19 | 20 | int main(void) { 21 | //Declare 2 int variables that are going to be swapped by reference 22 | int a = 1; 23 | int b = 2; 24 | 25 | printf("%d\n", a); 26 | printf("%d\n", b); 27 | 28 | //Pass the Address of the Values to the Swap function 29 | swap_ref(&a, &b); 30 | 31 | printf("%d\n", a); 32 | printf("%d\n", b); 33 | 34 | int c = 5; 35 | int d = 10; 36 | printf("%d\n", c); 37 | printf("%d\n", d); 38 | 39 | swap(c, d); 40 | 41 | printf("%d\n", c); 42 | printf("%d\n", d); 43 | } 44 | -------------------------------------------------------------------------------- /SwapIntegers.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int num1=5, num2=10; 5 | 6 | printf("The numbers are: %d and %d", &num1,&num2); 7 | 8 | num1=num1+num2; //num1=15 and num2=10 9 | num2=num1-num2; //num1=15 and num2=5 10 | num1=num1-num2; //num1=10 and num2=5 11 | 12 | printf("The numbers have been swapped with new positions: %d and %d", &num1,&num2); 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /SwapIntegersWithout3rdVariable(Arithmatic).c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int num1=5, num2=10; 5 | 6 | printf("The numbers are: %d and %d", &num1,&num2); 7 | 8 | num1=num1+num2; //num1=15 and num2=10 9 | num2=num1-num2; //num1=15 and num2=5 10 | num1=num1-num2; //num1=10 and num2=5 11 | 12 | printf("The numbers have been swapped with new positions: %d and %d", &num1,&num2); 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /SwapValueUsingThirdVariable.c: -------------------------------------------------------------------------------- 1 | // Swap two integers using third variable 2 | 3 | 4 | #include 5 | int main() 6 | { 7 | int a,b,c; 8 | printf("Enter two no :\n"); 9 | scanf("%d%d",&a,&b); 10 | c=a; 11 | a=b; 12 | b=c; 13 | printf("After swapping value of a = %d\n b =%d",a,b); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /SwapValueWithoutUsingThirdVariable.c: -------------------------------------------------------------------------------- 1 | // Swap two integers without using third variable 2 | 3 | 4 | #include 5 | int main() 6 | { 7 | int a, b; 8 | printf("Enter two no :\n"); 9 | scanf("%d%d",&a,&b); 10 | a = a^b; 11 | b = a^b; 12 | a= a^b; 13 | printf("After swapping value of a and b : %d,%d",a,b); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Swap_1.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { int a,b,t; 4 | printf("Enter two Numbers : "); 5 | scanf("%d %d",&a,&b); 6 | t=b; 7 | b=a; 8 | a=t; 9 | printf("Swap is %d %d",a,b); 10 | } 11 | -------------------------------------------------------------------------------- /Swap_2.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { int a,b; 4 | printf("Enter two Numbers : "); 5 | scanf("%d %d",&a,&b); 6 | a=a+b; 7 | b=a-b; 8 | a=a-b; 9 | printf("Swap is %d %d",a,b); 10 | } 11 | -------------------------------------------------------------------------------- /Swapping(without using extra variable).cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | int a,b; 6 | printf("Enter a and b\n"); 7 | scanf("%d%d",&a,&b); 8 | a=a+b; 9 | b=a-b; 10 | a=a-b; 11 | printf("After swapping a and b are %d %d",a,b); 12 | getch(); 13 | } 14 | -------------------------------------------------------------------------------- /Temperature.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { float a,c,f; 4 | printf("Enter the Temperature in Celcius : "); 5 | scanf("%f",&c); 6 | f=c*(9/5)+32; 7 | printf("Temperature in Fahernheit is %f",f); 8 | } 9 | -------------------------------------------------------------------------------- /TemperatureSwitch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | // The resultant temperatures can be in decimals as well, so we use double 6 | double c, f, result; 7 | // We use an integer type data to run the switch statement 8 | int choice; 9 | printf("Select your choice: \n"); 10 | printf("1. Celcius to Fahrenheit\n"); 11 | printf("2. Fahrenheit to Celcius\n"); 12 | 13 | printf("Enter your choice: "); 14 | scanf("%d", &choice); 15 | 16 | // We compute the temperatures for both the cases here respectively 17 | switch(choice) 18 | { 19 | case 1: 20 | printf("Enter the temperature in Celcius: "); 21 | scanf("%lf", &c); 22 | result = (9 / 5) * c + 32; 23 | break; 24 | case 2: 25 | printf("Enter the temperature in Fahrenheit: "); 26 | scanf("%lf", &f); 27 | result = (5 / 9) * (f - 32); 28 | break; 29 | 30 | // This case gets activated when the user inputs anything othrer than 1 or 2 31 | default: 32 | printf("Invalid case!\n"); 33 | } 34 | 35 | // Printing out the result according to the computation 36 | printf("The resultant temperature is: %lf", result); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /TernaryOperator.c: -------------------------------------------------------------------------------- 1 | //Largest number among 3 numbers using ternary operator 2 | 3 | #include 4 | int main() 5 | { 6 | float a,b,c,large; 7 | printf("Enter any 3 numbers\n"); 8 | scanf("%f%f%f",&a,&b,&c); 9 | large = a>b? (a>c?a:c): (b>c?b:c); 10 | printf("The larger no is :%f\n", large); 11 | return 0; 12 | 13 | } 14 | -------------------------------------------------------------------------------- /Trif.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { double a,b; 5 | printf("Enter the degree : "); 6 | scanf("%lf",&a); 7 | a=(a*3.14)/180; 8 | b=sin(a); 9 | printf("Sine is %lf",b); 10 | } 11 | -------------------------------------------------------------------------------- /UppercaseToLowercase.c: -------------------------------------------------------------------------------- 1 | // Uppercase character to lowercase character 2 | 3 | 4 | #include 5 | int main() 6 | { 7 | char a,u; 8 | printf("Enter Uppercase letter :\n"); 9 | scanf("%c", &a); 10 | u = a + 32; 11 | printf("Lowercase is : %c", u); 12 | return 0; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /VowelorConsonant.c: -------------------------------------------------------------------------------- 1 | // Program to input a character and check whether it is vowel or consonant using switch case 2 | #include 3 | int main(){ 4 | char ch; 5 | printf("Enter a character\n"); 6 | scanf("%c", &ch); 7 | switch(ch) 8 | { 9 | case 'a': 10 | case 'e': 11 | case 'i': 12 | case 'o': 13 | case 'u': 14 | case 'A': 15 | case 'E': 16 | case 'I': 17 | case 'O': 18 | case 'U': 19 | printf("Entered character is a vowel"); 20 | break; 21 | default: 22 | printf("Entered character is a consonant"); 23 | 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /alphabetTriangle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* 5 | A 6 | ABA 7 | ABCBA 8 | ABCDCBA 9 | ABCDEDCBA 10 | 11 | */ 12 | int main(){ 13 | int ch=65; 14 | int i,j,k,m; 15 | for(i=1;i<=5;i++) 16 | { 17 | for(j=5;j>=i;j--) 18 | printf(" "); 19 | for(k=1;k<=i;k++) 20 | printf("%c",ch++); 21 | ch--; 22 | for(m=1;m 2 | #include 3 | 4 | int convert_to_octal(long long binary); 5 | 6 | int main() { 7 | long long binary; 8 | printf("Enter a binary number: "); 9 | scanf("%lld", &binary); 10 | printf("%lld in binary = %d in octal", binary, convert(binary)); 11 | return 0; 12 | } 13 | 14 | int convert_to_octal(long long binary) { 15 | int octal = 0, decimal = 0, i = 0; 16 | 17 | //Step 1> convert binary to decimal 18 | //Step 2> convert decimal to octal 19 | 20 | // converting binary to decimal 21 | while (binary != 0) { 22 | decimal += (binary % 10) * pow(2, i); 23 | ++i; 24 | binary /= 10; 25 | } 26 | i = 1; 27 | 28 | // converting to decimal to octal 29 | while (decimal != 0) { 30 | octal += (decimal % 8) * i; 31 | decimal /= 8; 32 | i *= 10; 33 | } 34 | return octal; 35 | } 36 | -------------------------------------------------------------------------------- /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 | 43 | -------------------------------------------------------------------------------- /check functon is even or odd in c programming: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int n; 5 | printf("enter the value of n"); 6 | scanf("%d",&n); 7 | if (n % 2 == 0) 8 | { 9 | printf("n is even number"); 10 | } 11 | else; 12 | { 13 | printf{"n is odd number"); 14 | }} 15 | -------------------------------------------------------------------------------- /closestpowerof2.c: -------------------------------------------------------------------------------- 1 | //Program for printing closest of 2 of a number. 2 | 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | int num,a,b,c,d=0; 9 | scanf("%d",&num); 10 | for(b=31;b>=0;--b) 11 | { 12 | c=num>>b; 13 | if(c&1) 14 | { 15 | d++; 16 | if(d>1) 17 | { 18 | num=num-pow(2,b); 19 | } 20 | } 21 | } 22 | printf("%d",num); 23 | return 0; 24 | } 25 | //not working for negative no. 26 | -------------------------------------------------------------------------------- /combine_calculator.c: -------------------------------------------------------------------------------- 1 | // add a program for a calculator in C 2 | #include 3 | #include 4 | void Input(){ 5 | printf("Type number to choose the algorithm\n"); 6 | printf( " 1. + \n 2. - \n 3. *\n 4. / \n 5. ^ \n 6. square \n "); 7 | printf("7. log \n 8.floor \n 9. ceil \n 10.Exit\n InputNum: "); 8 | } 9 | int main() 10 | { 11 | int InputNum=0; 12 | float a=0; 13 | float b=0; 14 | float result=0; 15 | do{ 16 | Input(); 17 | scanf("%d", &InputNum); 18 | switch(InputNum){ 19 | case(1): 20 | printf("Enter First Number: "); 21 | scanf("%f", &a); 22 | printf("Enter Second Number: "); 23 | scanf("%f", &b); 24 | result = a + b; 25 | printf("%.2f + %.2f = %.2f\n",a , b, result); 26 | break; 27 | case(2): 28 | printf("Enter First Number: "); 29 | scanf("%f", &a); 30 | printf("Enter Second Number: "); 31 | scanf("%f", &b); 32 | result = a - b; 33 | printf("%.2f - %.2f = %.2f\n",a ,b, result); 34 | break; 35 | case(3): 36 | printf("Enter First Number: "); 37 | scanf("%f", &a); 38 | printf("Enter Second Number: "); 39 | scanf("%f", &b); 40 | result = a * b; 41 | printf("%.2f * %.2f = %.2f\n",a ,b, result); 42 | break; 43 | case(4): 44 | printf("Enter Dividend: "); 45 | scanf("%f", &a); 46 | printf("Enter Divisor: "); 47 | scanf("%f", &b); 48 | if (b == 0){ 49 | printf("Please check the divisor that can not be zero.\n"); 50 | } 51 | else{ 52 | result = a / b; 53 | printf("%.2f / %.2f = %.2f\n",a ,b, result); 54 | } 55 | break; 56 | case(5): 57 | printf("Enter the number "); 58 | scanf("%f",&a); 59 | printf("Enter the second number "); 60 | scanf("%f",&b); 61 | printf("first number to the power of second number is %.2f \n ",pow(a,b)); 62 | break; 63 | case(6): 64 | printf("Enter the radicand "); 65 | scanf("%f",&a); 66 | printf("square root is %.2f \n",sqrt(a)); 67 | break; 68 | 69 | case(7): 70 | printf("Enter the log "); 71 | scanf("%f",&a); 72 | printf("the log is %.2f \n",log(a)); 73 | break; 74 | case(8): 75 | printf("Enter the number "); 76 | scanf("%f",&a); 77 | printf("the float is %.2f \n",floor(a)); 78 | break; 79 | case(9): 80 | printf("Enter the number "); 81 | scanf("%f",&a); 82 | printf("the ceil is %.2f \n",ceil(a)); 83 | break; 84 | 85 | case(10): 86 | printf("Thank you for using code !!\n"); 87 | break; 88 | 89 | default: 90 | printf("Invalid number, please choose again.\n"); 91 | } 92 | }while(InputNum); 93 | return 0; 94 | } 95 | 96 | -------------------------------------------------------------------------------- /counting_sort.c: -------------------------------------------------------------------------------- 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 | } -------------------------------------------------------------------------------- /dynamicMemoryAllocation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define SIZE 0x100 5 | void main(){ 6 | 7 | char *ptr = malloc(SIZE); 8 | snprintf(ptr , SIZE , "data : %s " , "hello world"); 9 | printf("%s",ptr); 10 | 11 | } -------------------------------------------------------------------------------- /endian.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************/ 2 | // Since size of character is 1 byte when the character pointer is de-referenced 3 | // it will contain only first byte of integer. 4 | // If machine is little endian then *c will be 1 (because last byte is stored first) 5 | // if machine is big endian then *c will be 0. 6 | 7 | // higher memory 8 | // -----> 9 | // +----+----+----+----+ 10 | // |0x01|0x00|0x00|0x00| 11 | // +----+----+----+----+ 12 | // c 13 | // | 14 | // &i 15 | 16 | // +----+----+----+----+ 17 | // |0x00|0x00|0x00|0x01| 18 | // +----+----+----+----+ 19 | // c 20 | // | 21 | // &i 22 | /*************************************************************************************/ 23 | 24 | #include 25 | int main() 26 | { 27 | unsigned int i = 1; 28 | char *c = (char*)&i; 29 | if (*c) 30 | printf("Little endian\n"); 31 | else 32 | printf("Big endian\n"); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /fastModuloExponentiation.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Declaring a golbal variable as its going to be used again and again in the function 4 | long long m; 5 | 6 | long long modder(long long x, long long y) 7 | { 8 | // x to the power zero is 1 9 | if(y == 0) 10 | return 1; 11 | // x to the power one is x 12 | else if(y == 1) 13 | return x; 14 | 15 | // We now split the problem in two parts 16 | // solve one of them and use its resultant for the second part 17 | else 18 | { 19 | long long ans = modder(x, y/2); 20 | // If the power is not divisible by 2, we simply multiply by x 21 | // Using the concept: (a * b) % m = (a % m * b % m) % m 22 | // This concept is used repeatedly when y % 2 != 0 23 | 24 | if(y % 2 == 0) 25 | return (ans % m) * (ans % m); 26 | else 27 | return (((ans % m) * (ans % m)) % m * (x % m)) % m; 28 | } 29 | } 30 | 31 | int main() 32 | { 33 | printf("Enter three numbers, x to the power of y modulo m: "); 34 | long long x, y, result; 35 | scanf("%lld%lld%lld", &x, &y, &m); 36 | 37 | // We don't need to send m here now 38 | result = modder(x, y); 39 | 40 | printf("The resultant is: %lld\n", result); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /finding the first missing natural number: -------------------------------------------------------------------------------- 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 2 | #include 3 | #include 4 | 5 | int gcd(int x, int y) { 6 | int r = 0, a, b; 7 | a = (x > y) ? x : y; // a is greater number 8 | b = (x < y) ? x : y; // b is smaller number 9 | r = b; 10 | 11 | while (a % b != 0) { 12 | r = a % b; 13 | a = b; 14 | b = r; 15 | } 16 | return r; 17 | } 18 | 19 | int lcm(int x, int y) { 20 | int a; 21 | a = (x > y) ? x : y; // a is greater number 22 | while (1) { 23 | if (a % x == 0 && a % y == 0) 24 | return a; 25 | ++a; 26 | } 27 | } 28 | 29 | int main(int argc, char **argv) { 30 | printf("Enter the two numbers: "); 31 | int x, y; 32 | scanf("%d", &x); 33 | scanf("%d", &y); 34 | printf("The GCD of two numbers is: %d", gcd(x, y)); 35 | printf("The LCM of two numbers is: %d", lcm(x, y)); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /getPIDs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void main(){ 6 | pid_t pid; //variable to store process id 7 | pid_t ppid; //variable to store parent process id 8 | 9 | sleep(5); 10 | 11 | pid = getpid(); // returns pid 12 | ppid = getppid(); //returns parent pid 13 | 14 | printf("The process id is : %d\n" , pid); 15 | printf("The parent of process id is : %d\n" , ppid); 16 | } 17 | -------------------------------------------------------------------------------- /heap sort.c: -------------------------------------------------------------------------------- 1 | 2 | #include 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 | } -------------------------------------------------------------------------------- /increment_number.c: -------------------------------------------------------------------------------- 1 | /* A C-Program that Increments a Number */ 2 | 3 | #include 4 | #include // This Header is used so we can gain access to the sleep() Function 5 | 6 | int main(void) { 7 | int num = 0; // This is the Number that will be incremented. 8 | int increment = 1; // This is what we'll be using to increment(add) onto num. 9 | 10 | // While num's Value is less than 100, we'll increment the Value of num by 1 every second. 11 | while(num < 100) { 12 | sleep(1); // Wait 1 second each time, num has it's Value incremented 13 | num += increment; // Increment num 14 | printf("%d\n", num); // Print the current Value of num 15 | } 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /ip_address.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void extractIpAddress(unsigned char *sourceString,short *ipAddress) 5 | { 6 | unsigned short len=0; 7 | unsigned char oct[4]={0},cnt=0,cnt1=0,i,buf[5]; 8 | 9 | len=strlen(sourceString); 10 | for(i=0;i=0 && ipAddress[0]<=127) 40 | printf("Class A Ip Address.\n"); 41 | if(ipAddress[0]>127 && ipAddress[0]<191) 42 | printf("Class B Ip Address.\n"); 43 | if(ipAddress[0]>191 && ipAddress[0]<224) 44 | printf("Class C Ip Address.\n"); 45 | if(ipAddress[0]>224 && ipAddress[0]<=239) 46 | printf("Class D Ip Address.\n"); 47 | if(ipAddress[0]>239) 48 | printf("Class E Ip Address.\n"); 49 | 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /isInputLeapYear.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int input,i; 5 | printf("Please input a year and I will tell you if it's a leap year: "); 6 | scanf("%d",&input); 7 | if ((input%4==0 && input%100!=0) || input%400==0) { 8 | printf("Your input (%d) IS a leap year.\n\n",input); 9 | } 10 | else{ 11 | printf("Your input (%d) is NOT a leap year.\n\n",input); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /kth smallest no in an array.c: -------------------------------------------------------------------------------- 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(iy ? x:y; 7 | }; 8 | #elif defined(__GNUC__) || defined(__GNUG__) 9 | // gcc -std=gnu11 lambda_in_c.c 10 | int (*max)(int, int) = 11 | ({ 12 | int __fn__ (int x, int y) { return x > y ? x : y; } 13 | __fn__; 14 | }); 15 | #endif 16 | printf("%d",max(1,23)); 17 | } 18 | -------------------------------------------------------------------------------- /large_factorial.c: -------------------------------------------------------------------------------- 1 | //c program to find factorial upto input of 5000 2 | 3 | 4 | #include 5 | 6 | int main(){ 7 | int ans[16500]; 8 | int testcases; 9 | printf("Enter no of test cases: "); 10 | scanf("%d", &testcases); 11 | while(testcases--){ 12 | int num; 13 | printf("Enter number:"); 14 | scanf("%d", &num); 15 | for(int i=0; i<16500; i++) 16 | ans[i]=0; 17 | ans[0]=1; 18 | int m, carry; 19 | for(int i=2; i<=num; i++){ 20 | carry=0; 21 | for(int j=0; j<16500; j++){ 22 | m= i*ans[j]; 23 | m+= carry; 24 | ans[j]=m%10; 25 | carry=m/10; 26 | } 27 | } 28 | int i=16500-1; 29 | while(ans[i]==0 && i>=0){ 30 | i--; 31 | } 32 | printf("\t%d! =", num); 33 | while(i>=0){ 34 | printf("%d", ans[i]); 35 | i--; 36 | } 37 | printf("\n"); 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /last-digit-fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int last_digit_fib_optimized(const int index) 5 | { 6 | int first = 0; 7 | int second = 1; 8 | int current = 0; 9 | 10 | for (int i = 2; i <= index; ++i) { 11 | current = (first + second) % 10; 12 | first = second; 13 | second = current; 14 | } 15 | 16 | return (current); 17 | } 18 | 19 | /* This is the direct solution for reference */ 20 | int last_digit_fib_naive(const int index) 21 | { 22 | int *arr; 23 | arr = (int *)malloc((index+1) * sizeof(int)); 24 | arr[0] = 0; 25 | arr[1] = 1; 26 | 27 | for (int i = 2; i <= index; ++i) { // store only the last digit of each index 28 | arr[i] = (arr[i - 1] + arr[i - 2]) % 10; 29 | } 30 | 31 | int res = arr[index]; 32 | return (res); 33 | } 34 | 35 | int main(int argc, char **argv) 36 | { 37 | int index = 0; 38 | scanf("%d", &index); 39 | 40 | printf("%d\n", last_digit_fib_optimized(index)); 41 | 42 | return 0; 43 | } -------------------------------------------------------------------------------- /lcm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | long get_gcd_euclidian(long d1, long d2) 5 | { 6 | /* swap the numbers if d2 is greater than d1 */ 7 | if (d2 > d1) { 8 | d1 = d1 - d2; 9 | d2 = d1 + d2; 10 | d1 = d2 - d1; 11 | } 12 | 13 | if (d2 == 0) { 14 | return d1; 15 | } 16 | 17 | long rem = d1 % d2; 18 | 19 | return get_gcd_euclidian(d2, rem); 20 | } 21 | 22 | long get_lcm_euclidian(long val1, long val2) 23 | { 24 | if (val1 == 0 || val2 == 0) { 25 | return 0; 26 | } 27 | 28 | long prod = (long) (val1*val2); 29 | long gcd = get_gcd_euclidian(val1, val2); 30 | 31 | return (prod/gcd); 32 | } 33 | 34 | int main(int argc, char **argv) 35 | { 36 | long ip1 = 0; 37 | long ip2 = 0; 38 | scanf("%ld %ld", &ip1, &ip2); 39 | printf("%ld", get_lcm_euclidian(ip1, ip2)); 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /linearsearch.c: -------------------------------------------------------------------------------- 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 2 | #include 3 | 4 | struct node 5 | { 6 | int data; 7 | struct node* next; 8 | }; 9 | typedef struct node NODE; 10 | 11 | //NODE head = NULL; 12 | 13 | 14 | NODE *first, *second, *third; 15 | 16 | int main() 17 | { 18 | (*first).data = 5; 19 | (*first).next = second; 20 | 21 | printf("%d", (*first).data); 22 | } 23 | -------------------------------------------------------------------------------- /linkedlist.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* The program demonstrates a basic linked list implemented using classes */ 4 | 5 | using namespace std; 6 | 7 | class Node 8 | { 9 | public: 10 | int data; 11 | Node *next = NULL; 12 | }; 13 | 14 | class LinkedList 15 | { 16 | public: 17 | Node *head = NULL; 18 | Node *tail = NULL; 19 | }; 20 | 21 | void addNode(LinkedList* list, Node* node) 22 | { 23 | if(list->head == NULL && list->tail == NULL) { // For the first node only 24 | list->tail = node; 25 | list->head = node; 26 | } 27 | else { 28 | list->tail->next = node; 29 | list->tail = node; 30 | } 31 | } 32 | 33 | void display(LinkedList* list) 34 | { 35 | Node *ptr = list->head; 36 | 37 | while (ptr != NULL) { // Loop to traverse the list 38 | cout << ptr->data << " "; 39 | ptr = ptr->next; 40 | } 41 | } 42 | 43 | void search(LinkedList* list, int value) 44 | { 45 | Node *ptr = list->head; 46 | 47 | while(ptr != NULL) { 48 | if(value == ptr->data) { 49 | cout << "Value exists in the list" << endl; 50 | break; 51 | } 52 | ptr = ptr->next; 53 | } 54 | if(ptr == NULL) 55 | cout << "Value does not exist in the list" << endl; 56 | } 57 | 58 | int main() 59 | { 60 | LinkedList example_list; 61 | 62 | int arr[] = {5, 8, -2, 66, 78, 21, 90, 0, 2}; 63 | 64 | for(int i = 0; i < 9; i++) { 65 | Node *temp_node = new Node(); 66 | temp_node->data = arr[i]; 67 | addNode(&example_list, temp_node); 68 | } 69 | 70 | search(&example_list, 78); 71 | search(&example_list, 9); 72 | 73 | display(&example_list); 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /multiply.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { int a,b; 4 | printf("Enter two Numbers : "); 5 | scanf("%d %d",&a,&b); 6 | c=a*b; 7 | printf("The Multiplication is %d",c); 8 | } 9 | -------------------------------------------------------------------------------- /nCrCalculatorLargeNumbers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define m 1000000007 3 | #include 4 | 5 | // This function calcualtes the large powers with O(log(n)) 6 | long fastexp(long x, long y) 7 | { 8 | // In the form of pow(x, y) % m 9 | if(y == 0) 10 | return 1; 11 | else if (y == 1) 12 | return x; 13 | else 14 | { 15 | long ans = fastexp(x, y/2); 16 | if(y % 2 == 0) 17 | return (ans % m * ans % m) % m; 18 | else 19 | return ((ans % m * ans % m) % m * x % m) % m; 20 | } 21 | } 22 | 23 | long fact[2000001] = {1}; 24 | 25 | int main() 26 | { 27 | // FACTORIAL CALCULATION 28 | // Using (a * b) % m = (a % m * b % m) % m 29 | for(int i = 1; i < 2000001; i++) 30 | fact[i] = ((fact[i-1] % m) * (i % m)) % m; 31 | 32 | // Number of test cases 33 | int T; 34 | long n, r, ans; 35 | printf("Enter the number of test cases: "); 36 | scanf("%d", &T); 37 | while(T--) 38 | { 39 | printf("Enter the two numbers in the form of xCy: "); 40 | scanf("%ld%ld", &n, &r); 41 | 42 | // Formula used: (n)!/ (r! * (n-r)!) 43 | // = (n+r)! * pow((r! * (n-r)!), -1) 44 | // Using FERMAT's LITTLE THEOREM (Google it!) 45 | // = (fact[n]%m * pow((r! * (n-r)!), m - 2) % m) % m 46 | // = (fact[n]%m * fastexp((fact[r] * fact[n-r])%m, m - 2)%m)%m 47 | 48 | ans = (fact[n]%m * fastexp((fact[r]%m * fact[n-r]%m) % m, m - 2) % m) % m; 49 | printf("%ld\n", ans); 50 | } 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /oddandeven.c: -------------------------------------------------------------------------------- 1 | // Program to tell if an integer number is even or odd 2 | 3 | #include 4 | 5 | int main(){ 6 | 7 | int number; 8 | printf("Enter an integer number\n"); 9 | scanf("%d",&number); 10 | if (number %2 ==0) 11 | printf("The number %d is even\n",number); 12 | else{ 13 | printf("The number %d is odd\n",number); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /omang_fibonacci.c: -------------------------------------------------------------------------------- 1 | #include 2 | void printFibonacci(int n){ 3 | static int n1=0,n2=1,n3; 4 | if(n>0){ 5 | n3 = n1 + n2; 6 | n1 = n2; 7 | n2 = n3; 8 | printf("%d ",n3); 9 | printFibonacci(n-1); 10 | } 11 | } 12 | int main(){ 13 | int n; 14 | printf("Enter the number of elements: "); 15 | scanf("%d",&n); 16 | printf("Fibonacci Series: "); 17 | printf("%d %d ",0,1); 18 | printFibonacci(n-2); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /pattern.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int row, c, n, s; 6 | 7 | printf("Enter the number of rows in pyramid of stars you wish to see\n"); 8 | scanf("%d", &n); 9 | 10 | s = n; 11 | 12 | for (row = 1; row <= n; row++) // Loop to print rows 13 | { 14 | for (c = 1; c < s; c++) // Loop to print spaces in a row 15 | printf(" "); 16 | 17 | s--; 18 | 19 | for (c = 1; c <= 2*row - 1; c++) // Loop to print stars in a row 20 | printf("*"); 21 | 22 | printf("\n"); 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /priority_queue.c: -------------------------------------------------------------------------------- 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 | } -------------------------------------------------------------------------------- /simple_interest.c: -------------------------------------------------------------------------------- 1 | //Program to Calculate Simple Interest 2 | #include 3 | 4 | #include 5 | 6 | void main() { 7 | float P, R, T, SI; 8 | scanf("%f%f%f", & P, & R, & T); 9 | SI = (P * R * T) / 100; 10 | printf("%f", SI); 11 | getch(); 12 | } 13 | -------------------------------------------------------------------------------- /stack_using_linklist: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /swappingwithoutthirdvariable.c: -------------------------------------------------------------------------------- 1 | //Swap Values Without using Third Variable. 2 | #include 3 | int main() 4 | { 5 | int a,b; /*declaration of variables*/ 6 | printf("Enter Two Numbers\n"); 7 | scanf("%d %d",&a,&b); /*Taking input of Two Variables*/ 8 | a=a+b; /*swapping */ 9 | b=a-b; /* the two */ 10 | a=a-b; /*numbers*/ 11 | printf("the number after swapping are %d %d\n",a,b);/*printing*/ 12 | return 0; 13 | } -------------------------------------------------------------------------------- /to find average & sum of a,b,c in one function: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int a,b,c,sum,average; 5 | printf("enter the values of a,b,c"); 6 | scanf("%d",&a); 7 | scanf("%d",&b); 8 | scanf("%d",&c); 9 | sum = a+b+c; 10 | printf("%d\n",sum); 11 | { 12 | average = (a+b+c)%3; 13 | printf("%d\n",average); 14 | }} 15 | -------------------------------------------------------------------------------- /transposeOfMatrix.c: -------------------------------------------------------------------------------- 1 | //C program to input a matrix of order MxN and find its transpose 2 | 3 | #include 4 | #include 5 | int main() 6 | { 7 | static int array[10][10]; 8 | int i, j, row, col; 9 | 10 | printf("Enter the order of the matrix \n"); 11 | scanf("%d %d", &row, &col); 12 | printf("Enter the coefficients of the matrix\n"); 13 | for (i = 0; i < row; ++i) 14 | { 15 | for (j = 0; j < col; ++j) 16 | { 17 | scanf("%d", &array[i][j]); 18 | } 19 | } 20 | printf("The given matrix is \n"); 21 | for (i = 0; i < row; ++i) 22 | { 23 | for (j = 0; j < col; ++j) 24 | { 25 | printf(" %d", array[i][j]); 26 | } 27 | printf("\n"); 28 | } 29 | printf("Transpose of matrix is \n"); 30 | for (j = 0; j < col; ++j) 31 | { 32 | for (i = 0; i < row; ++i) 33 | { 34 | printf(" %d", array[i][j]); 35 | } 36 | printf("\n"); 37 | } 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /volume_cone_sphere.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define pi 3.1415 5 | 6 | int main() 7 | { 8 | 9 | float raio, altura, volume, area; 10 | char tipo[10]; 11 | 12 | scanf("%f",&raio); 13 | scanf("%f",&altura); 14 | scanf("%s",tipo); 15 | 16 | if (strcmp(tipo,"CONE")==0){ 17 | volume = ((pi * pow(raio,2)) * altura) / 3; 18 | area = (pi * raio) * sqrt(pow(raio,2) + pow(altura,2)); 19 | printf("VOLUME = %.2f\n",volume); 20 | printf("AREA = %.2f\n",area); 21 | } 22 | 23 | else if (strcmp(tipo,"CILINDRO")==0){ 24 | volume = ((pi * pow(raio,2)) * altura); 25 | area = (2 * pi) * (raio * altura); 26 | printf("VOLUME = %.2f\n",volume); 27 | printf("AREA = %.2f\n",area); 28 | } 29 | 30 | else if (strcmp(tipo,"ESFERA")==0){ 31 | volume = (pi * 4 * pow(raio,3)) / 3; 32 | area = (4 * (pi * pow(raio,2))); 33 | printf("VOLUME = %.2f\n",volume); 34 | printf("AREA = %.2f\n",area); 35 | } 36 | 37 | return 0; 38 | } --------------------------------------------------------------------------------