diff --git a/Merge Sort in C. b/Merge Sort in C. deleted file mode 100644 index a1330b1..0000000 --- a/Merge Sort in C. +++ /dev/null @@ -1,140 +0,0 @@ -/* C program for Merge Sort */ -#include -#include -main() -{ - int i,j,k,n,temp; - int arr[100]; - printf("How many numbers are u going to enter?: "); - scanf("%d",&n); - - printf("Enter %d numbers: ",n); - - for(i=0;iarr[j+1]) - { - temp=arr[j]; - arr[j]=arr[j+1]; - arr[j+1]=temp; - } - } - } - printf("Sorted elements: "); - for(i=0;i -#include - -// Merges two subarrays of arr[]. -// First subarray is arr[l..m] -// Second subarray is arr[m+1..r] -void merge(int arr[], int l, int m, int r) -{ - int i, j, k; - int n1 = m - l + 1; - int n2 = r - m; - - /* create temp arrays */ - int L[n1], R[n2]; - - /* Copy data to temp arrays L[] and R[] */ - for (i = 0; i < n1; i++) - L[i] = arr[l + i]; - for (j = 0; j < n2; j++) - R[j] = arr[m + 1+ j]; - - /* Merge the temp arrays back into arr[l..r]*/ - i = 0; // Initial index of first subarray - j = 0; // Initial index of second subarray - k = l; // Initial index of merged subarray - while (i < n1 && j < n2) - { - if (L[i] <= R[j]) - { - arr[k] = L[i]; - i++; - } - else - { - arr[k] = R[j]; - j++; - } - k++; - } - - /* Copy the remaining elements of L[], if there - are any */ - while (i < n1) - { - arr[k] = L[i]; - i++; - k++; - } - - /* Copy the remaining elements of R[], if there - are any */ - while (j < n2) - { - arr[k] = R[j]; - j++; - k++; - } -} - -/* l is for left index and r is right index of the -sub-array of arr to be sorted */ -void mergeSort(int arr[], int l, int r) -{ - if (l < r) - { - // Same as (l+r)/2, but avoids overflow for - // large l and h - int m = l+(r-l)/2; - - // Sort first and second halves - mergeSort(arr, l, m); - mergeSort(arr, m+1, r); - - merge(arr, l, m, r); - } -} - -/* UTILITY FUNCTIONS */ -/* Function to print an array */ -void printArray(int A[], int size) -{ - int i; - for (i=0; i < size; i++) - printf("%d ", A[i]); - printf("\n"); -} - -/* Driver program to test above functions */ -int main() -{ - int arr[] = {12, 11, 13, 5, 6, 7}; - int arr_size = sizeof(arr)/sizeof(arr[0]); - - printf("Given array is \n"); - printArray(arr, arr_size); - - mergeSort(arr, 0, arr_size - 1); - - printf("\nSorted array is \n"); - printArray(arr, arr_size); - return 0; -} diff --git a/calculator.c b/calculator.c index c0ce7cc..7660434 100644 --- a/calculator.c +++ b/calculator.c @@ -1,32 +1,36 @@ -//C program to display simple calculator. - -#include -#include -int main() -{ - float n1,n2; //Declaring two float varibles, which we will later use as two operands. - char operator; //The operator is a char varible which will store our choice of operator. ex. +, -, *, / - printf("Enter an operator(+,-,/,*,^):"); - scanf("%c",&operator); //Taking input from user as what operation they want to perform. - - printf("Enter two numbers:"); - scanf("%f%f",&n1,&n2); //Taking float inputs as operands and storing then in n1 and n2 respectively - - switch(operator) //Here depending upon the operator input switch statement will decide which case to execute - { - //The %.3f will allow only three values after the dot(.) to print in the float - case'+' : printf("%.3f + %.3f = %.3f",n1,n2,(n1+n2)); - break; - case'-' : printf("%.3f - %.3f = %.3f",n1,n2,(n1-n2)); - break; - case'/' : printf("%.3f / %.3f = %.3f",n1,n2,(n1/n2)); - break; - case'*' : printf("%.3f * %.3f = %.3f",n1,n2,(n1*n2)); - break; - case'^' : printf("%.3f ^ %.3f = %.3f",n1,n2,pow(n1, n2)); - break; - default: printf("Error! operator is wrong"); - - } - return 0; -} +#include +int main() +{ + int a, b; + char op; + int result; + printf("Enter First Number:"); + scanf("%d", &a); + printf("Enter Second Number:"); + scanf("%d", &b); + printf("Enter Operator of the Operation you want to perform :"); + scanf(" %c", &op); + switch (op) + { + case '+': + result=a+b; + printf("The Sum Is %d", result); + break; + case '-': + result=a-b; + printf("The Subtract Is %d", result); + break; + case '*': + result=a*b; + printf("The Multiply Is %d", result); + break; + case '/': + result=a/b; + printf("The Division Is %d", result); + break; + default: + printf("Please Enter a valid Operator"); + break; + } + return 0; +} diff --git a/primeinrange.c b/primeinrange.c index a6ea431..f05f6a3 100644 --- a/primeinrange.c +++ b/primeinrange.c @@ -3,23 +3,24 @@ #include void main() { - int num,i,count; - printf("Prime numbers b/w 1 to 100:"); + int num,i,count; //initialization of variables + printf("Prime numbers b/w 1 to 100:\n"); - for(num=1;num<=100;num++) + for(num=1;num<=100;num++) //loop to iterate number from 1 to 100 { - count=0; - for(i=2;i=(num/2);i++) + count=0; + for(i=2;i<=(num/2);i++) { - if(num%i==0) + if(num%i==0) // if number is divisible by i then it is not a prime number { - count++; - break; + count++; // so increment the count + break; // breaking the loop } } - if(count==0&&num!=1) + if(count==0&&num!=1) //checking whether it is a prime number or not if it is a prime number { - printf("%d",num); + printf("%d ",num); //print the number + } } }