From 0eed935c361a2e6d1e4ba4cc9e93b6afe1144b13 Mon Sep 17 00:00:00 2001 From: Manaarth Date: Mon, 7 Oct 2019 00:21:29 +0530 Subject: [PATCH 001/109] Added the file Finding Factorial of an Integer.c --- Finding Factorial of an Integer.c | 22 ++++++++++++++++++++++ RecursiveFactorial.c | 2 +- a.out | Bin 0 -> 8464 bytes 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 Finding Factorial of an Integer.c create mode 100755 a.out diff --git a/Finding Factorial of an Integer.c b/Finding Factorial of an Integer.c new file mode 100644 index 0000000..5cdcc56 --- /dev/null +++ b/Finding Factorial of an Integer.c @@ -0,0 +1,22 @@ +#include + +//Defining a recurcive function to calculate factorial +long double factorial(long double n) +{ + if(n==0) + return 1; + else if(n==1) + return 1; + else + return factorial(n-1)*n; +} +int main() +{ + long double n; + //Taking input from the user + printf("Enter a non-negative Integer: "); + scanf("%Lf",&n); + //Printing the answer + printf("The factorial of %.0Lf is: %.0Lf\n",n,factorial(n)); + return 0; +} diff --git a/RecursiveFactorial.c b/RecursiveFactorial.c index 4308b9d..6b966bf 100644 --- a/RecursiveFactorial.c +++ b/RecursiveFactorial.c @@ -16,4 +16,4 @@ int main() { printf("%d! is equal to %d", number, factorial(number)); return 0; -} \ No newline at end of file +} diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..5fe2fc7e81dfdd2ea817b4b1708096c09e20e79d GIT binary patch literal 8464 zcmeHMYiv~45kBj+!6dL=2nh~(-BOUCtv5E|VUm*U`o(o*ASEV`CgkRNANDr;pu2Yy zKvjzhiMk?AP?aA+N~=hfD*E_At4KxpQzKUh)Ji4l3qnXJC6!4@>n07NX=8Wh-1&Cz zUay;0YX9X(yL09{Gjrz5dF-6CU+W#{YX}4sN>JUcmscSaMkUZ^w7FBw`USU_nw+T1=!o;tp&$2qWqPWI6jO&zTrHlFa zh7DFRZs$_O79BhOgcToo!b;icjM`a9=N!i4yKF0!&e@sto+Pnsfn^Jhm9^73wP~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YzY~%KM84rs%X(_H!*BRtK?=Jq!*xMLXv}@&lR*Sc)a9tdy zR#lq!U!(f#bQN)HeEv01+@ijCh6La1!#^cB&QsTlQtPP_pi>I|V88cid}bW7Vh86l z?CjR^GvoOp;r0BVZCtNvJB!o+4pek7i=7j?{kj?Rbw=BnIbRn<9={8krLPZkW#;;6 zCPxEx=J!^@7g7B8$~we0ju39ZsE7Vg<1^R8y<%sn9Ow^<4(H%uT0fW-Hyv8&$KF+Ht}cHY`y%Ptouz#>o=)sr+oH3xspr!V0j<^A!Q68SGnJP(QeAWcxc_o`b6XaD_nRuOK9u9tKZ zzRU|q{j%=2&UqLwI7O$FN=4$T`sQRgSu4)3PDOfgO60BKOn%7DSP8n;6)n58OU3iq zotdPQOhneN+pxaA5Wj(?ExS;#cU#GvQ`oIi1v{Ix5~XZ*H>r3yizJ;I(Y|ypkcCs8|#;`aQDt zU9;LLPb)n>BEbAAS@)^2_QA8N&tp=N;_j?t4-s_=E*(LQUQ3gOohp*cJIP3_tG~^$ zhlLx?l_Eo>G`(i06Dq>oh+P~}k;LvC*>kB=a9f^C7K-V7u75e^~+yIwpeX-#`BS zqTeNQ$df@$kNyo*_v=4L7{^3@-YWSt@@o1et|{VP1uIj{tB<@JG+L2#EBIdm^aY=O z#Sw7`{=y7+&~N$l@johP0f7|*=oj|B76q+cF9=x<23#iNh+j3e?O0yz@V z1w82Q3FWCq|G3}a|6lyii}eXT%p>RbJVfZ@9(0(UqVkYSD&NoteTQ1S`jxy>IzT@# z06p-NRACJLBaaJ%O9Xju4tQ`i0LLd1inxXHdSbLCR8kPRrC2;&Z!?xfL&QD(7 h@SUoavXc)BDwfYU`gL15$F8scOX*+=f Date: Mon, 7 Oct 2019 01:46:51 +0530 Subject: [PATCH 002/109] Added the file Palindrome.c --- Finding Factorial of an Integer.c | 22 ---------------------- Palindrome.c | 29 +++++++++++++++++++++++++++++ a.out | Bin 8464 -> 8488 bytes 3 files changed, 29 insertions(+), 22 deletions(-) delete mode 100644 Finding Factorial of an Integer.c create mode 100644 Palindrome.c diff --git a/Finding Factorial of an Integer.c b/Finding Factorial of an Integer.c deleted file mode 100644 index 5cdcc56..0000000 --- a/Finding Factorial of an Integer.c +++ /dev/null @@ -1,22 +0,0 @@ -#include - -//Defining a recurcive function to calculate factorial -long double factorial(long double n) -{ - if(n==0) - return 1; - else if(n==1) - return 1; - else - return factorial(n-1)*n; -} -int main() -{ - long double n; - //Taking input from the user - printf("Enter a non-negative Integer: "); - scanf("%Lf",&n); - //Printing the answer - printf("The factorial of %.0Lf is: %.0Lf\n",n,factorial(n)); - return 0; -} diff --git a/Palindrome.c b/Palindrome.c new file mode 100644 index 0000000..e76683f --- /dev/null +++ b/Palindrome.c @@ -0,0 +1,29 @@ +#include +#include +long long int max=1e6 +5; +int main() +{ + //Defining a string to take input of the number + char number[max]; + + long long int len,i,count=0; + + //Taking the inputin form of a string + printf("Enter a Number: "); + scanf("%s",number); + + //Finding the number of digits by calculating the lenght of the string + len=strlen(number); + + //Checking if the number is a palindrome or not + for(i=0;iu6~FV>PW?^l`1^J2eB-pPzbUvQAmX|-B!y(D(}ta;4#H@FjX~qhrU|5h zq>fa^s-!Yw^0G`}Z$b!($z!WXD-fwFp>}_)?hk0GsFeIOkj5X@M3J*HsnMpj>k_F7O#`X#!rPrF@iL;uDq$nY_04UDfKJO7C zQd=;J9&l9f2{5T;&PuCUX*CPm=$@t}y<)SPc2r=}0VnArP5&!Gzl_ii&5o!1H$JpJ z|4C?i%hfXnJq+^>%e4)| zZG?I1hu5@N)&9)rP<1}e5mxeDH?3;3d(yC zmRG&Dpd52Ur_1ulBntEdBoQe{8}3_!eJ*R9Z&Z!BTeLE|P_gV+ZZ_#!)@{OmR>(NY zb1Zw#eO4_a<<_@RRgU}tXK#2ra$FwZ`h{W{N90W|^TJq|6Y{UQ%=0w9P!PZ`IG}NZ zSs`-=&rYK(M(pp2-25{v_g9wv;%e{iT~n?bxR_0OvVwGeiB{#nHv9az&Oq zi$M?J8aCBiX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dcb409304c374140e845bfd2 Mon Sep 17 00:00:00 2001 From: Ayan Raj Date: Mon, 7 Oct 2019 02:39:10 +0530 Subject: [PATCH 003/109] Added solution HackerRank Diagonal Difference problem solution --- Diagonal-Difference.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Diagonal-Difference.c diff --git a/Diagonal-Difference.c b/Diagonal-Difference.c new file mode 100644 index 0000000..70863ad --- /dev/null +++ b/Diagonal-Difference.c @@ -0,0 +1,36 @@ +#include +#include +int main() +{ + int n,i,j,sum1=0,sum2=0; + + scanf("%d", &n); + int a[n][n]; + for (i=0;i=-100&&a[i][j]<=100) + { + if(i==j) + { + sum1+=a[i][j]; + } + if(j==(n-1-i)) + { + sum2+=a[i][j]; + } + } + } + } + if((sum1-sum2)<0) + { + printf("%d", (-((sum1)-(sum2)))); + } + else + { + printf("%d", ((sum1)-(sum2))); + } + return 0; +} From a13315ac7a0311cf4821fe5f12865ff24385eeed Mon Sep 17 00:00:00 2001 From: Sarvesh Nath Tiwari <43759182+sarveshsrv@users.noreply.github.com> Date: Mon, 7 Oct 2019 09:48:32 +0530 Subject: [PATCH 004/109] Added Swapping(without using extra variable).cpp This algo swaps two numbers without using any extra space or variable(By Sarvesh). --- Swapping(without using extra variable).cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Swapping(without using extra variable).cpp diff --git a/Swapping(without using extra variable).cpp b/Swapping(without using extra variable).cpp new file mode 100644 index 0000000..cf03d3c --- /dev/null +++ b/Swapping(without using extra variable).cpp @@ -0,0 +1,13 @@ +#include +#include +void main() +{ + int a,b; + printf("Enter a and b\n"); + scanf("%d%d",&a,&b); + a=a+b; + b=a-b; + a=a-b; + printf("After swapping a and b are %d %d",a,b); + getch(); +} From 2f110ea6d195e55d3c5a6abcc439651a89077a2c Mon Sep 17 00:00:00 2001 From: Sarvesh Nath Tiwari <43759182+sarveshsrv@users.noreply.github.com> Date: Mon, 7 Oct 2019 09:53:39 +0530 Subject: [PATCH 005/109] Added LenghtOfString(without using strlen).cpp This algo finds the length of any string without using any inbuilt function or library (By Sarvesh). --- LenghtOfString(without using strlen).cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 LenghtOfString(without using strlen).cpp diff --git a/LenghtOfString(without using strlen).cpp b/LenghtOfString(without using strlen).cpp new file mode 100644 index 0000000..1db6f42 --- /dev/null +++ b/LenghtOfString(without using strlen).cpp @@ -0,0 +1,23 @@ +#include +#include +slength(char name[30]); +void main() +{ + int len; + char name[30]; + printf("Enter any string\n"); + gets(name); + len=slength(name); + printf("lenght of '%s' is %d\n",name,len); + getch(); +} +slength(char name[30]) +{ + int i=0,count=0; + while(name[i]!='\0') + { + i++; + count++; + } + return count; +} From 673b44b87007a5b219aebf697231be7e18ec2c46 Mon Sep 17 00:00:00 2001 From: Shubham Choudhary <43514094+skcshubham@users.noreply.github.com> Date: Mon, 7 Oct 2019 14:15:21 +0530 Subject: [PATCH 006/109] Anagram-Program-in-C This is he code to check whether string is anagram. Anagram is a word, phrase, or name formed by rearranging the letters of another, such as spar, formed from rasp. --- Anagram-Program-in-C | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Anagram-Program-in-C diff --git a/Anagram-Program-in-C b/Anagram-Program-in-C new file mode 100644 index 0000000..63c503f --- /dev/null +++ b/Anagram-Program-in-C @@ -0,0 +1,52 @@ +#include + +int check_anagram(char [], char []); + +int main() +{ + char a[100], b[100]; + + printf("Enter a string\n"); + gets(a); + + printf("Enter a string\n"); + gets(b); + + if (check_anagram(a, b) == 1) + printf("The strings are anagrams.\n"); + else + printf("The strings aren't anagrams.\n"); + + return 0; +} + +int check_anagram(char a[], char b[]) +{ + int first[26] = {0}, second[26] = {0}, c=0; + + // Calculating frequency of characters of first string + + while (a[c] != '\0') + { + first[a[c]-'a']++; + c++; + } + + c = 0; + + while (b[c] != '\0') + { + second[b[c]-'a']++; + c++; + } + + // Comparing frequency of characters + + for (c = 0; c < 26; c++) + { + if (first[c] != second[c]) + return 0; + } + + return 1; +} From d6b9785c42c9180ddd3352d6a0aa8c8a131d2e99 Mon Sep 17 00:00:00 2001 From: Shubham Choudhary <43514094+skcshubham@users.noreply.github.com> Date: Mon, 7 Oct 2019 14:43:24 +0530 Subject: [PATCH 007/109] checkAnagram.c fgets() added to the program instead of gets() function. The code runs without breaking and has proper comments. Thanks! --- Anagram-Program-in-C | 109 +++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/Anagram-Program-in-C b/Anagram-Program-in-C index 63c503f..6851084 100644 --- a/Anagram-Program-in-C +++ b/Anagram-Program-in-C @@ -1,52 +1,71 @@ -#include - -int check_anagram(char [], char []); - +#include +#include +#include + +int checkAnagram(char *str1, char *str2); + int main() { - char a[100], b[100]; - - printf("Enter a string\n"); - gets(a); + char str1[100], str2[100]; + + printf("Function : whether two given strings are anagram :"); + printf("\nExample : pears and spare, stone and tones :"); + + printf(" Input the first String : "); + fgets(str1, sizeof str1, stdin); + printf(" Input the second String : "); + fgets(str2, sizeof str2, stdin); - printf("Enter a string\n"); - gets(b); - - if (check_anagram(a, b) == 1) - printf("The strings are anagrams.\n"); - else - printf("The strings aren't anagrams.\n"); - - return 0; + if(checkAnagram(str1, str2) == 1) + { + str1[strlen(str1)-1] = '\0'; + str2[strlen(str2)-1] = '\0'; + printf(" %s and %s are Anagram.\n\n",str1,str2); + } + else + { + str1[strlen(str1)-1] = '\0'; + str2[strlen(str2)-1] = '\0'; + printf(" %s and %s are not Anagram.\n\n",str1,str2); + } + return 0; } -int check_anagram(char a[], char b[]) + +//Function to check whether two passed strings are anagram or not + +int checkAnagram(char *str1, char *str2) { - int first[26] = {0}, second[26] = {0}, c=0; - - // Calculating frequency of characters of first string - - while (a[c] != '\0') - { - first[a[c]-'a']++; - c++; - } - - c = 0; - - while (b[c] != '\0') - { - second[b[c]-'a']++; - c++; - } - - // Comparing frequency of characters - - for (c = 0; c < 26; c++) - { - if (first[c] != second[c]) - return 0; - } - - return 1; + int str1ChrCtr[256] = {0}, str2ChrCtr[256] = {0}; + int ctr; + + /* check the length of equality of Two Strings */ + + if(strlen(str1) != strlen(str2)) + { + return 0; + } + + //count frequency of characters in str1 + + for(ctr = 0; str1[ctr] != '\0'; ctr++) + { + str1ChrCtr[str1[ctr]]++; + } + + //count frequency of characters in str2 + + for(ctr = 0; str2[ctr] != '\0'; ctr++) + { + str2ChrCtr[str2[ctr]]++; + } + + //compare character counts of both strings + + for(ctr = 0; ctr < 256; ctr++) + { + if(str1ChrCtr[ctr] != str2ChrCtr[ctr]) + return 0; + } + return 1; } From cd3b42695db1acf124d86aa482782e98a1b7529c Mon Sep 17 00:00:00 2001 From: sudhakar-mnsr Date: Mon, 7 Oct 2019 16:12:42 +0530 Subject: [PATCH 008/109] Added endian.c which identifies machine is Little/Big Endian --- README.md | 1 + endian.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 endian.c diff --git a/README.md b/README.md index e1d81e1..7b633b4 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ These program are written in codeblocks ide for windows. These programs are not - [Stack implemenation of linklist](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Stack%20-%20Linked%20List.c) - [Swap integers without 3rd variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapIntegers.c) - [Swap value without third variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapValueWithoutUsingThirdVariable.c) +- [Identify machine is big-endian or little-endian] (https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/endian.c) diff --git a/endian.c b/endian.c new file mode 100644 index 0000000..0a53909 --- /dev/null +++ b/endian.c @@ -0,0 +1,34 @@ +/*************************************************************************************/ +// Since size of character is 1 byte when the character pointer is de-referenced +// it will contain only first byte of integer. +// If machine is little endian then *c will be 1 (because last byte is stored first) +// if machine is big endian then *c will be 0. + +// higher memory +// -----> +// +----+----+----+----+ +// |0x01|0x00|0x00|0x00| +// +----+----+----+----+ +// c +// | +// &i + +// +----+----+----+----+ +// |0x00|0x00|0x00|0x01| +// +----+----+----+----+ +// c +// | +// &i +/*************************************************************************************/ + +#include +int main() +{ + unsigned int i = 1; + char *c = (char*)&i; + if (*c) + printf("Little endian\n"); + else + printf("Big endian\n"); + return 0; +} From bfaace9ed7e5f79b41717a8397732468dc5d2399 Mon Sep 17 00:00:00 2001 From: Ayan Raj Date: Mon, 7 Oct 2019 16:21:45 +0530 Subject: [PATCH 009/109] Updated --- Diagonal-Difference.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Diagonal-Difference.c b/Diagonal-Difference.c index 70863ad..3f96562 100644 --- a/Diagonal-Difference.c +++ b/Diagonal-Difference.c @@ -34,3 +34,15 @@ int main() } return 0; } + +/* +Sample Input +4 +-1 1 -7 -8 +-10 -8 -5 -2 +0 9 7 -1 +4 4 -2 1 + +Output +1 +*/ From 804ea063b27d815909839f2b12445549f88cb0ca Mon Sep 17 00:00:00 2001 From: gourav thakur Date: Mon, 7 Oct 2019 16:23:07 +0530 Subject: [PATCH 010/109] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7b633b4..9c887dc 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ These program are written in codeblocks ide for windows. These programs are not - [Stack implemenation of linklist](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Stack%20-%20Linked%20List.c) - [Swap integers without 3rd variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapIntegers.c) - [Swap value without third variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapValueWithoutUsingThirdVariable.c) -- [Identify machine is big-endian or little-endian] (https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/endian.c) +- [Identify machine is big-endian or little-endian](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/endian.c) From 0814062f017fbcb864fd83bf364b89af451b6b42 Mon Sep 17 00:00:00 2001 From: Ayan Raj Date: Mon, 7 Oct 2019 16:40:32 +0530 Subject: [PATCH 011/109] Updated --- Diagonal-Difference.c | 47 ++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/Diagonal-Difference.c b/Diagonal-Difference.c index 3f96562..adf5611 100644 --- a/Diagonal-Difference.c +++ b/Diagonal-Difference.c @@ -2,28 +2,30 @@ #include int main() { - int n,i,j,sum1=0,sum2=0; + int n,i,j,sum1=0,sum2=0; //n denotes the number of rows and columns in the matrix arr. scanf("%d", &n); - int a[n][n]; + int arr[n][n]; for (i=0;i=-100&&a[i][j]<=100) + scanf("%d ", &arr[i][j]); + //Taking diagonal sum of the matrix arr from the both side + if(arr[i][j]>=-100 && arr[i][j]<=100) { if(i==j) { - sum1+=a[i][j]; + sum1+=arr[i][j]; } if(j==(n-1-i)) { - sum2+=a[i][j]; + sum2+=arr[i][j]; } } } } + // This code part belongs to the absolute difference between the sums of the matrix's along two diagonals if((sum1-sum2)<0) { printf("%d", (-((sum1)-(sum2)))); @@ -37,12 +39,29 @@ int main() /* Sample Input -4 --1 1 -7 -8 --10 -8 -5 -2 -0 9 7 -1 -4 4 -2 1 - -Output -1 + +3 +11 2 4 +4 5 6 +10 8 -12 +Sample Output + +15 + +Explanation + +The primary diagonal is: + +11 + 5 + -12 +Sum across the primary diagonal: 11 + 5 - 12 = 4 + +The secondary diagonal is: + + 4 + 5 +10 +Sum across the secondary diagonal: 4 + 5 + 10 = 19 +Difference: |4 - 19| = 15 */ From ce7b8a04865cb5b36b3f024ea2063cc2b7213065 Mon Sep 17 00:00:00 2001 From: Sarvesh Nath Tiwari <43759182+sarveshsrv@users.noreply.github.com> Date: Mon, 7 Oct 2019 17:43:52 +0530 Subject: [PATCH 012/109] Added Perfect Number.cpp This algo checks whether a given no. is perfect or not. A perfect no. is a positive no. which is equal to the sum of its positive divisors.As: 6,28,496,8128 --- PerfectNumber.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 PerfectNumber.cpp diff --git a/PerfectNumber.cpp b/PerfectNumber.cpp new file mode 100644 index 0000000..2124b97 --- /dev/null +++ b/PerfectNumber.cpp @@ -0,0 +1,19 @@ +#include +#include +int main () +{ + int n,i,sum=0; + printf("enter n\n"); + scanf("%d",&n); + for (i=1;i Date: Mon, 7 Oct 2019 18:16:04 +0530 Subject: [PATCH 013/109] Add lcm calculator --- lcm.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 lcm.c diff --git a/lcm.c b/lcm.c new file mode 100644 index 0000000..87dd180 --- /dev/null +++ b/lcm.c @@ -0,0 +1,42 @@ +#include +#include + +long get_gcd_euclidian(long d1, long d2) +{ + /* swap the numbers if d2 is greater than d1 */ + if (d2 > d1) { + d1 = d1 - d2; + d2 = d1 + d2; + d1 = d2 - d1; + } + + if (d2 == 0) { + return d1; + } + + long rem = d1 % d2; + + return get_gcd_euclidian(d2, rem); +} + +long get_lcm_euclidian(long val1, long val2) +{ + if (val1 == 0 || val2 == 0) { + return 0; + } + + long prod = (long) (val1*val2); + long gcd = get_gcd_euclidian(val1, val2); + + return (prod/gcd); +} + +int main(int argc, char **argv) +{ + long ip1 = 0; + long ip2 = 0; + scanf("%ld %ld", &ip1, &ip2); + printf("%ld", get_lcm_euclidian(ip1, ip2)); + + return 0; +} \ No newline at end of file From 2efe70f84e6ac8e1a0e76b3102a386cee7637874 Mon Sep 17 00:00:00 2001 From: Pulkit555 Date: Mon, 7 Oct 2019 18:29:23 +0530 Subject: [PATCH 014/109] Added a Cprogram changing the base of a no. --- Changingbase.c | 39 +++++++++++++++++++++++++++++++++++++++ a | Bin 8456 -> 8496 bytes 2 files changed, 39 insertions(+) create mode 100644 Changingbase.c diff --git a/Changingbase.c b/Changingbase.c new file mode 100644 index 0000000..fa8ee17 --- /dev/null +++ b/Changingbase.c @@ -0,0 +1,39 @@ +#include +#include //used because of predefined power function + +int main() +{ + long long int n,x,y,b,j=-1,c,d,e,a=0,k=-1,f=0; + + printf("This program converts the no. from one base to another\n"); + + printf("enter the no. you want to convert\n"); + scanf("%lld",&n); + + printf("enter the base of no.\n"); + scanf("%lld",&x); + + printf("enter the base to which you want to convert\n"); + scanf("%lld",&y); + + while(n>0) + { + b=n%10; + n=n/10; + j++; + d=b*pow(x,j); + a=d+a;//converted the no. to base 10 + } + + while(a>0) + { + c=a%y; + a=a/y; + k++; + e=c*pow(10,k); + f=e+f;//converted the no. to base user wanted + } + + printf("%lld\n",f); + return 0; +} \ No newline at end of file diff --git a/a b/a index 821fd59f42e0ce98de2cd26beacf0f47e80dddd2..4b0d909683954edb7eac6ac14c5ef0e099d7e3b3 100755 GIT binary patch delta 3125 zcmZ`*eQZ5*1P9a5Ook+OfXp>?d%q?dML>B?5~*I2bb(yAbbwNe^wYt}-&bMLz^ zOHy$q-*e9IoO{lB=iCpE?VjG9UFXf05-~z1Aq(f5FNH@cOv$2z=mMZPbNjJMAtKcU zvrKI!o&_(}yg6x=lU6y{MUVJ`<)+R5DO{$9H{8$Vh;%su&9?7X=Of>_bIO=_^+y}; ze02Nyt{op{UQR*Iwx1pcs%Ugb%%gmU5oH~n5L^(s!-DHZ?x%2G=~o65!*Ev#mJSZZ z;>kXEuwMbpv>)G74lG%a!N z(gqjM`ntWM(pZ6(z+dbdwZkp@w$uexC+sIi(pJo~(LXxgT6R?2@i3~2O^@L04up{7 z@&MP*&lPbrZRaxI7V~)_Z{sqr%agC;GAoh|%_OXZprr zmi~Z^ez>sz;NEfjv$&XzS1N*Z+(xTn&25KH{^bsQgKIie^w7n|?G!27wZzi5l75!4 zzu|!ErCZdu(Ugtvc+&xQO9S)_)R%2;K8+YT*~&&cp5$B?UsAy`9p6K#P>E?37*5yh zkHZhkHm3%ASf-?DD+Xri$WoiZ{uH^&Q%7nbNRZA-IWOe#j&3YK;lLMYGv0BI^N$HW zGRe}{5PAintK77ZFl}DsHk(YFDbr@yvPl)bWg5M}jf(fU)5x@GbigtyY(bm(OFWy* zz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program to change base of a no. --- ChangingBase.c | 39 +++++++++++++++++++++++++++++++++++++++ a | Bin 8496 -> 8496 bytes 2 files changed, 39 insertions(+) create mode 100644 ChangingBase.c diff --git a/ChangingBase.c b/ChangingBase.c new file mode 100644 index 0000000..fa8ee17 --- /dev/null +++ b/ChangingBase.c @@ -0,0 +1,39 @@ +#include +#include //used because of predefined power function + +int main() +{ + long long int n,x,y,b,j=-1,c,d,e,a=0,k=-1,f=0; + + printf("This program converts the no. from one base to another\n"); + + printf("enter the no. you want to convert\n"); + scanf("%lld",&n); + + printf("enter the base of no.\n"); + scanf("%lld",&x); + + printf("enter the base to which you want to convert\n"); + scanf("%lld",&y); + + while(n>0) + { + b=n%10; + n=n/10; + j++; + d=b*pow(x,j); + a=d+a;//converted the no. to base 10 + } + + while(a>0) + { + c=a%y; + a=a/y; + k++; + e=c*pow(10,k); + f=e+f;//converted the no. to base user wanted + } + + printf("%lld\n",f); + return 0; +} \ No newline at end of file diff --git a/a b/a index 4b0d909683954edb7eac6ac14c5ef0e099d7e3b3..cb719d1a148556bd2fd12672b9f91bbe586b742c 100755 GIT binary patch delta 14 Vcmdnsw83eEhB%|sW=(N+E&wMn1RDSV delta 14 Vcmdnsw83eEhB#x=W=(N+E&wO-1Umo# From 271f24642fc196fce13e1ce079c4c77413af7745 Mon Sep 17 00:00:00 2001 From: Sarvesh Nath Tiwari <43759182+sarveshsrv@users.noreply.github.com> Date: Mon, 7 Oct 2019 18:37:26 +0530 Subject: [PATCH 016/109] Added PerfectNumber.c This algo checks whether a given no. is perfect or not. A perfect no. is a positive no. which is equal to the sum of its positive divisors.As: 6,28,496,8128 --- PerfectNumber.cpp => PerfectNumber.c | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename PerfectNumber.cpp => PerfectNumber.c (100%) diff --git a/PerfectNumber.cpp b/PerfectNumber.c similarity index 100% rename from PerfectNumber.cpp rename to PerfectNumber.c From cd44f887594190b128e09ebcd5ccd987fb72721e Mon Sep 17 00:00:00 2001 From: Sarvesh Nath Tiwari <43759182+sarveshsrv@users.noreply.github.com> Date: Mon, 7 Oct 2019 18:43:48 +0530 Subject: [PATCH 017/109] Delete PerfectNumber.c --- PerfectNumber.c | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 PerfectNumber.c diff --git a/PerfectNumber.c b/PerfectNumber.c deleted file mode 100644 index 2124b97..0000000 --- a/PerfectNumber.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include -int main () -{ - int n,i,sum=0; - printf("enter n\n"); - scanf("%d",&n); - for (i=1;i Date: Mon, 7 Oct 2019 19:00:55 +0530 Subject: [PATCH 018/109] Added Perfect Num.c This algo checks whether a given no. is perfect or not. A positive no. is said to be perfect if it i equal to the sum of its positive divisors.As: 6,28,496,8128,etc --- PerfectNumber.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 PerfectNumber.c diff --git a/PerfectNumber.c b/PerfectNumber.c new file mode 100644 index 0000000..2124b97 --- /dev/null +++ b/PerfectNumber.c @@ -0,0 +1,19 @@ +#include +#include +int main () +{ + int n,i,sum=0; + printf("enter n\n"); + scanf("%d",&n); + for (i=1;i Date: Mon, 7 Oct 2019 19:16:11 +0530 Subject: [PATCH 019/109] Resolved the conflict --- a | Bin 8496 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100755 a diff --git a/a b/a deleted file mode 100755 index cb719d1a148556bd2fd12672b9f91bbe586b742c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8496 zcmeHMZ){uD6~B&~HleMPmKI9O*0(}M%T_ljX`2EWaT2#LTT)7!z}N_{PHZO@j-A=h zG^xhu6sYh*AVbquG$BQr7;O8}O&W`kra1(4K}=fsFs);o)FD7l2U=DJ1UBmZ&VBbh zKfgGDw0+!_oO93b{JH-#Vfujh?`q!AfG>b-2jlZ7} z*9uZwAu&tdY!OJUvZ!1$tx~ubl1)YnJW zN)}X;6`QJlOeo5B#FE_^Wp_r|F%_(@D36cEM!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i!Ln=?G@1I5?VX@V)7@awA^}F{h3v z$6}+jxOmd!nWwkTvJaT0&yYoX8l>>2M+VJMFQ}Qa=m6L7h8al>2|q5H5r3eieU};P zS6qK0#(*ppiHFEQ)rR7x@XHDC zqw4QZf;A(<_)EbGrKDU%e`G*~=Rg=vn6pf+IJGRejut|Lu|D*Pg20ct6@KiILBxG= z5B@hbx<_COQQmpbH2#*dRA1_ zZQlm`J>>4(KIfH8x&M6M;rjx+J&X#aPHaD=Dom?ra!@F(qbl3;J>*VcbZ20D&QqDb z44JbicQ!=-H=w%-+jBn4^eUyv{bxO<_d!l~9p*XjWqMrMQ~yba`>$Ye5*3+2zw5HkDSM{eUv|Sh)2Cea{Jm!SgzG}d?Xtfgy6pMB&y?@ItnVKGkCi>IALkuR zb8M(6_nYCLp+fV=_U`{v-pY!Ky4(M?%bxEcOnLvX8+ZFJK|nb@PZQ^@{Qpb0KJ!d} z58E1tJ->fUs0S3*gNiK7GyN-2T5@im??3$iVV)Qj7IysdI#T{m^Jm-FXyEY9LY+bx zu!#Ma+huwY5~sbLf2sz!9d^KW%rBrq7`M;)T&*&6>r-3KV&(sM({zHUvpwgXEM|Kr z<=9~zqRpy(T}>Q)XUG@#htCg+Z|*y)c6me{d=Zy(ZkH9;p;FHNZPmbKR#en&{~uV+ BjR^n% From 57c70cc768a47591484c890ba5ac3d0f9c159c0e Mon Sep 17 00:00:00 2001 From: venkat-abhi Date: Mon, 7 Oct 2019 19:26:59 +0530 Subject: [PATCH 020/109] add gcd --- gcd.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 gcd.c diff --git a/gcd.c b/gcd.c new file mode 100644 index 0000000..c7b962e --- /dev/null +++ b/gcd.c @@ -0,0 +1,34 @@ +#include +#include + +long long get_gcd_recursive(long long a, long long b) +{ + if (a == 0) { + return b; + } else if (b == 0) { + return a; + } + + /* Swap the two numbers */ + if (b > a) { + a = a - b; + b = a + b; + a = b - a; + } + + long long remainder = a % b; + if (remainder == 0) { + return b; + } + + return get_gcd_recursive(b, remainder); +} + +int main() +{ + long long a = 0; + long long b = 0; + scanf("%lld", &a); + scanf("%lld", &b); + printf("%lld", get_gcd_recursive(a, b)); +} \ No newline at end of file From e904554c86618d68e9699988406bc4cebb0a8cb3 Mon Sep 17 00:00:00 2001 From: Pulkit555 Date: Mon, 7 Oct 2019 19:30:09 +0530 Subject: [PATCH 021/109] Deleted the extra file --- ChangingBase.c | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 ChangingBase.c diff --git a/ChangingBase.c b/ChangingBase.c deleted file mode 100644 index fa8ee17..0000000 --- a/ChangingBase.c +++ /dev/null @@ -1,39 +0,0 @@ -#include -#include //used because of predefined power function - -int main() -{ - long long int n,x,y,b,j=-1,c,d,e,a=0,k=-1,f=0; - - printf("This program converts the no. from one base to another\n"); - - printf("enter the no. you want to convert\n"); - scanf("%lld",&n); - - printf("enter the base of no.\n"); - scanf("%lld",&x); - - printf("enter the base to which you want to convert\n"); - scanf("%lld",&y); - - while(n>0) - { - b=n%10; - n=n/10; - j++; - d=b*pow(x,j); - a=d+a;//converted the no. to base 10 - } - - while(a>0) - { - c=a%y; - a=a/y; - k++; - e=c*pow(10,k); - f=e+f;//converted the no. to base user wanted - } - - printf("%lld\n",f); - return 0; -} \ No newline at end of file From d29d92cabd15f9375c2ce96f2a32085158bab62e Mon Sep 17 00:00:00 2001 From: venkat-abhi Date: Mon, 7 Oct 2019 19:49:57 +0530 Subject: [PATCH 022/109] add calculator for last digit of nth fibonacci number --- last-digit-fibonacci.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 last-digit-fibonacci.c diff --git a/last-digit-fibonacci.c b/last-digit-fibonacci.c new file mode 100644 index 0000000..bc665d5 --- /dev/null +++ b/last-digit-fibonacci.c @@ -0,0 +1,43 @@ +#include +#include + +int last_digit_fib_optimized(const int index) +{ + int first = 0; + int second = 1; + int current = 0; + + for (int i = 2; i <= index; ++i) { + current = (first + second) % 10; + first = second; + second = current; + } + + return (current); +} + +/* This is the direct solution for reference */ +int last_digit_fib_naive(const int index) +{ + int *arr; + arr = (int *)malloc((index+1) * sizeof(int)); + arr[0] = 0; + arr[1] = 1; + + for (int i = 2; i <= index; ++i) { // store only the last digit of each index + arr[i] = (arr[i - 1] + arr[i - 2]) % 10; + } + + int res = arr[index]; + return (res); +} + +int main(int argc, char **argv) +{ + int index = 0; + scanf("%d", &index); + + printf("%d\n", last_digit_fib_optimized(index)); + + return 0; +} \ No newline at end of file From 99503c857b17526cc793859cd2f606c508bfb3d0 Mon Sep 17 00:00:00 2001 From: Architjain128 Date: Tue, 8 Oct 2019 00:01:18 +0530 Subject: [PATCH 023/109] added a file for multiplication by Russian Peasant Algorithm --- RussianPeasantMultiplication.c | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 RussianPeasantMultiplication.c diff --git a/RussianPeasantMultiplication.c b/RussianPeasantMultiplication.c new file mode 100644 index 0000000..d08ce35 --- /dev/null +++ b/RussianPeasantMultiplication.c @@ -0,0 +1,36 @@ +/*Russian peasant multiplication is an interesting way to multiply +numbers that uses a process of halving and doubling. Like standard + multiplication and division*/ + + +#include + +int main() +{ + + long long int a, b, k, l, sum=0, i=0; + +// input variables + printf("ENTER TWO NUMBERS:\n"); + scanf("%lld %lld", &a, &b); + k=a; + l=b; + + +// working of algorithm + while(a>0) + { + if(a%2==1){ + sum = sum + b; + } + a = a>>1; + b = b<<1; + i++; + } + + +// output + printf("PRODUCT OF %lld AND %lld IS = %lld \n",k,l,sum); + + return 0; +} \ No newline at end of file From 4708c736cbefd697967670d3dcbd23f8949fb7d2 Mon Sep 17 00:00:00 2001 From: venkat-abhi Date: Tue, 8 Oct 2019 11:18:42 +0530 Subject: [PATCH 024/109] Add counting sort --- counting_sort.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 counting_sort.c diff --git a/counting_sort.c b/counting_sort.c new file mode 100644 index 0000000..62a922f --- /dev/null +++ b/counting_sort.c @@ -0,0 +1,54 @@ +#include +#include + +long long* count_sort_naive(long long *arr, long n, long m) +{ + long long *count_arr = malloc((m + 1) * sizeof(long long)); + long long *sorted_arr = malloc(n * sizeof(long long)); + + long i_arr = 0; + for (long i = 0; i <= m; ++i) { + /* compute the frequency of the ith element */ + for (long j = 0; j < n; ++j) { + if (arr[j] == i) { + count_arr[i] += 1; + } + } + + /* place the ith element count number of times */ + for (long j = 0; j < count_arr[i]; ++j) { + sorted_arr[i_arr] = i; + i_arr += 1; + } + } + free(arr); + + return sorted_arr; +} + +int main() +{ + /* Enter the size of the array */ + long n = 0; + printf("Enter the number of elements to be sorted: "); + scanf("%ld", &n); + + /* Enter the range of the array [0 .. m] */ + long m = 0; + printf("Enter the maximum value of the numbers to be sorted: "); + scanf("%ld", &m); + + /* Enter the values of the array */ + printf("Enter the values to be sorted: "); + long long *arr = malloc(n * sizeof(long long)); + for (long i = 0; i < n; ++i) { + scanf("%lld", &arr[i]); + } + + arr = count_sort_naive(arr, n, m); + + printf("After sorting\n"); + for (long i = 0; i < n; ++i) { + printf("%lld ", arr[i]); + } +} \ No newline at end of file From 65dcf2af170e787373a199d97315950f10d0ffc4 Mon Sep 17 00:00:00 2001 From: Architjain128 Date: Tue, 8 Oct 2019 13:23:43 +0530 Subject: [PATCH 025/109] added a pattern for pascal triangle --- PascalTriangle.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 PascalTriangle.c diff --git a/PascalTriangle.c b/PascalTriangle.c new file mode 100644 index 0000000..600c2ea --- /dev/null +++ b/PascalTriangle.c @@ -0,0 +1,31 @@ +#include +int main() +{ + long long int r,k,j,i,coeff; + // input nuber of rows + scanf("%lld",&r); + + + for(i=0;i Date: Tue, 8 Oct 2019 19:30:24 +0530 Subject: [PATCH 026/109] Added a Cprogram to print the no. pattern --- NumberPattern.c | 26 ++++++++++++++++++++++++++ a.out | Bin 0 -> 8456 bytes 2 files changed, 26 insertions(+) create mode 100644 NumberPattern.c create mode 100755 a.out diff --git a/NumberPattern.c b/NumberPattern.c new file mode 100644 index 0000000..4085ee8 --- /dev/null +++ b/NumberPattern.c @@ -0,0 +1,26 @@ +/*Prints the pattern +1 +2 3 +4 5 6 +7 8 9 10 +. +*/ +#include +int main() +{ + int i, j, rows ,num=1; + + printf("Enter number of rows: "); + scanf("%d",&rows); + + for(i=1; i<=rows; i++) + { + for(j=1; j<=i; j++) + { + printf("%3d ",num); + num++; + } + printf("\n"); + } + return 0; +} \ No newline at end of file diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..9a9c425c814a9fa2e11dd64318ef4802762261b8 GIT binary patch literal 8456 zcmeHMZ){Y@5#RIK_z3uX0Rs+@@Jew7X?@sWjLo0MXZy@^WrIs>(kj67`R;6Axj*jR zyT&F}f@Biu;y9oxQc%@OsA(gjMwR=a6>UFY3Ib9^6?~`~g+`)ni0nWVMrr&-y#siX0BX#VNinkn3GwAWhV8=^?5BX%Y=$ zA^bipRtpp>mYC7rWDsaI${jMz)J3=xDC#v78GxOxCOM|;A<`0RD@0rm7${RA6bYan z`g*cVvOpDCVLC*93=?HNWKnN|^d?A;=?po>l*fl-!>60<*=Z}ZVPT50Sh}w3A*`P# zLw34DN(sS~^}Y#u^vX!$-xCit@1_3ci%T#0&yrJgq3UQPo*u3qZm6%W zuk)r7-c54cXdh*F?e391;rr&$qR$oZAcpNXm#*3H?A1SBdH1)U4L<#gUw!`m`8R%p zy8J-(KQ3Pd$t4A(p9V}RfUg9sko@L(@b-D|R{&Swv6n>v6te#s;EG)s_-*j4Er1J% zcM!u{FElNZN(37lwNx+=?*}%e2ZD#R;NT&xKM;wEp|l6>$;n<;M@n= zxnsd$0A@K&IJ$>%;4*2!ah-6vWWjmuW7}m5PD7O{>gY|EI#zObp(51hCiSB1Wp(tt z>l~XC*)2bTrfk(u;kSHE6V#9&#P;cHStwOML>^t6zAX74AdexMo|OCx$fJwX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&w!d)nF__jhaEzUB@;T9sFE!Bkqh&KGx0O?)GF&)c_fD4++xPW}yG z&OjYq0_G5D(tZVKZ$_)eogUcEzu?nCsf0Edh==gK#!0^gTP zkr{c>n16AZ>lEK7+$wUc@w&0#9b*1XW=%c@swWLP$TheI4JU_$H=fYL zUSD&2wH_ECZXlla_N60mjU5RIFLHx{)S&Q&M&e-4pnB41`Cd4giX`HBj0SDVa5R7l zR2z!w!Yc#dg{pTT0jwS#hF_eYAe4|V%^M!1895jNBg`49#?&-4xDGu8Vv!*Dgzwx7 zu@zoe7BQIWxn=iX*YI2bOBTMNMroi5Q$80^vy|mJ(l5h) zSoJv%WV((8iQ4sB0ly8oIQP%_B2)IC&oB0LX*cNOS%vjaQ-$da^^4ms#)etxe%aouHhn(7GqtDd_VK??`rHlY z8BB3~T8mx(m(YmuWBvL5cVruO{WCUw^L#;$yKEDlBxQ4KBKFa?~+U1#N z`X|sShllm~KI0T!h_D>m#=<<)zXFC+h4uMd!~gs8|GPXtS&!!r=Jhy4)<0RU00-kh zr=ax8-`o%DGyMRXtomlYNgZ%MY=QNdpMeTu+&|}U9#XW++g^wG6m$`*fyY`{pYu#^ zV|^>-xx+F@o2dU93LJiC&=&i{`zKBl_8qEb8N$s1%C~IBxnH9dbL+zTm#BkT79?ud F{}&J4s Date: Tue, 8 Oct 2019 22:22:06 +0530 Subject: [PATCH 027/109] heap sort in c --- heap sort.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 heap sort.c diff --git a/heap sort.c b/heap sort.c new file mode 100644 index 0000000..6ed9006 --- /dev/null +++ b/heap sort.c @@ -0,0 +1,130 @@ + +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 +16 +17 +18 +19 +20 +21 +22 +23 +24 +25 +26 +27 +28 +29 +30 +31 +32 +33 +34 +35 +36 +37 +38 +39 +40 +41 +42 +43 +44 +45 +46 +47 +48 +49 +50 +51 +52 +53 +54 +55 +56 +57 +58 +59 +60 +61 +62 +63 +64 +65 +#include + +void create(int []); +void down_adjust(int [],int); + +void main() +{ + int heap[30],n,i,last,temp; + printf("Enter no. of elements:"); + scanf("%d",&n); + printf("\nEnter elements:"); + for(i=1;i<=n;i++) + scanf("%d",&heap[i]); + + //create a heap + heap[0]=n; + create(heap); + + //sorting + while(heap[0] > 1) + { + //swap heap[1] and heap[last] + last=heap[0]; + temp=heap[1]; + heap[1]=heap[last]; + heap[last]=temp; + heap[0]--; + down_adjust(heap,1); + } + + //print sorted data + printf("\nArray after sorting:\n"); + for(i=1;i<=n;i++) + printf("%d ",heap[i]); +} + +void create(int heap[]) +{ + int i,n; + n=heap[0]; //no. of elements + for(i=n/2;i>=1;i--) + down_adjust(heap,i); +} + +void down_adjust(int heap[],int i) +{ + int j,temp,n,flag=1; + n=heap[0]; + + while(2*i<=n && flag==1) + { + j=2*i; //j points to left child + if(j+1<=n && heap[j+1] > heap[j]) + j=j+1; + if(heap[i] > heap[j]) + flag=0; + else + { + temp=heap[i]; + heap[i]=heap[j]; + heap[j]=temp; + i=j; + } + } +} \ No newline at end of file From 503aeb691a210d1da68b8872b993ff22b49b9837 Mon Sep 17 00:00:00 2001 From: Ananyan25 Date: Tue, 8 Oct 2019 22:51:37 +0530 Subject: [PATCH 028/109] fixed the issue --- heap sort.c | 64 ----------------------------------------------------- 1 file changed, 64 deletions(-) diff --git a/heap sort.c b/heap sort.c index 6ed9006..b07e296 100644 --- a/heap sort.c +++ b/heap sort.c @@ -1,68 +1,4 @@ -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22 -23 -24 -25 -26 -27 -28 -29 -30 -31 -32 -33 -34 -35 -36 -37 -38 -39 -40 -41 -42 -43 -44 -45 -46 -47 -48 -49 -50 -51 -52 -53 -54 -55 -56 -57 -58 -59 -60 -61 -62 -63 -64 -65 #include void create(int []); From 51bbbec6866369248e713999a0207eca8f4f6a11 Mon Sep 17 00:00:00 2001 From: Aman Khandelwal Date: Wed, 9 Oct 2019 01:43:33 +0530 Subject: [PATCH 029/109] Basic_Examples --- Addition.c | 8 ++++++++ Area_of_Circle.c | 8 ++++++++ Area_of_Square.c | 8 ++++++++ Area_of_Triangle.c | 10 ++++++++++ Division.c | 8 ++++++++ Gross_Salary.c | 12 ++++++++++++ Separate.c | 11 +++++++++++ Simple_Interest.c | 12 ++++++++++++ Subtraction.c | 8 ++++++++ Swap_1.c | 10 ++++++++++ Swap_2.c | 10 ++++++++++ Temperature.c | 8 ++++++++ Trif.c | 10 ++++++++++ 13 files changed, 123 insertions(+) create mode 100755 Addition.c create mode 100755 Area_of_Circle.c create mode 100755 Area_of_Square.c create mode 100755 Area_of_Triangle.c create mode 100755 Division.c create mode 100755 Gross_Salary.c create mode 100755 Separate.c create mode 100755 Simple_Interest.c create mode 100755 Subtraction.c create mode 100755 Swap_1.c create mode 100755 Swap_2.c create mode 100755 Temperature.c create mode 100755 Trif.c diff --git a/Addition.c b/Addition.c new file mode 100755 index 0000000..10a2496 --- /dev/null +++ b/Addition.c @@ -0,0 +1,8 @@ +#include +void main() +{ int a,b,c; +printf("Enter two Numbers : "); +scanf("%d %d",&a,&b); +c=a+b; +printf("Sum is %d",c); +} diff --git a/Area_of_Circle.c b/Area_of_Circle.c new file mode 100755 index 0000000..d38ffef --- /dev/null +++ b/Area_of_Circle.c @@ -0,0 +1,8 @@ +#include +void main() +{ float a,r; +printf("Enter the Radius : "); +scanf("%f",&r); +a=3.14*r*r; +printf("Area is %f",a); +} diff --git a/Area_of_Square.c b/Area_of_Square.c new file mode 100755 index 0000000..e1afd7d --- /dev/null +++ b/Area_of_Square.c @@ -0,0 +1,8 @@ +#include +void main() +{ float a,r; +printf("Enter the Radius : "); +scanf("%f",&s); +a=s*s; +printf("Area is %f",a); +} diff --git a/Area_of_Triangle.c b/Area_of_Triangle.c new file mode 100755 index 0000000..8700ba5 --- /dev/null +++ b/Area_of_Triangle.c @@ -0,0 +1,10 @@ +#include +void main() +{ float a,b,h; +printf("Enter the base : "); +scanf("%f",&b); +printf("Enter the height : "); +scanf("%f",&h); +a=0.5*b*h; +printf("Area is %f",a); +} diff --git a/Division.c b/Division.c new file mode 100755 index 0000000..7c411f5 --- /dev/null +++ b/Division.c @@ -0,0 +1,8 @@ +#include +void main() +{ int a,b,c; +printf("Enter two Numbers : "); +scanf("%d %d",&a,&b); +c=a/b; +printf("Division is %d",c); +} diff --git a/Gross_Salary.c b/Gross_Salary.c new file mode 100755 index 0000000..614d2f1 --- /dev/null +++ b/Gross_Salary.c @@ -0,0 +1,12 @@ +#include +void main() +{ float agp,b,da,gs; +printf("Enter the Basic Salary : \n"); +scanf("%f",&b); +printf("Enter the AGP : \n"); +scanf("%f",&agp); +printf("Enter the DA in Percentage : \n"); +scanf("%f",&da); +gs=(b+agp)*(1+(da/100)); +printf("Gross Salary is %f",gs); +} diff --git a/Separate.c b/Separate.c new file mode 100755 index 0000000..ba9e5e3 --- /dev/null +++ b/Separate.c @@ -0,0 +1,11 @@ +#include +void main() +{ float a,c; + int b; +printf("Enter the Number : "); +scanf("%f",&a); +b=a/1; +c=a-b; +printf("Integer Part is : %d\n",b); +printf("Decimal Part is : %f",c); +} diff --git a/Simple_Interest.c b/Simple_Interest.c new file mode 100755 index 0000000..1929aa4 --- /dev/null +++ b/Simple_Interest.c @@ -0,0 +1,12 @@ +#include +void main() +{ float p,r,t,s; +printf("Enter the principal : "); +scanf("%f",&p); +printf("Enter the rate : "); +scanf("%f",&r); +printf("Enter the time : "); +scanf("%f",&t); +s=(p*r*t)/100; +printf("SI is %f",s); +} diff --git a/Subtraction.c b/Subtraction.c new file mode 100755 index 0000000..12b47b8 --- /dev/null +++ b/Subtraction.c @@ -0,0 +1,8 @@ +#include +void main() +{ int a,b,c; +printf("Enter two Numbers : "); +scanf("%d %d",&a,&b); +c=a-b; +printf("Subtraction is %d",c); +} diff --git a/Swap_1.c b/Swap_1.c new file mode 100755 index 0000000..0021579 --- /dev/null +++ b/Swap_1.c @@ -0,0 +1,10 @@ +#include +void main() +{ int a,b,t; +printf("Enter two Numbers : "); +scanf("%d %d",&a,&b); +t=b; +b=a; +a=t; +printf("Swap is %d %d",a,b); +} diff --git a/Swap_2.c b/Swap_2.c new file mode 100755 index 0000000..f881af8 --- /dev/null +++ b/Swap_2.c @@ -0,0 +1,10 @@ +#include +void main() +{ int a,b; +printf("Enter two Numbers : "); +scanf("%d %d",&a,&b); +a=a+b; +b=a-b; +a=a-b; +printf("Swap is %d %d",a,b); +} diff --git a/Temperature.c b/Temperature.c new file mode 100755 index 0000000..a85a7bb --- /dev/null +++ b/Temperature.c @@ -0,0 +1,8 @@ +#include +void main() +{ float a,c,f; +printf("Enter the Temperature in Celcius : "); +scanf("%f",&c); +f=c*(9/5)+32; +printf("Temperature in Fahernheit is %f",f); +} diff --git a/Trif.c b/Trif.c new file mode 100755 index 0000000..242172f --- /dev/null +++ b/Trif.c @@ -0,0 +1,10 @@ +#include +#include +void main() +{ double a,b; +printf("Enter the degree : "); +scanf("%lf",&a); +a=(a*3.14)/180; +b=sin(a); +printf("Sine is %lf",b); +} From 65a05888be94c64eb8244f702e8bba82ed3b245b Mon Sep 17 00:00:00 2001 From: dcjc29 Date: Thu, 10 Oct 2019 01:06:19 +0530 Subject: [PATCH 030/109] Updated Code --- SumUsingThreads.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/SumUsingThreads.c b/SumUsingThreads.c index ba85e47..4d198db 100644 --- a/SumUsingThreads.c +++ b/SumUsingThreads.c @@ -1,10 +1,11 @@ #include #include -pthread_t tids[3]; -int numbers[10] = {1,2,3,4,5,6,7,8,9,10}; -int num[2]={0,0}; +pthread_t tids[3]; //creating three threads using pthread library +int numbers[10] = {1,2,3,4,5,6,7,8,9,10}; //decleration of numbers array with 10 elements +int num[2]={0,0}; //decleration of num array with 2 elements +//dividing firt five numbers into one array void* first5(void* arg){ int ans = 0; for(int i=0;i<5;i++){ @@ -14,6 +15,7 @@ void* first5(void* arg){ num[0] = ans; } +diving second five numbers into another array void* second5(void* arg){ int ans = 0; for(int i=5;i<10;i++){ @@ -23,6 +25,7 @@ void* second5(void* arg){ num[1]=ans; } +//funtion to calculate sum void* sum(void* arg){ pthread_join(tids[0],NULL); pthread_join(tids[1],NULL); @@ -32,18 +35,12 @@ void* sum(void* arg){ int main (void){ - - - - pthread_create(&tids[0],NULL,first5,(void*)NULL); - - pthread_create(&tids[1],NULL,second5,(void*)NULL); - pthread_create(&tids[2],NULL,sum,(void*)NULL); - pthread_join(tids[2],NULL); + pthread_create(&tids[0],NULL,first5,(void*)NULL); //creating one thread to calculate sum of first five numbers + pthread_create(&tids[1],NULL,second5,(void*)NULL); //creating second thread to calculate sum of last five numbers + pthread_create(&tids[2],NULL,sum,(void*)NULL); //creating another thread to to take sum of the above + pthread_join(tids[2],NULL); // thread 3 will wait until thread 1 and 2 finish executing printf("Main Ended\n"); - - return 0; } From 4769050cd7ca37e5cf84b1a3d1d723c159bb1e3e Mon Sep 17 00:00:00 2001 From: Sarvesh Nath Tiwari <43759182+sarveshsrv@users.noreply.github.com> Date: Thu, 10 Oct 2019 08:46:27 +0530 Subject: [PATCH 031/109] Added transpose Of Matrix.c This code finds the transpose of the given matrix. Input:- Order of matrix and the coefficients of matrix Output:- Given matrix and its transpose. --- transposeOfMatrix.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 transposeOfMatrix.c diff --git a/transposeOfMatrix.c b/transposeOfMatrix.c new file mode 100644 index 0000000..1d9d0c6 --- /dev/null +++ b/transposeOfMatrix.c @@ -0,0 +1,40 @@ +//C program to input a matrix of order MxN and find its transpose + +#include +#include +int main() +{ + static int array[10][10]; + int i, j, row, col; + + printf("Enter the order of the matrix \n"); + scanf("%d %d", &row, &col); + printf("Enter the coefficients of the matrix\n"); + for (i = 0; i < row; ++i) + { + for (j = 0; j < col; ++j) + { + scanf("%d", &array[i][j]); + } + } + printf("The given matrix is \n"); + for (i = 0; i < row; ++i) + { + for (j = 0; j < col; ++j) + { + printf(" %d", array[i][j]); + } + printf("\n"); + } + printf("Transpose of matrix is \n"); + for (j = 0; j < col; ++j) + { + for (i = 0; i < row; ++i) + { + printf(" %d", array[i][j]); + } + printf("\n"); + } + + return 0; +} From c8f5123b9278c329f2440c14884bc9731cfd1f48 Mon Sep 17 00:00:00 2001 From: FalguniAjmera <43901389+FalguniAjmera@users.noreply.github.com> Date: Fri, 11 Oct 2019 00:35:28 +0530 Subject: [PATCH 032/109] Automorphic Number --- Automorphic_number.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Automorphic_number.c diff --git a/Automorphic_number.c b/Automorphic_number.c new file mode 100644 index 0000000..773c671 --- /dev/null +++ b/Automorphic_number.c @@ -0,0 +1,24 @@ +#include +#include +#include +int main() { + int num, sqr, temp, last; + int n = 0; + printf("Enter a number \n"); + scanf("%d", & num); + sqr = num * num; //calculating square of num + temp = num; + //Counting number of digits + while (temp > 0) { + n++; + temp = temp / 10; + } + //Extracting last n digits + int den = floor(pow(10, n)); + last = sqr % den; + if (last == num) + printf("Automorphic number \n"); + else + printf("Not Automorphic \n"); + return 0; +} From 3bf7699308a821c9489347bd2bcc84d3c73b11e7 Mon Sep 17 00:00:00 2001 From: Sudeep Swain Date: Fri, 11 Oct 2019 08:17:53 +0530 Subject: [PATCH 033/109] Even Fibonacci Number A program that prompts the user to input the range up to which they want to display the even Fibonacci number and finds it's sum. --- Even Fibonacci numbers | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Even Fibonacci numbers diff --git a/Even Fibonacci numbers b/Even Fibonacci numbers new file mode 100644 index 0000000..8dd2cf5 --- /dev/null +++ b/Even Fibonacci numbers @@ -0,0 +1,34 @@ +#include +void main() +{ + int i;//loop counter + int n;//max value + int t1=1;//First term + int t2=1;//Second term + int nextTerm;//Next term + int sum=0;//Summation of total even Fibonacci number + int count=0;//counter to count the number of even Fibonacci numbers + printf("Enter the max value up to which you want to print the values: "); + scanf("%d",&n); + printf("The listed even Fibonacci number are: "); + printf("\nSlno.\t\tFibonacci number"); + for(i=1;i<=n;i++) + { + if(t1>=1 && t1<=n) + { + nextTerm=t1+t2; + t1=t2; + t2=nextTerm; + + if(t1%2==0 && t1<=n) + { + sum=sum+t1; + count++; + printf("\n %d\t\t\t%d",count,t1); + } + } + + } + printf("\n\nThe required sum of the total even Fibonacci number is: %u\n\n\n",sum); + +} From 5ece8030669a9520a0b35af6ade6625f8b44df0e Mon Sep 17 00:00:00 2001 From: ade ishs Date: Fri, 11 Oct 2019 16:40:42 +1100 Subject: [PATCH 034/109] Make the code strictly conforming to ISO C MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The code is now strictly conforming to ISO C 90. That also means that it is forward compatible to newer ISO C standards. Hence, you can compile it with any conforming implementation. /* */ comments are accepted in all standards. The comments and captions are now more accurate. If `main()` doesn’t accept `argc` and `argv`, it should be `(void)`. The portable format specifier for pointers is `"%p"` (though the output differs from one implementation to another). The corresponding argument must be `(void *)`. --- Pointers.c | 56 +++++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/Pointers.c b/Pointers.c index ce271b8..137773f 100644 --- a/Pointers.c +++ b/Pointers.c @@ -1,26 +1,30 @@ -// Handling of pointers in C program -// Pointers in C language is a variable that stores/points the address of another variable. -// A Pointer in C is used to allocate memory dynamically i.e. at run time. - -#include - int main() { - int * pc; // * is used to make pointer - int c; - - c = 22; - printf("Address of c: %u\n", & c); - printf("Value of c: %d\n\n", c); - - pc = & c; - printf("Address of pointer pc: %u\n", pc); - printf("Content of pointer pc: %d\n\n", * pc); - - c = 11; - printf("Address of pointer pc: %u\n", pc); - printf("Content of pointer pc: %d\n\n", * pc); - - * pc = 2; - printf("Address of c: %u\n", & c); - printf("Value of c: %d\n\n", c); - return 0; - } +/* + * Handling of pointers in a C program + * A pointer in the C language is an object that stores the address of another + * object. + * A pointer in C is used to allocate memory dynamically i.e. at run time. + */ + +#include + +int main(void) { + int * pc; /* the '*' is used to make pointer */ + int c; + + c = 22; + printf("Address of c: %p\n", (void * ) & c); + printf("Value of c: %d\n\n", c); + + pc = & c; + printf("Address of c: %p\n", (void * ) pc); + printf("Content of c: %d\n\n", * pc); + + c = 11; + printf("Address of c: %p\n", (void * ) pc); + printf("Content of c: %d\n\n", * pc); + + * pc = 2; + printf("Address of c: %p\n", (void * ) & c); + printf("Value of c: %d\n\n", c); + return 0; +} From 43960d50ed2075bf4ab390304df675f25aed7600 Mon Sep 17 00:00:00 2001 From: Manaarth Date: Fri, 11 Oct 2019 21:21:15 +0530 Subject: [PATCH 035/109] Resolved Conflicts --- a.out | Bin 8488 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100755 a.out diff --git a/a.out b/a.out deleted file mode 100755 index e575b5de366b30d19a11b894e135895c1c37cbb4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8488 zcmeHMYiwM_6`u9li9?e05<+4EG`DG7C7^8L;3PIA;B~ytxN?FUZ2BTxuf6Mdn|(yP zcUwD@*2n?04Njv9Ayf$|f+9gxsi`WV&>zLYDTE3n^P?>iRC24P+5}LID3DT;?RVzR zxqJ6|4OP|tW2E(&^PR_>nK?6eX8eOlPgjl4C%E{#nrXep{h(ymT4MuVdV|(u$`KN+)3RzrF11mnLKq4lJBsz> za>args={nr|Ljig_h|gpvH?->!b^$c-nq=I{T_?X~Gg9|;}GzecwF zL-ju{N1$m`(cTJdMHPI_0{G1f;Bo={G2jM%-a_eEt=*@A8(wAbZTPLLf(wlI5Do8k zVVa3tW~i;r%ne1;!(ek(bm)*dG;+usjwX`ga5j;)hDFZGCgW*{heo63a3UQ|CXU3x zk|Q$7T4pMmNQ*r^-5tBlt)U%Gt;MNr4Q&%xI^>yiuC zOItN@TH_QRuMZMTxo}!1T+X?0Ui&1QapAhF%0y1QQ!kIzU0z}c`QVgQQ<{+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bR#PV0jX0^#FTAD(v;yxNe5P5g z@o^mp<5RV^fa9LhdU_a~_P?NU*28w9`iJpU5SF##$F)CfcPv+ybDAH~@_%Zc`=Q1a@@dv@=>)!5KKm`_`IV@IeZv}IFsUUALe zZ*C23X$duNwDHgKO!qvWbI*5c(`|t7#g({USeythfgkq*&O;Xr|8-9ORpN`BJgvkR z3!cA9yk79USK>tujA;v0U_4#RJYeOr@U&XMI=VK{3t= z?-KaM)y_MD3&yyu5UspmT>`(j#<^FyVCb)O_K8aTTD*@KZb;SrVwJP5-9o>>D%SU~ zO1x1tR>gT2D{IsKS1aBtl@Yr}`(cd;h!dXi3}}4DgMU%uyuaFBgxGruF$d zp;-KxRh9Yq^FDB2mHAzQ4y?fVAJ%!u%lJCrhVoVU=V^t{&xdWmgUAyef3~^?_zE#U z|92{!waT=20X(??J`Vh9v>Wui6C7PY|0&=Ur#GKZ0XNEFIJFmmUtP0W@P5mJR|$tF zUcdVDBHjc}QA$oSo;mIkM zF%Kp)gVCfJvohJ78O@K1p-k#CVOqiYD*caa0y}MGPp4eUu=`GE#S&>VpNnHy z$~~gt1wWX}X*cR6vhrcG+$hZ_o*j9(={>UQBV*-LXW5?mB|^EelocHWwX!xH;ToPx z{|I z`#n?dvESSOXSF@gAD=Ur-p37T%5gLNOOL(x|C2jeQB!aGl;1S|+&SPv?yFwgYQKq*z&p6@&Se_{UrFXt!Q z@jTLbpSsBQCmRfK0{0_D#DGnlziiL+A|!5m=e(&K;C+T2upRSrs1U~O^ZBh&+k5pr z(;+^9En!>ma~HPfb0*8!-c5P#S%;|9?Mppy^qnDJ91r)4@|$Bv)hQ=vvq1T}$2qra TOX+M~-M*+BxWbB>dhP!Woe~}w From f7c29c73e9a12429622e51d30768705d388dba5b Mon Sep 17 00:00:00 2001 From: bharatchhipa <36991847+bharatchhipa@users.noreply.github.com> Date: Fri, 11 Oct 2019 22:24:57 +0530 Subject: [PATCH 036/109] Create Linked List Creation Program which creates N Nodes in a linked list --- Linked List Creation | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Linked List Creation diff --git a/Linked List Creation b/Linked List Creation new file mode 100644 index 0000000..ebd346c --- /dev/null +++ b/Linked List Creation @@ -0,0 +1,47 @@ +#include +#include +int n; +struct node +{ + int data; + struct node *info; +}*start; +void create(int n) +{ + int i; + struct node *temp,*p,*newnode; + p=(struct node *)malloc(sizeof(struct node)); + start=p; + printf("enter the data\n"); + scanf("%d",&p->data); + p->info=NULL; + temp=start; + for(i=2;i<=n;i++) + { + newnode=(struct node *)malloc(sizeof(struct node)); + printf("enter the data\n"); + scanf("%d",&newnode->data); + newnode->info=NULL; + temp->info=newnode; + temp=temp->info; + } +} +void display() +{ + struct node *temp; + temp=start; + while(temp!=NULL) + { + printf("%d",temp->data); + temp=temp->info; + } +} +int main() +{ + printf("enter the no of node"); + scanf("%d",&n); + create(n); + printf("\n"); + display(); + return 0; + } From 1b20b4398077943ff07c98e0fcd5aec4ffa62e3f Mon Sep 17 00:00:00 2001 From: bharatchhipa <36991847+bharatchhipa@users.noreply.github.com> Date: Fri, 11 Oct 2019 22:34:34 +0530 Subject: [PATCH 037/109] Create Binary to decimal A program to convert binary number to decimal number. --- Binary to decimal | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Binary to decimal diff --git a/Binary to decimal b/Binary to decimal new file mode 100644 index 0000000..c672b1f --- /dev/null +++ b/Binary to decimal @@ -0,0 +1,19 @@ +#include +#include +using namespace std; +int main() +{ + long long int a; + cout<<"enter the no in binary\n"; + cin>>a; + int b=0,j=0; + int c; + while(a!=0) + { + c=a%10; + b=b+c*pow(2,j); + j++; + a=a/10; + } + cout< Date: Fri, 11 Oct 2019 22:45:06 +0530 Subject: [PATCH 038/109] Create Decimal to Binary A program to convert Decimal to Binary. --- Decimal to Binary | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Decimal to Binary diff --git a/Decimal to Binary b/Decimal to Binary new file mode 100644 index 0000000..cde40cd --- /dev/null +++ b/Decimal to Binary @@ -0,0 +1,28 @@ +#include +#include +using namespace std; +int main() +{ + int a,s; + cout<<"enter a decimal no"; + int b; + cin>>b; + int c,d,e[100],count=0,i=0; + while(b!=1) + { + c=b%2; + b=b/2; + e[i]=c; + count++; + i++; + } + e[i]=1; + + int j; + for(j=count;j>=0;j--) + { + cout< Date: Fri, 11 Oct 2019 23:00:05 +0530 Subject: [PATCH 039/109] Create Address of 1-D array --- Address of 1-D array | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Address of 1-D array diff --git a/Address of 1-D array b/Address of 1-D array new file mode 100644 index 0000000..bad40b3 --- /dev/null +++ b/Address of 1-D array @@ -0,0 +1,17 @@ +#include +int main() +{ + int a[100],i,n,*add; + printf("enter the size"); + scanf("%d",&n); + printf("enter the no"); + for(i=0;i Date: Sat, 12 Oct 2019 16:14:46 +0530 Subject: [PATCH 040/109] Create SelectionSort.c --- SelectionSort.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 SelectionSort.c diff --git a/SelectionSort.c b/SelectionSort.c new file mode 100644 index 0000000..bbb015a --- /dev/null +++ b/SelectionSort.c @@ -0,0 +1,41 @@ +#include + +void swap(int * xp, int * yp) { + int temp = * xp; + * xp = * yp; + * yp = temp; +} + +void selectionSort(int arr[], int n) { + int i, j, min_idx; + + // One by one move boundary of unsorted subarray + for (i = 0; i < n - 1; i++) { + // Find the minimum element in unsorted array + min_idx = i; + for (j = i + 1; j < n; j++) + if (arr[j] < arr[min_idx]) + min_idx = j; + + // Swap the found minimum element with the first element + //so that the minimum element of unsorted subarray + //comes to first position + swap( & arr[min_idx], & arr[i]); + } +} + +int main() { + + int n, i; //n is size of array + scanf("%d",&n); + int a[n]; //array of n integers to be sorted + for (i = 0; i < n; i++) + scanf("%d",&a[i]); + + selectionSort(a, n); //calling the function to sort the array + //printing sorted array + for (i = 0; i < n; i++) + printf("%d ", a[i]); + + return 0; +} From 870b7cd66565fab35faa5c7905ebc54cb5f6394f Mon Sep 17 00:00:00 2001 From: Sripad <49719676+TVSSSRIPAD@users.noreply.github.com> Date: Sat, 12 Oct 2019 16:16:03 +0530 Subject: [PATCH 041/109] Update SelectionSort.c --- SelectionSort.c | 1 + 1 file changed, 1 insertion(+) diff --git a/SelectionSort.c b/SelectionSort.c index bbb015a..fece413 100644 --- a/SelectionSort.c +++ b/SelectionSort.c @@ -1,5 +1,6 @@ #include +//function for swapping two numbers void swap(int * xp, int * yp) { int temp = * xp; * xp = * yp; From 489398e7e92beb45b6f16d52123fa29aef3ae903 Mon Sep 17 00:00:00 2001 From: Sripad <49719676+TVSSSRIPAD@users.noreply.github.com> Date: Sat, 12 Oct 2019 16:19:15 +0530 Subject: [PATCH 042/109] Update SelectionSort.c --- SelectionSort.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SelectionSort.c b/SelectionSort.c index fece413..93dd989 100644 --- a/SelectionSort.c +++ b/SelectionSort.c @@ -28,8 +28,10 @@ void selectionSort(int arr[], int n) { int main() { int n, i; //n is size of array + printf("Enter the number of elements "); scanf("%d",&n); int a[n]; //array of n integers to be sorted + printf("Enter the elements "); for (i = 0; i < n; i++) scanf("%d",&a[i]); From 2869488200539683be01b1d91cab6494ca855ab7 Mon Sep 17 00:00:00 2001 From: Sripad <49719676+TVSSSRIPAD@users.noreply.github.com> Date: Sat, 12 Oct 2019 16:25:19 +0530 Subject: [PATCH 043/109] Corrected a typo in readme.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9c887dc..ec90931 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ These program are written in codeblocks ide for windows. These programs are not - [Decimal to base n conversion](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DecimalToBaseN.c) - [Two-way decimal to hexadecimal](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DecimalToHexadecimalViceVersa.c) - [Encrypt and Decrypt XOR](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/EncryptDecryptXOR.c) -- [Fibonnacia Generation](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/FibonacciGeneration.c) +- [Fibonnaci Generation](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/FibonacciGeneration.c) - [Calculate remainder](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/FindRemainder.c) - [Check leap year](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/isInputLeapYear.c) - [Find largest number](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Largest.c) From c00e47cf5a69288bb39d5ef0992d6bb119591924 Mon Sep 17 00:00:00 2001 From: Sripad <49719676+TVSSSRIPAD@users.noreply.github.com> Date: Sat, 12 Oct 2019 17:15:24 +0530 Subject: [PATCH 044/109] Added MagicNumbers.c --- MagicNumbers.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 MagicNumbers.c diff --git a/MagicNumbers.c b/MagicNumbers.c new file mode 100644 index 0000000..af4bd87 --- /dev/null +++ b/MagicNumbers.c @@ -0,0 +1,60 @@ +//C program to check if a number is magic number or not +//Magic Number:A number is said to be a magic number +//if the product of the sum and the reverse number of the sum is the given number then the number is the magic number. +//Ex:1729 is a magic number +//Code: + +#include + /* sum of digits of a number */ +int sumOfDigits(int num) { + int sum = 0; + while (num > 0) { + sum = sum + (num % 10); + num = num / 10; + } + return sum; + } + + /* returns reverse of a given number */ + int reverse(int num) { + int rev = 0; + while (num > 0) { + rev = (rev * 10) + (num % 10); + num = num / 10; + } + return rev; + } + +int main () { + int num, sum, rev; + + printf("Enter the value for a number:"); + scanf("%d", &num); + + /* find sum of digits by calling function */ + sum = sumOfDigits(num); + + + //if the value is single digit, then + //the value and its reverse are same + if (sum < 10) { + if ((sum * sum) == num) { + printf("%d is a magic number\n", num); + } else { + printf("%d is not a magic number\n", num); + } + return 0; + } + + /* reverse of the given number */ + rev = reverse(sum);//calling reverse function + + /* printing the outputs */ + if ((sum * rev) == num) { + printf("%d is a magic number\n", num); + } else { + printf("%d is not a magic number\n", num); + } + + return 0; +} From 0bd7282c83e5d90c6d697b36a25be8a44f6fc65b Mon Sep 17 00:00:00 2001 From: Tanishq Chaudhary Date: Sat, 12 Oct 2019 23:36:32 +0530 Subject: [PATCH 045/109] Added a program for temperature switch using c algorithm --- TemperatureSwitch.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 TemperatureSwitch.c diff --git a/TemperatureSwitch.c b/TemperatureSwitch.c new file mode 100644 index 0000000..d997d6b --- /dev/null +++ b/TemperatureSwitch.c @@ -0,0 +1,38 @@ +#include + +int main() +{ + // The resultant temperatures can be in decimals as well, so we use double + double c, f, result; + // We use an integer type data to run the switch statement + int choice; + printf("Select your choice: \n"); + printf("1. Celcius to Fahrenheit\n"); + printf("2. Fahrenheit to Celcius\n"); + + printf("Enter your choice: "); + scanf("%d", &choice); + + // We compute the temperatures for both the cases here respectively + switch(choice) + { + case 1: + printf("Enter the temperature in Celcius: "); + scanf("%lf", &c); + result = (9 / 5) * c + 32; + break; + case 2: + printf("Enter the temperature in Fahrenheit: "); + scanf("%lf", &f); + result = (5 / 9) * (f - 32); + break; + + // This case gets activated when the user inputs anything othrer than 1 or 2 + default: + printf("Invalid case!\n"); + } + + // Printing out the result according to the computation + printf("The resultant temperature is: %lf", result); + return 0; +} From c5f3354fc98dd66aaaccfaa443db502d9e5e1ff2 Mon Sep 17 00:00:00 2001 From: Rishit Chaudhary Date: Sun, 13 Oct 2019 02:35:25 +0530 Subject: [PATCH 046/109] Implementation of Priority Queue using Max Heap Using C Programming Language I have implemented a priority queue program, which takes the program ID and priority value as inputs, and stores them in a Max-Heap, for efficient querying, in the program. I have implemented this program in the C Programming Language. In my program, the greater the priority value of the task, the higher is the priority of the task. --- priority_queue.c | 182 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 priority_queue.c diff --git a/priority_queue.c b/priority_queue.c new file mode 100644 index 0000000..d24c9ff --- /dev/null +++ b/priority_queue.c @@ -0,0 +1,182 @@ +#include +#include +#include + +int CAPACITY = 10; +int size; + +typedef struct task { + int id; + int priority; +}task; + +int getLeftChildIndex(int i); +int getRightChildIndex(int i); +int getParentIndex(int i); +bool hasLeftChild(int i); +bool hasRightChild(int i); +bool hasParent(int i); +task leftChild(int i, task* taskList); +task rightChild(int i, task* taskList); +task parent(int i, task* taskList); +void swap(int i, int j, task* taskList); +void ensureExtraCapacity(task** taskList); +task peek(task *taskList); +task poll(task *taskList); +void addTask(task t, task* taskList); +void heapifyUp(task* taskList); +void heapifyDown(task* taskList); + + +int main() { + task *taskList = malloc(sizeof(int)*CAPACITY); + task task; + int op; + printf("\tIn this program the greater the priority value of the task, the higher is the priority of the task\n"); + for(int i = 0; i < 110; ++i) + printf("-"); + printf("\n"); + do { + printf("1...Enter a new task\n"); + printf("2...Remove highest priority task\n"); + printf("3...Show highest priority task\n"); + printf("4...Exit\n"); + printf("Enter you option below:\n"); + printf("-> "); scanf("%d", &op); + printf("\n"); + + switch(op) { + case 1: + printf("Enter the task ID:\n"); + printf("-> "); scanf("%d", &task.id); + + printf("Enter the task Priority:\n"); + printf("-> "); scanf("%d", &task.priority); + + addTask(task, taskList); + break; + case 2: + task = poll(taskList); + printf("Element Processed is:\n"); + printf("ID: %d\n", task.id); + printf("Priority: %d\n", task.priority); + break; + case 3: + task = peek(taskList); + printf("Element Ready to be Processed first is:\n"); + printf("ID: %d\n", task.id); + printf("Priority: %d\n", task.priority); + break; + case 4: + // nothing to see here... + break; + default: + printf("Invalid option entered, please try again !\n\n"); + break; + } + for(int i = 0; i < 100; ++i) + printf("-"); + printf("\n"); + } while(op != 4); + free(taskList); + return 0; +} + +void heapifyDown(task* taskList) { + int index = 0; + int greaterChildIndex = getLeftChildIndex(index); + while(hasLeftChild(index)) { + if(hasRightChild(index) && rightChild(index, taskList).priority > leftChild(index, taskList).priority) { + greaterChildIndex = getRightChildIndex(index); + } + if(taskList[index].priority > taskList[greaterChildIndex].priority) { + break; + } + else { + swap(index, greaterChildIndex, taskList); + } + index = greaterChildIndex; + } +} + +void heapifyUp(task* taskList) { + int index = size - 1; + while(hasParent(index) && parent(index, taskList).priority < taskList[index].priority) { + swap(getParentIndex(index), index, taskList); + index = getParentIndex(index); + } +} + +void addTask(task t, task* taskList) { + ensureExtraCapacity(&taskList); + taskList[size] = t; + ++size; + heapifyUp(taskList); +} + +task poll(task *taskList) { + if(!size) { + printf("Error !\n"); + exit(0); + } + task t = taskList[0]; + taskList[0] = taskList[size-1]; + --size; + heapifyDown(taskList); + return t; +} + +task peek(task *taskList) { + if(!size) { + printf("Error !\n"); + exit(0); + } + return taskList[0]; +} + +void ensureExtraCapacity(task** taskList) { + if(size > 2*CAPACITY/3) + *taskList = realloc(*taskList,CAPACITY*=2); +} + +void swap(int i, int j, task* taskList) { + task temp = taskList[i]; + taskList[i] = taskList[j]; + taskList[j] = temp; +} + +task parent(int i, task* taskList) { + return taskList[getParentIndex(i)]; +} + +task rightChild(int i, task* taskList) { + return taskList[getRightChildIndex(i)]; +} + +task leftChild(int i, task* taskList) { + return taskList[getLeftChildIndex(i)]; +} + +bool hasParent(int i) { + return getParentIndex(i) >= 0; +} + +bool hasRightChild(int i) { + return getRightChildIndex(i) < size; +} + +bool hasLeftChild(int i) { + return getLeftChildIndex(i) < size; +} + +int getLeftChildIndex(int i) { + return 2*i + 1; +} + +int getRightChildIndex(int i) { + return 2*i + 2; +} + +int getParentIndex(int i) { + return (i-1)/2; +} \ No newline at end of file From d71a2020d0c3083d3d9ea29f8b27b18346dd39c6 Mon Sep 17 00:00:00 2001 From: Sudeep Swain Date: Sun, 13 Oct 2019 10:18:11 +0530 Subject: [PATCH 047/109] New pattern added Diamond shaped numeric combo. --- Pattern1.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/Pattern1.c b/Pattern1.c index 3427e84..89240f2 100644 --- a/Pattern1.c +++ b/Pattern1.c @@ -10,7 +10,7 @@ int main() **** ***** **/ - + int i,j,k;//Declaration of variable for(i=1;i<=5;i++) { for(j=1;j<=i;j++) @@ -20,5 +20,47 @@ int main() printf("\n"); } +/** Program to print the following pattern: + + 1 + 123 + 12345 + 1234567 +123456789 + 1234567 + 12345 + 123 + 1 + + **/ + for (i=1;i<=5;i++) + { + for (j=i;j<5;j++) + { + printf(" "); + } + for (k=1;k<(i*2);k++) + { + printf("%d",k); + } + printf("\n"); + } + + for (i=4;i>=1;i--) + { + for (j=5;j>i;j--) + { + printf(" "); + } + for (k=1;k<(i*2);k++) + { + printf("%d",k); + } + printf("\n"); + } + + + return 0; -} \ No newline at end of file +} + From 91e7cc868f3a7407bf4e8792c47a1239533064cd Mon Sep 17 00:00:00 2001 From: santhoshganesh13 Date: Sun, 13 Oct 2019 11:19:28 +0530 Subject: [PATCH 048/109] Modified Addition.c --- Addition.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Addition.c b/Addition.c index 10a2496..06f23b8 100755 --- a/Addition.c +++ b/Addition.c @@ -4,5 +4,5 @@ void main() printf("Enter two Numbers : "); scanf("%d %d",&a,&b); c=a+b; -printf("Sum is %d",c); +printf("The Sum is %d",c); } From 31526ac73203c52540456065d887d9a9d5aaf187 Mon Sep 17 00:00:00 2001 From: Sudeep Swain Date: Sun, 13 Oct 2019 11:41:13 +0530 Subject: [PATCH 049/109] Patterns and it's Algorithm The user has to run the program and tally that to it's respective algorithm. --- Pattern Combos | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 Pattern Combos diff --git a/Pattern Combos b/Pattern Combos new file mode 100644 index 0000000..2ec32ed --- /dev/null +++ b/Pattern Combos @@ -0,0 +1,88 @@ +//Run these Pattern Program for the required output...tally it with it's respective algorithm. + +#include +void main() +{ + int i,j,k; + int t=0, temp=1; +//Problem:1------------------------------------1 +printf("Problem 1\n"); + for (i=0; i<5; i++) + { + for (j=0; j<5; j++) + { + printf("*"); + } + printf("\n"); + } + printf("\n\n"); +//Problem:2-------------------------------------2 +printf("Problem 2\n"); + for (i=1; i<=5; i++) + { + for (j=5; j>=i; j--) + { + printf(" "); + } + for (k=1; k<=i; k++) + { + printf("*"); + } + printf("\n"); + } + printf("\n\n"); +//Problem:3--------------------------------------3 +printf("Problem 3\n"); + for (i=0; i<5; i++) + { + for (j=0; j<=i; j++) + { + printf("*"); + } + printf("\n"); + } + printf("\n\n"); +//Problem:4-------------------------------------4 +printf("Problem 4\n"); + for (i=5; i>=1; i--) + { + for (k=temp; k>=0; k--) + { + printf(" "); + } + for (j=i; j>=1; j--) + { + printf("*"); + } + temp = temp + 1; + printf("\n"); + } + printf("\n\n"); +//Problem:5--------------------------------------5 +printf("Problem 5\n"); + for (i=5; i>=1; i--) + { + for (j=1; j<=i; j++) + { + printf("*"); + } + printf("\n"); + } + printf("\n\n"); +//Problem:6--------------------------------------6 +printf("Problem 6\n"); + for (i=1; i<=5; i++) + {printf(" "); + for (k=t; k<5; k++) + { + printf(" "); + } + for (j=0; j< i; j++) + { + printf(" * "); + t = t + 1; + } + printf("\n"); + } + +} From 4501d1dba6ef6f85eeea2b949fb1414a11c418d7 Mon Sep 17 00:00:00 2001 From: Ved246 Date: Sun, 13 Oct 2019 18:15:31 +0530 Subject: [PATCH 050/109] added code to calculate upto 5000! --- large_factorial.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 large_factorial.c diff --git a/large_factorial.c b/large_factorial.c new file mode 100644 index 0000000..3ebd6d2 --- /dev/null +++ b/large_factorial.c @@ -0,0 +1,36 @@ +//c program to find factorial upto input of 5000 + +#include + +int main(){ + int ans[16500]; + int testcases; + scanf("%d", &testcases); + while(testcases--){ + int num; + scanf("%d", &nm); + for(int i=0; i<16500; i++) + ans[i]=0; + ans[0]=1; + int m, carry; + for(int i=2; i<=num; i++){ + carry=0; + for(int j=0; j<16500; j++){ + m= i*ans[j]; + m+= carry; + ans[j]=m%10; + carry=m/10; + } + } + int f=0, i=16500-1; + while(ans[i]==0 && i>=0){ + i--; + } + while(i>=0){ + printf("%d", ans[i]); + i--; + } + printf("\n"); + } +} + From a44df32365c9d891a3cff1647dcc7055485d593e Mon Sep 17 00:00:00 2001 From: Ved246 Date: Sun, 13 Oct 2019 19:09:36 +0530 Subject: [PATCH 051/109] fixed a typo in code large_factorial and made it more interactive --- large_factorial.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/large_factorial.c b/large_factorial.c index 3ebd6d2..7937bb1 100644 --- a/large_factorial.c +++ b/large_factorial.c @@ -1,14 +1,17 @@ //c program to find factorial upto input of 5000 + #include int main(){ int ans[16500]; int testcases; + printf("Enter no of test cases: "); scanf("%d", &testcases); while(testcases--){ int num; - scanf("%d", &nm); + printf("Enter number:"); + scanf("%d", &num); for(int i=0; i<16500; i++) ans[i]=0; ans[0]=1; @@ -22,10 +25,11 @@ int main(){ carry=m/10; } } - int f=0, i=16500-1; + int i=16500-1; while(ans[i]==0 && i>=0){ i--; } + printf("\t%d! =", num); while(i>=0){ printf("%d", ans[i]); i--; From e4d96b64b03d35a37771234d9caf755637b589cd Mon Sep 17 00:00:00 2001 From: Sudeep Swain Date: Sun, 13 Oct 2019 20:15:16 +0530 Subject: [PATCH 052/109] First and Second max element in an array To find the First and Second maximum in an array without sorting the array --- ...d maximum element in Array without sorting | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Position of First'n'Second maximum element in Array without sorting diff --git a/Position of First'n'Second maximum element in Array without sorting b/Position of First'n'Second maximum element in Array without sorting new file mode 100644 index 0000000..2067e60 --- /dev/null +++ b/Position of First'n'Second maximum element in Array without sorting @@ -0,0 +1,50 @@ +#include +void main() +{ + int size; + int i; + int arr[100]; + + //initializing the array + printf("Enter the size of the array:"); + scanf("%d",&size); + for(i=0;ifmax) + { + smax=fmax; + fmax=arr[i]; + spos=fpos; + fpos=i+1; + } + else if(arr[i]>smax && arr[i]!=fmax) + { + smax=arr[i]; + spos=i+1; + } + } + + printf("First max element :%d at position:%d\n",fmax,fpos); + printf("Second max element:%d at position:%d\n",smax,spos); + +} From 393df779a011aa7130048378d1a20de6110918d4 Mon Sep 17 00:00:00 2001 From: Sudeep Swain Date: Sun, 13 Oct 2019 20:43:22 +0530 Subject: [PATCH 053/109] Create Position_of_First'n'Second_max_elements_without_sorting_array Find the position of first and second max element without sorting. --- ...'Second_max_elements_without_sorting_array | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Position_of_First'n'Second_max_elements_without_sorting_array diff --git a/Position_of_First'n'Second_max_elements_without_sorting_array b/Position_of_First'n'Second_max_elements_without_sorting_array new file mode 100644 index 0000000..f2a5341 --- /dev/null +++ b/Position_of_First'n'Second_max_elements_without_sorting_array @@ -0,0 +1,44 @@ +#include +void main() +{ + int size; + int i; + int arr[100]; + printf("Enter the size of the array:"); + scanf("%d",&size); + for(i=0;ifmax) + { + smax=fmax; + fmax=arr[i]; + spos=fpos; + fpos=i+1; + } + else if(arr[i]>smax && arr[i]!=fmax) + { + smax=arr[i]; + spos=i+1; + } + } + + printf("First max element :%d at position:%d\n",fmax,fpos); + printf("Second max element:%d at position:%d\n",smax,spos); + +} From 59b57d9cb7c1e66a327fc390308c8877fd2ebe49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pedro?= <44150570+jpedrinhoxd@users.noreply.github.com> Date: Sun, 13 Oct 2019 14:52:11 -0400 Subject: [PATCH 054/109] Create SimpleArithmeticAverage --- SimpleArithmeticAverage | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 SimpleArithmeticAverage diff --git a/SimpleArithmeticAverage b/SimpleArithmeticAverage new file mode 100644 index 0000000..8dc8824 --- /dev/null +++ b/SimpleArithmeticAverage @@ -0,0 +1,26 @@ +//A simple code that calculates a student's arithmetic average over 3 grades. + +#include + +int main(void) { + + float grade1, grade2, grade3, average; + + //Grades input + printf("Type de first grade: "); + scanf("%f", & grade1); + + printf("Type the second grade : "); + scanf("%f", & grade2); + + printf("Type the third grade: "); + scanf("%f", & grade3); + + //Processing + average = (grade1 + grade2 + grade3) / 3; + + //Output + printf("Student Average = %.1f\n", average); + + system("pause"); + return 0; From f30407e3fac37131ccc63a25e6d3b1db96bf99f3 Mon Sep 17 00:00:00 2001 From: utkars-h Date: Mon, 14 Oct 2019 02:27:05 +0530 Subject: [PATCH 055/109] Prints Basic Star Pattern --- Star_Pattern.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Star_Pattern.c diff --git a/Star_Pattern.c b/Star_Pattern.c new file mode 100644 index 0000000..33016e5 --- /dev/null +++ b/Star_Pattern.c @@ -0,0 +1,17 @@ +#include + +int main(){ + + int n, i,j; + + printf("Enter number of rows : "); + scanf("%d" , &n); + + for(i=1;i<=n;i++){ + for(j=1;j<=i;j++) + printf("* "); + printf("\n"); + } + return 0; +} + From 6149dab52f6931b0a10a843bfcd8f793f4dcf014 Mon Sep 17 00:00:00 2001 From: Rishit Chaudhary <12199011+rishitc@users.noreply.github.com> Date: Mon, 14 Oct 2019 19:41:52 +0530 Subject: [PATCH 056/109] Added few more programs to the readme.md list Added few more programs to the readme.md list along with the corresponding links to them. I have followed the same pattern as in the readme.md file. --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ec90931..2d8d81c 100644 --- a/README.md +++ b/README.md @@ -101,8 +101,9 @@ These program are written in codeblocks ide for windows. These programs are not - [Swap integers without 3rd variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapIntegers.c) - [Swap value without third variable](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SwapValueWithoutUsingThirdVariable.c) - [Identify machine is big-endian or little-endian](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/endian.c) - - +- [Perform Selection Sort](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SelectionSort.c) +- [Calculate Factorial upto input of 5000](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/large_factorial.c) +- [To check if a matrix is a sparse matrix or not](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SparseMatrix_017.c) # Contributing This is a personal learning project for me. From 729f415461ab464acaf30397de5866ae050b436b Mon Sep 17 00:00:00 2001 From: Dan-Kode <55064257+Dan-Kode@users.noreply.github.com> Date: Mon, 14 Oct 2019 21:02:51 +0100 Subject: [PATCH 057/109] Added New File: increment_number.c Added: - increment_number.c - What goes on in this Program is that we have an Integer('num') with the Value of 0. Then there is another Integer('increment') with a Value of 1. We then have a While-Loop that says: "While num is less than 100, use the Integer, 'increment' to increment the Value of num by 1 every second". --- increment_number.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 increment_number.c diff --git a/increment_number.c b/increment_number.c new file mode 100644 index 0000000..6c35ecc --- /dev/null +++ b/increment_number.c @@ -0,0 +1,18 @@ +/* A C-Program that Increments a Number */ + +#include +#include // This Header is used so we can gain access to the sleep() Function + +int main(void) { + int num = 0; // This is the Number that will be incremented. + int increment = 1; // This is what we'll be using to increment(add) onto num. + + // While num's Value is less than 100, we'll increment the Value of num by 1 every second. + while(num < 100) { + sleep(1); // Wait 1 second each time, num has it's Value incremented + num += increment; // Increment num + printf("%d\n", num); // Print the current Value of num + } + + return 0; +} From bc09c5795a11aa04bae968833f33199033f58304 Mon Sep 17 00:00:00 2001 From: tripoloski1337 Date: Tue, 15 Oct 2019 20:17:53 +0700 Subject: [PATCH 058/109] add an example of malloc() --- dynamicMemoryAllocation.c | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 dynamicMemoryAllocation.c diff --git a/dynamicMemoryAllocation.c b/dynamicMemoryAllocation.c new file mode 100644 index 0000000..8c3b8b3 --- /dev/null +++ b/dynamicMemoryAllocation.c @@ -0,0 +1,11 @@ +#include +#include + +#define SIZE 0x100 +void main(){ + + char *ptr = malloc(SIZE); + snprintf(ptr , SIZE , "data : %s " , "hello world"); + printf("%s",ptr); + +} \ No newline at end of file From 73ff154d0da3476ec2f26c02a5748537cc0cbcc6 Mon Sep 17 00:00:00 2001 From: obiwan69 <42412299+obiwan69@users.noreply.github.com> Date: Tue, 15 Oct 2019 23:21:06 +0530 Subject: [PATCH 059/109] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2d8d81c..fdac8f2 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,7 @@ These program are written in codeblocks ide for windows. These programs are not - [Perform Selection Sort](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SelectionSort.c) - [Calculate Factorial upto input of 5000](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/large_factorial.c) - [To check if a matrix is a sparse matrix or not](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SparseMatrix_017.c) +- [To calculate the Least Common Multiple](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/lcm.c) # Contributing This is a personal learning project for me. From cdcaad3649300cab7286df7a1f422cbfe2d1345e Mon Sep 17 00:00:00 2001 From: OmangRawat Date: Wed, 16 Oct 2019 16:46:44 +0530 Subject: [PATCH 060/109] Fibonnaci Series --- omang_fibonacci.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 omang_fibonacci.c diff --git a/omang_fibonacci.c b/omang_fibonacci.c new file mode 100644 index 0000000..a6c854d --- /dev/null +++ b/omang_fibonacci.c @@ -0,0 +1,20 @@ + #include +void printFibonacci(int n){ + static int n1=0,n2=1,n3; + if(n>0){ + n3 = n1 + n2; + n1 = n2; + n2 = n3; + printf("%d ",n3); + printFibonacci(n-1); + } +} +int main(){ + int n; + printf("Enter the number of elements: "); + scanf("%d",&n); + printf("Fibonacci Series: "); + printf("%d %d ",0,1); + printFibonacci(n-2); + return 0; + } From 9b4296cd6acd0a5412eb996f5401174de8860289 Mon Sep 17 00:00:00 2001 From: Tanishq Chaudhary Date: Wed, 16 Oct 2019 20:21:31 +0530 Subject: [PATCH 061/109] Added Program to Calculate nCr of Large Numbers Using Modular Arithmetic --- a.out | Bin 8456 -> 0 bytes nCrCalculatorLargeNumbers.c | 52 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) delete mode 100755 a.out create mode 100644 nCrCalculatorLargeNumbers.c diff --git a/a.out b/a.out deleted file mode 100755 index 9a9c425c814a9fa2e11dd64318ef4802762261b8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8456 zcmeHMZ){Y@5#RIK_z3uX0Rs+@@Jew7X?@sWjLo0MXZy@^WrIs>(kj67`R;6Axj*jR zyT&F}f@Biu;y9oxQc%@OsA(gjMwR=a6>UFY3Ib9^6?~`~g+`)ni0nWVMrr&-y#siX0BX#VNinkn3GwAWhV8=^?5BX%Y=$ zA^bipRtpp>mYC7rWDsaI${jMz)J3=xDC#v78GxOxCOM|;A<`0RD@0rm7${RA6bYan z`g*cVvOpDCVLC*93=?HNWKnN|^d?A;=?po>l*fl-!>60<*=Z}ZVPT50Sh}w3A*`P# zLw34DN(sS~^}Y#u^vX!$-xCit@1_3ci%T#0&yrJgq3UQPo*u3qZm6%W zuk)r7-c54cXdh*F?e391;rr&$qR$oZAcpNXm#*3H?A1SBdH1)U4L<#gUw!`m`8R%p zy8J-(KQ3Pd$t4A(p9V}RfUg9sko@L(@b-D|R{&Swv6n>v6te#s;EG)s_-*j4Er1J% zcM!u{FElNZN(37lwNx+=?*}%e2ZD#R;NT&xKM;wEp|l6>$;n<;M@n= zxnsd$0A@K&IJ$>%;4*2!ah-6vWWjmuW7}m5PD7O{>gY|EI#zObp(51hCiSB1Wp(tt z>l~XC*)2bTrfk(u;kSHE6V#9&#P;cHStwOML>^t6zAX74AdexMo|OCx$fJwX6Ou>$ z{^P*QQu|?h=E^}e^EY+$y_v4=j`5O}psS9Ty$UAP@tyC3RCd`u(79EXli&5)6LqG7K(8gY(%tvU3{5M0%q-y7MB|deEQ6>e$Xd05??zL!MIM z=R(O@;2r4B5p?X6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XW=%c@swWLP$TheI4JU_$H=fYL zUSD&2wH_ECZXlla_N60mjU5RIFLHx{)S&Q&M&e-4pnB41`Cd4giX`HBj0SDVa5R7l zR2z!w!Yc#dg{pTT0jwS#hF_eYAe4|V%^M!1895jNBg`49#?&-4xDGu8Vv!*Dgzwx7 zu@zoe7BQIWxn=iX*YI2bOBTMNMroi5Q$80^vy|mJ(l5h) zSoJv%WV((8iQ4sB0ly8oIQP%_B2)IC&oB0LX*cNOS%vjaQ-$da^^4ms#)etxe%aouHhn(7GqtDd_VK??`rHlY z8BB3~T8mx(m(YmuWBvL5cVruO{WCUw^L#;$yKEDlBxQ4KBKFa?~+U1#N z`X|sShllm~KI0T!h_D>m#=<<)zXFC+h4uMd!~gs8|GPXtS&!!r=Jhy4)<0RU00-kh zr=ax8-`o%DGyMRXtomlYNgZ%MY=QNdpMeTu+&|}U9#XW++g^wG6m$`*fyY`{pYu#^ zV|^>-xx+F@o2dU93LJiC&=&i{`zKBl_8qEb8N$s1%C~IBxnH9dbL+zTm#BkT79?ud F{}&J4s +#define m 1000000007 +#include + +// This function calcualtes the large powers with O(log(n)) +long fastexp(long x, long y) +{ + // In the form of pow(x, y) % m + if(y == 0) + return 1; + else if (y == 1) + return x; + else + { + long ans = fastexp(x, y/2); + if(y % 2 == 0) + return (ans % m * ans % m) % m; + else + return ((ans % m * ans % m) % m * x % m) % m; + } +} + +long fact[2000001] = {1}; + +int main() +{ + // FACTORIAL CALCULATION + // Using (a * b) % m = (a % m * b % m) % m + for(int i = 1; i < 2000001; i++) + fact[i] = ((fact[i-1] % m) * (i % m)) % m; + + // Number of test cases + int T; + long n, r, ans; + printf("Enter the number of test cases: "); + scanf("%d", &T); + while(T--) + { + printf("Enter the two numbers in the form of xCy: "); + scanf("%ld%ld", &n, &r); + + // Formula used: (n)!/ (r! * (n-r)!) + // = (n+r)! * pow((r! * (n-r)!), -1) + // Using FERMAT's LITTLE THEOREM (Google it!) + // = (fact[n]%m * pow((r! * (n-r)!), m - 2) % m) % m + // = (fact[n]%m * fastexp((fact[r] * fact[n-r])%m, m - 2)%m)%m + + ans = (fact[n]%m * fastexp((fact[r]%m * fact[n-r]%m) % m, m - 2) % m) % m; + printf("%ld\n", ans); + } + return 0; +} From 97152088fe9c085d4e494f61cf6b8193109f3b89 Mon Sep 17 00:00:00 2001 From: Leandro F Date: Wed, 16 Oct 2019 23:53:07 -0300 Subject: [PATCH 062/109] =?UTF-8?q?calculate=20and=20print=20the=20volume?= =?UTF-8?q?=20and=20surface=20area=20of=20=E2=80=8B=E2=80=8Ba=20straight?= =?UTF-8?q?=20cone,=20a=20cylinder,=20or=20a=20sphere?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- volume_cone_sphere.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 volume_cone_sphere.c diff --git a/volume_cone_sphere.c b/volume_cone_sphere.c new file mode 100644 index 0000000..4ea4538 --- /dev/null +++ b/volume_cone_sphere.c @@ -0,0 +1,38 @@ +#include +#include +#include +#define pi 3.1415 + +int main() +{ + + float raio, altura, volume, area; + char tipo[10]; + + scanf("%f",&raio); + scanf("%f",&altura); + scanf("%s",tipo); + + if (strcmp(tipo,"CONE")==0){ + volume = ((pi * pow(raio,2)) * altura) / 3; + area = (pi * raio) * sqrt(pow(raio,2) + pow(altura,2)); + printf("VOLUME = %.2f\n",volume); + printf("AREA = %.2f\n",area); + } + +else if (strcmp(tipo,"CILINDRO")==0){ + volume = ((pi * pow(raio,2)) * altura); + area = (2 * pi) * (raio * altura); + printf("VOLUME = %.2f\n",volume); + printf("AREA = %.2f\n",area); + } + + else if (strcmp(tipo,"ESFERA")==0){ + volume = (pi * 4 * pow(raio,3)) / 3; + area = (4 * (pi * pow(raio,2))); + printf("VOLUME = %.2f\n",volume); + printf("AREA = %.2f\n",area); + } + +return 0; +} \ No newline at end of file From 9b78502b16d412b9ac614926d927d92902653591 Mon Sep 17 00:00:00 2001 From: Tanishq Chaudhary Date: Thu, 17 Oct 2019 19:21:29 +0530 Subject: [PATCH 063/109] Added prorgam to Calculate Modular Inverse --- NaiveModularInverse.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 NaiveModularInverse.c diff --git a/NaiveModularInverse.c b/NaiveModularInverse.c new file mode 100644 index 0000000..9383f81 --- /dev/null +++ b/NaiveModularInverse.c @@ -0,0 +1,23 @@ +#include + +int naiveModInv(int x, int y) +{ + // If there is an immediate value reduction + x %= y; + + for(int i = 1; i < y; i++) + if((x * i) % y == 1) + return i; +} + +int main() +{ + printf("Enter a and m: "); + int a, m; + scanf("%d%d", &a, &m); + + int ans = naiveModInv(a, m); + printf("%d\n", ans); + + return 0; +} From 112c9c3aa1fb76812ac44c7ec29b3ca58dcc9908 Mon Sep 17 00:00:00 2001 From: Tanishq Chaudhary Date: Thu, 17 Oct 2019 19:38:34 +0530 Subject: [PATCH 064/109] Added Program to calculate Modular Exponents Quickly --- fastModuloExponentiation.c | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 fastModuloExponentiation.c diff --git a/fastModuloExponentiation.c b/fastModuloExponentiation.c new file mode 100644 index 0000000..45179b7 --- /dev/null +++ b/fastModuloExponentiation.c @@ -0,0 +1,42 @@ +#include + +// Declaring a golbal variable as its going to be used again and again in the function +long long m; + +long long modder(long long x, long long y) +{ + // x to the power zero is 1 + if(y == 0) + return 1; + // x to the power one is x + else if(y == 1) + return x; + + // We now split the problem in two parts + // solve one of them and use its resultant for the second part + else + { + long long ans = modder(x, y/2); + // If the power is not divisible by 2, we simply multiply by x + // Using the concept: (a * b) % m = (a % m * b % m) % m + // This concept is used repeatedly when y % 2 != 0 + + if(y % 2 == 0) + return (ans % m) * (ans % m); + else + return (((ans % m) * (ans % m)) % m * (x % m)) % m; + } +} + +int main() +{ + printf("Enter three numbers, x to the power of y modulo m: "); + long long x, y, result; + scanf("%lld%lld%lld", &x, &y, &m); + + // We don't need to send m here now + result = modder(x, y); + + printf("The resultant is: %lld\n", result); + return 0; +} From 67a360a757f4b35d4b3ad82f355d4ff3496c682f Mon Sep 17 00:00:00 2001 From: HelioAdson Date: Fri, 18 Oct 2019 20:51:55 -0300 Subject: [PATCH 065/109] Add Number_to_Chacarter Program --- Number_to_Character.c | 64 +++++++++++++++++++++++++++++++++++++++++++ README.md | 1 + 2 files changed, 65 insertions(+) create mode 100644 Number_to_Character.c diff --git a/Number_to_Character.c b/Number_to_Character.c new file mode 100644 index 0000000..ccc1629 --- /dev/null +++ b/Number_to_Character.c @@ -0,0 +1,64 @@ +#include +#include + +//This program convert numbers into their corresponding characters + +void convertNumbertoChar(long int n); + +int main(){ + long int n; + printf("Please Enter the Number = "); + scanf("%ld",&n); + convertNumbertoChar(n); /* It's important to break your code in bloks, + So let's make a function that solve our problem*/ +} + +void convertNumbertoChar(long int n){ + long int r,sum = 0; + while(n > 0){ // This loop change the position of number so we can print in the righ order + r = (n % 10); // Decompose the number + sum = (sum * 10) + r; // To compose the number + n = n / 10; // n decrement + } + n = sum; + while(n > 0){ // Now we can print the characters + r = n % 10; // Selecting one number to print + switch(r){ // Print the corresponding character for each case + case 1: + printf("one "); + break; + case 2: + printf("two "); + break; + case 3: + printf("three "); + break; + case 4: + printf("four "); + break; + case 5: + printf("five "); + break; + case 6: + printf("six "); + break; + case 7: + printf("seven "); + break; + case 8: + printf("eight "); + break; + case 9: + printf("nine "); + break; + case 0: + printf("zero "); + break; + default: + printf("undefined"); + break; + } + n = n / 10; //Decrementing n, ensuring the condition to the loop break + } + printf("\n"); +} \ No newline at end of file diff --git a/README.md b/README.md index 2d8d81c..dadcc6a 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ These program are written in codeblocks ide for windows. These programs are not - [Decrement Operator](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DecrementOperator.c) - [Nested If Leap Year](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/NestedIfLeapYear.c) - [Nested if Greatest Integer](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/NestedifGreatestInteger.c) +- [Number to Character](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/Number-to-Character.c) - [Check Character Type](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/CheckCharacterType.c) - [Employee Grade](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/EmployeeGrade.c) - [Daily Wage Calc](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/DailyWageCalc.c) From 70738aa24a38d441d0dacc851d129930fb1a16a7 Mon Sep 17 00:00:00 2001 From: Manan <33518498+manan012@users.noreply.github.com> Date: Mon, 21 Oct 2019 19:13:37 +0530 Subject: [PATCH 066/109] Create pattern.c --- pattern.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 pattern.c diff --git a/pattern.c b/pattern.c new file mode 100644 index 0000000..64e5214 --- /dev/null +++ b/pattern.c @@ -0,0 +1,26 @@ +#include + +int main() +{ + int row, c, n, s; + + printf("Enter the number of rows in pyramid of stars you wish to see\n"); + scanf("%d", &n); + + s = n; + + for (row = 1; row <= n; row++) // Loop to print rows + { + for (c = 1; c < s; c++) // Loop to print spaces in a row + printf(" "); + + s--; + + for (c = 1; c <= 2*row - 1; c++) // Loop to print stars in a row + printf("*"); + + printf("\n"); + } + + return 0; +} From e428766b3bdd260ffca7d86e4705288be36e5223 Mon Sep 17 00:00:00 2001 From: CO18325 <42858969+CO18325@users.noreply.github.com> Date: Fri, 25 Oct 2019 08:14:41 +0530 Subject: [PATCH 067/109] ARRAY IMPLEMENTATION IT IS THE IMPLEMENTATION OF VARIOUS ARRAY FUNCTIONALITY WHICH INCLUDES: =>SEARCHING =>SORTING =>INSERTION =>DELETION =>TRAVERSAL --- ARRAY.c | 199 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 ARRAY.c diff --git a/ARRAY.c b/ARRAY.c new file mode 100644 index 0000000..c8c7734 --- /dev/null +++ b/ARRAY.c @@ -0,0 +1,199 @@ +//ARRAY simple operations +#include +#include + +int arr[100],i,j,position,n,element,choice; + +void menu(); + +void insertion(){ + printf("ENTER THE POSITION YOU WANT TO ENTER THE ELEMENT"); + scanf("%d",&position); + position=position-1; + if(position>n-1){ + printf("\n INVALID POSITION\n"); + menu(); + } + printf("ENTER THE ELEMENT "); + scanf("%d",&element); + for(i=n;i>=position;i--){ + arr[i+1]=arr[i]; + } + arr[position]=element; + n=n+1; + menu(); +} + +void traversing(){ + printf("\n THE ARRAY ENTERED IS:--\n"); + for(i=0;in-1){ + printf("\n INVALID POSITION\n"); + menu(); + } + for(i=position;in){ + printf("\n INVALID POSITION\n"); + menu(); + } + printf("\n ENTER THE ELEMENT :-"); + scanf("%d",&element); + arr[position]=element; + +} + +void linear_search(){ + printf("ENTER THE ELEMENT YOU WANT TO SEARCH\n"); + scanf("%d",&element); + for(i=0;i arr[j+1]){ + arr[j]=arr[j]+arr[j+1]; + arr[j+1]=arr[j]-arr[j+1]; + arr[j]=arr[j]-arr[j+1]; + } + } + } +} +void bubble_sort(){ + bubble_sort_algo(); + printf("\nTHE ARRAY HAS BEEN SORTED\n"); + menu(); +} + +void binary_search(){ + printf("ENTER THE ELEMENT YOU WANT TO SEARCH\n"); + scanf("%d",&element); + int beg,mid,end,check; + check=0; + beg=0; + end=n-1; + bubble_sort_algo(); + while(beg<=end){ + mid=(beg+end)/2; + if(arr[mid]==element){ + printf("ELEMENT FOUND AT POSITION: %d, AFTER THE SORTING",mid+1); + check=10; + break; + } + else if(arr[mid]>element){ + end = mid-1; + } + else if(arr[mid]arr[j]){ + arr[i]=arr[i]+arr[j]; + arr[j]=arr[i]-arr[j]; + arr[i]=arr[i]-arr[j]; + } + } + } + printf("ARRAY IS SORTED"); + menu(); +} + + +void menu(){ + while(1){ + printf("\n1. INSERTION\t 2. UPDATION\t 3. TRAVERSING\t 4. DELETION\t 5.EXIT\t 6. RECREATE THE ARRAY \n"); + printf("7. LINEAR SEARCH\t 8. BUBBLE SORT\t 9. BINARY SEARCH\t 10. SELECTION SORT\n\n"); + scanf("%d",&choice); + switch(choice){ + case 1: + insertion(); + break; + case 2: + updation(); + break; + case 3: + traversing(); + break; + case 4: + deletion(); + break; + case 5: + exit(1); + case 6: + main(); + break; + case 7: + linear_search(); + break; + case 8: + bubble_sort(); + break; + case 9: + binary_search(); + break; + case 10: + selection_sort(); + break; + default: + menu(); + break; + } + } + +} + +int main(){ + + printf("ENTER THE ELEMENTS OF ARRAY"); + i=0; + while(1){ + printf("\nENTER THE ELEMENT:-\n"); + scanf("%d",arr+i); + printf("\nDO YOU WANT TO ENTER ANOTHER ELEMENT PRESS ANY KEY ELSE PRESS 1\n"); + scanf("%d",&choice); + if(choice == 1){ + n=i+1; + menu(); + } + i++; + } + + return 0; +} From 21717a0622a2e5ada039c66203285e54de1f69f6 Mon Sep 17 00:00:00 2001 From: PJ Pollina Date: Fri, 25 Oct 2019 12:00:40 -0400 Subject: [PATCH 068/109] Add linear congruential PRNG example program --- LinearCongruentialGenerator.c | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 LinearCongruentialGenerator.c diff --git a/LinearCongruentialGenerator.c b/LinearCongruentialGenerator.c new file mode 100644 index 0000000..bdd7a54 --- /dev/null +++ b/LinearCongruentialGenerator.c @@ -0,0 +1,37 @@ +// A basic implementation of a linear congruential pseudorandom number generator in C + +#include +#include +#include + +struct lcg_rand { + unsigned int modulus; + unsigned int multiplier; + unsigned int increment; + unsigned int seed; + unsigned int last; +}; + +struct lcg_rand new_generator(unsigned int modulus, unsigned int multiplier, unsigned int increment) { + struct lcg_rand random; + random.modulus = modulus; + random.multiplier = multiplier; + random.increment = increment; + random.seed = time(NULL); + random.last = random.seed; + return random; +} + +int random(struct lcg_rand * random) { + int number = ((random->multiplier * random->last) + random->increment) % random->modulus; + random->last = number; + return number; +} + +int main() { + struct lcg_rand rng = new_generator(pow(2, 31), 1103515245, 12345); + for (int i = 0; i < 10; i++) { + printf("%d\n", random( & rng)); + } + return 0; +} From 6425cc236ecdd0c5c5215d0b4deeaff461bde39c Mon Sep 17 00:00:00 2001 From: shivang2k Date: Sat, 26 Oct 2019 13:38:35 +0530 Subject: [PATCH 069/109] Create linked_list.c --- linked_list.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 linked_list.c diff --git a/linked_list.c b/linked_list.c new file mode 100644 index 0000000..c961cfb --- /dev/null +++ b/linked_list.c @@ -0,0 +1,22 @@ +#include +#include + +struct node +{ + int data; + struct node* next; +}; +typedef struct node NODE; + +//NODE head = NULL; + + +NODE *first, *second, *third; + +int main() +{ + (*first).data = 5; + (*first).next = second; + + printf("%d", (*first).data); +} From 216bf9c2cd19521e8c9c03d969b6e997df0f0967 Mon Sep 17 00:00:00 2001 From: shivang2k Date: Sat, 26 Oct 2019 15:43:10 +0530 Subject: [PATCH 070/109] Create FIND_EVEN_ AND_DIVISIBLE_BY_3.c --- FIND_EVEN_ AND_DIVISIBLE_BY_3.c | 75 +++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 FIND_EVEN_ AND_DIVISIBLE_BY_3.c diff --git a/FIND_EVEN_ AND_DIVISIBLE_BY_3.c b/FIND_EVEN_ AND_DIVISIBLE_BY_3.c new file mode 100644 index 0000000..65d2019 --- /dev/null +++ b/FIND_EVEN_ AND_DIVISIBLE_BY_3.c @@ -0,0 +1,75 @@ +#include +#define MAX 5 + +int main() + { + int i,j = 0,num[MAX]; + printf("Enter numbers in the range of 1 to 9 \n"); + for(i = 0; i Date: Sat, 26 Oct 2019 18:53:22 +0530 Subject: [PATCH 071/109] kth smallest no in an array.c To print the kth smallest no in an array.c --- kth smallest no in an array.c | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 kth smallest no in an array.c diff --git a/kth smallest no in an array.c b/kth smallest no in an array.c new file mode 100644 index 0000000..1fa1ff7 --- /dev/null +++ b/kth smallest no in an array.c @@ -0,0 +1,68 @@ +#include +#include +void merge(int a[],int f,int m,int l) +{ + int n1=m+1-f; + int n2=l-m; + int t1[n1],t2[n2]; + for (int i = 0; it2[j]) + { + a[k]=t2[j]; + j++; + } + else + { + a[k]=t1[i]; + i++; + } + k++; + } + while(i Date: Sat, 26 Oct 2019 19:14:07 +0530 Subject: [PATCH 072/109] Convert an array to reduced form.c MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Given an array with n distinct elements, convert the given array to a reduced form where all elements are in the range from 0 to n-1. The order of elements is the same, i.e., 0 is placed in the place of smallest element, 1 is placed for second smallest element, … n-1 is placed for largest element. --- Convert an array to reduced form.c | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Convert an array to reduced form.c diff --git a/Convert an array to reduced form.c b/Convert an array to reduced form.c new file mode 100644 index 0000000..935c643 --- /dev/null +++ b/Convert an array to reduced form.c @@ -0,0 +1,36 @@ +#include +int main() +{ + int num;scanf("%d",&num); + while(num--) + { + int n;scanf("%d",&n); + int a[n],t[n],tmp,k=0; + for(int i=0;it[j+1]) + { + tmp=t[j]; + t[j]=t[j+1]; + t[j+1]=tmp; + } + } + for(int i=0;i Date: Sun, 27 Oct 2019 13:26:40 +0530 Subject: [PATCH 073/109] LinkedLists program --- LinkedLists.c | 323 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 323 insertions(+) create mode 100755 LinkedLists.c diff --git a/LinkedLists.c b/LinkedLists.c new file mode 100755 index 0000000..d4fe080 --- /dev/null +++ b/LinkedLists.c @@ -0,0 +1,323 @@ +//Normal LL with all fns +//kinda incomplete - work in progress +//TO ADD - fn to reverse the LL + + +#include +#include + +struct node +{ + int info; + struct node* link; +}; + +typedef struct node node; + +node *head=NULL, *head2=NULL; + +//*head=NULL; +//*head2=NULL; + +void Binsert(int); +void Einsert(int,node*); +void Minsert(int); +void Display(node*); +void Bdelete(); +void Edelete(); +void Mdelete(int); +void DeleteLL(node*); +//void copy(); +//node* locate(node*, int); +//void reverse(node*); +//void swaplink(node*); +//void len(node*); + + +int main() +{ + while(1) + { + int opt,ele; + 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"); + scanf("%d",&opt); + switch(opt) + { + case 1: + { + printf("\nEnter the element"); + scanf("%d",&ele); + Binsert(ele); + break; + } + case 2: + { + printf("\nEnter the element"); + scanf("%d",&ele); + Einsert(ele,head); + break; + } + case 3: + { + printf("\nEnter the element"); + scanf("%d",&ele); + Minsert(ele); + break; + } + case 4: + { + printf("\nEnter the LL(1/2)"); + scanf("%d",&ele); + if(ele==1) + Display(head); + else + Display(head2); + break; + } + case 5: + { + Bdelete(); + break; + } + case 6: + { + Edelete(); + break; + } + case 7: + { + printf("\nEnter the element you wanna delete:"); + scanf("%d",&ele); + Mdelete(ele); + break; + } + /*case 8: + { + copy(); + break; + } + case 9: + { + printf("\nEnter the LL(1/2)"); + scanf("%d",&ele); + if(ele==1) + DeleteLL(head); + else + DeleteLL(head2); + break; + } + case 10: + { + reverse(head); + break; + } + case 11: + { + swaplink(head); + } + + case 12: + { + exit(0); + }*/ + } + } + return 0; +} + +void Binsert(int ele) +{ +// if (head==NULL) +// return; + node* p=head; + node* temp=(node*)malloc(sizeof(node)); + temp->info=ele; + temp->link=head; + head=temp; +} + +void Einsert(int ele,node* temph) +{ + node* temp=(node*)malloc(sizeof(node)); + temp->info=ele; + node* p; + p=temph; + while(p->link!=NULL) + p=p->link; + temp->link=NULL; + p->link=temp; +} + +void Minsert(int ele) +{ + if(head==NULL) + { + Binsert(ele); + return; + } + if (head->info>ele) + { + Binsert(ele); + return; + } + node *p,*q,*temp=(node*)malloc(sizeof(node)); + temp->info=ele; + p=head; + q=head->link; + while(q!=NULL) //q->link!=null?????????????????????????????????????????? works?????????????????????????????????? + { + if(q->info>=ele) + { + p->link=temp; + temp->link=q; + return; + } + p=p->link; + q=q->link; + } + Einsert(ele,head); +} + +void Display(node* temph) +{ + node* p=temph; + while(p!=NULL) + { + printf("%d->",p->info); + p=p->link; + } + printf("\n"); +} + +void Bdelete() +{ + node* p=head; + head=head->link; + free(p); +} + +void Edelete() +{ + node *p,*q; + p=head; + while(p->link->link!=NULL) + p=p->link; + q=p->link; + p->link=NULL; + free(q); +} + +void Mdelete(int ele) +{ + if (head->info==ele) + { + Bdelete(); + return; + } + node *p=head,*q=head->link; + while(q->link!=NULL) //q->link!=null?????????????????????????????????????????? doesnt work + { + if(q->info==ele) + { + p->link=q->link; + free(q); + return; + } + } +} + +void DeleteLL(node* temph) +{ + node *p=temph,*q=temph->link; + while(q!=NULL) + { + free(p); + p=q; + q=q->link; + } + free(p); + temph=NULL; +} + +void copy() +{ + DeleteLL(head2); + node *p,*q; + p=head; + q=head2; + while(p!=NULL) + Einsert(p->info,head2); +} + +node* locate(node* head, int ind) +{ + node *p; + int x; + p= head->link; + x=0; + while(x!=ind||p!=NULL) + { + p=p->link; + x++; + } + return p; +} +/* +void reverse(node* head) +{ + printf("111111111111111111111111111111111111"); + node *x = head, *p, *q; + int n=0, temp=0,i; + for (i=0;x!=NULL;i++) + n++; + printf("222222222222222222222222222222222222"); + for(i=0;iinfo; + p->info=q->info; + q->info = temp;//mind fucked + } +} + + +void swaplink(node* head) +{ + node *p,*q; + q=head; + int leng=len(head); + int count=leng-1,i,n=0; + for(i=0;ilink; + } + q->link=p; + n++; + } +} + +int len(node* head) +{ + node* p=head; + int x=1; + while (p->link!=NULL) + { + p=p->link; + x++; + } + return x; +} + + + + */ + + + + + From b409b605dcf80f20bbc6b35f613f7a7facb1e9a0 Mon Sep 17 00:00:00 2001 From: Dvogiatz Date: Mon, 28 Oct 2019 01:45:34 +0200 Subject: [PATCH 074/109] Adding fractions algorithm --- Adding_Fractions.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Adding_Fractions.c diff --git a/Adding_Fractions.c b/Adding_Fractions.c new file mode 100644 index 0000000..f1f76b9 --- /dev/null +++ b/Adding_Fractions.c @@ -0,0 +1,28 @@ +#include +int main(){ + /* variable declaration */ + int numerator1, numerator2, denominator1, denominator2, +num_result, denom_result ; + /* Read each fraction */ + printf("Please provide the first numerator:\n"); + scanf("%d",&numerator1); + printf("Please provide the first denominator:\n"); + scanf("%d",&denominator1); + printf("Please provide the second numerator:\n"); + scanf("%d",&numerator2); + printf("Please provide the second denominator:\n"); + scanf("%d",&denominator2); + /*compare the demonators*/ + if ( denominator1 == denominator2 ) { + num_result = numerator1 + numerator2; + denom_result = denominator1; /* or 2, they are equal */ + } else { + num_result = (numerator1 * denominator2) + (numerator2 * denominator1 ); + denom_result = denominator1 * denominator2; + } + printf("The result of %d / %d + %d / %d is: %d / %d \n", numerator1,denominator1, numerator2, denominator2,num_result,denom_result); + return 0; +} + + + From 47916dfbd5a6db34d12a9f1c22a63fce90a485f8 Mon Sep 17 00:00:00 2001 From: Abhishek Jindal Date: Thu, 31 Oct 2019 08:10:32 +0000 Subject: [PATCH 075/109] Added C program to find the closest power of 2 for a number --- closestpowerof2.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 closestpowerof2.c diff --git a/closestpowerof2.c b/closestpowerof2.c new file mode 100644 index 0000000..ad9961f --- /dev/null +++ b/closestpowerof2.c @@ -0,0 +1,25 @@ +//Program for printing closest of 2 of a number. + +#include +#include + +int main() +{ + int num,a,b,c,d=0; + scanf("%d",&num); + for(b=31;b>=0;--b) + { + c=num>>b; + if(c&1) + { + d++; + if(d>1) + { + num=num-pow(2,b); + } + } + } + printf("%d",num); + return 0; +} +//not working for negative no. From 303a47c92b71c5090349305ceb47351e90a6c154 Mon Sep 17 00:00:00 2001 From: Srishti Guleria <61888364+sg7801@users.noreply.github.com> Date: Tue, 12 May 2020 17:55:14 +0530 Subject: [PATCH 076/109] Finding the first missing natural number Enter the squence of natural numbers. The very first missing natural number will be resulted by this program. If no number is missing then the very next natural number in the continuing sequence will be calculated. --- finding the first missing natural number | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 finding the first missing natural number diff --git a/finding the first missing natural number b/finding the first missing natural number new file mode 100644 index 0000000..36b99a9 --- /dev/null +++ b/finding the first missing natural number @@ -0,0 +1,32 @@ +#include +#include +int main() +{ int a,k=1,i,c,d,n,arr[10]; + +printf("Enter the no of elements:\n"); +scanf("%d",&n); +for(a=0;a Date: Fri, 15 May 2020 16:18:46 +0530 Subject: [PATCH 077/109] Calculate Simple Interest --- simple_interest.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 simple_interest.c diff --git a/simple_interest.c b/simple_interest.c new file mode 100644 index 0000000..4ed4cfa --- /dev/null +++ b/simple_interest.c @@ -0,0 +1,12 @@ +//Program to Calculate Simple Interest +#include + +#include + +void main() { + float P, R, T, SI; + scanf("%f%f%f", & P, & R, & T); + SI = (P * R * T) / 100; + printf("%f", SI); + getch(); +} From c856d843b9535200200cb736df3069ea66ba0331 Mon Sep 17 00:00:00 2001 From: Harshita Goyal <51262627+goelhr@users.noreply.github.com> Date: Thu, 2 Jul 2020 15:42:59 +0530 Subject: [PATCH 078/109] Update BasicArithmatic.c --- BasicArithmatic.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/BasicArithmatic.c b/BasicArithmatic.c index b0e95dd..7673695 100644 --- a/BasicArithmatic.c +++ b/BasicArithmatic.c @@ -1,21 +1,21 @@ // A simple arithmetic operation on two integers #include -int main() -{ - int number1,number2,addition,subtraction,multiplication,division,modulo; - printf("Enter two numbers :\n"); - scanf("%d%d",&number1,&number2); - addition = number1+number2; - subtraction = number1-number2; - multiplication = number1*number2; - division = number1/number2; - modulo = number1%number2; - printf("Addition of number 1 and number 2 : %d\n",addition); - printf("Subtraction of number 1 and number 2 : %d\n",subtraction); - printf("Multiplication of number 1 and number 2 : %d\n",multiplication); - printf("Division of number 1 and number 2 : %d\n",division); - printf("Modulo of number 1 and number 2 : %d\n",modulo); - return 0; + +int main() { + int number1, number2, addition, subtraction, multiplication, division, modulo; + printf("Enter two numbers :\n"); + scanf("%d%d", & number1, & number2); + addition = number1 + number2; + subtraction = number1 - number2; + multiplication = number1 * number2; + division = number1 / number2; + modulo = number1 % number2; + printf("Addition of number 1 and number 2 : %d\n", addition); + printf("Subtraction of number 1 and number 2 : %d\n", subtraction); + printf("Multiplication of number 1 and number 2 : %d\n", multiplication); + printf("Division of number 1 and number 2 : %d\n", division); + printf("Modulo of number 1 and number 2 : %d\n", modulo); + return 0; } From 188a4c704d636e40d10ac7ed055330123b1386d2 Mon Sep 17 00:00:00 2001 From: Ritika Das <61988162+Ritika-Das@users.noreply.github.com> Date: Sat, 25 Jul 2020 19:40:05 +0530 Subject: [PATCH 079/109] Create Linked_List.c --- Linked_List.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 Linked_List.c diff --git a/Linked_List.c b/Linked_List.c new file mode 100644 index 0000000..ca92b99 --- /dev/null +++ b/Linked_List.c @@ -0,0 +1,124 @@ +/*Write a C program that uses functions to perform the following: +a) Create a singly linked list of integers +b) Delete a given integer from the above linked list +c) Display the contents of the above list after deletion +CODE:-*/ +#include +#include +struct node +{ + int data; + struct node *next; +}; +struct node *head, *tail = NULL; +void addNode(int data) +{ + struct node *newNode=(struct node*)malloc(sizeof(struct node)); + newNode->data=data; + newNode->next=NULL; + if(head==NULL) + { + head=newNode; + tail=newNode; + } + else + { + tail->next = newNode; + tail = newNode; + } +} +void display() +{ + struct node *current = head; // the node current will point to head + if(head==NULL) + { + printf("List is empty\n"); + return; + } + printf("\nNodes of singly linked list: \n"); + while(current!= NULL) + { + printf("%d ", current->data); // each node is printed + current = current->next; + } + printf("\n"); +} +void delete() +{ + struct node *ptr,*ptr1; + int item,i=0,flag,loc,j; + ptr = head; + if(ptr == NULL) + { + printf("\nEmpty List\n"); + } + else + { + printf("\nWhich integer do you want to delete?\n"); + scanf("%d",&item); + while (ptr!=NULL) + { + if(ptr->data == item) + { + printf("Integer found at node %d ",i+1); + flag=0; + loc=i; + break; + } + else + { + flag=1; + } + i++; + ptr = ptr -> next; + } + if(flag==1) + { + printf("Integer not found!\n"); + } + } + ptr=head; + for(j=0;jnext; + if(ptr == NULL) + { + printf("\nDeletion is not possible!"); + return; + } + } + ptr1->next = ptr ->next; + free(ptr); + printf("\nDeleted node %d \n",loc+1); +} +int main() +{ + int n,m,data; + printf("How many nodes do you want to create?\n"); + scanf("%d",&n); + for(int i=0;i Date: Sun, 20 Sep 2020 16:40:16 +0530 Subject: [PATCH 080/109] Create Number_guessing_game C program to guess a number --- Number_guessing_game | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Number_guessing_game diff --git a/Number_guessing_game b/Number_guessing_game new file mode 100644 index 0000000..20d701b --- /dev/null +++ b/Number_guessing_game @@ -0,0 +1,35 @@ +// Number guessing Game + +#include +int main() +{ + int secretNumber = 6; + int guess; + int guessCount = 0; + int guessLimit = 5; + int outOfGuesses = 0; + + while(guess != secretNumber && outOfGuesses == 0) + { + if(guessCount < guessLimit) + { + printf("Enter a number :"); + scanf("%d",&guess); + guessCount++; + } + else + { + outOfGuesses = 1; + } + } + if(outOfGuesses == 1) + { + printf("\n Out Of Guesses "); + } + else + { + printf("\n You won!"); + } + return 0; + +} From 9985816fcee413038ea3c6b2f8ffc926a445c692 Mon Sep 17 00:00:00 2001 From: Deepansh Agrawal Date: Mon, 28 Sep 2020 01:21:48 +0530 Subject: [PATCH 081/109] Create camelcase --- camelcase | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 camelcase diff --git a/camelcase b/camelcase new file mode 100644 index 0000000..8bc1427 --- /dev/null +++ b/camelcase @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +int main() +{ + char s[100],fs[100]; + gets(s); + /*int l=strlen(s); + char fins[l-1]; + int i=0; + while(s[i]!='_') + { + k++; + fins[i]=s[i]; + i++; + } + fins[i]=toupper(s[k+1]); + for(z=i+1;z65&&int(s[i]<90)) + { + fs[k++]='_'; + fs[k++]=tolower(s[i]); + } + else + { + fs[k++]=s[i]; + } +} +puts(fs); + return 0; +} + From 7e453659c1631698fa0be3912b79b0aca3c93695 Mon Sep 17 00:00:00 2001 From: Shrutijain005 <71955906+Shrutijain005@users.noreply.github.com> Date: Thu, 1 Oct 2020 10:09:05 +0530 Subject: [PATCH 082/109] Slicestring Writing a function slice() to slice a string. It will change the original string such that it is now the sliced strings. Taking m and n as the start and ending position for slice. --- Slicestring | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Slicestring diff --git a/Slicestring b/Slicestring new file mode 100644 index 0000000..32b3fdd --- /dev/null +++ b/Slicestring @@ -0,0 +1,18 @@ +#include +void slice(char *st, int m, int n) +{ + int i = 0; + while ((m + i) < n) + { + st[i] = st[m + i]; + i++; + } + st[i] = '\0'; +} +int main() +{ + char st[] = "Hello"; + slice(st, 1, 4); + printf("%s", st); + return 0; +} From 5393c3c23b232418b14f2b2e429372135bc48c27 Mon Sep 17 00:00:00 2001 From: nivanjanapramod <48645516+nivanjanapramod@users.noreply.github.com> Date: Thu, 1 Oct 2020 15:50:20 +0530 Subject: [PATCH 083/109] Create MostFrequentWordInString.c --- MostFrequentWordInString.c | 65 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 MostFrequentWordInString.c diff --git a/MostFrequentWordInString.c b/MostFrequentWordInString.c new file mode 100644 index 0000000..7b086b2 --- /dev/null +++ b/MostFrequentWordInString.c @@ -0,0 +1,65 @@ +#include +#include + +using namespace std; + +int main() +{ + cout << "Value of inputs in your array?" << endl; + int arraysize; + cin >> arraysize; + + cout << "Input array elements:" << endl; + + string array[arraysize]; + + for(int i=0; i> array[i]; + } + + int most_occur = 1; + + for(int i=0; imost_occur) + { + most_occur = times_occur; + } + + } + + if(most_occur>1) + { + cout << "Most occuring: "; + for(int i=0; i Date: Thu, 1 Oct 2020 21:30:39 +0530 Subject: [PATCH 084/109] Add files via upload --- CalculateSimpleInterest.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 CalculateSimpleInterest.c diff --git a/CalculateSimpleInterest.c b/CalculateSimpleInterest.c new file mode 100644 index 0000000..b28808f --- /dev/null +++ b/CalculateSimpleInterest.c @@ -0,0 +1,19 @@ +#include +main() +{ + int principleAmount; + int numberOfYears; + int rateOfInterest; + int SimpleInterest; + printf("enter principleAmount\n",principleAmount); + scanf("%d",&principleAmount); + printf("enter number of year\n",numberOfYears); + scanf("%d",&numberOfYears); + printf("enter rate of interest\n",rateOfInterest); + scanf("%d",&rateOfInterest); + + SimpleInterest=(principleAmount*numberOfYears*rateOfInterest)/100; + printf("Simple interest is %d",SimpleInterest); + + +} From 5e926bb879ac30e406310437796e6a61c6291d3e Mon Sep 17 00:00:00 2001 From: sonal10red100 <34841035+sonal10red100@users.noreply.github.com> Date: Fri, 2 Oct 2020 20:56:39 +0530 Subject: [PATCH 085/109] Create binary_to_octal.c --- binary_to_octal.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 binary_to_octal.c diff --git a/binary_to_octal.c b/binary_to_octal.c new file mode 100644 index 0000000..8cbf0c1 --- /dev/null +++ b/binary_to_octal.c @@ -0,0 +1,35 @@ +#include +#include + +int convert_to_octal(long long binary); + +int main() { + long long binary; + printf("Enter a binary number: "); + scanf("%lld", &binary); + printf("%lld in binary = %d in octal", binary, convert(binary)); + return 0; +} + +int convert_to_octal(long long binary) { + int octal = 0, decimal = 0, i = 0; + + //Step 1> convert binary to decimal + //Step 2> convert decimal to octal + + // converting binary to decimal + while (binary != 0) { + decimal += (binary % 10) * pow(2, i); + ++i; + binary /= 10; + } + i = 1; + + // converting to decimal to octal + while (decimal != 0) { + octal += (decimal % 8) * i; + decimal /= 8; + i *= 10; + } + return octal; +} From 86fc2b6b57d646476b57c7b889a4823e4cd1edd0 Mon Sep 17 00:00:00 2001 From: PranjalBagal <57905574+PranjalBagal@users.noreply.github.com> Date: Fri, 2 Oct 2020 22:30:48 +0530 Subject: [PATCH 086/109] Create StringReverse.c C program to reverse a given string . --- StringReverse.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 StringReverse.c diff --git a/StringReverse.c b/StringReverse.c new file mode 100644 index 0000000..16834b6 --- /dev/null +++ b/StringReverse.c @@ -0,0 +1,22 @@ +#include +#include +void main() +{ + int i, j, k; + char str[100]; + char rev[100]; + printf("Enter a string:\t"); + scanf("%s", str); + printf("The original string is %s\n", str); + for(i = 0; str[i] != '\0'; i++); + { + k = i-1; + } + for(j = 0; j <= i-1; j++) + { + rev[j] = str[k]; + k--; + } + printf("The reverse string is %s\n", rev); + getch(); +} From 0d075048ccf9aa9121812fa0854f791e3121ccb4 Mon Sep 17 00:00:00 2001 From: Tej Pratap Yadav <60545022+tyadav4268@users.noreply.github.com> Date: Sun, 4 Oct 2020 07:22:13 +0530 Subject: [PATCH 087/109] Update Address of 1-D array Corrected the code to return address of each element of array and with the help of printf statement and comments made the code more descriptive. --- Address of 1-D array | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Address of 1-D array b/Address of 1-D array index bad40b3..3ccbda6 100644 --- a/Address of 1-D array +++ b/Address of 1-D array @@ -1,17 +1,30 @@ +//The & operator is used to get the address. +//But in case of array the name of array itself returns its address. +//In array the elements occupy consecutive address, +//therefore incrementing it by 1 each time would give +//the address of next element. + #include int main() { int a[100],i,n,*add; - printf("enter the size"); + + printf("enter the size: "); scanf("%d",&n); - printf("enter the no"); + + printf("enter the numbers: \n"); for(i=0;i Date: Sun, 4 Oct 2020 19:12:04 +0530 Subject: [PATCH 088/109] Create stack_using_linklist This is data structure in C --- stack_using_linklist | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 stack_using_linklist diff --git a/stack_using_linklist b/stack_using_linklist new file mode 100644 index 0000000..296c55b --- /dev/null +++ b/stack_using_linklist @@ -0,0 +1,80 @@ +#include +#include + +struct node +{ + int data; + struct node* link; +} *top=NULL; +void push(int x) +{ + + struct node *new; + new=(struct node*)malloc(sizeof(struct node)); + new->data=x; + new->link=top; + top=new; +} +void display() +{ + + struct node *temp; + temp=top; + + if(top==NULL) + { + printf("Underflow"); + } + else + { + while(temp!=NULL) + { + + printf("%d",temp->data); + printf("\n"); + temp=temp->link; + } + } +} +void pop() +{ + + struct node *temp; + + if(top==NULL) + { + printf("No element to deletes "); + } + else + { + temp=top; + printf("Popped element is %d\t",temp->data); + top=top->link; + free(temp); + } +} + +int main() +{ + + + int x; + int ch; + do{ + printf("\t1.Push\t2.pop\t3.display\t4.exit\t"); + scanf("%d",&ch); + switch(ch) + { + case 1:printf(" Enter element\t"); + scanf("%d",&x); + push(x); + break; + case 2:pop(); + break; + case 3:display(); + break; + case 4:exit(0); + } +}while(ch!=0); +getch(); +} From 0cef5263d13a9bb7ff68401e18680ec352c88ba5 Mon Sep 17 00:00:00 2001 From: Mansi-1120 <71720551+Mansi-1120@users.noreply.github.com> Date: Tue, 6 Oct 2020 14:50:41 +0530 Subject: [PATCH 089/109] Create check functon is even or odd in c programming simple program for learning c language --- check functon is even or odd in c programming | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 check functon is even or odd in c programming diff --git a/check functon is even or odd in c programming b/check functon is even or odd in c programming new file mode 100644 index 0000000..78edb3c --- /dev/null +++ b/check functon is even or odd in c programming @@ -0,0 +1,14 @@ +#include +void main() +{ +int n; +printf("enter the value of n"); +scanf("%d",&n); +if (n % 2 == 0) +{ +printf("n is even number"); +} +else; +{ +printf{"n is odd number"); +}} From ad3bfaeb02f579780785859540a0e8482a641159 Mon Sep 17 00:00:00 2001 From: Mansi-1120 <71720551+Mansi-1120@users.noreply.github.com> Date: Tue, 6 Oct 2020 15:00:55 +0530 Subject: [PATCH 090/109] Create to find average & sum of a,b,c in one function you can learn how to find sum and average in one program. --- to find average & sum of a,b,c in one function | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 to find average & sum of a,b,c in one function diff --git a/to find average & sum of a,b,c in one function b/to find average & sum of a,b,c in one function new file mode 100644 index 0000000..2d687e9 --- /dev/null +++ b/to find average & sum of a,b,c in one function @@ -0,0 +1,14 @@ +#include +void main() +{ +int a,b,c,sum,average; +printf("enter the values of a,b,c"); +scanf("%d",&a); +scanf("%d",&b); +scanf("%d",&c); +sum = a+b+c; +printf("%d\n",sum); +{ +average = (a+b+c)%3; +printf("%d\n",average); +}} From ee365ce25c80929a97fd9f2424876dcc9736fca1 Mon Sep 17 00:00:00 2001 From: Rochisha24 <72006586+Rochisha24@users.noreply.github.com> Date: Thu, 8 Oct 2020 23:32:34 +0530 Subject: [PATCH 091/109] Create sparse_matrix.c --- sparse_matrix.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 sparse_matrix.c diff --git a/sparse_matrix.c b/sparse_matrix.c new file mode 100644 index 0000000..9934c07 --- /dev/null +++ b/sparse_matrix.c @@ -0,0 +1,43 @@ +#include +#define Row 4 +#define Col 3 +void +spiral_matrix(int r, int c, int arr[Row][Col]) +{ + int i; + int k = 0, l = 0; + while (k < r && l < c) + { + for (i = l; i < c ; ++i) { + printf("%d\t", arr[k][i]); + } + k++; + + for (i = k; i < r; ++i) { + printf("%d\t", arr[i][c - 1]); + } + c--; + if (k < r) { + for (i = c - 1; i >= l; --i) { + printf("%d\t", arr[r - 1][i]); + } + r--; + } + if (l < c) { + for (i = r - 1 ; i >= k; --i) { + printf("%d\t", arr[i][l]); + } + l++; + } + } +} + +int +main() +{ + int a[Row][Col] = {{1, 2, 3}, {10, 20, 30}, {110, 220, 330}, {1100, 2200, 3300}}; + + spiral_matrix (Row, Col, a); + return (0); +} + From 2345c3fed16214042c707405da0aeb18b508f0b9 Mon Sep 17 00:00:00 2001 From: Rochisha24 <72006586+Rochisha24@users.noreply.github.com> Date: Thu, 8 Oct 2020 23:44:01 +0530 Subject: [PATCH 092/109] Update and rename sparse_matrix.c to SpiralMatrix.c --- sparse_matrix.c => SpiralMatrix.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) rename sparse_matrix.c => SpiralMatrix.c (90%) diff --git a/sparse_matrix.c b/SpiralMatrix.c similarity index 90% rename from sparse_matrix.c rename to SpiralMatrix.c index 9934c07..0ffdea2 100644 --- a/sparse_matrix.c +++ b/SpiralMatrix.c @@ -1,8 +1,7 @@ #include #define Row 4 #define Col 3 -void -spiral_matrix(int r, int c, int arr[Row][Col]) +void spiral_matrix(int r, int c, int arr[Row][Col]) { int i; int k = 0, l = 0; @@ -32,8 +31,7 @@ spiral_matrix(int r, int c, int arr[Row][Col]) } } -int -main() +int main() { int a[Row][Col] = {{1, 2, 3}, {10, 20, 30}, {110, 220, 330}, {1100, 2200, 3300}}; From 1dd0042381815a84c0ffadf9121d5a3ff9d6b809 Mon Sep 17 00:00:00 2001 From: swt02026 Date: Fri, 9 Oct 2020 21:22:06 +0800 Subject: [PATCH 093/109] add lambda examples by compiler extension --- README.md | 2 +- lambda_in_c.c | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 lambda_in_c.c diff --git a/README.md b/README.md index fdac8f2..edcadc6 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ These program are written in codeblocks ide for windows. These programs are not - [Calculate Factorial upto input of 5000](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/large_factorial.c) - [To check if a matrix is a sparse matrix or not](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/SparseMatrix_017.c) - [To calculate the Least Common Multiple](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/lcm.c) - +- [Lambda in C](https://github.com/gouravthakur39/beginners-C-program-examples/blob/master/lambda_in_c.c) # Contributing This is a personal learning project for me. diff --git a/lambda_in_c.c b/lambda_in_c.c new file mode 100644 index 0000000..e963ddf --- /dev/null +++ b/lambda_in_c.c @@ -0,0 +1,17 @@ + +int main(){ +#if defined(__clang__) +// clang -fblocks lambda_in_c.c -lBlocksRuntime +int (^max)(int x, int y) = ^(int x, int y) { + return x>y ? x:y; +}; +#elif defined(__GNUC__) || defined(__GNUG__) +// gcc -std=gnu11 lambda_in_c.c +int (*max)(int, int) = + ({ + int __fn__ (int x, int y) { return x > y ? x : y; } + __fn__; + }); +#endif + printf("%d",max(1,23)); +} From 0cabe20194ce32ba434a73e6cec96d3598749607 Mon Sep 17 00:00:00 2001 From: Adarsh Kishore Mehra Date: Wed, 28 Oct 2020 17:35:22 +0530 Subject: [PATCH 094/109] Log_conversion I've Added Logarithm Conversion. Kindly review and merge it into your master branch. --- Log_Conversion.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Log_Conversion.c diff --git a/Log_Conversion.c b/Log_Conversion.c new file mode 100644 index 0000000..74cf165 --- /dev/null +++ b/Log_Conversion.c @@ -0,0 +1,17 @@ +#include +#include + +int main(int argc, const char * argv[], value) +{ + /* Define temporary variables */ + double value; + double result; + + /* Calculate the log of the value */ + result = log(value); + + /* Display the result of the calculation */ + printf("The Natural Logarithm of %f is %f\n", value, result); + + return 0; +} \ No newline at end of file From f6383ffe090ad7a5918b839421cfc66fa16e4749 Mon Sep 17 00:00:00 2001 From: anishtilak16 Date: Sat, 19 Dec 2020 21:37:07 +0530 Subject: [PATCH 095/109] Linked list file added --- a.out | Bin 0 -> 50840 bytes linkedlist.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100755 a.out create mode 100644 linkedlist.cpp diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..18c05e3be330a139595229424d2d6acbda0a1db9 GIT binary patch literal 50840 zcmeI5VQdt~702gn6I?>zOpOA9sF$=vP!wZRLrenEnXqB6v5etNndGS3$+c>=NTE`yQYjxIwW>;6sZm=c6q=mc4=rEP^h2all~%4W66z-_C0yT| zoxS7sY!p%TL#6*nv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emented using classes */ + +using namespace std; + +class Node +{ +public: + int data; + Node *next = NULL; +}; + +class LinkedList +{ +public: + Node *head = NULL; + Node *tail = NULL; +}; + +void addNode(LinkedList* list, Node* node) +{ + if(list->head == NULL && list->tail == NULL) { // For the first node only + list->tail = node; + list->head = node; + } + else { + list->tail->next = node; + list->tail = node; + } +} + +void display(LinkedList* list) +{ + Node *ptr = list->head; + + while (ptr != NULL) { // Loop to traverse the list + cout << ptr->data << " "; + ptr = ptr->next; + } +} + +void search(LinkedList* list, int value) +{ + Node *ptr = list->head; + + while(ptr != NULL) { + if(value == ptr->data) { + cout << "Value exists in the list" << endl; + break; + } + ptr = ptr->next; + } + if(ptr == NULL) + cout << "Value does not exist in the list" << endl; +} + +int main() +{ + LinkedList example_list; + + int arr[] = {5, 8, -2, 66, 78, 21, 90, 0, 2}; + + for(int i = 0; i < 9; i++) { + Node *temp_node = new Node(); + temp_node->data = arr[i]; + addNode(&example_list, temp_node); + } + + search(&example_list, 78); + search(&example_list, 9); + + display(&example_list); + + return 0; +} \ No newline at end of file From 2e2a9e76c75389dac233f08d36ac8be99ab58b50 Mon Sep 17 00:00:00 2001 From: anishtilak16 Date: Sat, 19 Dec 2020 21:47:34 +0530 Subject: [PATCH 096/109] linked list file added --- a.out | Bin 50840 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100755 a.out diff --git a/a.out b/a.out deleted file mode 100755 index 18c05e3be330a139595229424d2d6acbda0a1db9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 50840 zcmeI5VQdt~702gn6I?>zOpOA9sF$=vP!wZRLrenEnXqB6v5etNndGS3$+c>=NTE`yQYjxIwW>;6sZm=c6q=mc4=rEP^h2all~%4W66z-_C0yT| zoxS7sY!p%TL#6*nvor6_yf^Rtc6Pqa-5)=^d1s{%v34Ou8`X7GuRb8e*{MgVC&Wgo zCRHg%1`is?j3b9w+gxJJ;*t{k*-8sYIcmHx+B`+sC!2Lb%PUCDl|oWQO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FyG@!++V1AEcD&W6+dKXim~(#pgy5>SOD$`6ATZG5Y{%c*U$q^79jdhOfpKmY_l00ck)1V8`;KmY_l00ck)1V8`;KmY_l00ck)1V8`;KmY_l z00ck)1V8`;KmY_l00ck)1V8`;KmY_l00ck)1V8`;KmY_l00ck)1V8`;KmY_l00ck) z1paFR8TwfX{>6osqW`x&s$aE?J~|IF2!H?xfB*=900@8p2!H?xfB*=900@8p2!H?x zfB*=900@8p2!H?xfB*=900@8p2!H?xfB*=900@8p2!H?xfB*=900@8p2!H?xfB*=9 z00@8p2!H?xfB*=9z{e-h^;qKYr$sSdj2~7;T}dgA4W|n^&lq{`NV<@ghB)kXURbQ@ z5}Rwxa7C-$A8-qv9J4&9Kb3d=1AWFxQ4-21?f2V-vR@aRd?uH&(|%u{owZ8RFIled zrEJ4ELb}h93)hwf&o4RFcxqIR_DUnYTZYy-FZJ;(?NPGZB}bmOg_nrNn?E()l*77Y z=5#J2J-<9A$J~6zDT&gCIU=Ky?jJ5>oM$fze!&R zZ-d?ek@u*wo)cd0IwB$M2}@eOGvU(9)O#%^#R}0TMc26H=EVy z7NgvDOgz!X^V^>kU)=3ER>{te^z{z8`L8;eA=mR=@oH?iLHRZEVr4Q+y=mHrnj&Un z8P_Z3tkX?{;@h!yaqgsO7ysf%^^|DuxIRI>e^1cL=T5$O^n0;~Z^lwV+(r8Fi#1fU z`-0!Wpum-UQOlQD?hv9||L(;HEUyw`U@Dlu!19B%KeWv0ttDOc>0o@5mbB{zQ3<6=bqeMEjQV&6rcW1`{z z^AY*5l7)JUWGZAt?j#-I5UZ&^M74|RI;x+fx`ygns+?Un)qBlN2s($B-Xb*cA9L?(QlXUP|KApNQo!qXKb6+N@ke60IBQp-2#ARE_y)J_|ICN Date: Sat, 30 Jan 2021 19:21:17 +0330 Subject: [PATCH 097/109] Update Polynomial_linklist.c --- Polynomial_linklist.c | 88 ++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 39 deletions(-) diff --git a/Polynomial_linklist.c b/Polynomial_linklist.c index 4cb734b..5eca74b 100644 --- a/Polynomial_linklist.c +++ b/Polynomial_linklist.c @@ -1,47 +1,57 @@ #include #include #include -struct Node { //node structure for polynomial - int coeff; - int exp; //exponent - struct Node * next; //pointing to next node -}* poly = NULL; //type pointer polynomial -void create() { //creating polyno mial - struct Node * t, * last = NULL; //temporary pointer, last pointer - int num, i; - printf("Enter number of terms"); - scanf("%d", & num); - printf("Enter each term with coeff and exp\n"); - for (i = 0; i < num; i++) { //loop - t = (struct Node * ) malloc(sizeof(struct Node)); //create new node - scanf("%d%d", &t->coeff, &t->exp); //reading 2 data - t-> next = NULL; //linking each node into linklist - if (poly == NULL) { //first node check - poly = last = t; - } else { - last -> next = t; - last = t; +struct Node { //node structure for polynomial + int coeff; + int exp; //exponent + struct Node *next; //pointing to next node +} *poly = NULL; //type pointer polynomial +void create(void) +{ //creating polyno mial + struct Node *t, *last = NULL; //temporary pointer, last pointer + int num, i; + printf("Enter number of terms"); + scanf("%d", &num); + printf("Enter each term with coeff and exp\n"); + for (i = 0; i < num; i++) { //loop + t = (struct Node *) malloc(sizeof(struct Node)); //create new node + scanf("%d%d", &t->coeff, &t->exp); //reading 2 data + t->next = NULL; //linking each node into linklist + if (poly == NULL) { //first node check + poly = last = t; + } else { + last->next = t; + last = t; + } } - } } -void Display(struct Node * p) { - while (p) { - printf("%dx%d +", p -> coeff, p -> exp); //printing node - p = p -> next; //shifting node - } - printf("\n"); + +void Display(struct Node *p) +{ + while (p) { + printf("%dx%d +", p->coeff, p->exp); //printing node + p = p->next; //shifting node + } + printf("\n"); } -long Eval(struct Node * p, int x) { //evalution - long val = 0; - while (p) { //scanning through polynomial - val += p -> coeff * pow(x, p -> exp); - p = p -> next; - } - return val; + +long Eval(struct Node *p, int x) +{ //evalution + long val = 0; + while (p) { //scanning through polynomial + val += p->coeff * pow(x, p->exp); + p = p->next; + } + return val; } -int main() { - create(); - Display(poly); - printf("%ld\n", Eval(poly, 1)); - return 0; + +// TODO insert +// TODO delete + +int main(void) +{ + create(); + Display(poly); + printf("%ld\n", Eval(poly, 1)); + return 0; } From 831b63911a91d879d5561dd77a100cef5cea9c2a Mon Sep 17 00:00:00 2001 From: Tejas Magade <53045273+thetejasmagade@users.noreply.github.com> Date: Mon, 17 May 2021 09:46:31 +0530 Subject: [PATCH 098/109] updated gcd.c file with better functionality --- gcd.c | 63 +++++++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/gcd.c b/gcd.c index c7b962e..5f7277a 100644 --- a/gcd.c +++ b/gcd.c @@ -1,34 +1,37 @@ -#include -#include + #include + #include + #include -long long get_gcd_recursive(long long a, long long b) -{ - if (a == 0) { - return b; - } else if (b == 0) { - return a; - } + int gcd(int x, int y) { + int r = 0, a, b; + a = (x > y) ? x : y; // a is greater number + b = (x < y) ? x : y; // b is smaller number + r = b; - /* Swap the two numbers */ - if (b > a) { - a = a - b; - b = a + b; - a = b - a; - } + while (a % b != 0) { + r = a % b; + a = b; + b = r; + } + return r; + } - long long remainder = a % b; - if (remainder == 0) { - return b; - } + int lcm(int x, int y) { + int a; + a = (x > y) ? x : y; // a is greater number + while (1) { + if (a % x == 0 && a % y == 0) + return a; + ++a; + } + } - return get_gcd_recursive(b, remainder); -} - -int main() -{ - long long a = 0; - long long b = 0; - scanf("%lld", &a); - scanf("%lld", &b); - printf("%lld", get_gcd_recursive(a, b)); -} \ No newline at end of file + int main(int argc, char **argv) { + printf("Enter the two numbers: "); + int x, y; + scanf("%d", &x); + scanf("%d", &y); + printf("The GCD of two numbers is: %d", gcd(x, y)); + printf("The LCM of two numbers is: %d", lcm(x, y)); + return 0; + } From 09bc281eb3483ffc6a183e1744e29977a27cf11c Mon Sep 17 00:00:00 2001 From: Tejas Magade <53045273+thetejasmagade@users.noreply.github.com> Date: Mon, 17 May 2021 09:54:13 +0530 Subject: [PATCH 099/109] Create ip_address.c This C program helps you to find which type of IP address it is. --- ip_address.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 ip_address.c diff --git a/ip_address.c b/ip_address.c new file mode 100644 index 0000000..e126174 --- /dev/null +++ b/ip_address.c @@ -0,0 +1,51 @@ +#include +#include + +void extractIpAddress(unsigned char *sourceString,short *ipAddress) +{ + unsigned short len=0; + unsigned char oct[4]={0},cnt=0,cnt1=0,i,buf[5]; + + len=strlen(sourceString); + for(i=0;i=0 && ipAddress[0]<=127) + printf("Class A Ip Address.\n"); + if(ipAddress[0]>127 && ipAddress[0]<191) + printf("Class B Ip Address.\n"); + if(ipAddress[0]>191 && ipAddress[0]<224) + printf("Class C Ip Address.\n"); + if(ipAddress[0]>224 && ipAddress[0]<=239) + printf("Class D Ip Address.\n"); + if(ipAddress[0]>239) + printf("Class E Ip Address.\n"); + + return 0; +} From c4806529c373ca6e88cf331013c4577baa3fd715 Mon Sep 17 00:00:00 2001 From: C C Nipun Das Date: Fri, 1 Oct 2021 20:01:19 +0530 Subject: [PATCH 100/109] added a beautiful code for lexicographic sorting --- Lexicographic_Sorting.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Lexicographic_Sorting.c diff --git a/Lexicographic_Sorting.c b/Lexicographic_Sorting.c new file mode 100644 index 0000000..c0dc575 --- /dev/null +++ b/Lexicographic_Sorting.c @@ -0,0 +1,38 @@ +// Lexicographic sorting is the way of sorting words based on the alphabetical order of their component letters. + +#include +#include +void main() +{ + char str[20][20], temp[20]; + int n, i, j; + printf("Enter the Number of Strings:\n"); + scanf("%d", &n); + + // Getting strings input + printf("Enter the Strings:\n"); + for (i = 0; i < n; i++) + { + scanf("%s", str[i]); + } + + // storing strings in the lexicographical order + for (i = 0; i < n - 1; i++) + { + for (j = 0; j < n - 1 - i; j++) + { + if (strcmp(str[j], str[j + 1]) > 0) + { + // swapping strings if they are not in the lexicographical order + strcpy(temp, str[j]); + strcpy(str[j], str[j + 1]); + strcpy(str[j + 1], temp); + } + } + } + printf("Strings in the Lexicographical Order is:\n"); + for (i = 0; i < n; i++) + { + puts(str[i]); + } +} From a1a06dde4860b2d6c395e7c1c53070ac2604ff7a Mon Sep 17 00:00:00 2001 From: jeremie1112 Date: Sun, 3 Oct 2021 10:19:15 +0400 Subject: [PATCH 101/109] added a program to get all PIDs that is used by parent and child in a system --- getPIDs.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 getPIDs.c diff --git a/getPIDs.c b/getPIDs.c new file mode 100644 index 0000000..d9c02bf --- /dev/null +++ b/getPIDs.c @@ -0,0 +1,16 @@ +#include +#include +#include + +void main(){ + pid_t pid; //variable to store process id + pid_t ppid; //variable to store parent process id + + sleep(5); + + pid = getpid(); // returns pid + ppid = getppid(); //returns parent pid + + printf("The process id is : %d\n" , pid); + printf("The parent of process id is : %d\n" , ppid); +} From fe60661f61ce201302f951a4da9bf59f82321115 Mon Sep 17 00:00:00 2001 From: Nishant Agrawal <41500636+nishantagrawal01@users.noreply.github.com> Date: Sat, 30 Oct 2021 16:56:49 +0530 Subject: [PATCH 102/109] Create Binary-Tree-Traversals --- Binary-Tree-Traversals | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Binary-Tree-Traversals diff --git a/Binary-Tree-Traversals b/Binary-Tree-Traversals new file mode 100644 index 0000000..b1e0198 --- /dev/null +++ b/Binary-Tree-Traversals @@ -0,0 +1,53 @@ +/* Tree Node Structure */ + +struct Node { + int data; + Node* left; + Node* right; +}; + +/* Inorder Traversal of Binary tree */ + +void inorderTraversal(Node* root) { + if (root == NULL) + return; + /* recurring on left child */ + inorderTraversal(root->left); + + /* printing the data of node */ + cout << root->data << " "; + + /* recurring on right child */ + inorderTraversal(root->right); +} + +/* Preorder Traversal of Binary tree */ + +void preorderTraversal(Node* root) { + if (root == NULL) + return; + /* printing the data of node */ + cout << root->data << " "; + + /* recurring on left child */ + preorderTraversal(root->left); + + /* recurring on right child */ + preorderTraversal(root->right); +} + + +/* Postorder Traversal of Binary tree */ + +void postorderTraversal(Node* root) { + if (root == NULL) + return; + /* recurring on left child */ + postorderTraversal(root->left); + + /* recurring on right child */ + postorderTraversal(root->right); + + /* printing the data of node */ + cout << root->data << " "; +} From 171c0b3486173b9c181506d605f76b28e7ac25bb Mon Sep 17 00:00:00 2001 From: VARUN KUKRETI <78615537+implosion07@users.noreply.github.com> Date: Sat, 8 Jan 2022 22:24:56 +0530 Subject: [PATCH 103/109] Add files via upload --- linearsearch.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 linearsearch.c diff --git a/linearsearch.c b/linearsearch.c new file mode 100644 index 0000000..594e47e --- /dev/null +++ b/linearsearch.c @@ -0,0 +1,28 @@ +#include +int main(int argc,char argv[]) +{ + int n=0; + int i,a[]; + int x,c=0; + printf("ENTER SIZE OF ARRAY AND ARRAY ELEMENTS\n"); + scanf("%d",&n); + for(i=0;i Date: Sat, 8 Jan 2022 22:28:01 +0530 Subject: [PATCH 104/109] program to multiply two numbers --- multiply.c | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 multiply.c diff --git a/multiply.c b/multiply.c new file mode 100644 index 0000000..587c6fd --- /dev/null +++ b/multiply.c @@ -0,0 +1,8 @@ +#include +void main() +{ int a,b; +printf("Enter two Numbers : "); +scanf("%d %d",&a,&b); +c=a*b; +printf("The Multiplication is %d",c); +} From 96f8596f2260db4549a535a4fe9b5e60189db36e Mon Sep 17 00:00:00 2001 From: VARUN KUKRETI <78615537+implosion07@users.noreply.github.com> Date: Sat, 8 Jan 2022 22:29:31 +0530 Subject: [PATCH 105/109] Linear search program --- linearsearch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linearsearch.c b/linearsearch.c index 594e47e..4d32605 100644 --- a/linearsearch.c +++ b/linearsearch.c @@ -1,5 +1,5 @@ #include -int main(int argc,char argv[]) +int main() { int n=0; int i,a[]; From 3f53d5dd3091d7c25f083e957a5ac65589341174 Mon Sep 17 00:00:00 2001 From: VARUN KUKRETI <78615537+implosion07@users.noreply.github.com> Date: Sat, 8 Jan 2022 22:33:54 +0530 Subject: [PATCH 106/109] Program to check if a number is c or not --- Prime_No.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Prime_No.c diff --git a/Prime_No.c b/Prime_No.c new file mode 100644 index 0000000..9a08241 --- /dev/null +++ b/Prime_No.c @@ -0,0 +1,18 @@ +#include +void main() +{ +int i,n,c=0; +printf("Enter a Number : "); +scanf("%d",&n); +for(i=2;i Date: Sat, 25 Jun 2022 15:46:25 +0530 Subject: [PATCH 107/109] Update README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 2317fc2..ae41c59 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,6 @@ ## :octocat: Fork and :eight_pointed_black_star: star this repo Simple, short and sweet beginners friendly C language programs -“That's the thing about people who think they hate computers. What they really hate is lousy programmers.” -― Larry Niven - 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. - Only programs written in c language will be merged. From 40bda25ea72b480da6b8f90ea6e67cfaae6d6bef Mon Sep 17 00:00:00 2001 From: gourav thakur Date: Sat, 25 Jun 2022 16:19:46 +0530 Subject: [PATCH 108/109] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ae41c59..341baa9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ +[Visit my website to know more about me : gouravthakur.com](https://gouravthakur.com) # Beginners C Program Examples ## :octocat: Fork and :eight_pointed_black_star: star this repo Simple, short and sweet beginners friendly C language programs From 0eec8576f56dd43a6def8abfb7c18e926c381604 Mon Sep 17 00:00:00 2001 From: SANZ Date: Thu, 7 Jul 2022 22:31:26 +0700 Subject: [PATCH 109/109] Create DynamicMemoryAllocation --- DynamicMemoryAllocation | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 DynamicMemoryAllocation diff --git a/DynamicMemoryAllocation b/DynamicMemoryAllocation new file mode 100644 index 0000000..6fb695a --- /dev/null +++ b/DynamicMemoryAllocation @@ -0,0 +1,29 @@ +#include +#include +struct course { + int marks; + char subject[30]; +}; + +int main() { + struct course *ptr; + int noOfRecords; + printf("Enter the number of records: "); + scanf("%d", &noOfRecords); + + // Memory allocation for noOfRecords structures + ptr = (struct course *)malloc(noOfRecords * sizeof(struct course)); + for (int i = 0; i < noOfRecords; ++i) { + printf("Enter subject and marks:\n"); + scanf("%s %d", (ptr + i)->subject, &(ptr + i)->marks); + } + + printf("Displaying Information:\n"); + for (int i = 0; i < noOfRecords; ++i) { + printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks); + } + + free(ptr); + + return 0; +}