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; +} 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; +} + + + diff --git a/Addition.c b/Addition.c new file mode 100755 index 0000000..06f23b8 --- /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("The Sum is %d",c); +} diff --git a/Address of 1-D array b/Address of 1-D array new file mode 100644 index 0000000..3ccbda6 --- /dev/null +++ b/Address of 1-D array @@ -0,0 +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: "); + scanf("%d",&n); + + printf("enter the numbers: \n"); + for(i=0;i +#include +#include + +int checkAnagram(char *str1, char *str2); + +int main() +{ + 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); + + 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; +} + + +//Function to check whether two passed strings are anagram or not + +int checkAnagram(char *str1, char *str2) +{ + 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; +} 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/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; +} 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; } 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<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 << " "; +} 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); + + +} 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/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 +#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< +#include +int main() +{ + int n,i,j,sum1=0,sum2=0; //n denotes the number of rows and columns in the matrix arr. + + scanf("%d", &n); + int arr[n][n]; + for (i=0;i=-100 && arr[i][j]<=100) + { + if(i==j) + { + sum1+=arr[i][j]; + } + if(j==(n-1-i)) + { + 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)))); + } + else + { + printf("%d", ((sum1)-(sum2))); + } + return 0; +} + +/* +Sample Input + +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 +*/ 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/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; +} 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); + +} 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 +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/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; +} 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]); + } +} 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; +} 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; + } 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; +} + + + + */ + + + + + 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 +#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 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; +} 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 + +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; +} 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/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; + +} 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/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;i +int main() +{ + long long int r,k,j,i,coeff; + // input nuber of rows + scanf("%lld",&r); + + + for(i=0;i +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"); + } + +} 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 +} + 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 - 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; +} 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; } 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); + +} 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); + +} 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 + +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 diff --git a/SelectionSort.c b/SelectionSort.c new file mode 100644 index 0000000..93dd989 --- /dev/null +++ b/SelectionSort.c @@ -0,0 +1,44 @@ +#include + +//function for swapping two numbers +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 + 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]); + + 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; +} 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/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; 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/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; +} diff --git a/SpiralMatrix.c b/SpiralMatrix.c new file mode 100644 index 0000000..0ffdea2 --- /dev/null +++ b/SpiralMatrix.c @@ -0,0 +1,41 @@ +#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); +} + 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; +} + 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(); +} 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/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; } 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/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(); +} 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/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; +} 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); +} 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; +} 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; +} + 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"); +}} 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. 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 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 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; +} 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; +} 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 + #include + #include + + 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; + + while (a % b != 0) { + r = a % b; + a = b; + b = r; + } + return r; + } + + 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; + } + } + + 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; + } 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); +} diff --git a/heap sort.c b/heap sort.c new file mode 100644 index 0000000..b07e296 --- /dev/null +++ b/heap sort.c @@ -0,0 +1,66 @@ + +#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 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; +} 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; +} 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(iy ? 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)); +} diff --git a/large_factorial.c b/large_factorial.c new file mode 100644 index 0000000..7937bb1 --- /dev/null +++ b/large_factorial.c @@ -0,0 +1,40 @@ +//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; + printf("Enter number:"); + scanf("%d", &num); + 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 i=16500-1; + while(ans[i]==0 && i>=0){ + i--; + } + printf("\t%d! =", num); + while(i>=0){ + printf("%d", ans[i]); + i--; + } + printf("\n"); + } +} + 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 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 diff --git a/linearsearch.c b/linearsearch.c new file mode 100644 index 0000000..4d32605 --- /dev/null +++ b/linearsearch.c @@ -0,0 +1,28 @@ +#include +int main() +{ + 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 +#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); +} diff --git a/linkedlist.cpp b/linkedlist.cpp new file mode 100644 index 0000000..98b2c8d --- /dev/null +++ b/linkedlist.cpp @@ -0,0 +1,76 @@ +#include + +/* The program demonstrates a basic linked list implemented 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 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); +} diff --git a/nCrCalculatorLargeNumbers.c b/nCrCalculatorLargeNumbers.c new file mode 100644 index 0000000..9bba09c --- /dev/null +++ b/nCrCalculatorLargeNumbers.c @@ -0,0 +1,52 @@ +#include +#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; +} 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; + } 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; +} 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 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(); +} 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(); +} 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); +}} 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; +} 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