diff --git a/2D Matrix Multiplication b/2D Matrix Multiplication new file mode 100644 index 00000000..f6a47566 --- /dev/null +++ b/2D Matrix Multiplication @@ -0,0 +1,51 @@ +#include + +int main() +{ + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + printf("Enter elements of first matrix\n"); + + for (c = 0; c < m; c++) + for (d = 0; d < n; d++) + scanf("%d", &first[c][d]); + + printf("Enter number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if (n != p) + printf("The multiplication isn't possible.\n"); + else + { + printf("Enter elements of second matrix\n"); + + for (c = 0; c < p; c++) + for (d = 0; d < q; d++) + scanf("%d", &second[c][d]); + + for (c = 0; c < m; c++) { + for (d = 0; d < q; d++) { + for (k = 0; k < p; k++) { + sum = sum + first[c][k]*second[k][d]; + } + + multiply[c][d] = sum; + sum = 0; + } + } + + printf("Product of the matrices:\n"); + + for (c = 0; c < m; c++) { + for (d = 0; d < q; d++) + printf("%d\t", multiply[c][d]); + + printf("\n"); + } + } + + return 0; +} diff --git a/ALGO_BUCKETSORT b/ALGO_BUCKETSORT new file mode 100644 index 00000000..a861eee9 --- /dev/null +++ b/ALGO_BUCKETSORT @@ -0,0 +1,49 @@ +#include +#include +#include +using namespace std; + +//Bucket Sort Program when integers are floating point numbers in some range + +void bucketSort(float *arr,int n) +{ + vector b[n]; + int i,j; + for(i=0;i>n; +int i; +for(i=0;i>arr[i]; + +bucketSort(arr,n); +cout<<"Sorted Array"< +int main() { + + int number1, number2, sum; + + printf("Enter two integers: "); + scanf("%d %d", &number1, &number2); + + // calculating sum + sum = number1 + number2; + + printf("%d + %d = %d", number1, number2, sum); + return 0; +} diff --git a/Addfunction b/Addfunction new file mode 100644 index 00000000..8d3bdb44 --- /dev/null +++ b/Addfunction @@ -0,0 +1,13 @@ +void add(int,int); +#include +int main(){ +int num1,num2; +printf("Enter two integers\n"); +scanf("%d %d",&num1,&num2); +add(num1,num2); +return 0; +} +void add(int x,int y){ +int sum=x+y; +printf("Sum of two numbers is %d",sum); +} diff --git a/Addition b/Addition index 18f9f81d..a87392cb 100644 --- a/Addition +++ b/Addition @@ -6,7 +6,7 @@ int main() { printf("Enter two integers: "); scanf("%d %d", &number1, &number2); - // calculating sum + // calculating sum efficiently sum = number1 + number2; printf("%d + %d = %d", number1, number2, sum); diff --git a/Arithmatic Operations b/Arithmatic Operations new file mode 100644 index 00000000..2a5ec274 --- /dev/null +++ b/Arithmatic Operations @@ -0,0 +1,9 @@ +#include +#include + +int main() +{ + int a,b; + scanf("%d%d",&a,&b); + printf("%d\n%d\n%d\n%d\n%d",(a+b),(a-b),(a*b),(a%b),(a/b)); +} diff --git a/Armstrong Number b/Armstrong Number new file mode 100644 index 00000000..12d5f697 --- /dev/null +++ b/Armstrong Number @@ -0,0 +1,24 @@ +#include +int main() { + int num, originalNum, remainder, result = 0; + printf("Enter a three-digit integer: "); + scanf("%d", &num); + originalNum = num; + + while (originalNum != 0) { + // remainder contains the last digit + remainder = originalNum % 10; + + result += remainder * remainder * remainder; + + // removing last digit from the orignal number + originalNum /= 10; + } + + if (result == num) + printf("%d is an Armstrong number.", num); + else + printf("%d is not an Armstrong number.", num); + + return 0; +} diff --git a/Armstrong.c b/Armstrong.c new file mode 100644 index 00000000..12d5f697 --- /dev/null +++ b/Armstrong.c @@ -0,0 +1,24 @@ +#include +int main() { + int num, originalNum, remainder, result = 0; + printf("Enter a three-digit integer: "); + scanf("%d", &num); + originalNum = num; + + while (originalNum != 0) { + // remainder contains the last digit + remainder = originalNum % 10; + + result += remainder * remainder * remainder; + + // removing last digit from the orignal number + originalNum /= 10; + } + + if (result == num) + printf("%d is an Armstrong number.", num); + else + printf("%d is not an Armstrong number.", num); + + return 0; +} diff --git a/ArmstrongNumber b/ArmstrongNumber new file mode 100644 index 00000000..a3c2d76d --- /dev/null +++ b/ArmstrongNumber @@ -0,0 +1,19 @@ +#include + int main() +{ +int n,r,sum=0,temp; +printf("enter the number="); +scanf("%d",&n); +temp=n; +while(n>0) +{ +r=n%10; +sum=sum+(r*r*r); +n=n/10; +} +if(temp==sum) +printf("armstrong number "); +else +printf("not armstrong number"); +return 0; +} diff --git a/Armstrong_ number b/Armstrong_ number new file mode 100644 index 00000000..36aabc7a --- /dev/null +++ b/Armstrong_ number @@ -0,0 +1,24 @@ +#include +#include +void main() +{ +int n,t,rev=0; +clrscr(); +printf("Enter the any value to check armstrong no : "); +scanf("%d",&n); +t=n; +while(n>0) +{ +rev+rev+(n%10)*(n%10)*(n%10); +n=n/10; +} +if(rev=t) +{ +printf("\nThe no. is armstrong"); +} +else +{ +printf("\nThe no. is not armstrong"); +} +getch(); +} diff --git a/Array b/Array new file mode 100644 index 00000000..e7c71599 --- /dev/null +++ b/Array @@ -0,0 +1,23 @@ +/ Program to take 5 values from the user and store them in an array +// Print the elements stored in the array +#include + +int main() { + int values[5]; + + printf("Enter 5 integers:\n"); + + // taking input and storing it in an array + for(int i = 0; i < 5; ++i) { + scanf("%d", &values[i]); + + } + + printf("Displaying integers:\n"); + + // printing elements of an array + for(int i = 0; i < 5; ++i) { + printf("%d\n", values[i]); + } + return 0; +} diff --git a/BFS in Graph b/BFS in Graph new file mode 100644 index 00000000..e3ae319b --- /dev/null +++ b/BFS in Graph @@ -0,0 +1,95 @@ +// Program to print BFS traversal from a given +// source vertex. BFS(int s) traverses vertices +// reachable from s. +#include +#include + +using namespace std; + +// This class represents a directed graph using +// adjacency list representation +class Graph +{ + int V; // No. of vertices + + // Pointer to an array containing adjacency + // lists + list *adj; +public: + Graph(int V); // Constructor + + // function to add an edge to graph + void addEdge(int v, int w); + + // prints BFS traversal from a given source s + void BFS(int s); +}; + +Graph::Graph(int V) +{ + this->V = V; + adj = new list[V]; +} + +void Graph::addEdge(int v, int w) +{ + adj[v].push_back(w); // Add w to v’s list. +} + +void Graph::BFS(int s) +{ + // Mark all the vertices as not visited + bool *visited = new bool[V]; + for(int i = 0; i < V; i++) + visited[i] = false; + + // Create a queue for BFS + list queue; + + // Mark the current node as visited and enqueue it + visited[s] = true; + queue.push_back(s); + + // 'i' will be used to get all adjacent + // vertices of a vertex + list::iterator i; + + while(!queue.empty()) + { + // Dequeue a vertex from queue and print it + s = queue.front(); + cout << s << " "; + queue.pop_front(); + + // Get all adjacent vertices of the dequeued + // vertex s. If a adjacent has not been visited, + // then mark it visited and enqueue it + for (i = adj[s].begin(); i != adj[s].end(); ++i) + { + if (!visited[*i]) + { + visited[*i] = true; + queue.push_back(*i); + } + } + } +} + +// Driver program to test methods of graph class +int main() +{ + // Create a graph given in the above diagram + Graph g(4); + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(1, 2); + g.addEdge(2, 0); + g.addEdge(2, 3); + g.addEdge(3, 3); + + cout << "Following is Breadth First Traversal " + << "(starting from vertex 2) \n"; + g.BFS(2); + + return 0; +} diff --git a/BST_Level-Order_Traversal.cpp b/BST_Level-Order_Traversal.cpp new file mode 100644 index 00000000..b8792eba --- /dev/null +++ b/BST_Level-Order_Traversal.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include + +using namespace std; +class Node{ + public: + int data; + Node *left,*right; + Node(int d){ + data=d; + left=right=NULL; + } +}; +class Solution{ + public: + Node* insert(Node* root, int data){ + if(root==NULL){ + return new Node(data); + } + else{ + Node* cur; + if(data<=root->data){ + cur=insert(root->left,data); + root->left=cur; + } + else{ + cur=insert(root->right,data); + root->right=cur; + } + return root; + } + } + + void levelOrder(Node * root){ + //Write your code here + if (root == NULL) + return; + queue q; + q.push(root); + while(!q.empty()){ + Node* current = q.front(); + cout<< current->data << " "; + if(current->left != NULL) + q.push(current->left); + + if(current->right !=NULL) + q.push(current->right); + + q.pop(); + } + } +};//End of Solution +int main(){ + Solution myTree; + Node* root=NULL; + int T,data; + cin>>T; + while(T-->0){ + cin>>data; + root= myTree.insert(root,data); + } + myTree.levelOrder(root); + return 0; +} diff --git a/BestTimeToBuyStocks.cpp b/BestTimeToBuyStocks.cpp new file mode 100644 index 00000000..a7610394 --- /dev/null +++ b/BestTimeToBuyStocks.cpp @@ -0,0 +1,21 @@ +/* Say you have an array for which the ith element is the price of a given stock on day i. + +If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. + +Note that you cannot sell a stock before you buy one. */ + + +class Solution { +public: + int maxProfit(vector& v) { + int profit=0; + int mini=INT_MAX; + for(int i=0;i + +void main() +{ + int num1, num2, num3; + + printf("Enter the values of num1, num2 and num3\n"); + scanf("%d %d %d", &num1, &num2, &num3); + printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3); + if (num1 > num2) + { + if (num1 > num3) + { + printf("num1 is the greatest among three \n"); + } + else + { + printf("num3 is the greatest among three \n"); + } + } + else if (num2 > num3) + printf("num2 is the greatest among three \n"); + else + printf("num3 is the greatest among three \n"); +} diff --git a/Binary Search b/Binary Search new file mode 100644 index 00000000..9d247c34 --- /dev/null +++ b/Binary Search @@ -0,0 +1,38 @@ + + + +#include + + +int binarySearch(int arr[], int l, int r, int x) +{ + if (r >= l) { + int mid = l + (r - l) / 2; + + + if (arr[mid] == x) + return mid; + + + if (arr[mid] > x) + return binarySearch(arr, l, mid - 1, x); + + + return binarySearch(arr, mid + 1, r, x); + } + + + return -1; +} + +int main(void) +{ + int arr[] = { 2, 3, 4, 10, 40 }; + int n = sizeof(arr) / sizeof(arr[0]); + int x = 10; + int result = binarySearch(arr, 0, n - 1, x); + (result == -1) ? printf("Element is not present in array") + : printf("Element is present at index %d", + result); + return 0; +} diff --git a/Binary_Search b/Binary_Search new file mode 100644 index 00000000..60e81e81 --- /dev/null +++ b/Binary_Search @@ -0,0 +1,40 @@ +#include +int binarySearch(int[], int, int, int); +void main () +{ + int arr[10] = {16, 19, 20, 23, 45, 56, 78, 90, 96, 100}; + int item, location=-1; + printf("Enter the item which you want to search "); + scanf("%d",&item); + location = binarySearch(arr, 0, 9, item); + if(location != -1) + { + printf("Item found at location %d",location); + } + else + { + printf("Item not found"); + } +} +int binarySearch(int a[], int beg, int end, int item) +{ + int mid; + if(end >= beg) + { + mid = (beg + end)/2; + if(a[mid] == item) + { + return mid+1; + } + else if(a[mid] < item) + { + return binarySearch(a,mid+1,end,item); + } + else + { + return binarySearch(a,beg,mid-1,item); + } + + } + return -1; +} diff --git a/Binary_to_decimal_conversion b/Binary_to_decimal_conversion new file mode 100644 index 00000000..b1eefffd --- /dev/null +++ b/Binary_to_decimal_conversion @@ -0,0 +1,20 @@ +#include +#include + +int main() +{ + int decimalnum = 0, temp = 0, remainder; + long binarynum; + printf("Enter a binary number: "); + scanf("%ld", &binarynum); + while (binarynum!=0) + { + remainder = binarynum % 10; + binarynum = binarynum / 10; + decimalnum = decimalnum + remainder*pow(2,temp); + temp++; + } + + printf("Equivalent decimal number is: %d", decimalnum); + return 0; +} diff --git a/BitsSwap b/BitsSwap new file mode 100644 index 00000000..40649690 --- /dev/null +++ b/BitsSwap @@ -0,0 +1,29 @@ + + +#include + +int swapBits(unsigned int x, unsigned int p1, unsigned int p2, unsigned int n) +{ + + unsigned int set1 = (x >> p1) & ((1U << n) - 1); + + unsigned int set2 = (x >> p2) & ((1U << n) - 1); + + + unsigned int xor = (set1 ^ set2); + + xor = (xor << p1) | (xor << p2); + + + unsigned int result = x ^ xor; + + return result; +} + + +int main() +{ + int res = swapBits(28, 0, 3, 2); + printf("\nResult = %d ", res); + return 0; +} diff --git a/Bubble Sort b/Bubble Sort new file mode 100644 index 00000000..ef2663e2 --- /dev/null +++ b/Bubble Sort @@ -0,0 +1,41 @@ +#include +using namespace std; + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +// A function to implement bubble sort +void bubbleSort(int arr[], int n) +{ + int i, j; + for (i = 0; i < n-1; i++) + + // Last i elements are already in place + for (j = 0; j < n-i-1; j++) + if (arr[j] > arr[j+1]) + swap(&arr[j], &arr[j+1]); +} + +/* Function to print an array */ +void printArray(int arr[], int size) +{ + int i; + for (i = 0; i < size; i++) + cout << arr[i] << " "; + cout << endl; +} + +// Driver code +int main() +{ + int arr[] = {64, 34, 25, 12, 22, 11, 90}; + int n = sizeof(arr)/sizeof(arr[0]); + bubbleSort(arr, n); + cout<<"Sorted array: \n"; + printArray(arr, n); + return 0; +} diff --git a/Bubble Sort using C b/Bubble Sort using C new file mode 100644 index 00000000..eadf78c9 --- /dev/null +++ b/Bubble Sort using C @@ -0,0 +1,23 @@ +#include +void main () +{ + int i, j,temp; + int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; + for(i = 0; i<10; i++) + { + for(j = i+1; j<10; j++) + { + if(a[j] > a[i]) + { + temp = a[i]; + a[i] = a[j]; + a[j] = temp; + } + } + } + printf("Printing Sorted Element List ...\n"); + for(i = 0; i<10; i++) + { + printf("%d\n",a[i]); + } +} diff --git a/C Pattern b/C Pattern new file mode 100644 index 00000000..99c37768 --- /dev/null +++ b/C Pattern @@ -0,0 +1,18 @@ +include + +int main() +{ int i,j,n; +printf("enter n= "); +scanf("%d",&n); +for(i=1;i<=n;i++) +{ + for(j=1;j<=n;j++) + {if(i==j) + printf("\\"); + else if(i+j==n+1) + printf("/"); + else printf("*"); + } + printf("\n"); + +}} diff --git a/C Program for Rat in a Maze b/C Program for Rat in a Maze new file mode 100644 index 00000000..0742ee22 --- /dev/null +++ b/C Program for Rat in a Maze @@ -0,0 +1,93 @@ + + +#include + +// Maze size +#define N 4 + +bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N]); + +/* A utility function to print solution matrix sol[N][N] */ +void printSolution(int sol[N][N]) +{ + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) + printf(" %d ", sol[i][j]); + printf("\n"); + } +} + +/* A utility function to check if x, y is valid index for N*N maze */ +bool isSafe(int maze[N][N], int x, int y) +{ + // if (x, y outside maze) return false + if (x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1) + return true; + + return false; +} + +/* This function solves the Maze problem using Backtracking. It mainly + uses solveMazeUtil() to solve the problem. It returns false if no + path is possible, otherwise return true and prints the path in the + form of 1s. Please note that there may be more than one solutions, + this function prints one of the feasible solutions.*/ +bool solveMaze(int maze[N][N]) +{ + int sol[N][N] = { { 0, 0, 0, 0 }, + { 0, 0, 0, 0 }, + { 0, 0, 0, 0 }, + { 0, 0, 0, 0 } }; + + if (solveMazeUtil(maze, 0, 0, sol) == false) { + printf("Solution doesn't exist"); + return false; + } + + printSolution(sol); + return true; +} + +/* A recursive utility function to solve Maze problem */ +bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N]) +{ + // if (x, y is goal) return true + if (x == N - 1 && y == N - 1) { + sol[x][y] = 1; + return true; + } + + // Check if maze[x][y] is valid + if (isSafe(maze, x, y) == true) { + // mark x, y as part of solution path + sol[x][y] = 1; + + /* Move forward in x direction */ + if (solveMazeUtil(maze, x + 1, y, sol) == true) + return true; + + /* If moving in x direction doesn't give solution then + Move down in y direction */ + if (solveMazeUtil(maze, x, y + 1, sol) == true) + return true; + + /* If none of the above movements work then BACKTRACK: + unmark x, y as part of solution path */ + sol[x][y] = 0; + return false; + } + + return false; +} + +// driver program to test above function +int main() +{ + int maze[N][N] = { { 1, 0, 0, 0 }, + { 1, 1, 0, 1 }, + { 0, 1, 0, 0 }, + { 1, 1, 1, 1 } }; + + solveMaze(maze); + return 0; +} diff --git a/C Program to Add Two Matrices Using Multi-dimensional Arrays b/C Program to Add Two Matrices Using Multi-dimensional Arrays new file mode 100644 index 00000000..136681df --- /dev/null +++ b/C Program to Add Two Matrices Using Multi-dimensional Arrays @@ -0,0 +1,40 @@ +#include +int main() { + int r, c, a[100][100], b[100][100], sum[100][100], i, j; + printf("Enter the number of rows (between 1 and 100): "); + scanf("%d", &r); + printf("Enter the number of columns (between 1 and 100): "); + scanf("%d", &c); + + printf("\nEnter elements of 1st matrix:\n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("Enter element a%d%d: ", i + 1, j + 1); + scanf("%d", &a[i][j]); + } + + printf("Enter elements of 2nd matrix:\n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("Enter element a%d%d: ", i + 1, j + 1); + scanf("%d", &b[i][j]); + } + + // adding two matrices + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + sum[i][j] = a[i][j] + b[i][j]; + } + + // printing the result + printf("\nSum of two matrices: \n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("%d ", sum[i][j]); + if (j == c - 1) { + printf("\n\n"); + } + } + + return 0; +} diff --git a/C Program to Find the Size of int, float, double and char b/C Program to Find the Size of int, float, double and char new file mode 100644 index 00000000..1faedbc2 --- /dev/null +++ b/C Program to Find the Size of int, float, double and char @@ -0,0 +1,15 @@ +#include +int main() { + int intType; + float floatType; + double doubleType; + char charType; + + // sizeof evaluates the size of a variable + printf("Size of int: %ld bytes\n", sizeof(intType)); + printf("Size of float: %ld bytes\n", sizeof(floatType)); + printf("Size of double: %ld bytes\n", sizeof(doubleType)); + printf("Size of char: %ld byte\n", sizeof(charType)); + + return 0; +} diff --git a/C Program to convert Decimal to Binary b/C Program to convert Decimal to Binary new file mode 100644 index 00000000..7a53d3f7 --- /dev/null +++ b/C Program to convert Decimal to Binary @@ -0,0 +1,19 @@ +#include +#include +int main(){ +int a[10],n,i; +system ("cls"); +printf("Enter the number to convert: "); +scanf("%d",&n); +for(i=0;n>0;i++) +{ +a[i]=n%2; +n=n/2; +} +printf("\nBinary of Given Number is="); +for(i=i-1;i>=0;i--) +{ +printf("%d",a[i]); +} +return 0; +} diff --git a/C Program to print Alphabet Triangle b/C Program to print Alphabet Triangle new file mode 100644 index 00000000..6f01273b --- /dev/null +++ b/C Program to print Alphabet Triangle @@ -0,0 +1,20 @@ +#include +#include +int main(){ + int ch=65; + int i,j,k,m; + system("cls"); + for(i=1;i<=5;i++) + { + for(j=5;j>=i;j--) + printf(" "); + for(k=1;k<=i;k++) + printf("%c",ch++); + ch--; + for(m=1;m +#include +int main(){ + int i,j,k,l,n; +system("cls"); +printf("enter the range="); +scanf("%d",&n); +for(i=1;i<=n;i++) +{ +for(j=1;j<=n-i;j++) +{ +printf(" "); +} +for(k=1;k<=i;k++) +{ +printf("%d",k); +} +for(l=i-1;l>=1;l--) +{ +printf("%d",l); +} +printf("\n"); +} +return 0; +} diff --git a/C program for Prime Number b/C program for Prime Number new file mode 100644 index 00000000..4fba8ae0 --- /dev/null +++ b/C program for Prime Number @@ -0,0 +1,27 @@ +#include +int main() { + int n, i, flag = 0; + printf("Enter a positive integer: "); + scanf("%d", &n); + + for (i = 2; i <= n / 2; ++i) { + + // condition for non-prime + if (n % i == 0) { + flag = 1; + break; + } + } + + if (n == 1) { + printf("1 is neither prime nor composite."); + } + else { + if (flag == 0) + printf("%d is a prime number.", n); + else + printf("%d is not a prime number.", n); + } + + return 0; +} diff --git a/C program to insert an element in an array b/C program to insert an element in an array new file mode 100644 index 00000000..8316bef3 --- /dev/null +++ b/C program to insert an element in an array @@ -0,0 +1,31 @@ +#include + +int main() +{ + int array[100], position, c, n, value; + printf("Enter number of elements in array\n"); + scanf("%d", &n); + + printf("Enter %d elements\n", n); + + for (c = 0; c < n; c++) + scanf("%d", &array[c]); + + printf("Enter the location where you wish to insert an element\n"); + scanf("%d", &position); + + printf("Enter the value to insert\n"); + scanf("%d", &value); + + for (c = n - 1; c >= position - 1; c--) + array[c+1] = array[c]; + + array[position-1] = value; + + printf("Resultant array is\n"); + + for (c = 0; c <= n; c++) + printf("%d\n", array[c]); + + return 0; +} diff --git a/C program to reverse given string b/C program to reverse given string new file mode 100644 index 00000000..ee1b57f7 --- /dev/null +++ b/C program to reverse given string @@ -0,0 +1,18 @@ + +#include + +#include + +int main() + +{ + + char name[30] = "Hello"; + + printf("String before strrev( ) : %s\n",name); + + printf("String after strrev( ) : %s",strrev(name)); + + return 0; + +} diff --git a/C++ Program to Find All Roots of a Quadratic Equation b/C++ Program to Find All Roots of a Quadratic Equation new file mode 100644 index 00000000..1024112d --- /dev/null +++ b/C++ Program to Find All Roots of a Quadratic Equation @@ -0,0 +1,35 @@ +#include +#include +using namespace std; + +int main() { + + float a, b, c, x1, x2, discriminant, realPart, imaginaryPart; + cout << "Enter coefficients a, b and c: "; + cin >> a >> b >> c; + discriminant = b*b - 4*a*c; + + if (discriminant > 0) { + x1 = (-b + sqrt(discriminant)) / (2*a); + x2 = (-b - sqrt(discriminant)) / (2*a); + cout << "Roots are real and different." << endl; + cout << "x1 = " << x1 << endl; + cout << "x2 = " << x2 << endl; + } + + else if (discriminant == 0) { + cout << "Roots are real and same." << endl; + x1 = (-b + sqrt(discriminant)) / (2*a); + cout << "x1 = x2 =" << x1 << endl; + } + + else { + realPart = -b/(2*a); + imaginaryPart =sqrt(-discriminant)/(2*a); + cout << "Roots are complex and different." << endl; + cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl; + cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl; + } + + return 0; +} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8b137891..1a1702a4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1 +1,35 @@ +#include + +int main(){ + int arr[100]; + int size , n; + printf("\n Enter the size of array \n"); + scanf("%d",&size); + printf("\n Enter the elements of array \n"); + for (int i = 0; i < size; i++) + { + scanf("%d",&arr[i]); + } + + for (int i = 0; i < size; i++) + { + for(int j=i+1; jarr[j]) + { + n = arr[i]; + arr[i] = arr[j]; + arr[j] = n; + } + + } + } + printf("\n The numbers arranged in ascending order is \n"); + for (int i = 0; i < size; i++) + { + printf("%d\n",arr[i]); + } + + + return 0; +} diff --git a/Calendar b/Calendar new file mode 100644 index 00000000..68092c3b --- /dev/null +++ b/Calendar @@ -0,0 +1,420 @@ +#include +#include +#include +struct Date{ +int dd; +int mm; +int yy; +}; +struct Date date; + +struct Remainder{ +int dd; +int mm; +char note[50]; +}; +struct Remainder R; + +COORD xy = {0, 0}; + +void gotoxy (int x, int y) +{ +xy.X = x; xy.Y = y; // X and Y coordinates +SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), xy); +} + +//This will set the forground color for printing in a console window. +void SetColor(int ForgC) +{ +WORD wColor; +//We will need this handle to get the current background attribute +HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); +CONSOLE_SCREEN_BUFFER_INFO csbi; + + //We use csbi for the wAttributes word. + if(GetConsoleScreenBufferInfo(hStdOut, &csbi)) + { + //Mask out all but the background attribute, and add in the forgournd color + wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F); + SetConsoleTextAttribute(hStdOut, wColor); + } + return; +} + +void ClearColor(){ +SetColor(15); +} + +void ClearConsoleToColors(int ForgC, int BackC) +{ +WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F); +//Get the handle to the current output buffer... +HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); +//This is used to reset the carat/cursor to the top left. +COORD coord = {0, 0}; +//A return value... indicating how many chars were written +// not used but we need to capture this since it will be +// written anyway (passing NULL causes an access violation). +DWORD count; + + //This is a structure containing all of the console info + // it is used here to find the size of the console. + CONSOLE_SCREEN_BUFFER_INFO csbi; + //Here we will set the current color + SetConsoleTextAttribute(hStdOut, wColor); + if(GetConsoleScreenBufferInfo(hStdOut, &csbi)) + { + //This fills the buffer with a given character (in this case 32=space). + FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count); + + FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count ); + //This will set our cursor position for the next print statement. + SetConsoleCursorPosition(hStdOut, coord); + } + return; +} + +void SetColorAndBackground(int ForgC, int BackC) +{ +WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);; +SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor); +return; +} + +int check_leapYear(int year){ //checks whether the year passed is leap year or not +if(year % 400 == 0 || (year % 100!=0 && year % 4 ==0)) +return 1; +return 0; +} + +void increase_month(int *mm, int *yy){ //increase the month by one +++*mm; +if(*mm > 12){ +++*yy; +*mm = *mm - 12; +} +} + +void decrease_month(int *mm, int *yy){ //decrease the month by one +--*mm; +if(*mm < 1){ +--*yy; +if(*yy<1600){ +printf("No record available"); +return; +} +*mm = *mm + 12; +} +} + +int getNumberOfDays(int month,int year){ //returns the number of days in given month +switch(month){ //and year +case 1 : return(31); +case 2 : if(check_leapYear(year)==1) +return(29); +else +return(28); +case 3 : return(31); +case 4 : return(30); +case 5 : return(31); +case 6 : return(30); +case 7 : return(31); +case 8 : return(31); +case 9 : return(30); +case 10: return(31); +case 11: return(30); +case 12: return(31); +default: return(-1); +} +} + +char *getName(int day){ //returns the name of the day +switch(day){ +case 0 :return("Sunday"); +case 1 :return("Monday"); +case 2 :return("Tuesday"); +case 3 :return("Wednesday"); +case 4 :return("Thursday"); +case 5 :return("Friday"); +case 6 :return("Saturday"); +default:return("Error in getName() module.Invalid argument passed"); +} +} + +void print_date(int mm, int yy){ //prints the name of month and year +printf("---------------------------\n"); +gotoxy(25,6); +switch(mm){ +case 1: printf("January"); break; +case 2: printf("February"); break; +case 3: printf("March"); break; +case 4: printf("April"); break; +case 5: printf("May"); break; +case 6: printf("June"); break; +case 7: printf("July"); break; +case 8: printf("August"); break; +case 9: printf("September"); break; +case 10: printf("October"); break; +case 11: printf("November"); break; +case 12: printf("December"); break; +} +printf(" , %d", yy); +gotoxy(20,7); +printf("---------------------------"); +} +int getDayNumber(int day,int mon,int year){ //retuns the day number +int res = 0, t1, t2, y = year; +year = year - 1600; +while(year >= 100){ +res = res + 5; +year = year - 100; +} +res = (res % 7); +t1 = ((year - 1) / 4); +t2 = (year-1)-t1; +t1 = (t1*2)+t2; +t1 = (t1%7); +res = res + t1; +res = res%7; +t2 = 0; +for(t1 = 1;t1 < mon; t1++){ +t2 += getNumberOfDays(t1,y); +} +t2 = t2 + day; +t2 = t2 % 7; +res = res + t2; +res = res % 7; +if(y > 2000) +res = res + 1; +res = res % 7; +return res; +} + +char *getDay(int dd,int mm,int yy){ +int day; +if(!(mm>=1 && mm<=12)){ +return("Invalid month value"); +} +if(!(dd>=1 && dd<=getNumberOfDays(mm,yy))){ +return("Invalid date"); +} +if(yy>=1600){ +day = getDayNumber(dd,mm,yy); +day = day%7; +return(getName(day)); +}else{ +return("Please give year more than 1600"); +} +} + +int checkNote(int dd, int mm){ +FILE *fp; +fp = fopen("note.dat","rb"); +if(fp == NULL){ +printf("Error in Opening the file"); +} +while(fread(&R,sizeof(R),1,fp) == 1){ +if(R.dd == dd && R.mm == mm){ +fclose(fp); +return 1; +} +} +fclose(fp); +return 0; +} + +void printMonth(int mon,int year,int x,int y){ //prints the month with all days +int nod, day, cnt, d = 1, x1 = x, y1 = y, isNote = 0; +if(!(mon>=1 && mon<=12)){ +printf("INVALID MONTH"); +getch(); +return; +} +if(!(year>=1600)){ +printf("INVALID YEAR"); +getch(); +return; +} +gotoxy(20,y); +print_date(mon,year); +y += 3; +gotoxy(x,y); +printf("S M T W T F S "); +y++; +nod = getNumberOfDays(mon,year); +day = getDayNumber(d,mon,year); +switch(day){ //locates the starting day in calender +case 0 : +x=x; +cnt=1; +break; +case 1 : +x=x+4; +cnt=2; +break; +case 2 : +x=x+8; +cnt=3; +break; +case 3 : +x=x+12; +cnt=4; +break; +case 4 : +x=x+16; +cnt=5; +break; +case 5 : +x=x+20; +cnt=6; +break; +case 6 : +x=x+24; +cnt=7; +break; +default : +printf("INVALID DATA FROM THE getOddNumber()MODULE"); +return; +} +gotoxy(x,y); +if(cnt == 1){ +SetColor(12); +} +if(checkNote(d,mon)==1){ +SetColorAndBackground(15,12); +} +printf("%02d",d); +SetColorAndBackground(15,1); +for(d=2;d<=nod;d++){ +if(cnt%7==0){ +y++; +cnt=0; +x=x1-4; +} +x = x+4; +cnt++; +gotoxy(x,y); +if(cnt==1){ +SetColor(12); +}else{ +ClearColor(); +} +if(checkNote(d,mon)==1){ +SetColorAndBackground(15,12); +} +printf("%02d",d); +SetColorAndBackground(15,1); +} +gotoxy(8, y+2); +SetColor(14); +printf("Press 'n' to Next, Press 'p' to Previous and 'q' to Quit"); +gotoxy(8,y+3); +printf("Red Background indicates the NOTE, Press 's' to see note: "); +ClearColor(); +} + +void AddNote(){ +FILE *fp; +fp = fopen("note.dat","ab+"); +system("cls"); +gotoxy(5,7); +printf("Enter the date(DD/MM): "); +scanf("%d%d",&R.dd, &R.mm); +gotoxy(5,8); +printf("Enter the Note(50 character max): "); +fflush(stdin); +scanf("%[^\n]",R.note); +if(fwrite(&R,sizeof(R),1,fp)){ +gotoxy(5,12); +puts("Note is saved sucessfully"); +fclose(fp); +}else{ +gotoxy(5,12); +SetColor(12); +puts("\aFail to save!!\a"); +ClearColor(); +} +gotoxy(5,15); +printf("Press any key............"); +getch(); +fclose(fp); +} + +void showNote(int mm){ +FILE *fp; +int i = 0, isFound = 0; +system("cls"); +fp = fopen("note.dat","rb"); +if(fp == NULL){ +printf("Error in opening the file"); +} +while(fread(&R,sizeof(R),1,fp) == 1){ +if(R.mm == mm){ +gotoxy(10,5+i); +printf("Note %d Day = %d: %s", i+1, R.dd, R.note); +isFound = 1; +i++; +} +} +if(isFound == 0){ +gotoxy(10,5); +printf("This Month contains no note"); +} +gotoxy(10,7+i); +printf("Press any key to back......."); +getch(); + +} + +int main(){ +ClearConsoleToColors(15, 1); +SetConsoleTitle("Calender Project - Programming-technique.blogspot.com"); +int choice; +char ch = 'a'; +while(1){ +system("cls"); +printf("1. Find Out the Day\n"); +printf("2. Print all the day of month\n"); +printf("3. Add Note\n"); +printf("4. EXIT\n"); +printf("ENTER YOUR CHOICE : "); +scanf("%d",&choice); +system("cls"); +switch(choice){ +case 1: +printf("Enter date (DD MM YYYY) : "); +scanf("%d %d %d",&date.dd,&date.mm,&date.yy); +printf("Day is : %s",getDay(date.dd,date.mm,date.yy)); +printf("\nPress any key to continue......"); +getch(); +break; +case 2 : +printf("Enter month and year (MM YYYY) : "); +scanf("%d %d",&date.mm,&date.yy); +system("cls"); +while(ch!='q'){ +printMonth(date.mm,date.yy,20,5); +ch = getch(); +if(ch == 'n'){ +increase_month(&date.mm,&date.yy); +system("cls"); +printMonth(date.mm,date.yy,20,5); +}else if(ch == 'p'){ +decrease_month(&date.mm,&date.yy); +system("cls"); +printMonth(date.mm,date.yy,20,5); +}else if(ch == 's'){ +showNote(date.mm); +system("cls"); +} +} +break; +case 3: +AddNote(); +break; +case 4 : +exit(0); +} +} +return 0; +} diff --git a/Check Armstrong number of n digits b/Check Armstrong number of n digits new file mode 100644 index 00000000..8d0605c7 --- /dev/null +++ b/Check Armstrong number of n digits @@ -0,0 +1,36 @@ + + + + +Check Armstrong Number of n digits +#include +#include + +int main() { + int num, originalNum, remainder, n = 0; + float result = 0.0; + + printf("Enter an integer: "); + scanf("%d", &num); + + originalNum = num; + + // store the number of digits of num in n + for (originalNum = num; originalNum != 0; ++n) { + originalNum /= 10; + } + + for (originalNum = num; originalNum != 0; originalNum /= 10) { + remainder = originalNum % 10; + + // store the sum of the power of individual digits in result + result += pow(remainder, n); + } + + // if num is equal to result, the number is an Armstrong number + if ((int)result == num) + printf("%d is an Armstrong number.", num); + else + printf("%d is not an Armstrong number.", num); + return 0; +} diff --git a/Check Leap Year b/Check Leap Year new file mode 100644 index 00000000..c6a1b8d2 --- /dev/null +++ b/Check Leap Year @@ -0,0 +1,20 @@ +#include +int main() { + int year; + printf("Enter a year: "); + scanf("%d", &year); + if (year % 400 == 0) { + printf("%d is a leap year.", year); + } + else if (year % 100 == 0) { + printf("%d is not a leap year.", year); + } + else if (year % 4 == 0) { + printf("%d is a leap year.", year); + } + else { + printf("%d is not a leap year.", year); + } + + return 0; +} diff --git a/Check whether the given year of input is a leap year or not. b/Check whether the given year of input is a leap year or not. new file mode 100644 index 00000000..27c14559 --- /dev/null +++ b/Check whether the given year of input is a leap year or not. @@ -0,0 +1,22 @@ +#include + +int main() +{ + int year; + printf("Enter a year to check if it is a leap year\n"); + scanf("%d", &year); + + if ( year%400 == 0) + printf("%d is a leap year.\n", year); + + else if ( year%100 == 0) + printf("%d is not a leap year.\n", year); + + else if ( year%4 == 0 ) + printf("%d is a leap year.\n", year); + + else + printf("%d is not a leap year.\n", year); + + return 0; +} diff --git a/Circle b/Circle new file mode 100644 index 00000000..db312b54 --- /dev/null +++ b/Circle @@ -0,0 +1,14 @@ +#include +#include +#include +#include +int main() +{ + int gd=DETECT,gm; + initgraph(&gd, &gm, "c:\\turboc3\\bgi"); + circle(200,200,50); + getch(); + closegraph(); + return 0; +} + diff --git a/CodeForces_Solution b/CodeForces_Solution new file mode 100644 index 00000000..a969cced --- /dev/null +++ b/CodeForces_Solution @@ -0,0 +1,34 @@ +#include +using namespace std; +#define int long long +#define fio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) +#define endl '\n' +#define mod 1000000007 +int min(int a, int b) { + if (a < b) + return a; + else + return b; +} +bool compare(int a, int b) { + return a > b; +} +int32_t main() { + fio; + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vectorv; + v.push_back(0); + for (int i = 3; i <= n; i += 2) { + v.push_back(2 * i + (i - 2) * 2); + } + int cnt = 0; + for (int i = 0; i < v.size(); i++) { + cnt += v[i] * i; + } + cout << cnt << endl; + } +} diff --git a/Convert Binary Number to Decimal Number b/Convert Binary Number to Decimal Number new file mode 100644 index 00000000..0fcc79c6 --- /dev/null +++ b/Convert Binary Number to Decimal Number @@ -0,0 +1,24 @@ +#include +#include +int binaryToDecimal(long binarynum) +{ + int decimalnum = 0, temp = 0, remainder; + while (binarynum!=0) + { + remainder = binarynum % 10; + binarynum = binarynum / 10; + decimalnum = decimalnum + remainder*pow(2,temp); + temp++; + } + return decimalnum; +} + +int main() +{ + long binarynum; + printf("Enter a binary number: "); + scanf("%ld", &binarynum); + + printf("Equivalent decimal number is: %d", binaryToDecimal(binarynum)); + return 0; +} diff --git a/Convert Numbers to Roman Numerals b/Convert Numbers to Roman Numerals new file mode 100644 index 00000000..708ab350 --- /dev/null +++ b/Convert Numbers to Roman Numerals @@ -0,0 +1,123 @@ +#include + +void predigit(char num1, char num2); +void postdigit(char c, int n); + +char romanval[1000]; +int i = 0; +int main() +{ + int j; + long number; + + printf("Enter the number: "); + scanf("%d", &number); + if (number <= 0) + { + printf("Invalid number"); + return 0; + } + while (number != 0) + { + if (number >= 1000) + { + postdigit('M', number / 1000); + number = number - (number / 1000) * 1000; + } + else if (number >= 500) + { + if (number < (500 + 4 * 100)) + { + postdigit('D', number / 500); + number = number - (number / 500) * 500; + } + else + { + predigit('C','M'); + number = number - (1000-100); + } + } + else if (number >= 100) + { + if (number < (100 + 3 * 100)) + { + postdigit('C', number / 100); + number = number - (number / 100) * 100; + } + else + { + predigit('L', 'D'); + number = number - (500 - 100); + } + } + else if (number >= 50 ) + { + if (number < (50 + 4 * 10)) + { + postdigit('L', number / 50); + number = number - (number / 50) * 50; + } + else + { + predigit('X','C'); + number = number - (100-10); + } + } + else if (number >= 10) + { + if (number < (10 + 3 * 10)) + { + postdigit('X', number / 10); + number = number - (number / 10) * 10; + } + else + { + predigit('X','L'); + number = number - (50 - 10); + } + } + else if (number >= 5) + { + if (number < (5 + 4 * 1)) + { + postdigit('V', number / 5); + number = number - (number / 5) * 5; + } + else + { + predigit('I', 'X'); + number = number - (10 - 1); + } + } + else if (number >= 1) + { + if (number < 4) + { + postdigit('I', number / 1); + number = number - (number / 1) * 1; + } + else + { + predigit('I', 'V'); + number = number - (5 - 1); + } + } + } + printf("Roman number is: "); + for(j = 0; j < i; j++) + printf("%c", romanval[j]); + return 0; +} + +void predigit(char num1, char num2) +{ + romanval[i++] = num1; + romanval[i++] = num2; +} + +void postdigit(char c, int n) +{ + int j; + for (j = 0; j < n; j++) + romanval[i++] = c; +} diff --git a/Convert binary to decimal b/Convert binary to decimal new file mode 100644 index 00000000..6b73a730 --- /dev/null +++ b/Convert binary to decimal @@ -0,0 +1,21 @@ +#include +#include +int convert(long long n); +int main() { + long long n; + printf("Enter a binary number: "); + scanf("%lld", &n); + printf("%lld in binary = %d in decimal", n, convert(n)); + return 0; +} + +int convert(long long n) { + int dec = 0, i = 0, rem; + while (n != 0) { + rem = n % 10; + n /= 10; + dec += rem * pow(2, i); + ++i; + } + return dec; +} diff --git a/Convert decimal to binary b/Convert decimal to binary new file mode 100644 index 00000000..41e931f8 --- /dev/null +++ b/Convert decimal to binary @@ -0,0 +1,31 @@ +#include +using namespace std; + +// function to convert decimal to binary +void decToBinary(int n) +{ + // array to store binary number + int binaryNum[32]; + + // counter for binary array + int i = 0; + while (n > 0) { + + // storing remainder in binary array + binaryNum[i] = n % 2; + n = n / 2; + i++; + } + + // printing binary array in reverse order + for (int j = i - 1; j >= 0; j--) + cout << binaryNum[j]; +} + +// Driver program to test above function +int main() +{ + int n = 17; + decToBinary(n); + return 0; +} diff --git a/Copy String Without Using strcpy() b/Copy String Without Using strcpy() new file mode 100644 index 00000000..398acf7d --- /dev/null +++ b/Copy String Without Using strcpy() @@ -0,0 +1,14 @@ +#include +int main() { + char s1[100], s2[100], i; + printf("Enter string s1: "); + fgets(s1, sizeof(s1), stdin); + + for (i = 0; s1[i] != '\0'; ++i) { + s2[i] = s1[i]; + } + + s2[i] = '\0'; + printf("String s2: %s", s2); + return 0; +} diff --git a/Count largest elements in Array b/Count largest elements in Array new file mode 100644 index 00000000..9701c76b --- /dev/null +++ b/Count largest elements in Array @@ -0,0 +1,34 @@ +#include +/* Given by Mayank Pathak */ +int main() +{ + int n,i,large=0,count=0; + scanf("%d",&n); + int a[n]; + for(i=0;ilarge) + { + large=a[i]; + } + } + + //Counting the large element + for(i=0;i + +int main() +{ + float celsius, fahrenheit; + + printf("Enter temperature in Celsius: "); + scanf("%f", &celsius); + fahrenheit = (celsius * 9 / 5) + 32; + + printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit); + + return 0; +} diff --git a/Creat A Simple Calculator b/Creat A Simple Calculator new file mode 100644 index 00000000..080bede2 --- /dev/null +++ b/Creat A Simple Calculator @@ -0,0 +1,37 @@ +#include +int main() { + char operator; + double first, second; + printf("Enter an operator (+, -, *,): "); + scanf("%c", &operator); + printf("Enter two operands: "); + scanf("%lf %lf", &first, &second); + + switch (operator) { + case '+': + printf("%.1lf + %.1lf = %.1lf", first, second, first + second); + break; + case '-': + printf("%.1lf - %.1lf = %.1lf", first, second, first - second); + break; + case '*': + printf("%.1lf * %.1lf = %.1lf", first, second, first * second); + break; + case '/': + printf("%.1lf / %.1lf = %.1lf", first, second, first / second); + break; + // operator doesn't match any case constant + default: + printf("Error! operator is not correct"); + } + + return 0; +} + + +OUTPUT + +Enter an operator (+, -, *,): * +Enter two operands: 1.5 +4.5 +1.5 * 4.5 = 6.8 diff --git a/DDA_Algo b/DDA_Algo new file mode 100644 index 00000000..288ae990 --- /dev/null +++ b/DDA_Algo @@ -0,0 +1,33 @@ +#include +#include +#include + +int main() +{ + int gd = DETECT, gm, i; + int dx, dy, steps, x, y; + float xinc, yinc; + int x0, x1, y0, y1; + initgraph(&gd, &gm, ""); + x0 =10, y0=20, x1=100, y1=110; + dx = x1-x0; + dy = y1-y0; + if(abs(dx)>=abs(dy)){ + steps = abs(dx); + } + else{ + steps = abs(dy); + } + xinc = dx/steps; + yinc = dy/steps; + x=x0; + y=y0; + putpixel(x0, y0, 3); + for (i=0; i + +int main() +{ + int days, years, weeks; + + printf("Enter days: "); + scanf("%d", &days); + + years = (days / 365); // Ignoring leap year + weeks = (days % 365) / 7; + days = days - ((years * 365) + (weeks * 7)); + + printf("YEARS: %d\n", years); + printf("WEEKS: %d\n", weeks); + printf("DAYS: %d", days); + + return 0; +} diff --git a/Decimal to Binary Number Conversion b/Decimal to Binary Number Conversion new file mode 100644 index 00000000..e6969300 --- /dev/null +++ b/Decimal to Binary Number Conversion @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +int main() { + long n; + scanf("%ld",&n); + int a[64],i=0; + if(n==0)printf("0"); + else{ + while(n>0){ + a[i]=n%2; + n/=2; + i++; + } + for(int j=i-1;j>=0;j--)printf("%d",a[j]); + } + + return 0; +} diff --git a/Display occurrence of word in given string. b/Display occurrence of word in given string. new file mode 100644 index 00000000..7eff1cf9 --- /dev/null +++ b/Display occurrence of word in given string. @@ -0,0 +1,28 @@ +#include +#include +main() +{ + int i,len,sublen,count=0; + char str[100],substr[20],tempstr[20]; + printf("Enter String\n"); + gets(str); + printf("Enter substring to find Occurances\n"); + gets(substr); + len=strlen(str); + sublen=strlen(substr); + if(len>=sublen) + { + for(i=0;i + +int main() +{ + //Initialize array + int arr[] = {1, 2, 3, 4, 2, 7, 8, 8, 3}; + + //Calculate length of array arr + int length = sizeof(arr)/sizeof(arr[0]); + + printf("Duplicate elements in given array: \n"); + //Searches for duplicate element + for(int i = 0; i < length; i++) { + for(int j = i + 1; j < length; j++) { + if(arr[i] == arr[j]) + printf("%d\n", arr[j]); + } + } + return 0; +} diff --git a/EMI_calculator b/EMI_calculator new file mode 100644 index 00000000..ba0d2e3b --- /dev/null +++ b/EMI_calculator @@ -0,0 +1,26 @@ +#include +#include + +int main() +{ + float principal, rate, time, emi; + + printf("Enter principal: "); + scanf("%f",&principal); + + printf("Enter rate: "); + scanf("%f",&rate); + + printf("Enter time in year: "); + scanf("%f",&time); + + rate=rate/(12*100); /*one month interest*/ + time=time*12; /*one month period*/ + + + emi= (principal*rate*pow(1+rate,time))/(pow(1+rate,time)-1); + + printf("Monthly EMI is= %f\n",emi); + + return 0; +} diff --git a/Electric Bill b/Electric Bill new file mode 100644 index 00000000..772a5d9b --- /dev/null +++ b/Electric Bill @@ -0,0 +1,81 @@ +#include + +int main() +{ + int Units; + float Amount, Sur_Charge, Total_Amount; + + printf("\n Please Enter the Units that you Consumed : "); + scanf("%d", &Units); + + if (Units < 50) + { + Amount = Units * 2.60; + Sur_Charge = 25; + } + else if (Units <= 100) + { + // First Fifty Units charge is 130 (50 * 2.60) + // Next, we are removing those 50 units from total units + Amount = 130 + ((Units - 50 ) * 3.25); + Sur_Charge = 35; + } + else if (Units <= 200) + { + // First Fifty Units charge is 130, and 50 - 100 is 162.50 (50 * 3.25) + // Next, we are removing those 100 units from total units + Amount = 130 + 162.50 + ((Units - 100 ) * 5.26); + Sur_Charge = 45; + } + else + { + // First Fifty Units 130, 50 - 100 is 162.50, and 100 - 200 is 526 (100 * 5.65) + // Next, we are removing those 200 units from total units + Amount = 130 + 162.50 + 526 + ((Units - 200 ) * 7.75); + Sur_Charge = 55; + } + + Total_Amount = Amount + Sur_Charge; + printf("\n Electricity Bill = %.2f", Total_Amount); + + return 0; +} +{ + int Units; + float Amount, Sur_Charge, Total_Amount; + + printf("\n Please Enter the Units that you Consumed : "); + scanf("%d", &Units); + + if (Units < 50) + { + Amount = Units * 2.60; + Sur_Charge = 25; + } + else if (Units <= 100) + { + // First Fifty Units charge is 130 (50 * 2.60) + // Next, we are removing those 50 units from total units + Amount = 130 + ((Units - 50 ) * 3.25); + Sur_Charge = 35; + } + else if (Units <= 200) + { + // First Fifty Units charge is 130, and 50 - 100 is 162.50 (50 * 3.25) + // Next, we are removing those 100 units from total units + Amount = 130 + 162.50 + ((Units - 100 ) * 5.26); + Sur_Charge = 45; + } + else + { + // First Fifty Units 130, 50 - 100 is 162.50, and 100 - 200 is 526 (100 * 5.65) + // Next, we are removing those 200 units from total units + Amount = 130 + 162.50 + 526 + ((Units - 200 ) * 7.75); + Sur_Charge = 55; + } + + Total_Amount = Amount + Sur_Charge; + printf("\n Electricity Bill = %.2f", Total_Amount); + + return 0; +} diff --git a/FactFunc b/FactFunc new file mode 100644 index 00000000..d36b8934 --- /dev/null +++ b/FactFunc @@ -0,0 +1,41 @@ +#include +#include +int isneg (int n); +int fact (int n); +int +main () +{ + int n; + printf ("Enter no.\n"); + scanf ("%d", &n); + if (isneg (n)) + { + printf ("Fact not possible\n"); + + } + else + { + printf ("The fact of no.is:%d", fact (n)); + } +} + +int +isneg (int n) +{ + if (n > 0) + return 0; + else + return 1; +} + +int +fact (int n) +{ + int i, s; + for (i = 0; i < n; i++) + { + s = n * i; + } + return s; +} + diff --git a/Factorial b/Factorial new file mode 100644 index 00000000..1663258b --- /dev/null +++ b/Factorial @@ -0,0 +1,12 @@ +#include +int main() +{ + int i,fact=1,number; + printf("Enter a number: "); + scanf("%d",&number); + for(i=1;i<=number;i++){ + fact=fact*i; + } + printf("Factorial of %d is: %d",number,fact); +return 0; +} diff --git a/Factorial number b/Factorial number new file mode 100644 index 00000000..a200050f --- /dev/null +++ b/Factorial number @@ -0,0 +1,15 @@ +#include + +int main() +{ + int c,n,f=1; + + printf("Enter a number to calculate its factorial\n"); + scanf("%d", &n); + for (c=1; c<=n; c++) + f=f*c; + + printf("Factorial of %d = %d\n", n, f); + + return 0; +} diff --git a/Factorial of any number b/Factorial of any number new file mode 100644 index 00000000..36027841 --- /dev/null +++ b/Factorial of any number @@ -0,0 +1,31 @@ +#include +void main() +{ +int n,i,fact=1; +printf("enter the number whose factorial is required:"); +scanf("%d",&n); +if(n==0) +{ +printf("\nFactorial of negative number doesn't exist"); +} +else +{ +for(i=1;i<=n;i++) +{ +fact=fact*i; +} +} +printf("\nFactorial of %d is :%d",n,fact); +getch(); +} + + + + + + + + + + + diff --git a/Factorial_of_a_number b/Factorial_of_a_number new file mode 100644 index 00000000..07b09ea2 --- /dev/null +++ b/Factorial_of_a_number @@ -0,0 +1,19 @@ +#include +int main() { + int n, i; + unsigned long long fact = 1; + printf("Enter an integer: "); + scanf("%d", &n); + + // shows error if the user enters a negative integer + if (n < 0) + printf("Error! Factorial of a negative number doesn't exist."); + else { + for (i = 1; i <= n; ++i) { + fact *= i; + } + printf("Factorial of %d = %llu", n, fact); + } + + return 0; +} diff --git a/FahrenheitToCentigrade b/FahrenheitToCentigrade new file mode 100644 index 00000000..2b35b6ca --- /dev/null +++ b/FahrenheitToCentigrade @@ -0,0 +1,10 @@ +/*Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.*/ +#include +int main() +{ + int c,f; + printf("Enter the value in Fahrenheit "); + scanf("%d",&f); + c=(f-32)*.5556; ;/*(50°F - 32) x .5556 = 10°C*/ + printf("%d",c); +} diff --git a/Fibonacci b/Fibonacci new file mode 100644 index 00000000..88b630aa --- /dev/null +++ b/Fibonacci @@ -0,0 +1,15 @@ +#include +int fib(int n) +{ + if (n <= 1) + return n; + return fib(n-1) + fib(n-2); +} + +int main () +{ + int n = 9; + printf("%d", fib(n)); + getchar(); + return 0; +} diff --git a/Fibonacci Sequence b/Fibonacci Sequence new file mode 100644 index 00000000..d3375464 --- /dev/null +++ b/Fibonacci Sequence @@ -0,0 +1,16 @@ +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/Fibonacci Series in C b/Fibonacci Series in C new file mode 100644 index 00000000..a1b526a1 --- /dev/null +++ b/Fibonacci Series in C @@ -0,0 +1,16 @@ +#include +int main() +{ + int n1=0,n2=1,n3,i,number; + printf("Enter the number of elements:"); + scanf("%d",&number); + printf("\n%d %d",n1,n2);//printing 0 and 1 + for(i=2;i +int main() +{ + int n1=0,n2=1,n3,i,number; + printf("Enter the number of elements:"); + scanf("%d",&number); + printf("\n%d %d",n1,n2);//printing 0 and 1 + for(i=2;i +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/Fibonachi series b/Fibonachi series new file mode 100644 index 00000000..145518c7 --- /dev/null +++ b/Fibonachi series @@ -0,0 +1,25 @@ +#include +int main() +{ + int count, first_term = 0, second_term = 1, next_term, i; + + //Ask user to input number of terms + printf("Enter the number of terms:\n"); + scanf("%d",&count); + + printf("First %d terms of Fibonacci series:\n",count); + for ( i = 0 ; i < count ; i++ ) + { + if ( i <= 1 ) + next_term = i; + else + { + next_term = first_term + second_term; + first_term = second_term; + second_term = next_term; + } + printf("%d\n",next_term); + } + + return 0; +} diff --git a/Find missing number b/Find missing number new file mode 100644 index 00000000..20dc8287 --- /dev/null +++ b/Find missing number @@ -0,0 +1,20 @@ +#include + +/* getMissingNo takes array and size of array as arguments*/ +int getMissingNo(int a[], int n) +{ + int i, total; + total = (n + 1) * (n + 2) / 2; + for (i = 0; i < n; i++) + total -= a[i]; + return total; +} + +/*program to test above function */ +int main() +{ + int a[] = { 1, 2, 4, 5, 6 }; + int miss = getMissingNo(a, 5); + printf("%d", miss); + getchar(); +} diff --git a/Find odd or even no b/Find odd or even no new file mode 100644 index 00000000..8a29813a --- /dev/null +++ b/Find odd or even no @@ -0,0 +1,20 @@ +/* Program to check whether the input integer number + * is even or odd using the modulus operator (%) + */ +#include +int main() +{ + // This variable is to store the input number + int num; + + printf("Enter an integer: "); + scanf("%d",&num); + + // Modulus (%) returns remainder + if ( num%2 == 0 ) + printf("%d is an even number", num); + else + printf("%d is an odd number", num); + + return 0; +} diff --git a/First small letter in string b/First small letter in string new file mode 100644 index 00000000..aac1a531 --- /dev/null +++ b/First small letter in string @@ -0,0 +1,20 @@ +#include +#include +main() +{ + int i,flag=0; + char str[100]; + printf("Enter String to get First Small Letter of String\n"); + gets(str); + for(i=0;i'a' && str[i]<'z') + { + printf("The First Small Letter is %c\n",str[i]); + flag=1; + break; + } + } + if(flag!=1) + printf("There is no small letter\n"); +} diff --git a/Floyd warshall algorthim b/Floyd warshall algorthim new file mode 100644 index 00000000..d6f102e1 --- /dev/null +++ b/Floyd warshall algorthim @@ -0,0 +1,86 @@ +// C Program for Floyd Warshall Algorithm +#include + +// Number of vertices in the graph +#define V 4 + +/* Define Infinite as a large enough value. This value will be used + for vertices not connected to each other */ +#define INF 99999 + +// A function to print the solution matrix +void printSolution(int dist[][V]); + +// Solves the all-pairs shortest path problem using Floyd Warshall algorithm +void floydWarshall (int graph[][V]) +{ + /* dist[][] will be the output matrix that will finally have the shortest + distances between every pair of vertices */ + int dist[V][V], i, j, k; + + /* Initialize the solution matrix same as input graph matrix. Or + we can say the initial values of shortest distances are based + on shortest paths considering no intermediate vertex. */ + for (i = 0; i < V; i++) + for (j = 0; j < V; j++) + dist[i][j] = graph[i][j]; + + /* Add all vertices one by one to the set of intermediate vertices. + ---> Before start of an iteration, we have shortest distances between all + pairs of vertices such that the shortest distances consider only the + vertices in set {0, 1, 2, .. k-1} as intermediate vertices. + ----> After the end of an iteration, vertex no. k is added to the set of + intermediate vertices and the set becomes {0, 1, 2, .. k} */ + for (k = 0; k < V; k++) + { + // Pick all vertices as source one by one + for (i = 0; i < V; i++) + { + // Pick all vertices as destination for the + // above picked source + for (j = 0; j < V; j++) + { + // If vertex k is on the shortest path from + // i to j, then update the value of dist[i][j] + if (dist[i][k] + dist[k][j] < dist[i][j]) + dist[i][j] = dist[i][k] + dist[k][j]; + } + } + } + + // Print the shortest distance matrix + printSolution(dist); +} + +/* A utility function to print solution */ +void printSolution(int dist[][V]) +{ + printf ("The following matrix shows the shortest distances" + " between every pair of vertices \n"); + for (int i = 0; i < V; i++) + { + for (int j = 0; j < V; j++) + { + if (dist[i][j] == INF) + printf("%7s", "INF"); + else + printf ("%7d", dist[i][j]); + } + printf("\n"); + } +} + +// driver program to test above function +int main() +{ + + int graph[V][V] = { {0, 5, INF, 10}, + {INF, 0, 3, INF}, + {INF, INF, 0, 1}, + {INF, INF, INF, 0} + }; + + // Print the solution + floydWarshall(graph); + return 0; +} diff --git a/Funnystring b/Funnystring new file mode 100644 index 00000000..d50b20aa --- /dev/null +++ b/Funnystring @@ -0,0 +1,35 @@ +#include +#include +#include + +int main() +{ + int q,i,len; + char str[10000],r[10000]; + scanf("%d",&q); + while(q!=0) + { + scanf("%s",str); + len=strlen(str); + for (i = 0; i < len; i++) + { + r[i] = str[strlen(str) - i - 1]; + } + int flag=1; + for (i = 1; i < len; i++) + { + if (abs(r[i] - r[i-1]) != abs(str[i] - str[i-1])) + { + printf("Not Funny\n"); + flag=0; + break; + } + } + + if (flag==1) + printf("Funny\n"); + q--; + } + + return 0; +} diff --git a/GCD Of numbers b/GCD Of numbers new file mode 100644 index 00000000..a0b7eaa4 --- /dev/null +++ b/GCD Of numbers @@ -0,0 +1,19 @@ +import Scanner.*; +public class GCD { + + public static void main(String[] args) { + Scanner s=new Scanner(System.in); + int n1=s.nextInt(); + int n2=s.nextInt(); + int gcd = 1; + + for(int i = 1; i <= n1 && i <= n2; ++i) + { + // Checks if i is factor of both integers + if(n1 % i==0 && n2 % i==0) + gcd = i; + } + + System.out.printf("G.C.D of %d and %d is %d", n1, n2, gcd); + } +} diff --git a/GCD of 2 number b/GCD of 2 number new file mode 100644 index 00000000..dc76a125 --- /dev/null +++ b/GCD of 2 number @@ -0,0 +1,19 @@ +#include +int main() +{ + int n1, n2; + + printf("Enter two positive integers: "); + scanf("%d %d",&n1,&n2); + + while(n1!=n2) + { + if(n1 > n2) + n1 -= n2; + else + n2 -= n1; + } + printf("GCD = %d",n1); + + return 0; +} diff --git a/Game b/Game new file mode 100644 index 00000000..99940517 --- /dev/null +++ b/Game @@ -0,0 +1,530 @@ +#include +#include +#include +#include +#include +void show_record(); +void reset_score(); +void help(); +void edit_score(float , char []); +int main() +{ +int countr,r,r1,count,i,n; +float score; +char choice; +char playername[20]; +mainhome: +system("cls"); +printf("\t\t\tC PROGRAM QUIZ GAME\n"); +printf("\n\t\t________________________________________"); + + printf("\n\t\t\t WELCOME "); + printf("\n\t\t\t to "); + printf("\n\t\t\t THE GAME "); + printf("\n\t\t________________________________________"); + printf("\n\t\t________________________________________"); + printf("\n\t\t BECOME A MILLIONAIRE!!!!!!!!!!! ") ; + printf("\n\t\t________________________________________"); + printf("\n\t\t________________________________________"); + printf("\n\t\t > Press S to start the game"); + printf("\n\t\t > Press V to view the highest score "); + printf("\n\t\t > Press R to reset score"); + printf("\n\t\t > press H for help "); + printf("\n\t\t > press Q to quit "); + printf("\n\t\t________________________________________\n\n"); + choice=toupper(getch()); + if (choice=='V') +{ +show_record(); +goto mainhome; +} + else if (choice=='H') +{ +help();getch(); +goto mainhome; +} +else if (choice=='R') +{reset_score(); +getch(); +goto mainhome;} +else if (choice=='Q') +exit(1); +else if(choice=='S') +{ + system("cls"); + +printf("\n\n\n\n\n\n\n\n\n\n\t\t\tResister your name:"); + gets(playername); + +system("cls"); +printf("\n ------------------ Welcome %s to C Program Quiz Game --------------------------",playername); +printf("\n\n Here are some tips you might wanna know before playing:"); +printf("\n -------------------------------------------------------------------------"); +printf("\n >> There are 2 rounds in this Quiz Game,WARMUP ROUND & CHALLANGE ROUND"); +printf("\n >> In warmup round you will be asked a total of 3 questions to test your"); +printf("\n general knowledge. You are eligible to play the game if you give atleast 2"); +printf("\n right answers, otherwise you can't proceed further to the Challenge Round."); +printf("\n >> Your game starts with CHALLANGE ROUND. In this round you will be asked a"); +printf("\n total of 10 questions. Each right answer will be awarded $100,000!"); +printf("\n By this way you can win upto ONE MILLION cash prize!!!!!.........."); +printf("\n >> You will be given 4 options and you have to press A, B ,C or D for the"); +printf("\n right option."); +printf("\n >> You will be asked questions continuously, till right answers are given"); +printf("\n >> No negative marking for wrong answers!"); +printf("\n\n\t!!!!!!!!!!!!! ALL THE BEST !!!!!!!!!!!!!"); +printf("\n\n\n Press Y to start the game!\n"); +printf("\n Press any other key to return to the main menu!"); +if (toupper(getch())=='Y') + { + goto home; + } +else + { + goto mainhome; + system("cls"); + } + + home: + system("cls"); + count=0; + for(i=1;i<=3;i++) + { +system("cls"); + r1=i; + + + switch(r1) + { + case 1: + printf("\n\nWhich of the following is a Palindrome number?"); + printf("\n\nA.42042\t\tB.101010\n\nC.23232\t\tD.01234"); + if (toupper(getch())=='C') + { + printf("\n\nCorrect!!!");count++; + getch(); + break; +} +else +{ +printf("\n\nWrong!!! The correct answer is C.23232"); +getch(); +break; +} + + case 2: + printf("\n\n\nThe country with the highest environmental performance index is..."); + printf("\n\nA.France\t\tB.Denmark\n\nC.Switzerland\t\tD.Finland"); + if (toupper(getch())=='C') + {printf("\n\nCorrect!!!");count++; + getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is C.Switzerland"); + getch(); + break;} + + case 3: + printf("\n\n\nWhich animal laughs like human being?"); + printf("\n\nA.Polar Bear\t\tB.Hyena\n\nC.Donkey\t\tD.Chimpanzee"); + if (toupper(getch())=='B') + {printf("\n\nCorrect!!!");count++; + getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is B.Hyena"); + getch(); + break;} + + case 4: + printf("\n\n\nWho was awarded the youngest player award in Fifa World Cup 2006?"); + printf("\n\nA.Wayne Rooney\t\tB.Lucas Podolski\n\nC.Lionel Messi\t\tD.Christiano Ronaldo"); + if (toupper(getch())=='B') + {printf("\n\nCorrect!!!");count++; + getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is B.Lucas Podolski"); + getch(); + break;} + + case 5: + printf("\n\n\nWhich is the third highest mountain in the world?"); + printf("\n\nA.Mt. K2\t\tB.Mt. Kanchanjungha\n\nC.Mt. Makalu\t\tD.Mt. Kilimanjaro"); + if (toupper(getch())=='B') + {printf("\n\nCorrect!!!");count++; + getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is B.Mt. Kanchanjungha"); + getch(); + break;} + + case 6: + printf("\n\n\nWhat is the group of frogs known as?"); + printf("\n\nA.A traffic\t\tB.A toddler\n\nC.A police\t\tD.An Army"); + if (toupper(getch())=='D' ) + {printf("\n\nCorrect!!!");count++; + getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is D.An Army"); + getch(); + break;}} + } + +if(count>=2) +{goto test;} +else +{ +system("cls"); +printf("\n\nSORRY YOU ARE NOT ELIGIBLE TO PLAY THIS GAME, BETTER LUCK NEXT TIME"); +getch(); +goto mainhome; +} + test: + system("cls"); + printf("\n\n\t*** CONGRATULATION %s you are eligible to play the Game ***",playername); + printf("\n\n\n\n\t!Press any key to Start the Game!"); + if(toupper(getch())=='p') + {goto game;} +game: +countr=0; +for(i=1;i<=10;i++) +{system("cls"); +r=i; + + switch(r) + { + case 1: + printf("\n\nWhat is the National Game of England?"); + printf("\n\nA.Football\t\tB.Basketball\n\nC.Cricket\t\tD.Baseball"); + if (toupper(getch())=='C') + {printf("\n\nCorrect!!!");countr++;getch(); + break;getch();} + else + {printf("\n\nWrong!!! The correct answer is C.Cricket");getch(); + goto score; + break;} + + case 2: + printf("\n\n\nStudy of Earthquake is called............,"); + printf("\n\nA.Seismology\t\tB.Cosmology\n\nC.Orology\t\tD.Etimology"); + if (toupper(getch())=='A') + {printf("\n\nCorrect!!!");countr++;getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is A.Seismology");getch(); + goto score; + break; + } + + case 3: + printf("\n\n\nAmong the top 10 highest peaks in the world, how many lie in Nepal? "); + printf("\n\nA.6\t\tB.7\n\nC.8\t\tD.9"); + if (toupper(getch())=='C') + {printf("\n\nCorrect!!!");countr++;getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is C.8");getch(); + goto score; + break;} + + case 4: + printf("\n\n\nThe Laws of Electromagnetic Induction were given by?"); + printf("\n\nA.Faraday\t\tB.Tesla\n\nC.Maxwell\t\tD.Coulomb"); + if (toupper(getch())=='A') + {printf("\n\nCorrect!!!");countr++;getch(); + break;} + else + { + printf("\n\nWrong!!! The correct answer is A.Faraday");getch(); + goto score; + break; + } + + case 5: + printf("\n\n\nIn what unit is electric power measured?"); + printf("\n\nA.Coulomb\t\tB.Watt\n\nC.Power\t\tD.Units"); + if (toupper(getch())=='B') + {printf("\n\nCorrect!!!");countr++;getch(); break;} + else + { + printf("\n\nWrong!!! The correct answer is B.Power"); + getch(); + goto score; + break; + } + + case 6: + printf("\n\n\nWhich element is found in Vitamin B12?"); + printf("\n\nA.Zinc\t\tB.Cobalt\n\nC.Calcium\t\tD.Iron"); + if (toupper(getch())=='B' ) + {printf("\n\nCorrect!!!");countr++;getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is B.Cobalt");goto score; + getch(); + break;} + + case 7: + printf("\n\n\nWhat is the National Name of Japan?"); + printf("\n\nA.Polska\t\tB.Hellas\n\nC.Drukyul\t\tD.Nippon"); + if (toupper(getch())=='D') + {printf("\n\nCorrect!!!");countr++;getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is D.Nippon");getch(); + goto score; + break;} + + case 8: + printf("\n\n\nHow many times a piece of paper can be folded at the most?"); + printf("\n\nA.6\t\tB.7\n\nC.8\t\tD.Depends on the size of paper"); + if (toupper(getch())=='B') + {printf("\n\nCorrect!!!");countr++;getch(); break;} + else + {printf("\n\nWrong!!! The correct answer is B.7");getch(); + goto score; + break;} + + case 9: + printf("\n\n\nWhat is the capital of Denmark?"); + printf("\n\nA.Copenhagen\t\tB.Helsinki\n\nC.Ajax\t\tD.Galatasaray"); + if (toupper(getch())=='A') + {printf("\n\nCorrect!!!");countr++; getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is A.Copenhagen");getch(); + goto score; + break;} + + case 10: + printf("\n\n\nWhich is the longest River in the world?"); + printf("\n\nA.Nile\t\tB.Koshi\n\nC.Ganga\t\tD.Amazon"); + if (toupper(getch())=='A') + {printf("\n\nCorrect!!!");countr++;getch(); break;} + else + {printf("\n\nWrong!!! The correct answer is A.Nile");getch();break;goto score;} + + case 11: + printf("\n\n\nWhat is the color of the Black Box in aeroplanes?"); + printf("\n\nA.White\t\tB.Black\n\nC.Orange\t\tD.Red"); + if (toupper(getch())=='C') + {printf("\n\nCorrect!!!");countr++;getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is C.Orange");getch(); + break;goto score;} + + case 12: + printf("\n\n\nWhich city is known at 'The City of Seven Hills'?"); + printf("\n\nA.Rome\t\tB.Vactican City\n\nC.Madrid\t\tD.Berlin"); + if (toupper(getch())=='A') + {printf("\n\nCorrect!!!");countr++;getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is A.Rome");getch(); + break;goto score;} + + case 13: + printf("\n\n\nName the country where there no mosquitoes are found?"); + printf("\n\nA.Japan\t\tB.Italy\n\nC.Argentina\t\tD.France"); + if (toupper(getch())=='D') + {printf("\n\nCorrect!!!");countr++;getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is D.France");getch(); + break;goto score;} + + case 14: + printf("\n\n\nWho is the author of 'Pulpasa Cafe'?"); + printf("\n\nA.Narayan Wagle\t\tB.Lal Gopal Subedi\n\nC.B.P. Koirala\t\tD.Khagendra Sangraula"); + if (toupper(getch())=='A') + {printf("\n\nCorrect!!!");countr++;getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is A.Narayan Wagle");getch(); + break;goto score;} + + case 15: + printf("\n\n\nWhich Blood Group is known as the Universal Recipient?"); + printf("\n\nA.A\t\tB.AB\n\nC.B\t\tD.O"); + if (toupper(getch())=='B') + {printf("\n\nCorrect!!!");countr++;getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is B.AB");getch(); + goto score; + break;} + + case 16: + printf("\n\n\nWhat is the unit of measurement of distance between Stars?"); + printf("\n\nA.Light Year\t\tB.Coulomb\n\nC.Nautical Mile\t\tD.Kilometer"); + if (toupper(getch())=='A') + {printf("\n\nCorrect!!!");countr++; getch(); + break; + } + else + {printf("\n\nWrong!!! The correct answer is A.Light Year");getch(); + goto score; + break;} + + + case 17: + printf("\n\n\nThe country famous for Samba Dance is........"); + printf("\n\nA.Brazil\t\tB.Venezuela\n\nC.Nigeria\t\tD.Bolivia"); + if (toupper(getch())=='A') + {printf("\n\nCorrect!!!");countr++; getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is A.Brazil");getch();goto score; + break;} + + case 18: + printf("\n\n\nWind speed is measure by__________?"); + printf("\n\nA.Lysimeter\t\tB.Air vane\n\nC.Hydrometer\t\tD.Anemometer\n\n"); + if (toupper(getch())=='D') + {printf("\n\nCorrect!!!");countr++; getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is D.Anemometer");getch();goto score; + break;} + + case 19: + printf("\n\n\nWhich city in the world is popularly known as The City of Temple?"); + printf("\n\nA.Delhi\tB.Bhaktapur\n\nC.Kathmandu\tD.Agra\n\n"); + if (toupper(getch())=='C') + {printf("\n\nCorrect!!!");countr++; getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is C.Kathmandu");getch();goto score; + break;} + + case 20: + printf("\n\n\nWhich hardware was used in the First Generation Computer?"); + printf("\n\nA.Transistor\t\tB.Valves\n\nC.I.C\t\tD.S.S.I"); + if (toupper(getch())=='B') + {printf("\n\nCorrect!!!");countr++; getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is B.Valves");getch();goto score; + break;} + + case 21: + printf("\n\n\nOzone plate is being destroyed regularly because of____ ?"); + printf("\n\nA.L.P.G\t\tB.Nitrogen\n\nC.Methane\t\tD. C.F.C"); + if (toupper(getch())=='D') + {printf("\n\nCorrect!!!");countr++; getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is D. C.F.C");getch();goto score; + break;} + + case 22: + printf("\n\n\nWho won the Women's Australian Open Tennis in 2007?"); + printf("\n\nA.Martina Hingis\t\tB.Maria Sarapova\n\nC.Kim Clijster\t\tD.Serena Williams"); + if (toupper(getch())=='D') + {printf("\n\nCorrect!!!");countr++; getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is D.Serena Williams");getch();goto score; + break;} + + case 23: + printf("\n\n\nWhich film was awarded the Best Motion Picture at Oscar in 2010?"); + printf("\n\nA.The Secret in their Eyes\t\tB.Shutter Island\n\nC.The King's Speech\t\tD.The Reader"); + if (toupper(getch())=='C') + {printf("\n\nCorrect!!!");countr++; getch(); + break;} + else + {printf("\n\nWrong!!! The correct answer is C.The King's Speech");getch();goto score; + break;}}} +score: +system("cls"); +score=(float)countr*100000; +if(score>0.00 && score<1000000) +{ + printf("\n\n\t\t**************** CONGRATULATION *****************"); + printf("\n\t You won $%.2f",score);goto go;} + + else if(score==1000000.00) +{ + printf("\n\n\n \t\t**************** CONGRATULATION ****************"); + printf("\n\t\t\t\t YOU ARE A MILLIONAIRE!!!!!!!!!"); + printf("\n\t\t You won $%.2f",score); + printf("\t\t Thank You!!"); +} + else +{ +printf("\n\n\t******** SORRY YOU DIDN'T WIN ANY CASH ********"); +printf("\n\t\t Thanks for your participation"); +printf("\n\t\t TRY AGAIN");goto go;} + +go: +puts("\n\n Press Y if you want to play next game"); +puts(" Press any key if you want to go main menu"); +if (toupper(getch())=='Y') + goto home; +else + { + edit_score(score,playername); + goto mainhome;}}} +void show_record() +{system("cls"); +char name[20]; +float scr; +FILE f; +f=fopen("score.txt","r"); +fscanf(f,"%s%f",&name,&scr); +printf("\n\n\t\t************************************************************"); +printf("\n\n\t\t %s has secured the Highest Score %0.2f",name,scr); +printf("\n\n\t\t*************************************************************"); +fclose(f); +getch();} + +void reset_score() +{system("cls"); +float sc; +char nm[20]; +FILE *f; +f=fopen("score.txt","r+"); +fscanf(f,"%s%f",&nm,&sc); +sc=0; +fprintf(f,"%s,%.2f",nm,sc); +fclose(f);} + +void help() +{system("cls"); +printf("\n\n HELP"); +printf("\n -------------------------------------------------------------------------"); +printf("\n ......................... C program Quiz Game..........."); +printf("\n >> There are two rounds in the game, WARMUP ROUND & CHALLANGE ROUND"); +printf("\n >> In warmup round you will be asked a total of 3 questions to test your general"); +printf("\n knowledge. You will be eligible to play the game if you can give atleast 2"); +printf("\n right answers otherwise you can't play the Game..........."); +printf("\n >> Your game starts with the CHALLANGE ROUND. In this round you will be asked"); +printf("\n total 10 questions each right answer will be awarded $100,000."); +printf("\n By this way you can win upto ONE MILLION cash prize in USD..............."); +printf("\n >> You will be given 4 options and you have to press A, B ,C or D for the"); +printf("\n right option"); +printf("\n >> You will be asked questions continuously if you keep giving the right answers."); +printf("\n >> No negative marking for wrong answers"); + +printf("\n\n\t*********************BEST OF LUCK*********************************"); +printf("\n\n\t*****C PROGRAM QUIZ GAME is developed by CODE WITH C TEAM********");} +void edit_score(float score, char plnm[20]) +{system("cls"); +float sc; +char nm[20]; +FILE *f; +f=fopen("score.txt","r"); +fscanf(f,"%s%f",&nm,&sc); +if (score>=sc) +{ sc=score; +fclose(f); +f=fopen("score.txt","w"); +fprintf(f,"%s\n%.2f",plnm,sc); +fclose(f);}} + + + diff --git a/Gcd of any number using recursion in c b/Gcd of any number using recursion in c new file mode 100644 index 00000000..154c5f2b --- /dev/null +++ b/Gcd of any number using recursion in c @@ -0,0 +1,29 @@ +#include +int GetGCD(int temp1,int temp2); +main() +{ +int num1,num2,gcd,lcm,x; +printf("Enter number 1 and number 2\n"); +scanf("%d%d",&num1,&num2); +gcd=GetGCD(num1,num2); +lcm=(num1*num2)/gcd; +printf("gcd is %d\n",gcd); +printf("Lcm is %d\n",lcm); + +} +int GetGCD(int temp1,int temp2) +{ + if(temp2!=0) +{ + GetGCD(temp2,temp1%temp2); + +} + else +{ + return(temp1); + +} + +} + + diff --git a/Gcdrecursion.cpp b/Gcdrecursion.cpp new file mode 100644 index 00000000..17f58714 --- /dev/null +++ b/Gcdrecursion.cpp @@ -0,0 +1,16 @@ +#include +int hcf(int n1, int n2); +int main() { + int n1, n2; + printf("Enter two positive integers: "); + scanf("%d %d", &n1, &n2); + printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2)); + return 0; +} + +int hcf(int n1, int n2) { + if (n2 != 0) + return hcf(n2, n1 % n2); + else + return n1; +} diff --git a/Greater Integer b/Greater Integer new file mode 100644 index 00000000..7df937ac --- /dev/null +++ b/Greater Integer @@ -0,0 +1,9 @@ +#include +#include +using namespace std; +int main(){ + int a,b; + cin>>a>>b; + cout< + +int main() { + int a, b; + printf("Please Enter Two different values\n"); + scanf("%d %d", &a, &b); + + if(a > b) + { + printf("%d is Largest\n", a); + } + else if (b > a) + { + printf("%d is Largest\n", b); + } + else + { + printf("Both are Equal\n"); + } + + return 0; +} diff --git a/Greatest No. b/Greatest No. new file mode 100644 index 00000000..7b9bc949 --- /dev/null +++ b/Greatest No. @@ -0,0 +1,25 @@ +#include + +void main() +{ + int num1, num2, num3; + + printf("Enter the values of num1, num2 and num3\n"); + scanf("%d %d %d", &num1, &num2, &num3); + printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3); + if (num1 > num2) + { + if (num1 > num3) + { + printf("num1 is the greatest among three \n"); + } + else + { + printf("num3 is the greatest among three \n"); + } + } + else if (num2 > num3) + printf("num2 is the greatest among three \n"); + else + printf("num3 is the greatest among three \n"); +} diff --git a/Half pyramid of * b/Half pyramid of * new file mode 100644 index 00000000..70a1a944 --- /dev/null +++ b/Half pyramid of * @@ -0,0 +1,13 @@ +#include +int main() { + int i, j, rows; + printf("Enter the number of rows: "); + scanf("%d", &rows); + for (i = 1; i <= rows; ++i) { + for (j = 1; j <= i; ++j) { + printf("* "); + } + printf("\n"); + } + return 0; +} diff --git a/HangmangameinC b/HangmangameinC new file mode 100644 index 00000000..c083802f --- /dev/null +++ b/HangmangameinC @@ -0,0 +1,320 @@ +#include +#include +#include +#include +#include +#include +#include +/********************************************************************** +* * +* Code written by P Sashwat. * +* More information regarding this code and me can be found on my * +* Linkedin Profile : https://www.linkedin.com/in/sashwat-p-1952471b1/ * +* * +***********************************************************************/ +char fileLoc[500]; // backup file location +void showLogo() { + printf("--------------------------------------------\n"); + printf("| # # # # # #### # # # # # |\n"); + printf("| # # # # ## # # ## ## # # ## # |\n"); + printf("| #### ##### # # # # ## # # # ##### # # # |\n"); + printf("| # # # # # ## # # # # # # # ## |\n"); + printf("| # # # # # # ### # # # # # # |\n"); + printf("--------------------------------------------\n\n"); +} +void prn_galg(int i) { + switch (i) { + case 0 : + printf("Amount of wrong letters: %d\n\n", i); + printf("\n"); + printf("\n"); + printf("\n"); + printf("\n"); + printf("\n"); + printf("\n"); + printf("____________\n\n"); + break; + case 1 : + printf("Amount of wrong letters: %d\n\n", i); + printf("\n"); + printf(" |\n"); + printf(" |\n"); + printf(" |\n"); + printf(" |\n"); + printf(" |\n"); + printf("__|_________\n\n"); + break; + case 2 : + printf("Amount of wrong letters: %d\n\n", i); + printf(" _______\n"); + printf(" |\n"); + printf(" |\n"); + printf(" |\n"); + printf(" |\n"); + printf(" |\n"); + printf("__|_________\n\n"); + break; + case 3 : + printf("Amount of wrong letters: %d\n\n", i); + printf(" _______\n"); + printf(" |/\n"); + printf(" |\n"); + printf(" |\n"); + printf(" |\n"); + printf(" |\n"); + printf("__|_________\n\n"); + break; + case 4 : + printf("Amount of wrong letters: %d\n\n", i); + printf(" _______\n"); + printf(" |/ | \n"); + printf(" | O \n"); + printf(" |\n"); + printf(" |\n"); + printf(" |\n"); + printf("__|_________\n\n"); + break; + case 5 : + printf("Amount of wrong letters: %d\n\n", i); + printf(" _______\n"); + printf(" |/ | \n"); + printf(" | O \n"); + printf(" | |\n"); + printf(" | |\n"); + printf(" |\n"); + printf("__|_________\n\n"); + break; + case 6 : + printf("Amount of wrong letters: %d\n\n", i); + printf(" _______\n"); + printf(" |/ | \n"); + printf(" | O \n"); + printf(" | \\|\n"); + printf(" | | \n"); + printf(" |\n"); + printf("__|_________\n\n"); + break; + case 7 : + printf("Amount of wrong letters: %d\n\n", i); + printf(" _______\n"); + printf(" |/ | \n"); + printf(" | O \n"); + printf(" | \\|/\n"); + printf(" | | \n"); + printf(" |\n"); + printf("__|_________\n\n"); + break; + case 8 : + printf("Amount of wrong letters: %d\n\n", i); + printf(" _______\n"); + printf(" |/ | \n"); + printf(" | O \n"); + printf(" | \\|/\n"); + printf(" | | \n"); + printf(" | /\n"); + printf("__|_________\n\n"); + break; + case 9 : + printf("Amount of wrong letters: %d\n\n", i); + printf(" _______\n"); + printf(" |/ | \n"); + printf(" | O \n"); + printf(" | \\|/\n"); + printf(" | | \n"); + printf(" | / \\\n"); + printf("__|_________\n\n"); + break; + case 10 : + printf("Amount of wrong letters: %d\n\n", i); + printf(" _______\n"); + printf(" |/ | \n"); + printf(" | X \n"); + printf(" | \\|/\n"); + printf(" | | \n"); + printf(" | / \\\n"); + printf("__|_________\n\n"); + break; + } +} +char randomNumber(int max_number) { +srand(time(NULL)); +int g = (rand() % (max_number + 1)); +return g; +} +char *getWord() { + char c[50000]; // declare a char array + int n; + FILE *file; // declare a FILE pointer + // Opening words file + if (strcmp(fileLoc, "") != 1) { + // Here is the default words file, you can change this into whatever words file you'd like. + file = fopen("words.txt", "r"); + } else { + file = fopen(fileLoc, "r"); + } + /* Incase the file can't be openend */ + if(file==NULL) { + printf("An error has occured: can't open words file.\nPlease type the location of the words file:\n"); + scanf("%s", fileLoc); + printf("Reading file '%s'.....\n\n", fileLoc); + file = fopen(fileLoc, "r"); + if (file == NULL) { + while (file==NULL) { + printf("That file doesn't exist. Enter the location of the words file:\n"); + scanf("%s", fileLoc); + printf("Reading file '%s'.....\n\n", fileLoc); + file = fopen(fileLoc, "r"); + } + } + printf("File has been read.\n\n"); + n = fread(c, 1, 50000, file); + c[n] = '\0'; + } else { + /* Reading the contents of the file */ + n = fread(c, 1, 50000, file); + c[n] = '\0'; + } + /* Separating the contents, divided by | and declaring variables */ + char *token = strtok(c, "|"); + char *words[200] = {0}; + int f = 0; + while(token != NULL) + { + /* Allocating memory for the pointer */ + words[f] = malloc(strlen(token)+1); + /* Copying entire string to pointer */ + strcpy(words[f],token); + /* Resetting pointer */ + token = strtok(NULL, "|"); + f++; + } + /* Closing the file */ + fclose(file); + /* Retrieving a random number */ + int wordN = randomNumber(f); + /* Freeing all the memory allocated for the strings */ + int q; + for(q = 0; q < 200; q++) + { + if( q != wordN) { + free(words[q]); + } + } + /* Returning string */ + return words[wordN]; +} +int main(void) { +char udi[] = "EMPTY"; +while ((strcmp(udi, "END") != 0) && ((strcmp(udi, "AGAIN") == 0) || (strcmp(udi, "EMPTY") == 0))) { + strcpy(udi, "EMPTY"); + /* Declaring variables */ + /* Random deciding which word is chosen to be guessed: + guessWord is the word that needs to be guessed + currentWord is the word that is filled with dots */ + /* Retrieving the word that matches with the wordNumber */ + /* Check which number was chosen: printf("%d", wordNumber); */ + char *tempWord = getWord(); + /* Declaring the guessWord with the length of dkljafoue */ + char guessWord[strlen(tempWord)+1]; + /* Copying the string of dkljafoue into guessWord */ + strcpy(guessWord, tempWord); + /* Freeing the pointer */ + free(tempWord); + /* Calculate the length of the guessWord */ + int wordlength = strlen(guessWord); + /* Creating the dotword (name: currentWord) */ + char* currentWord = malloc( wordlength ); + int t; + for (t = 0; t <= wordlength; t++) { + if (t == wordlength) { + currentWord[t] = '\0'; + } else { + currentWord[t] = '.'; + } + } + /* Currentword check: printf("Currentword: \"%s\"", currentWord); */ + /* Declaring variables */ + int errors = 0; /* Error amount, if its higher than 10 the loop stops */ + int guessedLetter = 0; /* Boolean Integer used to check wether the player entered a correct letter 0 = false, 1 = true */ + int i,n = 1; + char c; + /* Printing logo */ + showLogo(); + /* Printing introduction message */ + printf("%s\n\n%s\n%s\n%s\n%s\n\n%s%s\n\n", + "Welcome to the Hangman game!", + "The objective in this game is to guess the word.", + "You can enter both uppercase and lowercase letters.", + "If you think you know the word, you can type it in.", + "You will loose if you have guessed 10 letters wrong.", + " -Made By P Sashwat.\n", + "This is the word you need to guess: ", + currentWord); + printf("%d. %s", n, "Enter the letter(s) you want to guess: "); + /* As long as the word hasn't been guessed or the errors are lower than 10: */ + while( (strcmp(currentWord, guessWord) != 0) && (errors < 10) ){ + scanf("%c", &c); /* Retrieving the user entry */ + c = tolower(c); /* Removing caps */ + if (c != '\n') { + if (isalpha(c)) { /* Making sure that the letter is alphanumeric */ + /* Checking wether the letter that has been entered (c) occurs in the guessWord */ + for (i = 0; i < wordlength; i++) { + if (guessWord[i] == c) { + currentWord[i] = c; + guessedLetter = 1; + } + } + /* Actions taken if the letter c doesn't occur in the guessWord and when it does */ + if (guessedLetter == 0) { + errors++; + printf("\nThat letter was incorrect.\n\n"); + } else { + guessedLetter = 0; + printf("\nThat letter was correct.\n\n"); + } + /* Showing the galg and the amount of errors */ + printf("%s%s\n\n", "The word including the letters you guessed: ", currentWord); + prn_galg(errors); + n++; /* Increasing attempt amount */ + /* Showing header if the word has not been guessed and the errors are lower than 10 */ + if ( (strcmp(currentWord, guessWord) != 0) && (errors < 10) ) { + printf("%d. %s", n, "Enter the letter(s) you want to guess: "); + } + /* If the letter isn't alphanumeric (isalpha()) */ + } else { + printf("Only alphanumeric symbols are allowed (a-z, A-Z), try again:\n"); + } + } + } + /* Showing the results, wether the player won or not */ + printf("---------------\n"); + printf("--- Results ---\n"); + printf("---------------\n\n"); + if (errors < 10) { + if (strcmp(currentWord, guessWord) == 0) { + printf("Congratulations you have guessed the right word!\n\n"); + } else { + printf("You have guessed the wrong word, better luck next time!\n\n"); + } + } else { + printf("You have guessed the wrong word, better luck next time!\n\n"); + } + printf("\nLetters guessed wrong: %d\nThe word that needed to be guessed: %s\nThe word you guessed: %s\n", errors, guessWord, currentWord); + printf("\nEnter 'end' to end the game or enter 'again' to guess another word:\n"); + // Making sure that the user doesn't enter strange things + while ((strcmp(udi, "END") != 0) && (strcmp(udi, "AGAIN") != 0)) { + if (strcmp(udi, "EMPTY") != 0) { + printf("\n\nIt is not allowed to enter anything else than 'again' or 'end', try again:\n"); + } + // Retrieving the udi (udi = user determined input) + scanf("%s", udi); + // Converting the udi to uppercase + int x; + for (x = 0; x < 5; x++) { + udi[x] = toupper(udi[x]); + } + } + printf("\n\n--------------------------------------------\n\n"); +} +return 0; +} diff --git a/Heapsort.c b/Heapsort.c new file mode 100644 index 00000000..92b2317e --- /dev/null +++ b/Heapsort.c @@ -0,0 +1,58 @@ +/* + * C Program to sort an array based on heap sort algorithm(MAX heap) + */ +#include + +void main() +{ + int heap[10], no, i, j, c, root, temp; + + printf("\n Enter no of elements :"); + scanf("%d", &no); + printf("\n Enter the nos : "); + for (i = 0; i < no; i++) + scanf("%d", &heap[i]); + for (i = 1; i < no; i++) + { + c = i; + do + { + root = (c - 1) / 2; + if (heap[root] < heap[c]) /* to create MAX heap array */ + { + temp = heap[root]; + heap[root] = heap[c]; + heap[c] = temp; + } + c = root; + } while (c != 0); + } + + printf("Heap array : "); + for (i = 0; i < no; i++) + printf("%d\t ", heap[i]); + for (j = no - 1; j >= 0; j--) + { + temp = heap[0]; + heap[0] = heap[j /* swap max element with rightmost leaf element */ + heap[j] = temp; + root = 0; + do + { + c = 2 * root + 1; /* left node of root element */ + if ((heap[c] < heap[c + 1]) && c < j-1) + c++; + if (heap[root] +int main() { + // printf() displays the string inside quotation + printf("Hello, World!"); + return 0; +} + +Output +Hello, World! diff --git a/Hello, World b/Hello, World new file mode 100644 index 00000000..d4421d7d --- /dev/null +++ b/Hello, World @@ -0,0 +1,5 @@ +#include +int main(){ + printf("Hello, World"); + return 0; +} diff --git a/Identifies_missing_numbers b/Identifies_missing_numbers new file mode 100644 index 00000000..4d7944e0 --- /dev/null +++ b/Identifies_missing_numbers @@ -0,0 +1,23 @@ +/* + * C Program to identify missing numbers in a given array + */ +#include + +void main() +{ + int n, i, j, c, t, b; + + printf("Enter size of array : "); + scanf("%d", &n); + int array[n - 1]; /* array size-1 */ + printf("Enter elements into array : \n"); + for (i = 0; i < n - 1; i++) + scanf("%d", &array[i]); + b = array[0]; + for (i = 1; i < n - 1; i++) + b = b ^ array[i]; + for (i = 2, c = 1; i <= n; i++) + c = c ^ i; + c = c ^ b; + printf("Missing element is : %d \n", c); +} diff --git a/Insertion sort b/Insertion sort new file mode 100644 index 00000000..e8a6dbb0 --- /dev/null +++ b/Insertion sort @@ -0,0 +1,40 @@ +// Write a program in C that sorts any given array in Ascending order + + + +#include +#include +int main() +{ + int arr[50], size, i, j, k, element, index; + printf("Enter Array Size: "); + scanf("%d", &size); + printf("Enter %d Array Elements: ", size); + for(i=0; ij; k--) + arr[k] = arr[k-1]; + break; + } + } + } + else + continue; + arr[index] = element; + } + printf("\nSorted Array:\n"); + for(i=0; i +#include + +/* Link list node */ +struct Node { + int data; + struct Node* next; +}; + +/* Function to get the counts of node in a linked list */ +int getCount(struct Node* head); + +/* function to get the intersection point of two linked +lists head1 and head2 where head1 has d more nodes than +head2 */ +int _getIntesectionNode(int d, struct Node* head1, struct Node* head2); + +/* function to get the intersection point of two linked +lists head1 and head2 */ +int getIntesectionNode(struct Node* head1, struct Node* head2) +{ + int c1 = getCount(head1); + int c2 = getCount(head2); + int d; + + if (c1 > c2) { + d = c1 - c2; + return _getIntesectionNode(d, head1, head2); + } + else { + d = c2 - c1; + return _getIntesectionNode(d, head2, head1); + } +} + +/* function to get the intersection point of two linked +lists head1 and head2 where head1 has d more nodes than +head2 */ +int _getIntesectionNode(int d, struct Node* head1, struct Node* head2) +{ + int i; + struct Node* current1 = head1; + struct Node* current2 = head2; + + for (i = 0; i < d; i++) { + if (current1 == NULL) { + return -1; + } + current1 = current1->next; + } + + while (current1 != NULL && current2 != NULL) { + if (current1 == current2) + return current1->data; + current1 = current1->next; + current2 = current2->next; + } + + return -1; +} + +/* Takes head pointer of the linked list and +returns the count of nodes in the list */ +int getCount(struct Node* head) +{ + struct Node* current = head; + int count = 0; + + while (current != NULL) { + count++; + current = current->next; + } + + return count; +} + +/* IGNORE THE BELOW LINES OF CODE. THESE LINES +ARE JUST TO QUICKLY TEST THE ABOVE FUNCTION */ +int main() +{ + /* + Create two linked lists + + 1st 3->6->9->15->30 + 2nd 10->15->30 + + 15 is the intersection point +*/ + + struct Node* newNode; + struct Node* head1 = (struct Node*)malloc(sizeof(struct Node)); + head1->data = 10; + + struct Node* head2 = (struct Node*)malloc(sizeof(struct Node)); + head2->data = 3; + + newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = 6; + head2->next = newNode; + + newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = 9; + head2->next->next = newNode; + + newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = 15; + head1->next = newNode; + head2->next->next->next = newNode; + + newNode = (struct Node*)malloc(sizeof(struct Node)); + newNode->data = 30; + head1->next->next = newNode; + + head1->next->next->next = NULL; + + printf("\n The node of intersection is %d \n", + getIntesectionNode(head1, head2)); + + getchar(); +} diff --git a/IpAddress.c b/IpAddress.c new file mode 100644 index 00000000..89f4ade9 --- /dev/null +++ b/IpAddress.c @@ -0,0 +1,57 @@ +#include +#include + +/* +Function : extractIpAddress +Arguments : +1) sourceString - String pointer that contains ip address +2) ipAddress - Target variable short type array pointer that will store ip address octets +*/ +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/Knapsack problem b/Knapsack problem new file mode 100644 index 00000000..33bff3ae --- /dev/null +++ b/Knapsack problem @@ -0,0 +1,41 @@ +/* A Naive recursive implementation +of 0-1 Knapsack problem */ +#include + +// A utility function that returns +// maximum of two integers +int max(int a, int b) { return (a > b) ? a : b; } + +// Returns the maximum value that can be +// put in a knapsack of capacity W +int knapSack(int W, int wt[], int val[], int n) +{ + // Base Case + if (n == 0 || W == 0) + return 0; + + // If weight of the nth item is more than + // Knapsack capacity W, then this item cannot + // be included in the optimal solution + if (wt[n - 1] > W) + return knapSack(W, wt, val, n - 1); + + // Return the maximum of two cases: + // (1) nth item included + // (2) not included + else + return max( + val[n - 1] + knapSack(W - wt[n - 1], wt, val, n - 1), + knapSack(W, wt, val, n - 1)); +} + +// Driver program to test above function +int main() +{ + int val[] = { 60, 100, 120 }; + int wt[] = { 10, 20, 30 }; + int W = 50; + int n = sizeof(val) / sizeof(val[0]); + printf("%d", knapSack(W, wt, val, n)); + return 0; +} diff --git a/LCM b/LCM new file mode 100644 index 00000000..ccc2faf1 --- /dev/null +++ b/LCM @@ -0,0 +1,22 @@ +#include + +int main() +{ + int Num1, Num2, max_Value; + + printf("Please Enter two integer Values \n"); + scanf("%d %d", &Num1, &Num2); + + max_Value = (Num1 > Num2)? Num1 : Num2; + + while(1) //Alway True + { + if(max_Value % Num1 == 0 && max_Value % Num2 == 0) + { + printf("LCM of %d and %d = %d", Num1, Num2, max_Value); + break; + } + ++max_Value; + } + return 0; +} diff --git a/LRU_Cache.cpp b/LRU_Cache.cpp new file mode 100644 index 00000000..069fb23b --- /dev/null +++ b/LRU_Cache.cpp @@ -0,0 +1,35 @@ +class LRUCache { +private: + int cap; + list keys; + unordered_map::iterator>> cache; +public: + LRUCache(int capacity) { + cap = capacity; + } + + int get(int key) { + if(cache.find(key) != cache.end()){ + keys.erase(cache[key].second); + keys.push_front(key); + cache[key].second = keys.begin(); + return cache[key].first; + } + return -1; + } + + void put(int key, int value) { + if(cache.find(key) != cache.end()){ + keys.erase(cache[key].second); + keys.push_front(key); + cache[key] = {value, keys.begin()}; + }else{ + if(keys.size() == cap){ + cache.erase(keys.back()); + keys.pop_back(); + } + keys.push_front(key); + cache[key] = {value, keys.begin()}; + } + } +}; diff --git a/LargestNo. b/LargestNo. new file mode 100644 index 00000000..8c571177 --- /dev/null +++ b/LargestNo. @@ -0,0 +1,20 @@ +#include +int main() { + double n1, n2, n3; + printf("Enter three different numbers: "); + scanf("%lf %lf %lf", &n1, &n2, &n3); + + // if n1 is greater than both n2 and n3, n1 is the largest + if (n1 >= n2 && n1 >= n3) + printf("%.2f is the largest number.", n1); + + // if n2 is greater than both n1 and n3, n2 is the largest + if (n2 >= n1 && n2 >= n3) + printf("%.2f is the largest number.", n2); + + // if n3 is greater than both n1 and n2, n3 is the largest + if (n3 >= n1 && n3 >= n2) + printf("%.2f is the largest number.", n3); + + return 0; +} diff --git a/Leap Year b/Leap Year new file mode 100644 index 00000000..aa973bb8 --- /dev/null +++ b/Leap Year @@ -0,0 +1,27 @@ +#include +void main() + { int year; + printf("Enter a year: "); + scanf("%d", &year); + + // leap year if perfectly visible by 400 + if (year % 400 == 0) { + printf("%d is a leap year.", year); + } + // not a leap year if visible by 100 + // but not divisible by 400 + else if (year % 100 == 0) { + printf("%d is not a leap year.", year); + } + // leap year if not divisible by 100 + // but divisible by 4 + else if (year % 4 == 0) { + printf("%d is a leap year.", year); + } + // all other years are not leap year + else { + printf("%d is not a leap year.", year); + } + + getch(); + } diff --git a/Leap Year Checker b/Leap Year Checker new file mode 100644 index 00000000..2b513647 --- /dev/null +++ b/Leap Year Checker @@ -0,0 +1,26 @@ +#include +int main() +{ + int y; + + printf("Enter year: "); + scanf("%d",&y); + + if(y % 4 == 0) + { + //Nested if else + if( y % 100 == 0) + { + if ( y % 400 == 0) + printf("%d is a Leap Year", y); + else + printf("%d is not a Leap Year", y); + } + else + printf("%d is a Leap Year", y ); + } + else + printf("%d is not a Leap Year", y); + + return 0; +} diff --git a/Library management system using structures in C b/Library management system using structures in C new file mode 100644 index 00000000..4a8a07b8 --- /dev/null +++ b/Library management system using structures in C @@ -0,0 +1,291 @@ +#include +#include +struct Book{ + int bookId; + char bookName[50]; + char author[50]; + int bookPrice; + int quantityInLibrary; + int quantityIssued ; +}; + +struct Member{ + int memberId; + char memberName[50]; + int memberFine; + int noOfBooksIssued; + int isMember; +}; + + +void displayDetailsOfAllMembers(struct Member members[] ,int noOfMembers) +{ + for(int i=0;i +#include +struct node +{ + int data; + struct node *next; +}; +struct node *front; +struct node *rear; +void insert(); +void delete(); +void display(); +void main () +{ + int choice; + while(choice != 4) + { + printf("\n*************************Main Menu*****************************\n"); + printf("\n=================================================================\n"); + printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n"); + printf("\nEnter your choice ?"); + scanf("%d",& choice); + switch(choice) + { + case 1: + insert(); + break; + case 2: + delete(); + break; + case 3: + display(); + break; + case 4: + exit(0); + break; + default: + printf("\nEnter valid choice??\n"); + } + } +} +void insert() +{ + struct node *ptr; + int item; + + ptr = (struct node *) malloc (sizeof(struct node)); + if(ptr == NULL) + { + printf("\nOVERFLOW\n"); + return; + } + else + { + printf("\nEnter value?\n"); + scanf("%d",&item); + ptr -> data = item; + if(front == NULL) + { + front = ptr; + rear = ptr; + front -> next = NULL; + rear -> next = NULL; + } + else + { + rear -> next = ptr; + rear = ptr; + rear->next = NULL; + } + } +} +void delete () +{ + struct node *ptr; + if(front == NULL) + { + printf("\nUNDERFLOW\n"); + return; + } + else + { + ptr = front; + front = front -> next; + free(ptr); + } +} +void display() +{ + struct node *ptr; + ptr = front; + if(front == NULL) + { + printf("\nEmpty queue\n"); + } + else + { printf("\nprinting values .....\n"); + while(ptr != NULL) + { + printf("\n%d\n",ptr -> data); + ptr = ptr -> next; + } + } +} diff --git a/Linked_List_in_C b/Linked_List_in_C new file mode 100644 index 00000000..271cc5a5 --- /dev/null +++ b/Linked_List_in_C @@ -0,0 +1,267 @@ +#include +#include +struct node +{ + int data; + struct node *next; +}; +struct node *head; + +void beginsert (); +void lastinsert (); +void randominsert(); +void begin_delete(); +void last_delete(); +void random_delete(); +void display(); +void search(); +void main () +{ + int choice =0; + while(choice != 9) + { + printf("\n\n*********Main Menu*********\n"); + printf("\nChoose one option from the following list ...\n"); + printf("\n===============================================\n"); + printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from Beginning\n + 5.Delete from last\n6.Delete node after specified location\n7.Search for an element\n8.Show\n9.Exit\n"); + printf("\nEnter your choice?\n"); + scanf("\n%d",&choice); + switch(choice) + { + case 1: + beginsert(); + break; + case 2: + lastinsert(); + break; + case 3: + randominsert(); + break; + case 4: + begin_delete(); + break; + case 5: + last_delete(); + break; + case 6: + random_delete(); + break; + case 7: + search(); + break; + case 8: + display(); + break; + case 9: + exit(0); + break; + default: + printf("Please enter valid choice.."); + } + } +} +void beginsert() +{ + struct node *ptr; + int item; + ptr = (struct node *) malloc(sizeof(struct node *)); + if(ptr == NULL) + { + printf("\nOVERFLOW"); + } + else + { + printf("\nEnter value\n"); + scanf("%d",&item); + ptr->data = item; + ptr->next = head; + head = ptr; + printf("\nNode inserted"); + } + +} +void lastinsert() +{ + struct node *ptr,*temp; + int item; + ptr = (struct node*)malloc(sizeof(struct node)); + if(ptr == NULL) + { + printf("\nOVERFLOW"); + } + else + { + printf("\nEnter value?\n"); + scanf("%d",&item); + ptr->data = item; + if(head == NULL) + { + ptr -> next = NULL; + head = ptr; + printf("\nNode inserted"); + } + else + { + temp = head; + while (temp -> next != NULL) + { + temp = temp -> next; + } + temp->next = ptr; + ptr->next = NULL; + printf("\nNode inserted"); + + } + } +} +void randominsert() +{ + int i,loc,item; + struct node *ptr, *temp; + ptr = (struct node *) malloc (sizeof(struct node)); + if(ptr == NULL) + { + printf("\nOVERFLOW"); + } + else + { + printf("\nEnter element value"); + scanf("%d",&item); + ptr->data = item; + printf("\nEnter the location after which you want to insert "); + scanf("\n%d",&loc); + temp=head; + for(i=0;inext; + if(temp == NULL) + { + printf("\ncan't insert\n"); + return; + } + + } + ptr ->next = temp ->next; + temp ->next = ptr; + printf("\nNode inserted"); + } +} +void begin_delete() +{ + struct node *ptr; + if(head == NULL) + { + printf("\nList is empty\n"); + } + else + { + ptr = head; + head = ptr->next; + free(ptr); + printf("\nNode deleted from the begining ...\n"); + } +} +void last_delete() +{ + struct node *ptr,*ptr1; + if(head == NULL) + { + printf("\nlist is empty"); + } + else if(head -> next == NULL) + { + head = NULL; + free(head); + printf("\nOnly node of the list deleted ...\n"); + } + + else + { + ptr = head; + while(ptr->next != NULL) + { + ptr1 = ptr; + ptr = ptr ->next; + } + ptr1->next = NULL; + free(ptr); + printf("\nDeleted Node from the last ...\n"); + } +} +void random_delete() +{ + struct node *ptr,*ptr1; + int loc,i; + printf("\n Enter the location of the node after which you want to perform deletion \n"); + scanf("%d",&loc); + ptr=head; + for(i=0;inext; + + if(ptr == NULL) + { + printf("\nCan't delete"); + return; + } + } + ptr1 ->next = ptr ->next; + free(ptr); + printf("\nDeleted node %d ",loc+1); +} +void search() +{ + struct node *ptr; + int item,i=0,flag; + ptr = head; + if(ptr == NULL) + { + printf("\nEmpty List\n"); + } + else + { + printf("\nEnter item which you want to search?\n"); + scanf("%d",&item); + while (ptr!=NULL) + { + if(ptr->data == item) + { + printf("item found at location %d ",i+1); + flag=0; + } + else + { + flag=1; + } + i++; + ptr = ptr -> next; + } + if(flag==1) + { + printf("Item not found\n"); + } + } + +} + +void display() +{ + struct node *ptr; + ptr = head; + if(ptr == NULL) + { + printf("Nothing to print"); + } + else + { + printf("\nprinting values . . . . .\n"); + while (ptr!=NULL) + { + printf("\n%d",ptr->data); + ptr = ptr -> next; + } + } +} + diff --git a/Liquidityno b/Liquidityno new file mode 100644 index 00000000..5065bb81 --- /dev/null +++ b/Liquidityno @@ -0,0 +1,21 @@ +#include + int main() { + int a[10]; + int i; + int greatest; + printf("Enter ten values:"); + //Store 10 numbers in an array + for (i = 0; i < 10; i++) { + scanf("%d", &a[i]); + } + //Assume that a[0] is greatest + greatest = a[0]; + for (i = 0; i < 10; i++) { +if (a[i] > greatest) { +greatest = a[i]; + } + } + printf(" + Greatest of ten numbers is %d", greatest); + return 0; + } diff --git a/MagicSquare.c b/MagicSquare.c new file mode 100644 index 00000000..ac62695f --- /dev/null +++ b/MagicSquare.c @@ -0,0 +1,72 @@ +#include +#include +// program to generate a magic square for a odd given input +main() +{ + int n,i,j,r,c; + printf("Enter the size of magic square: \n"); + scanf("%d",&n); + int a[n][n]; + memset(a,0,sizeof(a)); + + if(n%2!=0) + { + + r=n/2; + c=n-1; + + for(i=1;i<=n*n;i++) + { + + + if(r==-1 && c==n) + { + r=0; + c=n-2; + + } + + + else if(r==-1) + { + r=n-1; + } + else if(c==n) + { + c=0; + } + + if(a[r][c]) + { + r=(r+1)%n; + c=c-2; + if(c==-1) + c=n-1; + else if (c==-2) + c=1; + a[r][c]=i; + r--; + c++; + } + else + { + a[r][c]=i; + r--; + c++; + } + + + } + + for(i=0;i + +int main() +{ + int m, n, p, q, c, d, k, sum = 0; + int first[10][10], second[10][10], multiply[10][10]; + + printf("Enter the number of rows and columns of first matrix\n"); + scanf("%d%d", &m, &n); + printf("Enter the elements of first matrix\n"); + + for ( c = 0 ; c < m ; c++ ) + for ( d = 0 ; d < n ; d++ ) + scanf("%d", &first[c][d]); + + printf("Enter the number of rows and columns of second matrix\n"); + scanf("%d%d", &p, &q); + + if ( n != p ) + printf("Matrices with entered orders can't be multiplied with each other.\n"); + else + { + printf("Enter the elements of second matrix\n"); + + for ( c = 0 ; c < p ; c++ ) + for ( d = 0 ; d < q ; d++ ) + scanf("%d", &second[c][d]); + + for ( c = 0 ; c < m ; c++ ) + { + for ( d = 0 ; d < q ; d++ ) + { + for ( k = 0 ; k < p ; k++ ) + { + sum = sum + first[c][k]*second[k][d]; + } + + multiply[c][d] = sum; + sum = 0; + } + } + + printf("Product of entered matrices:-\n"); + + for ( c = 0 ; c < m ; c++ ) + { + for ( d = 0 ; d < q ; d++ ) + printf("%d\t", multiply[c][d]); + + printf("\n"); + } + } + + return 0; +} diff --git a/Matrixmultiplication.c b/Matrixmultiplication.c new file mode 100644 index 00000000..6aed5ded --- /dev/null +++ b/Matrixmultiplication.c @@ -0,0 +1,49 @@ +#include +#include +int main(){ +int a[10][10],b[10][10],mul[10][10],r,c,i,j,k; +system("cls"); +printf("enter the number of row="); +scanf("%d",&r); +printf("enter the number of column="); +scanf("%d",&c); +printf("enter the first matrix element=\n"); +for(i=0;i +int main() +{ +int a,b,c,max; +cout<<"Enter 3 numbers: "<>a>>b>>c; +cout<<"The max between a,b and c is: " +if(a>b) +{ +max=a; +} +if(b>a) +{ +max=b; +} +if(c>max) +{ +max=c; +} +cout< + +int main() +{ + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int n, i, *ptr, sum = 0; + + printf("\n\nEnter number of elements: "); + scanf("%d", &n); + + // dynamic memory allocation using malloc() + ptr = (int *) malloc(n*sizeof(int)); + + if(ptr == NULL) // if empty array + { + printf("\n\nError! Memory not allocated\n"); + return 0; // end of program + } + + printf("\n\nEnter elements of array: \n\n"); + for(i = 0; i < n; i++) + { + // storing elements at contiguous memory locations + scanf("%d", ptr+i); + sum = sum + *(ptr + i); + } + + // printing the array elements using pointer to the location + printf("\n\nThe elements of the array are: "); + for(i = 0; i < n; i++) + { + printf("%d ",ptr[i]); // ptr[i] is same as *(ptr + i) + } + + /* + freeing memory of ptr allocated by malloc + using the free() method + */ + free(ptr); + + printf("\n\n\t\t\tCoding is Fun !\n\n\n"); + return 0; +} diff --git a/Merge sort b/Merge sort new file mode 100644 index 00000000..a01eb008 --- /dev/null +++ b/Merge sort @@ -0,0 +1,176 @@ +/* C program for Merge Sort */ +#include +#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/Merge two arrays b/Merge two arrays new file mode 100644 index 00000000..ea923446 --- /dev/null +++ b/Merge two arrays @@ -0,0 +1,66 @@ +#include +void merge(int [], int, int [], int, int []); + +int main() { + int a[100], b[100], m, n, c, sorted[200]; + + printf("Input number of elements in first array\n"); + scanf("%d", &m); + + printf("Input %d integers\n", m); + for (c = 0; c < m; c++) { + scanf("%d", &a[c]); + } + + printf("Input number of elements in second array\n"); + scanf("%d", &n); + + printf("Input %d integers\n", n); + for (c = 0; c < n; c++) { + scanf("%d", &b[c]); + } + + merge(a, m, b, n, sorted); + + printf("Sorted array:\n"); + + for (c = 0; c < m + n; c++) { + printf("%d\n", sorted[c]); + } + + return 0; +} + +void merge(int a[], int m, int b[], int n, int sorted[]) { + int i, j, k; + + j = k = 0; + + for (i = 0; i < m + n;) { + if (j < m && k < n) { + if (a[j] < b[k]) { + sorted[i] = a[j]; + j++; + } + else { + sorted[i] = b[k]; + k++; + } + i++; + } + else if (j == m) { + for (; i < m + n;) { + sorted[i] = b[k]; + k++; + i++; + } + } + else { + for (; i < m + n;) { + sorted[i] = a[j]; + j++; + i++; + } + } + } +} diff --git a/Merge two sorted arrays b/Merge two sorted arrays new file mode 100644 index 00000000..3937a3a9 --- /dev/null +++ b/Merge two sorted arrays @@ -0,0 +1,63 @@ +#include +#include +int merge_two_sorted_arrays(int arr1[], int arr2[], int arr3[], int m, int n) +{ +int i,j,k; +i = j = k = 0; +for(i=0;i < m && j < n;) +{ +if(arr1[i] < arr2[j]) +{ +arr3[k] = arr1[i]; +k++; +i++; +} +else +{ +arr3[k] = arr2[j]; +k++; +j++; +} +} +while(i < m) +{ +arr3[k] = arr1[i]; +k++; +i++; +} +while(j < n) +{ +arr3[k] = arr2[j]; +k++; +j++; +} +} +int main() +{ +int n,m; +printf("\nEnter the size of Array 1 : "); +scanf("%d",&m); +printf("\nEnter the size of Array 2 : "); +scanf("%d",&n); +int arr1[m],arr2[n]; +int arr3[m+n]; +int i; +printf("\nInput the Array 1 elements : "); +for(i = 0; i < m; i++) +{ +scanf("%d",&arr1[i]); +} +printf("\nInput the Array 2 elements : "); +for(i = 0;i +using namespace std; + +// An interval has start and end limit +struct Interval +{ + int start, end; +}; + +// Compares two intervals according to their start limit. + +bool compareInterval(Interval i1, Interval i2) +{ + return (i1.start < i2.start); +} + +// The main function that takes a set of intervals, merges + +void mergeIntervals(Interval arr[], int n) +{ + if (n <= 0) + return; + + stack s; + + sort(arr, arr+n, compareInterval); + + s.push(arr[0]); + + for (int i = 1 ; i < n; i++) + { + Interval top = s.top(); + + if (top.end < arr[i].start) + s.push(arr[i]); + + else if (top.end < arr[i].end) + { + top.end = arr[i].end; + s.pop(); + s.push(top); + } + } + + cout << "\n The Merged Intervals are: "; + while (!s.empty()) + { + Interval t = s.top(); + cout << "[" << t.start << "," << t.end << "] "; + s.pop(); + } + return; +} + +int main() +{ + Interval arr[] = { {6,8}, {1,9}, {2,4}, {4,7} }; + int n = sizeof(arr)/sizeof(arr[0]); + mergeIntervals(arr, n); + return 0; +} diff --git a/Multiplication of Two Numbers b/Multiplication of Two Numbers new file mode 100644 index 00000000..824fbf98 --- /dev/null +++ b/Multiplication of Two Numbers @@ -0,0 +1,10 @@ +#include +int main() +{ + int num1,num2,product; + printf("Enter two numbers:"); + scanf("%d %d",&num1,&num2); + product=num1*num2; + printf("Product of two numbers: %d",product); + return 0; +} diff --git a/MultiplicationOfMatrix.c b/MultiplicationOfMatrix.c new file mode 100644 index 00000000..def03f7b --- /dev/null +++ b/MultiplicationOfMatrix.c @@ -0,0 +1,82 @@ +#include + +// function to get matrix elements entered by the user +void getMatrixElements(int matrix[][10], int row, int column) { + + printf("\nEnter elements: \n"); + + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("Enter a%d%d: ", i + 1, j + 1); + scanf("%d", &matrix[i][j]); + } + } +} + +// function to multiply two matrices +void multiplyMatrices(int first[][10], + int second[][10], + int result[][10], + int r1, int c1, int r2, int c2) { + + // Initializing elements of matrix mult to 0. + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + result[i][j] = 0; + } + } + + // Multiplying first and second matrices and storing it in result + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + for (int k = 0; k < c1; ++k) { + result[i][j] += first[i][k] * second[k][j]; + } + } + } +} + +// function to display the matrix +void display(int result[][10], int row, int column) { + + printf("\nOutput Matrix:\n"); + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("%d ", result[i][j]); + if (j == column - 1) + printf("\n"); + } + } +} + +int main() { + int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2; + printf("Enter rows and column for the first matrix: "); + scanf("%d %d", &r1, &c1); + printf("Enter rows and column for the second matrix: "); + scanf("%d %d", &r2, &c2); + + // Taking input until + // 1st matrix columns is not equal to 2nd matrix row + while (c1 != r2) { + printf("Error! Enter rows and columns again.\n"); + printf("Enter rows and columns for the first matrix: "); + scanf("%d%d", &r1, &c1); + printf("Enter rows and columns for the second matrix: "); + scanf("%d%d", &r2, &c2); + } + + // get elements of the first matrix + getMatrixElements(first, r1, c1); + + // get elements of the second matrix + getMatrixElements(second, r2, c2); + + // multiply two matrices. + multiplyMatrices(first, second, result, r1, c1, r2, c2); + + // display the result + display(result, r1, c2); + + return 0; +} diff --git a/Music Player b/Music Player new file mode 100644 index 00000000..6e08b928 --- /dev/null +++ b/Music Player @@ -0,0 +1,65 @@ + +#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers +// Windows Header Files: +#include +#include +#include +#include + +#pragma comment(lib, "strmiids.lib") + +class Mp3 +{ +public: + Mp3(); + ~Mp3(); + + bool Load(LPCWSTR filename); + void Cleanup(); + + bool Play(); + bool Pause(); + bool Stop(); + + // Poll this function with msTimeout = 0, so that it return immediately. + // If the mp3 finished playing, WaitForCompletion will return true; + bool WaitForCompletion(long msTimeout, long* EvCode); + + // -10000 is lowest volume and 0 is highest volume, positive value > 0 will fail + bool SetVolume(long vol); + + // -10000 is lowest volume and 0 is highest volume + long GetVolume(); + + // Returns the duration in 1/10 millionth of a second, + // meaning 10,000,000 == 1 second + // You have to divide the result by 10,000,000 + // to get the duration in seconds. + __int64 GetDuration(); + + // Returns the current playing position + // in 1/10 millionth of a second, + // meaning 10,000,000 == 1 second + // You have to divide the result by 10,000,000 + // to get the duration in seconds. + __int64 GetCurrentPosition(); + + // Seek to position with pCurrent and pStop + // bAbsolutePositioning specifies absolute or relative positioning. + // If pCurrent and pStop have the same value, the player will seek to the position + // and stop playing. Note: Even if pCurrent and pStop have the same value, + // avoid putting the same pointer into both of them, meaning put different + // pointers with the same dereferenced value. + bool SetPositions(__int64* pCurrent, __int64* pStop, bool bAbsolutePositioning); + +private: + IGraphBuilder * pigb; + IMediaControl * pimc; + IMediaEventEx * pimex; + IBasicAudio * piba; + IMediaSeeking * pims; + bool ready; + // Duration of the MP3. + __int64 duration; + +}; diff --git a/N reverse natural number b/N reverse natural number new file mode 100644 index 00000000..1c76c33f --- /dev/null +++ b/N reverse natural number @@ -0,0 +1,20 @@ +#include + +int main() +{ + int i, n; + + + printf("Enter any number: "); + scanf("%d", &n); + + printf("Natural numbers from 1 to %d : \n", n); + + + for(i=n; i<=1; i--) + { + printf("%d\n", i); + } + + return 0; +} diff --git a/NUmber to Words conversation b/NUmber to Words conversation new file mode 100644 index 00000000..f396ecc9 --- /dev/null +++ b/NUmber to Words conversation @@ -0,0 +1,62 @@ +#include + +int main() +{ + int n, num = 0; + + /* Input number from user */ + printf("Enter any number to print in words: "); + scanf("%d", &n); + + /* Store reverse of n in num */ + while(n != 0) + { + num = (num * 10) + (n % 10); + n /= 10; + } + + /* + * Extract last digit of number and print corresponding digit in words + * till num becomes 0 + */ + while(num != 0) + { + switch(num % 10) + { + case 0: + printf("Zero "); + break; + 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; + } + + num = num / 10; + } + + return 0; +} diff --git a/Neon b/Neon new file mode 100644 index 00000000..e2a5fb77 --- /dev/null +++ b/Neon @@ -0,0 +1,28 @@ +#include +int isNeon(int num) +{ + //storing the square of x + int square = 0; + //Store sum of digits (square number) + int sum_digits = 0; + //Calculate square of given number + square = (num * num); + while (square != 0) + { + sum_digits = (sum_digits + (square % 10)); + square = (square / 10); + } + return (sum_digits == num); +} +int main() +{ + int data = 0; + int isNeonNumber = 0; + //Ask to enter the number + printf("Enter the number = "); + scanf("%d",&data); + // if is isNeonNumber is 1, then neon number + isNeonNumber = isNeon(data); + (isNeonNumber)? printf("neon number\n\n"):printf("Not a neon number\n\n"); + return 0; +} diff --git a/Octal to Hexadecimal in C b/Octal to Hexadecimal in C new file mode 100644 index 00000000..539ac8c6 --- /dev/null +++ b/Octal to Hexadecimal in C @@ -0,0 +1,97 @@ +#include +#include +int main() +{ + int octaltobinary[]={0,1,10,11,100,101,110,111}; + char hexadecimal[10]; + char hex[10]; + long int binary=0; + int octal; + int rem=0; + int position=1; + int len=0; + int k=0; + printf("Enter a octal number"); + scanf("%d",&octal); +// Converting octal number into a binary number. +while(octal!=0) + { + rem=octal%10; + binary=octaltobinary[rem]*position+binary; + octal=octal/10; + position=position*1000; + } + printf("The binary number is : %ld",binary); + + // Converting binary number into a hexadecimal number. + while(binary > 0) + { + rem = binary % 10000; + switch(rem) + { + case 0: + strcat(hexadecimal, "0"); + break; + case 1: + strcat(hexadecimal, "1"); + break; + case 10: + strcat(hexadecimal, "2"); + break; + case 11: + strcat(hexadecimal, "3"); + break; + case 100: + strcat(hexadecimal, "4"); + break; + case 101: + strcat(hexadecimal, "5"); + break; + case 110: + strcat(hexadecimal, "6"); + break; + case 111: + strcat(hexadecimal, "7"); + break; + case 1000: + strcat(hexadecimal, "8"); + break; + case 1001: + strcat(hexadecimal, "9"); + break; + case 1010: + strcat(hexadecimal, "A"); + break; + case 1011: + strcat(hexadecimal, "B"); + break; + case 1100: + strcat(hexadecimal, "C"); + break; + case 1101: + strcat(hexadecimal, "D"); + break; + case 1110: + strcat(hexadecimal, "E"); + break; + case 1111: + strcat(hexadecimal, "F"); + break; + } +len=len+1; + binary /= 10000; + } + for(int i=len-1;i>=0;i--) +{ + hex[k]=hexadecimal[i]; + k++; +} +hex[len]='\0'; +printf("\nThe hexadecimal number is :"); +for(int i=0; hex[i]!='\0';i++) +{ + printf("%c",hex[i]); +} + + return 0; +} diff --git a/Odd and even b/Odd and even new file mode 100644 index 00000000..d169ebbc --- /dev/null +++ b/Odd and even @@ -0,0 +1,14 @@ +include +int main() { + int num; + printf("Enter an integer: "); + scanf("%d", &num); + + // True if num is perfectly divisible by 2 + if(num % 2 == 0) + printf("%d is even.", num); + else + printf("%d is odd.", num); + + return 0; +} diff --git a/Oil Spill Game. C b/Oil Spill Game. C new file mode 100644 index 00000000..284a33a5 --- /dev/null +++ b/Oil Spill Game. C @@ -0,0 +1,253 @@ +Oil Spill Game in C. + +#include +#include +#include +#include +#include + +#define L 75 +#define R 77 + /* variable initialisations */ + +union REGS i,o; +int ch1,ch2; +int mx,my,vx=270,vy; +int temp,retval; +char s[20],s1[20]; +char ch; +int gd=DETECT,gm; +int x,y=400,r,c=7,score=0,lifes=4,flag=0,dlyfactor=0; + + /* main function */ +main() +{ + initialise(); + firstscreen(); + displaylifes(); + displayscore(); + welcome(); + while(1) + { + condition1(); + display_drop(); + is_keyhit(); + check_catchdrop(); + check_looselife(); + } + nosound(); +} + /* sub functions */ + + +initialise() + { + initgraph(&gd,&gm,""); + mx=getmaxx();my=getmaxy(); + setcolor(4); + x=mx/2; + + image(x,y,c); + vy=20; + } +firstscreen() + { + + button3d(100,0,430,5); + button3d(100,0,5,450); + button3d(100,450,430,5); + button3d(525,0,5,455); + + setfillstyle(1,7); + bar(270,5,275,10); + bar(320,5,325,10); + bar(370,5,375,10); + + setfillstyle(1,8); + bar(267,10,278,15); + bar(317,10,328,15); + bar(367,10,378,15); + + button3d(280,448,80,9); + setcolor(4); + outtextxy(290,450,"OIL SPIL"); + } +welcome() + { + setfillstyle(1,0); + bar(200,250,450,350); + clk_but3d(200,250,250,100); + settextstyle(0,0,2); + setcolor(4); + outtextxy(240,280," WELCOME"); + outtextxy(215,310, " TO OILSPIL"); + getch(); + setfillstyle(1,0); + bar(200,250,453,353); + return; + } + + +is_keyhit() + { + if(kbhit()) + { + i.h.ah=0; + int86(22,&i,&o); + if(o.h.ah==1) { nosound();closegraph();exit(0); } + if(o.h.ah==75) { +sound(1800);image(x,y,0);x=x-10;image(x,y,c);nosound(); } + if(o.h.ah==77) { +sound(1800);image(x,y,0);x=x+10;image(x,y,c);nosound(); } + if(o.h.ah==80) { dlyfactor+=250; } + if(o.h.ah==72) { dlyfactor-=250; } + } + } +image(int x,int y,int c) + { + int b; + setcolor(c); + rectangle(x-10,y+6,x+10,y+20); + setcolor(0); + line(x-9,y+6,x+9,y+6); + setfillstyle(1,c); + bar(x-9,y+6,x+9,y+19); + setfillstyle(1,0); + bar(x-6,y+6,x+6,y+19); + setcolor(c); + for(b=1;b<=6;b++) + line(x-10+b,y+19+b,x+10-b,y+19+b); + setfillstyle(1,c); + bar(x-1,y+19+b,x+1,y+25+b); + fillellipse(x,y+25+b+3,8,3); + } +object(int x,int y,int c) + { + setcolor(c); + setfillstyle(1,c); + fillellipse(x,y+5,4,8); + } +condition1() + { + if(flag==1||vy>410) + { + vy = 20; + temp = random(3); + if(temp==0) vx=270; + else if(temp==1) vx=320; + else if(temp==2) vx =370; + flag=0; + } + } + + +check_looselife() + { + if(lifes==0) + { + setfillstyle(1,0); + bar(200,250,450,350); + clk_but3d(200,250,250,100); + settextstyle(0,0,2); + setcolor(1); + outtextxy(240,290,"GAME OVER"); + getch(); + closegraph(); + play_again(); + exit(0); + } + } +display_drop() + { + object(vx,vy,8); + delay(3000+dlyfactor); + object(vx,vy,0); + vy+=10; + } +check_catchdrop() + { + if((x-5<=vx)&&(vx<=x+5)&&vy==410) + displayscore(); + else if(vy>410) + displaylifes(); + nosound(); + } + displaylifes() + { + settextstyle(0,0,1); + clk_but3d(0,440,90,13); + setcolor(7); + outtextxy(5,445,s1); + lifes = lifes -1; + sprintf(s1,"LIFES = %d", lifes); + setcolor(4); + outtextxy(5,445,s1); + flag=0; + } + displayscore() + { + settextstyle(0,0,1); + clk_but3d(540,440,90,13); + setcolor(7); + outtextxy(545,445,s); + score=score+1; + sprintf(s,"SCORE = %d",score); + setcolor(4); + outtextxy(545,445,s); + flag=1; + vy=20; + } + button3d(int x,int y,int l,int w) + { + setcolor(LIGHTGRAY); + setfillstyle(1,LIGHTGRAY); + + bar(x,y,x+l,y+w); + + setcolor(WHITE); + line(x,y,x+l,y); + line(x,y,x,y+w); + + setcolor(DARKGRAY); + line(x+l+1,y,x+l+1,y+w+1); + line(x,y+w+1,x+l+1,y+w+1); + } + clk_but3d(int x,int y,int l,int w) + { + setcolor(LIGHTGRAY); + setfillstyle(1,LIGHTGRAY); + + bar(x,y,x+l,y+w); + + setcolor(DARKGRAY); + line(x,y,x+l,y); + line(x,y,x,y+w); + + setcolor(WHITE); + line(x+l+1,y,x+l+1,y+w+1); + line(x,y+w+1,x+l+1,y+w+1); + } + + + letter(int x,int y,int l,int w) + { + char s[1]; + int ch=3; + setcolor(RED); + sprintf(s,"%c",ch); + outtextxy(x + l/4 + 2,y + w/5, s); + } + restore_defaults() + { + y=400;c=7;score=0;lifes=4;flag=0;dlyfactor=0; + } + play_again() + { + printf("To Play again Press 'y'"); + if(getch()=='y') + { + restore_defaults(); + main(); + } + else + } diff --git a/Palindrome b/Palindrome new file mode 100644 index 00000000..9a3edbd7 --- /dev/null +++ b/Palindrome @@ -0,0 +1,19 @@ +#include +int main() +{ +int n,r,sum=0,temp; +printf("enter the number="); +scanf("%d",&n); +temp=n; +while(n>0) +{ +r=n%10; +sum=(sum*10)+r; +n=n/10; +} +if(temp==sum) +printf("palindrome number "); +else +printf("not palindrome"); +return 0; +} diff --git a/Palindrome Number b/Palindrome Number new file mode 100644 index 00000000..615dc04f --- /dev/null +++ b/Palindrome Number @@ -0,0 +1,22 @@ +#include +int main() { + int n, reversedN = 0, remainder, originalN; + printf("Enter an integer: "); + scanf("%d", &n); + originalN = n; + + // reversed integer is stored in reversedN + while (n != 0) { + remainder = n % 10; + reversedN = reversedN * 10 + remainder; + n /= 10; + } + + // palindrome if orignalN and reversedN are equal + if (originalN == reversedN) + printf("%d is a palindrome.", originalN); + else + printf("%d is not a palindrome.", originalN); + + return 0; +} diff --git a/Pascal's Triangle b/Pascal's Triangle new file mode 100644 index 00000000..29809b20 --- /dev/null +++ b/Pascal's Triangle @@ -0,0 +1,19 @@ +#include +int main() { + int rows, coef = 1, space, i, j; + printf("Enter the number of rows: "); + scanf("%d", &rows); + for (i = 0; i < rows; i++) { + for (space = 1; space <= rows - i; space++) + printf(" "); + for (j = 0; j <= i; j++) { + if (j == 0 || i == 0) + coef = 1; + else + coef = coef * (i - j + 1) / j; + printf("%4d", coef); + } + printf("\n"); + } + return 0; +} diff --git a/PascalTriangle.cpp b/PascalTriangle.cpp new file mode 100644 index 00000000..43feabdc --- /dev/null +++ b/PascalTriangle.cpp @@ -0,0 +1,30 @@ + +#include +#include +using namespace std; + + vector> pascal(int numRows) { + vector> res(numRows); + for(int i=0;i>row; + vector> res(row); + res=pascal(row); + for(int i=0;i +int main() { + int i, j, rows; + printf("Enter the number of rows: "); + scanf("%d", &rows); + for (i = 1; i <= rows; ++i) { + for (j = 1; j <= i; ++j) { + printf("* "); + } + printf("\n"); + } + return 0; +} diff --git a/Perfect Number b/Perfect Number new file mode 100644 index 00000000..bc422b0f --- /dev/null +++ b/Perfect Number @@ -0,0 +1,22 @@ +#include + +int main() +{ + int number, rem, sum = 0, i; + + printf("Enter a Number\n"); + scanf("%d", &number); + for (i = 1; i <= (number - 1); i++) + { + rem = number % i; + if (rem == 0) + { + sum = sum + i; + } + } + if (sum == number) + printf("Entered Number is perfect number"); + else + printf("Entered Number is not a perfect number"); + return 0; +} diff --git a/Positive Negative Zero b/Positive Negative Zero new file mode 100644 index 00000000..3d2d12a5 --- /dev/null +++ b/Positive Negative Zero @@ -0,0 +1,15 @@ +#include + +void main() +{ + int num; + + printf("Enter a number: \n"); + scanf("%d", &num); + if (num > 0) + printf("%d is a positive number \n", num); + else if (num < 0) + printf("%d is a negative number \n", num); + else + printf("0 is neither positive nor negative"); +} diff --git a/Positive or Negative b/Positive or Negative new file mode 100644 index 00000000..23e4e614 --- /dev/null +++ b/Positive or Negative @@ -0,0 +1,21 @@ +#include + +int main() +{ + int number; + + printf("Enter any number:\n"); + scanf("%d",&number); + + if (number >= 0) + { + if (number > 0) + printf("%d is Positive Number", number); + else + printf("You have entered Value zero."); + } + else + printf("%d is Negative Number", number); + + return 0; +} diff --git a/Prime number b/Prime number new file mode 100644 index 00000000..4fba8ae0 --- /dev/null +++ b/Prime number @@ -0,0 +1,27 @@ +#include +int main() { + int n, i, flag = 0; + printf("Enter a positive integer: "); + scanf("%d", &n); + + for (i = 2; i <= n / 2; ++i) { + + // condition for non-prime + if (n % i == 0) { + flag = 1; + break; + } + } + + if (n == 1) { + printf("1 is neither prime nor composite."); + } + else { + if (flag == 0) + printf("%d is a prime number.", n); + else + printf("%d is not a prime number.", n); + } + + return 0; +} diff --git a/Print Upper case Lower case alphabet b/Print Upper case Lower case alphabet new file mode 100644 index 00000000..bb9d67b9 --- /dev/null +++ b/Print Upper case Lower case alphabet @@ -0,0 +1,37 @@ +#include + +int main() + +{ + +char ch,ch1; + +for(ch='a';ch<='z';ch++) + +{ + +printf("%c %c\n",ch,ch-32); + +} + +} + +With ascii + +#include + +int main() + +{ + +char ch; + +for(ch='a';ch<='z';ch++) + +{ + +printf("%c %c %d %d\n",ch,ch-32,ch,ch-32); + +} + +} diff --git a/Print numbers b/Print numbers new file mode 100644 index 00000000..2d7ee0f0 --- /dev/null +++ b/Print numbers @@ -0,0 +1,12 @@ +// Print numbers from 1 to 10 +#include + +int main() { + int i; + + for (i = 1; i < 11; ++i) + { + printf("%d ", i); + } + return 0; +} diff --git a/Product of two No. b/Product of two No. new file mode 100644 index 00000000..bd79f0d0 --- /dev/null +++ b/Product of two No. @@ -0,0 +1,22 @@ +#include +#include + +void main(){ + +int one, two, multiply; + +printf("Enter first number - "); + +scanf("%d",&one); + +printf("Enter second number - "); + +scanf("%d",&two); + +multiply = one * two; + +printf("The multiplication of numbers %d and %d is %d",one,two,multiply); + +getch(); + +} diff --git a/Program of Fibonacci Series b/Program of Fibonacci Series new file mode 100644 index 00000000..d3375464 --- /dev/null +++ b/Program of Fibonacci Series @@ -0,0 +1,16 @@ +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/Program of adding two number in c b/Program of adding two number in c new file mode 100644 index 00000000..c7a914fb --- /dev/null +++ b/Program of adding two number in c @@ -0,0 +1,14 @@ +#include +int main() +{ + int x, y, z; + + printf("Enter two numbers to add\n"); + scanf("%d%d", &x, &y); + + z = x + y; + + printf("Sum of the numbers = %d\n", z); + + return 0; +} diff --git a/Program to Add Two Matrices b/Program to Add Two Matrices new file mode 100644 index 00000000..7da735bd --- /dev/null +++ b/Program to Add Two Matrices @@ -0,0 +1,27 @@ +#include +int main() { + int r, c, a[100][100], b[100][100], sum[100][100], i, j; + printf("Enter the number of rows (between 1 and 100): "); + scanf("%d", &r); + printf("Enter the number of columns (between 1 and 100): "); + scanf("%d", &c); + + printf("\nEnter elements of 1st matrix:\n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("Enter element a%d%d: ", i + 1, j + 1); + scanf("%d", &a[i][j]); + } + + printf("Enter elements of 2nd matrix:\n"); + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + printf("Enter element a%d%d: ", i + 1, j + 1); + scanf("%d", &b[i][j]); + } + + // adding two matrices + for (i = 0; i < r; ++i) + for (j = 0; j < c; ++j) { + sum[i][j] = a[i][j] + b[i][j]; + } diff --git a/Program to Reverse a String Using Recursion b/Program to Reverse a String Using Recursion new file mode 100644 index 00000000..a3c66421 --- /dev/null +++ b/Program to Reverse a String Using Recursion @@ -0,0 +1,31 @@ +#include +#include + +// declaring recursive function +char* reverse(char* str); + +void main() +{ + int i, j, k; + char str[100]; + char *rev; + printf("Enter the string:\t"); + scanf("%s", str); + printf("The original string is: %s\n", str); + rev = reverse(str); + printf("The reversed string is: %s\n", rev); + getch(); +} + +// defining the function +char* reverse(char *str) +{ + static int i = 0; + static char rev[100]; + if(*str) + { + reverse(str+1); + rev[i++] = *str; + } + return rev; +} diff --git a/Program to multiply two matrices b/Program to multiply two matrices new file mode 100644 index 00000000..0dc10d72 --- /dev/null +++ b/Program to multiply two matrices @@ -0,0 +1,51 @@ +#include + +using namespace std; + + + +// This function multiplies mat1[][] and mat2[][], and stores the result in res[][] +void multiply(int mat1[][N], + int mat2[][N], + int res[][N]) +{ + int i, j, k; + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + { + res[i][j] = 0; + for (k = 0; k < N; k++) + res[i][j] += mat1[i][k] * + mat2[k][j]; + } + } +} + +// Driver Code +int main() +{ + int i, j; + int res[N][N]; // To store result + int mat1[N][N] = {{1, 1, 1, 1}, + {2, 2, 2, 2}, + {3, 3, 3, 3}, + {4, 4, 4, 4}}; + + int mat2[N][N] = {{1, 1, 1, 1}, + {2, 2, 2, 2}, + {3, 3, 3, 3}, + {4, 4, 4, 4}}; + + multiply(mat1, mat2, res); + + cout << "Result matrix is \n"; + for (i = 0; i < N; i++) + { + for (j = 0; j < N; j++) + cout << res[i][j] << " "; + cout << "\n"; + } + + return 0; +} diff --git a/QuadraticRoots.c b/QuadraticRoots.c new file mode 100644 index 00000000..5941be24 --- /dev/null +++ b/QuadraticRoots.c @@ -0,0 +1,35 @@ +#include +#include +using namespace std; + +int main() { + + float a, b, c, x1, x2, discriminant, realPart, imaginaryPart; + cout << "Enter coefficients a, b and c: "; + cin >> a >> b >> c; + discriminant = b*b - 4*a*c; + + if (discriminant > 0) { + x1 = (-b + sqrt(discriminant)) / (2*a); + x2 = (-b - sqrt(discriminant)) / (2*a); + cout << "Roots are real and different." << endl; + cout << "x1 = " << x1 << endl; + cout << "x2 = " << x2 << endl; + } + + else if (discriminant == 0) { + cout << "Roots are real and same." << endl; + x1 = (-b + sqrt(discriminant)) / (2*a); + cout << "x1 = x2 =" << x1 << endl; + } + + else { + realPart = -b/(2*a); + imaginaryPart =sqrt(-discriminant)/(2*a); + cout << "Roots are complex and different." << endl; + cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl; + cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl; + } + + return 0; +} diff --git a/Quick Sort b/Quick Sort new file mode 100644 index 00000000..b5b4638d --- /dev/null +++ b/Quick Sort @@ -0,0 +1,48 @@ +#include +void quicksort(int number[25],int first,int last){ + int i, j, pivot, temp; + + if(firstnumber[pivot]) + j--; + if(i +int fact (int); +int main() +{ + int n,f; + printf("Enter the number whose factorial you want to calculate?"); + scanf("%d",&n); + f = fact(n); + printf("factorial = %d",f); +} +int fact(int n) +{ + if (n==0) + { + return 0; + } + else if ( n == 1) + { + return 1; + } + else + { + return n*fact(n-1); + } +} diff --git a/Radix-Sort b/Radix-Sort new file mode 100644 index 00000000..27b64848 --- /dev/null +++ b/Radix-Sort @@ -0,0 +1,57 @@ + +#include +using namespace std; + +int getMax(int arr[], int n) +{ + int mx = arr[0]; + for (int i = 1; i < n; i++) + if (arr[i] > mx) + mx = arr[i]; + return mx; +} + +void countSort(int arr[], int n, int exp) +{ + int output[n]; + int i, count[10] = { 0 }; + + for (i = 0; i < n; i++) + count[(arr[i] / exp) % 10]++; + + for (i = 1; i < 10; i++) + count[i] += count[i - 1]; + + for (i = n - 1; i >= 0; i--) { + output[count[(arr[i] / exp) % 10] - 1] = arr[i]; + count[(arr[i] / exp) % 10]--; + } + + for (i = 0; i < n; i++) + arr[i] = output[i]; +} + +void radixsort(int arr[], int n) +{ + int m = getMax(arr, n); + for (int exp = 1; m / exp > 0; exp *= 10) + countSort(arr, n, exp); +} + +void print(int arr[], int n) +{ + for (int i = 0; i < n; i++) + cout << arr[i] << " "; +} + +int main() +{ + int n; + cin>>n; + int arr[n]; + for(int i=0;i>arr[i]; + // Function Call + radixsort(arr, n); + print(arr, n); + return 0; +} diff --git a/ReadWrite.cpp b/ReadWrite.cpp new file mode 100644 index 00000000..e8940d04 --- /dev/null +++ b/ReadWrite.cpp @@ -0,0 +1,49 @@ +#include + +#include + +using namespace std; + +int main() { + + ofstream MyFile("sample.txt"); + + cout<<"Writing into a file\n"; + + string data; + + cout<<"Enter your name : \n"; + + cin>>data; + + + + MyFile << data << endl; + + cout<<"Enter your age : "; + + cin>>data; + + MyFile << data << endl; + + + + MyFile.close(); + + string myText; + +ifstream MyReadFile("sample.txt"); + +cout<<"Reading from a file\n"; + +while (getline (MyReadFile, myText)) { + + cout << myText< +#include +#include +#include + +int count_letters(string text); // function for counting letters +int count_words(string text); // function for counting words +int count_sent(string text); // function for counting sentences +int colemanformula(double L, double S); //Coleman-Liau index formula + + +int main(void) +{ + string text = get_string("Text: "); // user input of text + float letters = count_letters(text); // storing the number of letters + //printf("%.1f letters(s)\n",letters); + float words = count_words(text); // storing the number of words + //printf("%.1f word(s)\n",words); + float sentences = count_sent(text); // storing the number of sentences + //printf("%.1f sentence(s)\n", sentences); + + double L = (letters / words) * 100; // L is the average number of letters per 100 words in the text + double S = (sentences / words) * 100; // S is the average number of sentences per 100 words in the text. + + int grade = colemanformula(L, S); // calling function of coleman formula + + if (grade < 1) // checking grade condition + { + printf("Before Grade 1\n"); + } + else if (grade > 1 && grade <= 16) // checking grade condition + { + printf("Grade %i\n", grade); + } + else + { + printf("Grade 16+\n"); + } + + + + +} + +int count_letters(string text) // function for counting the number of letters in user input +{ + int count = 0; + + for (int i = 0, n = strlen(text); i < n; i++) + { + if (text[i] >= 'A' && text[i] <= 'Z') + { + count = count + 1; + } + else if (text[i] >= 'a' && text[i] <= 'z') + { + count = count + 1; + } + else if (text[i] == ' ') + { + count = count + 0; + } + } + + return count; + +} + +int count_words(string text) // function for counting the number of words in user input +{ + int words = 0, n = strlen(text); + + for (int i = 0; i <= n; i++) + { + + if (text[i] == ' ') + { + words = words + 1; + } + + } + + if (text[n - 1] == '.' || text[n - 1] == '!' || text[n - 1] == '?') + { + words = words + 1; + } + + return words; +} + +int count_sent(string text) // function for counting the number of sentences in user input +{ + int sent = 0, n = strlen(text); + + for (int i = 0; i <= n; i++) + { + if (text[i] == '!' || text[i] == '.' || text[i] == '?') + { + sent = sent + 1; + } + + } + return sent; +} + +int colemanformula(double L, double S) // coleman forumla for finding out the grade +{ + double index = 0; + index = (0.0588 * L) - (0.296 * S) - 15.8; + + int grade = round(index); + return grade; + +} + diff --git a/RegEx_Patterns_and_Intro_to_Databases.cpp b/RegEx_Patterns_and_Intro_to_Databases.cpp new file mode 100644 index 00000000..e29ba389 --- /dev/null +++ b/RegEx_Patterns_and_Intro_to_Databases.cpp @@ -0,0 +1,27 @@ +#include +#include + +using namespace std; + + +int main(){ + int N; + cin >> N; + list names; + for(int a0 = 0; a0 < N; a0++){ + string firstName; + string emailID; + cin >> firstName >> emailID; + if (regex_match (emailID, regex(".+@gmail.com") )){ + names.push_back(firstName); + } + } + names.sort(); + while (!names.empty()) + { + cout << names.front()< +#include +main() +{ +int i=0,flag=0,len1,len2,j,k=0; +char str1[100],str2[100],temp[100]={0}; +printf("Enter a string 1\n"); +gets(str1); +printf("Enter a string 2\n"); +gets(str2); +len1=strlen(str1); +len2=strlen(str2); + + for(i=0;i +#include +main() +{ + int i=0,j=0,k=0,a,minIndex=0,maxIndex=0,max=0,min=0; + char str1[100]={0},substr[100][100]={0},c; + printf("Enter a sentence\n"); + gets(str1); + while(str1[k]!='\0')//for splitting sentence + { + j=0; + while(str1[k]!=' '&&str1[k]!='\0') + { + substr[i][j]=str1[k]; + k++; + j++; + } + substr[i][j]='\0'; + i++; + if(str1[k]!='\0') + { + k++; + } + } + int len=i; + //Removing repeated words same as removing repeated elements in arrays + for(i=0;i +int main() { + int n, rev = 0, remainder; + printf("Enter an integer: "); + scanf("%d", &n); + while (n != 0) { + remainder = n % 10; + rev = rev * 10 + remainder; + n /= 10; + } + printf("Reversed number = %d", rev); + return 0; +} diff --git a/Reverseno. b/Reverseno. new file mode 100644 index 00000000..2555af0a --- /dev/null +++ b/Reverseno. @@ -0,0 +1,13 @@ +#include +int main() { + int n, rev = 0, remainder; + printf("Enter an integer: "); + scanf("%d", &n); + while (n != 0) { + remainder = n % 10; + rev = rev * 10 + remainder; + n /= 10; + } + printf("Reversed number = %d", rev); + return 0; +} diff --git a/Roots of a Quadratic Equation b/Roots of a Quadratic Equation new file mode 100644 index 00000000..8430b53f --- /dev/null +++ b/Roots of a Quadratic Equation @@ -0,0 +1,31 @@ +#include +#include +int main() { + double a, b, c, discriminant, root1, root2, realPart, imagPart; + printf("Enter coefficients a, b and c: "); + scanf("%lf %lf %lf", &a, &b, &c); + + discriminant = b * b - 4 * a * c; + + // condition for real and different roots + if (discriminant > 0) { + root1 = (-b + sqrt(discriminant)) / (2 * a); + root2 = (-b - sqrt(discriminant)) / (2 * a); + printf("root1 = %.2lf and root2 = %.2lf", root1, root2); + } + + // condition for real and equal roots + else if (discriminant == 0) { + root1 = root2 = -b / (2 * a); + printf("root1 = root2 = %.2lf;", root1); + } + + // if roots are not real + else { + realPart = -b / (2 * a); + imagPart = sqrt(-discriminant) / (2 * a); + printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart); + } + + return 0; +} diff --git a/SMALLNO b/SMALLNO new file mode 100644 index 00000000..b9fa1ddd --- /dev/null +++ b/SMALLNO @@ -0,0 +1,6 @@ +#define DELTA 1e-11 +.... + +if (abs(b-a) <= DELTA) { + (expression); +} diff --git a/Second Largest Element in Array b/Second Largest Element in Array new file mode 100644 index 00000000..0b246d14 --- /dev/null +++ b/Second Largest Element in Array @@ -0,0 +1,27 @@ +#include + +int main() { + int array[10] = {101, 11, 3, 4, 50, 69, 7, 8, 9, 0}; + int loop, largest, second; + + if(array[0] > array[1]) { + largest = array[0]; + second = array[1]; + } else { + largest = array[1]; + second = array[0]; + } + + for(loop = 2; loop < 10; loop++) { + if( largest < array[loop] ) { + second = largest; + largest = array[loop]; + } else if( second < array[loop] ) { + second = array[loop]; + } + } + + printf("Largest - %d \nSecond - %d \n", largest, second); + + return 0; +} diff --git a/Selection Sort Program in b/Selection Sort Program in C similarity index 100% rename from Selection Sort Program in rename to Selection Sort Program in C diff --git a/Selection sort b/Selection sort new file mode 100644 index 00000000..90425629 --- /dev/null +++ b/Selection sort @@ -0,0 +1,34 @@ +#include +int main(){ + /* Here i & j for loop counters, temp for swapping, + * count for total number of elements, number[] to + * store the input numbers in array. You can increase + * or decrease the size of number array as per requirement + */ + int i, j, count, temp, number[25]; + + printf("How many numbers u are going to enter?: "); + scanf("%d",&count); + + printf("Enter %d elements: ", count); + // Loop to get the elements stored in array + for(i=0;inumber[j]){ + temp=number[i]; + number[i]=number[j]; + number[j]=temp; + } + } + } + + printf("Sorted elements: "); + for(i=0;i +#include +void main() +{ +int p,r,t,i; +float si; +printf("\n enter Principle amount,rate,time"); +scanf("%d%d%d"&p,&r,&t); +si=(p*r*t)/100; +getch(); +} diff --git a/Simple calculator b/Simple calculator new file mode 100644 index 00000000..a8bd462a --- /dev/null +++ b/Simple calculator @@ -0,0 +1,29 @@ +#include +int main() { + char operator; + double first, second; + printf("Enter an operator (+, -, *,): "); + scanf("%c", &operator); + printf("Enter two operands: "); + scanf("%lf %lf", &first, &second); + + switch (operator) { + case '+': + printf("%.1lf + %.1lf = %.1lf", first, second, first + second); + break; + case '-': + printf("%.1lf - %.1lf = %.1lf", first, second, first - second); + break; + case '*': + printf("%.1lf * %.1lf = %.1lf", first, second, first * second); + break; + case '/': + printf("%.1lf / %.1lf = %.1lf", first, second, first / second); + break; + // operator doesn't match any case constant + default: + printf("Error! operator is not correct"); + } + + return 0; +} diff --git a/SimpleCalci b/SimpleCalci new file mode 100644 index 00000000..a8bd462a --- /dev/null +++ b/SimpleCalci @@ -0,0 +1,29 @@ +#include +int main() { + char operator; + double first, second; + printf("Enter an operator (+, -, *,): "); + scanf("%c", &operator); + printf("Enter two operands: "); + scanf("%lf %lf", &first, &second); + + switch (operator) { + case '+': + printf("%.1lf + %.1lf = %.1lf", first, second, first + second); + break; + case '-': + printf("%.1lf - %.1lf = %.1lf", first, second, first - second); + break; + case '*': + printf("%.1lf * %.1lf = %.1lf", first, second, first * second); + break; + case '/': + printf("%.1lf / %.1lf = %.1lf", first, second, first / second); + break; + // operator doesn't match any case constant + default: + printf("Error! operator is not correct"); + } + + return 0; +} diff --git a/Smaller Number b/Smaller Number new file mode 100644 index 00000000..441e2ce4 --- /dev/null +++ b/Smaller Number @@ -0,0 +1,26 @@ +#include + +int main() { + int a[30], i, num, smallest; + + printf("\nEnter no of elements :"); + scanf("%d", &num); + + //Read n elements in an array + for (i = 0; i < num; i++) + scanf("%d", &a[i]); + + //Consider first element as smallest + smallest = a[0]; + + for (i = 0; i < num; i++) { + if (a[i] < smallest) { + smallest = a[i]; + } + } + + // Print out the Result + printf("\nSmallest Element : %d", smallest); + + return (0); +} diff --git a/Smaller number b/Smaller number new file mode 100644 index 00000000..edb632bf --- /dev/null +++ b/Smaller number @@ -0,0 +1,29 @@ +#include +int main() +{ + int a[10], Size, i, Smallest, Position; + + printf("\nPlease Enter the size of an array \n"); + scanf("%d",&Size); + + printf("\nPlease Enter %d elements of an array: \n", Size); + for(i=0; i a[i]) + { + Smallest = a[i]; + Position = i; + } + } + + printf("\nSmallest element in an Array = %d", Smallest); + printf("\nIndex position of the Smallest element = %d", Position); + + return 0; +} diff --git a/Smallest b/Smallest new file mode 100644 index 00000000..ce10094c --- /dev/null +++ b/Smallest @@ -0,0 +1,38 @@ +#include + + +int main(void) +{ + + int num1,num2; + + printf("Enter two numbers:"); + scanf("%d %d",&num1,&num2); + + if(num1 + + + + + + + + + + diff --git a/Snake Game in C b/Snake Game in C new file mode 100644 index 00000000..7b1a1dee --- /dev/null +++ b/Snake Game in C @@ -0,0 +1,186 @@ +Snake Game in C + +#include +#include +#include +#include +#include +#include + +check(); +end(); +win(); +int m[500],n[500],con=20; +clock_t start,stop; + void main(void) +{ + +int gd=DETECT,gm,ch,maxx,maxy,x=13,y=14,p,q,spd=100; + +initgraph(&gd,&gm,"..\bgi"); + +setcolor(WHITE); +settextstyle(3,0,6); +outtextxy(200,2," SNAKE 2 "); +settextstyle(6,0,2); +outtextxy(20,80," Use Arrow Keys To Direct The Snake "); +outtextxy(20,140," Avoid The Head Of Snake Not To Hit Any Part Of Snake +"); +outtextxy(20,160," Pick The Beats Untill You Win The Game "); +outtextxy(20,200," Press 'Esc' Anytime To Exit "); +outtextxy(20,220," Press Any Key To Continue "); +ch=getch(); +if(ch==27) exit(0); +cleardevice(); +maxx=getmaxx(); +maxy=getmaxy(); + +randomize(); + +p=random(maxx); +int temp=p%13; +p=p-temp; +q=random(maxy); +temp=q%14; +q=q-temp; + + + + start=clock(); +int a=0,i=0,j,t; +while(1) +{ + + setcolor(WHITE); + setfillstyle(SOLID_FILL,con+5); + circle(p,q,5); + floodfill(p,q,WHITE); + + if( kbhit() ) + { + ch=getch(); if(ch==0) ch=getch(); + if(ch==72&& a!=2) a=1; + if(ch==80&& a!=1) a=2; + if(ch==75&& a!=4) a=3; + if(ch==77&& a!=3) a=4; + } + else + { + if(ch==27 + ) break; + } + + if(i<20){ + m[i]=x; + n[i]=y; + i++; + } + + if(i>=20) + + { + for(j=con;j>=0;j--){ + m[1+j]=m[j]; + n[1+j]=n[j]; + } + m[0]=x; + n[0]=y; + + setcolor(WHITE); + setfillstyle(SOLID_FILL,con); + circle(m[0],n[0],8); + floodfill(m[0],n[0],WHITE); + + setcolor(WHITE); + for(j=1;j=5) spd=spd-5; else spd=5; + if(con>490) win(); + p=random(maxx); temp=p%13; p=p-temp; + q=random(maxy); temp=q%14; q=q-temp; + } + if(a==1) y = y-14; if(y<0) { temp=maxy%14;y=maxy-temp;} + if(a==2) y = y+14; if(y>maxy) y=0; + if(a==3) x = x-13; if(x<0) { temp=maxx%13;x=maxx-temp;} + if(a==4) x = x+13; if(x>maxx) x=0; + if(a==0){ y = y+14 ; x=x+13; } + } + + } + + +check(){ + int a; + for(a=1;a +#include +#include +#include +#include +#include +#include + +enum whoplays{p1,p2}; +int counter1=0,counter2=0,counter11=0,counter22=0; +void graph(int,int,int); +void sohaib(void); +void ladder(int,int,int,int); +void cp1(int,int); +void cp2(int,int); +void snake(int,int,int,int); +whoplays setplayer(whoplays); +class dice +{ + private: + int dice1; + public: + dice(); + int rolldice(void); + operator int() + { + return dice1/1; + } +}; +void dice::dice() +{ + dice1=0; +} +int dice::rolldice(void) +{ + randomize(); + dice1=rand()%6+1; + return dice1; +} +class player1 +{ + private: + int a; + public: + player1(); + returna(); + reseta(int); + minusa(int); + operator int() + { + return a/1; + } + +}; +void player1::player1() +{ + a=0; +} +int player1::returna() +{ + return a; +} +int player1::reseta(int ra) +{ + a+=ra; +} +int player1::minusa(int ma) +{ + a-=ma; +} +class player2 +{ + private: + int b; + public: + player2(); + returnb(); + minusb(int); + resetb(int); + operator int() + { + return b/1; + } + +}; +void player2::player2() +{ + b=0; +} +int player2::returnb() +{ + return b; +} + +int player2::resetb(int rb) +{ + b+=rb; +} +int player2::minusb(int mb) +{ + b-=mb; +} +//starting of main +void main() +{ + sohaib(); +} +//end of main +void sohaib(void) +{ + int l=VGA,k=VGAMED; + initgraph(&l,&k,"e:\tc\bgi"); + static int x=0; + dice dice1; + player1 obj1; + player2 obj2; + int one=0,two=0,y=0; + y=setplayer(1); + g:if(x==6) + { + if(y==0) + { + y=setplayer(0); + } + else + if(y==1) + { + y=setplayer(1); + } + } + else + { + if(y==0) + { + y=setplayer(1); + } + else + if(y==1) + { + y=setplayer(0); + } + } + + x=int(dice1.rolldice()); + one=int(obj1.returna()); + two=int(obj2.returnb()); + graph(y,one,two); + if(x==6&&y==0) + { + counter11=1; + } + if(x==6&&y==1) + { + counter22=1; + } + if(y==0&&counter11==1) + { + counter1=1; + } + if(y==1&&counter22==1) + { + counter2=1; + } + if(y==0&&counter1>=1) + { + obj1.reseta(x); + if(one==10) + { + obj1.reseta(27); + cout<=1) + { + obj2.resetb(x); + if(two==24) + { + obj2.resetb(27); + cout<=100) + { + cout<<" +congratulations player 1 have won"; + goto b; + } + if(two>=100) + { + cout<<" +congratulations player 2 have won"; + goto b; + } + if((one<101&&two<101)&&(one>=0&&two>=0)) + { + goto g; + } + b:getch(); +} +whoplays setplayer(whoplays p) +{ + whoplays player=p; + return player; +} + +void graph(int y,int one,int two) +{ + int a=VGA,b=VGAMED; + initgraph(&a,&b,"e:\tc\bgi"); + cleardevice(); + int count=0,r=21; + if(y==0) + { + rectangle(200,100,400,200); + setfillstyle(1,BLUE); + floodfill(201,101,WHITE); + moveto(210,110); + setcolor(GREEN); + settextstyle(SCRIPT_FONT,HORIZ_DIR,5); + outtext("Player 2"); + getch(); + cleardevice(); + } + if(y==1) + { + rectangle(200,100,400,200); + setfillstyle(1,RED); + floodfill(201,101,WHITE); + moveto(210,110); + setcolor(YELLOW); + settextstyle(SCRIPT_FONT,HORIZ_DIR,5); + outtext("Player 2"); + getch(); + cleardevice(); + } + setcolor(4); + moveto(75,5); + settextstyle(GOTHIC_FONT,HORIZ_DIR,5); + outtext("SNAKE AND LADDER"); + rectangle(65,8,535,53); + setcolor(GREEN); + rectangle(15,58,590,298); + for(int e=73;e<590;e=e+58) + { + line(e,58,e,298); + } + for(int d=82;d<298;d=d+24) + { + line(15,d,590,d); + } + setcolor(RED); + moveto(44,286); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("1"); + moveto(102,286); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("2"); + moveto(160,286); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("3"); + moveto(218,286); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("4"); + moveto(276,286); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("5"); + moveto(334,286); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("6"); + moveto(392,286); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("7"); + moveto(450,286); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("8"); + moveto(508,286); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("9"); + moveto(566,286); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("10"); + moveto(44,262); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("11"); + moveto(102,262); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("12"); + moveto(160,262); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("13"); + moveto(218,262); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("14"); + moveto(276,262); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("15"); + moveto(334,262); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("16"); + moveto(392,262); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("17"); + moveto(450,262); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("18"); + moveto(508,262); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("19"); + moveto(566,262); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("20"); + moveto(44,238); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("21"); + moveto(102,238); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("22"); + moveto(160,238); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("23"); + moveto(218,238); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("24"); + moveto(276,238); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("25"); + moveto(334,238); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("26"); + moveto(392,238); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("27"); + moveto(450,238); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("28"); + moveto(508,238); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("29"); + moveto(566,238); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("30"); + moveto(44,214); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("31"); + moveto(102,214); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("32"); + moveto(160,214); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("33"); + moveto(218,214); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("34"); + moveto(276,214); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("35"); + moveto(334,214); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("36"); + moveto(392,214); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("37"); + moveto(450,214); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("38"); + moveto(508,214); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("39"); + moveto(566,214); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("40"); + moveto(44,190); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("41"); + moveto(102,190); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("42"); + moveto(160,190); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("43"); + moveto(218,190); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("44"); + moveto(276,190); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("45"); + moveto(334,190); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("46"); + moveto(392,190); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("47"); + moveto(450,190); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("48"); + moveto(508,190); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("49"); + moveto(566,190); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("50"); + moveto(44,166); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("51"); + moveto(102,166); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("52"); + moveto(160,166); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("53"); + moveto(218,166); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("54"); + moveto(276,166); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("55"); + moveto(334,166); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("56"); + moveto(392,166); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("57"); + moveto(450,166); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("58"); + moveto(508,166); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("59"); + moveto(566,166); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("60"); + moveto(44,142); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("61"); + moveto(102,142); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("62"); + moveto(160,142); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("63"); + moveto(218,142); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("64"); + moveto(276,142); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("65"); + moveto(334,142); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("66"); + moveto(392,142); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("67"); + moveto(450,142); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("68"); + moveto(508,142); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("69"); + moveto(566,142); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("70"); + moveto(44,118); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("71"); + moveto(102,118); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("72"); + moveto(160,118); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("73"); + moveto(218,118); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("74"); + moveto(276,118); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("75"); + moveto(334,118); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("76"); + moveto(392,118); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("77"); + moveto(450,118); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("78"); + moveto(508,118); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("79"); + moveto(566,118); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("80"); + moveto(44,94); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("81"); + moveto(102,94); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("82"); + moveto(160,94); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("83"); + moveto(218,94); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("84"); + moveto(276,94); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("85"); + moveto(334,94); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("86"); + moveto(392,94); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("87"); + moveto(450,94); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("88"); + moveto(508,94); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("89"); + moveto(566,94); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("90"); + moveto(44,70); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("91"); + moveto(102,70); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("92"); + moveto(160,70); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("93"); + moveto(218,70); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("94"); + moveto(276,70); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("95"); + moveto(334,70); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("96"); + moveto(392,70); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("97"); + moveto(450,70); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("98"); + moveto(508,70); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("99"); + moveto(558,70); + settextstyle(DEFAULT_FONT,HORIZ_DIR,1); + outtext("100"); + settextstyle(SMALL_FONT,HORIZ_DIR,5); + setcolor(GREEN); + rectangle(363,300,590,349); + setcolor(6); + moveto(375,306); + outtext("Dice -> "); + moveto(375,317); + outtext("Player 1 -> "); + moveto(375,328); + outtext("Player 2 -> "); + setcolor(GREEN); + rectangle(15,300,350,349); + setcolor(YELLOW); + moveto(19,302); + outtext("Help : "); + setcolor(7); + settextstyle(SMALL_FONT,HORIZ_DIR,4); + outtext("Just press any key, a number will appear before "); + moveto(72,312); + outtext("dice. The mark of player will automatically move"); + moveto(19,322); + outtext("to the accurate position. The player who will reach 100 "); + moveto(19,332); + outtext("will Win the game. "); + ladder(392,214,566,286); + ladder(218,142,276,166); + //ladder(508,94,508,118); + ladder(44,166,218,238); + ladder(160,190,450,166); + ladder(450,70,450,94); + snake(508,70,556,238); + snake(334,166,334,238); + snake(392,118,102,166); + snake(450,238,160,262); + snake(508,142,508,214); + cp1(515,310); + moveto(520,307); + outtext(" Player 1"); + cp2(515,330); + moveto(520,327); + outtext(" Player 2"); + + + if(one==1) + { + cp1(44,286); + } + if(one==2) + { + cp1(102,286); + } + if(one==3) + { + cp1(160,286); + } + if(one==4) + { + cp1(218,286); + } + if(one==5) + { + cp1(276,286); + } + if(one==6) + { + cp1(334,286); + } + if(one==7) + { + cp1(392,286); + } + if(one==8) + { + cp1(450,286); + } + if(one==9) + { + cp1(508,286); + } + if(one==10) + { + cp1(566,286); + } + if(one==11) + { + cp1(44,262); + } + if(one==12) + { + cp1(102,262); + } + if(one==13) + { + cp1(160,262); + } + if(one==14) + { + cp1(218,262); + } + if(one==15) + { + cp1(276,262); + } + if(one==16) + { + cp1(334,262); + } + if(one==17) + { + cp1(392,262); + } + if(one==18) + { + cp1(450,262); + } + if(one==19) + { + cp1(508,262); + } + if(one==20) + { + cp1(566,262); + } + if(one==21) + { + cp1(44,238); + } + if(one==22) + { + cp1(102,238); + } + if(one==23) + { + cp1(160,238); + } + if(one==24) + { + cp1(218,238); + } + if(one==25) + { + cp1(276,238); + } + if(one==26) + { + cp1(334,238); + } + if(one==27) + { + cp1(392,238); + } + if(one==28) + { + cp1(450,238); + } + if(one==29) + { + cp1(508,238); + } + if(one==30) + { + cp1(566,238); + } + if(one==31) + { + cp1(44,214); + } + if(one==32) + { + cp1(102,214); + } + if(one==33) + { + cp1(160,214); + } + if(one==34) + { + cp1(218,214); + } + if(one==35) + { + cp1(276,214); + } + if(one==36) + { + cp1(334,214); + } + if(one==37) + { + cp1(392,214); + } + if(one==38) + { + cp1(450,214); + } + if(one==39) + { + cp1(508,214); + } + if(one==40) + { + cp1(566,214); + } + if(one==41) + { + cp1(44,190); + } + if(one==42) + { + cp1(102,190); + } + if(one==43) + { + cp1(160,190); + } + if(one==44) + { + cp1(218,190); + } + if(one==45) + { + cp1(276,190); + } + if(one==46) + { + cp1(334,190); + } + if(one==47) + { + cp1(392,190); + } + if(one==48) + { + cp1(450,190); + } + if(one==49) + { + cp1(508,190); + } + if(one==50) + { + cp1(566,190); + } + if(one==51) + { + cp1(44,166); + } + if(one==52) + { + cp1(102,166); + } + if(one==53) + { + cp1(160,166); + } + if(one==54) + { + cp1(218,166); + } + if(one==55) + { + cp1(276,166); + } + if(one==56) + { + cp1(334,166); + } + if(one==57) + { + cp1(392,166); + } + if(one==58) + { + cp1(450,166); + } + if(one==59) + { + cp1(508,166); + } + if(one==60) + { + cp1(566,166); + } + if(one==61) + { + cp1(44,142); + } + if(one==62) + { + cp1(102,142); + } + if(one==63) + { + cp1(160,142); + } + if(one==64) + { + cp1(218,142); + } + if(one==65) + { + cp1(276,142); + } + if(one==66) + { + cp1(334,142); + } + if(one==67) + { + cp1(392,142); + } + if(one==68) + { + cp1(450,142); + } + if(one==69) + { + cp1(508,142); + } + if(one==70) + { + cp1(566,142); + } + if(one==71) + { + cp1(44,118); + } + if(one==72) + { + cp1(102,118); + } + if(one==73) + { + cp1(160,118); + } + if(one==74) + { + cp1(218,118); + } + if(one==75) + { + cp1(276,118); + } + if(one==76) + { + cp1(334,118); + } + if(one==77) + { + cp1(392,118); + } + if(one==78) + { + cp1(450,118); + } + if(one==79) + { + cp1(508,118); + } + if(one==80) + { + cp1(566,118); + } + if(one==81) + { + cp1(44,94); + } + if(one==82) + { + cp1(102,94); + } + if(one==83) + { + cp1(160,94); + } + if(one==84) + { + cp1(218,94); + } + if(one==85) + { + cp1(276,94); + } + if(one==86) + { + cp1(334,94); + } + if(one==87) + { + cp1(392,94); + } + if(one==88) + { + cp1(450,94); + } + if(one==89) + { + cp1(508,94); + } + if(one==90) + { + cp1(566,94); + } + if(one==91) + { + cp1(44,70); + } + if(one==92) + { + cp1(102,70); + } + if(one==93) + { + cp1(160,70); + } + if(one==70) + { + cp1(218,70); + } + if(one==95) + { + cp1(276,70); + } + if(one==96) + { + cp1(334,70); + } + if(one==97) + { + cp1(392,70); + } + if(one==98) + { + cp1(450,70); + } + if(one==99) + { + cp1(508,70); + } + if(one==100) + { + cp1(566,70); + } + + + + if(two==1) + { + cp2(44,286); + } + if(two==2) + { + cp2(102,286); + } + if(two==3) + { + cp2(160,286); + } + if(two==4) + { + cp2(218,286); + } + if(two==5) + { + cp2(276,286); + } + if(two==6) + { + cp2(334,286); + } + if(two==7) + { + cp2(392,286); + } + if(two==8) + { + cp2(450,286); + } + if(two==9) + { + cp2(508,286); + } + if(two==10) + { + cp2(566,286); + } + if(two==11) + { + cp2(44,262); + } + if(two==12) + { + cp2(102,262); + } + if(two==13) + { + cp2(160,262); + } + if(two==14) + { + cp2(218,262); + } + if(two==15) + { + cp2(276,262); + } + if(two==16) + { + cp2(334,262); + } + if(two==17) + { + cp2(392,262); + } + if(two==18) + { + cp2(450,262); + } + if(two==19) + { + cp2(508,262); + } + if(two==20) + { + cp2(566,262); + } + if(two==21) + { + cp2(44,238); + } + if(two==22) + { + cp2(102,238); + } + if(two==23) + { + cp2(160,238); + } + if(two==24) + { + cp2(218,238); + } + if(two==25) + { + cp2(276,238); + } + if(two==26) + { + cp2(334,238); + } + if(two==27) + { + cp2(392,238); + } + if(two==28) + { + cp2(450,238); + } + if(two==29) + { + cp2(508,238); + } + if(two==30) + { + cp2(566,238); + } + if(two==31) + { + cp2(44,214); + } + if(two==32) + { + cp2(102,214); + } + if(two==33) + { + cp2(160,214); + } + if(two==34) + { + cp2(218,214); + } + if(two==35) + { + cp2(276,214); + } + if(two==36) + { + cp2(334,214); + } + if(two==37) + { + cp2(392,214); + } + if(two==38) + { + cp2(450,214); + } + if(two==39) + { + cp2(508,214); + } + if(two==40) + { + cp2(566,214); + } + if(two==41) + { + cp2(44,190); + } + if(two==42) + { + cp2(102,190); + } + if(two==43) + { + cp2(160,190); + } + if(two==44) + { + cp2(218,190); + } + if(two==45) + { + cp2(276,190); + } + if(two==46) + { + cp2(334,190); + } + if(two==47) + { + cp2(392,190); + } + if(two==48) + { + cp2(450,190); + } + if(two==49) + { + cp2(508,190); + } + if(two==50) + { + cp2(566,190); + } + if(two==51) + { + cp2(44,166); + } + if(two==52) + { + cp2(102,166); + } + if(two==53) + { + cp2(160,166); + } + if(two==54) + { + cp2(218,166); + } + if(two==55) + { + cp2(276,166); + } + if(two==56) + { + cp2(334,166); + } + if(two==57) + { + cp2(392,166); + } + if(two==58) + { + cp2(450,166); + } + if(two==59) + { + cp2(508,166); + } + if(two==60) + { + cp2(566,166); + } + if(two==61) + { + cp2(44,142); + } + if(two==62) + { + cp2(102,142); + } + if(two==63) + { + cp2(160,142); + } + if(two==64) + { + cp2(218,142); + } + if(two==65) + { + cp2(276,142); + } + if(two==66) + { + cp2(334,142); + } + if(two==67) + { + cp2(392,142); + } + if(two==68) + { + cp2(450,142); + } + if(two==69) + { + cp2(508,142); + } + if(two==70) + { + cp2(566,142); + } + if(two==71) + { + cp2(44,118); + } + if(two==72) + { + cp2(102,118); + } + if(two==73) + { + cp2(160,118); + } + if(two==74) + { + cp2(218,118); + } + if(two==75) + { + cp2(276,118); + } + if(two==76) + { + cp2(334,118); + } + if(two==77) + { + cp2(392,118); + } + if(two==78) + { + cp2(450,118); + } + if(two==79) + { + cp2(508,118); + } + if(two==80) + { + cp2(566,118); + } + if(two==81) + { + cp2(44,94); + } + if(two==82) + { + cp2(102,94); + } + if(two==83) + { + cp2(160,94); + } + if(two==84) + { + cp2(218,94); + } + if(two==85) + { + cp2(276,94); + } + if(two==86) + { + cp2(334,94); + } + if(two==87) + { + cp2(392,94); + } + if(two==88) + { + cp2(450,94); + } + if(two==89) + { + cp2(508,94); + } + if(two==90) + { + cp2(566,94); + } + if(two==91) + { + cp2(44,70); + } + if(two==92) + { + cp2(102,70); + } + if(two==93) + { + cp2(160,70); + } + if(two==70) + { + cp2(218,70); + } + if(two==95) + { + cp2(276,70); + } + if(two==96) + { + cp2(334,70); + } + if(two==97) + { + cp2(392,70); + } + if(two==98) + { + cp2(450,70); + } + if(two==99) + { + cp2(508,70); + } + if(two==100) + { + cp2(566,70); + } + + getch(); + closegraph(); +} +void ladder(int s1,int s2,int s3,int s4) +{ + setcolor(WHITE); + setlinestyle(1,10,3); + line(s1,s2,s3,s4); + line(s1,s2,s1,s2+10); + line(s1,s2,s1+17,s2); + //circle(s1,s2,3); + +} +void cp1(int xx,int yy) +{ + setcolor(7); + circle(xx,yy,10); + setfillstyle(1,9); + floodfill(xx,yy,7); +} +void cp2(int xx,int yy) +{ + setcolor(7); + circle(xx,yy,10); + setfillstyle(1,4); + floodfill(xx,yy,7); +} + +void snake(int s1,int s2,int s3,int s4) +{ + setcolor(YELLOW); + setlinestyle(1,10,3); + line(s1,s2,s3,s4); + circle(s1,s2,4); +} diff --git a/Sort in Descending order b/Sort in Descending order new file mode 100644 index 00000000..86796efb --- /dev/null +++ b/Sort in Descending order @@ -0,0 +1,34 @@ + #include + void main () + { + int number[30]; + + int i, j, a, n; + printf("Enter the value of N\n"); + scanf("%d", &n); + + printf("Enter the numbers \n"); + for (i = 0; i < n; ++i) + scanf("%d", &number[i]); + + for (i = 0; i < n; ++i) + { + for (j = i + 1; j < n; ++j) + { + if (number[i] < number[j]) + { + a = number[i]; + number[i] = number[j]; + number[j] = a; + } + } + } + + printf("The numbers arranged in descending order are given below\n"); + + for (i = 0; i < n; ++i) + { + printf("%d\n", number[i]); + } + + } diff --git a/Sorting Arrays b/Sorting Arrays new file mode 100644 index 00000000..a24526d2 --- /dev/null +++ b/Sorting Arrays @@ -0,0 +1,40 @@ + /* + * C program to accept N numbers and arrange them in an ascending order + */ + + #include + void main() + { + + int i, j, a, n, number[30]; + printf("Enter the value of N \n"); + scanf("%d", &n); + + printf("Enter the numbers \n"); + for (i = 0; i < n; ++i) + scanf("%d", &number[i]); + + for (i = 0; i < n; ++i) + { + + for (j = i + 1; j < n; ++j) + { + + if (number[i] > number[j]) + { + + a = number[i]; + number[i] = number[j]; + number[j] = a; + + } + + } + + } + + printf("The numbers arranged in ascending order are given below \n"); + for (i = 0; i < n; ++i) + printf("%d\n", number[i]); + + } diff --git a/SquareFill with stars b/SquareFill with stars new file mode 100644 index 00000000..218b6d1e --- /dev/null +++ b/SquareFill with stars @@ -0,0 +1,26 @@ +#include +#include +void main() + +{ +int i,j; + +clrscr(); for(i=1;i<=5;i++) + +{ + +for(j=1;j<=5;j++) + +{ + +printf("*"); + +} + +printf("\n"); + +} + +getch(); + +} diff --git a/Squareroot b/Squareroot new file mode 100644 index 00000000..032b5041 --- /dev/null +++ b/Squareroot @@ -0,0 +1,13 @@ +#include +#include +#include +void main() +{ + float x,y; + clrscr(); + printf("Enter the number \n"); + scanf("%f",&x); + y-sqrt(x); + printf("Square root of %f \n",x,y); + getch(); +} diff --git a/Star Pattern b/Star Pattern new file mode 100644 index 00000000..3aa2cba5 --- /dev/null +++ b/Star Pattern @@ -0,0 +1,30 @@ +#include +int main(void) { + int n; + printf("Enter the number of rows\n"); + scanf("%d",&n); + int spaces=n-1; + int stars=1; + for(int i=1;i<=n;i++) + { + for(int j=1;j<=spaces;j++) + { + printf(" "); + } + for(int k=1;k<=stars;k++) + { + printf("*"); + } + if(spaces>i) + { + spaces=spaces-1; + stars=stars+2; + } + if(spaces +#include +#include + +void main(){ + + int n,i,j; + printf("Enter the value of n: "); + scanf("%d",&n); + + for( i = 0; i < n ; i++){ // upper part + + for ( j = 1; j <= 6*n-1 ; j++) + { + if( ( j >= n - i && j <= 2*n + i ) || ( j >= 4*n - i && j <= 5*n +i ) ){ + printf("*"); + }else { + printf(" "); + } + + } + + printf("\n"); + } + + for( i = 0; i < 3*n ; i++){ // lower part + + for ( j = 1; j <= 6*n-1 ; j++) + { + if(j >= 1 + i && j <= 6*n - i){ + printf("*"); + }else { + printf(" "); + } + + } + + printf("\n"); + } +} diff --git a/Stock_Span b/Stock_Span new file mode 100644 index 00000000..f4d07e9c --- /dev/null +++ b/Stock_Span @@ -0,0 +1,29 @@ +#include +int* stockSpan(int *price, int size) { + // Write your code here + stack st; + st.push(0); + int *S=new int[size]; + // Span value of first element + S[0] = 1; + // Span values for rest of the elements + for (int i = 1; i < size; i++){ + while (!st.empty() && price[st.top()] < price[i]) + st.pop(); + + if(st.empty()){ + S[i] = i + 1; + } + else{ + S[i] = i - st.top(); + } + + // Push this element to stack + st.push(i); + } + return S; + +} + + + diff --git a/String reverse b/String reverse new file mode 100644 index 00000000..8902cb47 --- /dev/null +++ b/String reverse @@ -0,0 +1,20 @@ +// Created by rjkalash + +#include +#include +int main() +{ + char mess[80], stress[80]; + int i,j,len; + printf("Enter any string:"); + scanf("%s",&mess); + j=0; + len=strlen(mess); + for (i=len-1;i>=0;i--) + { + stress[j++]=mess[i]; + } + stress[j]='\0'; + printf ("the reversed string is =%s",stress); + return 0; + } diff --git a/Sum b/Sum new file mode 100644 index 00000000..18f9f81d --- /dev/null +++ b/Sum @@ -0,0 +1,14 @@ +#include +int main() { + + int number1, number2, sum; + + printf("Enter two integers: "); + scanf("%d %d", &number1, &number2); + + // calculating sum + sum = number1 + number2; + + printf("%d + %d = %d", number1, number2, sum); + return 0; +} diff --git a/Sum of Two numbers b/Sum of Two numbers new file mode 100644 index 00000000..4a394004 --- /dev/null +++ b/Sum of Two numbers @@ -0,0 +1,14 @@ +#include + +int main() { + int a, b, sum; + + printf("\nEnter two no: "); + scanf("%d %d", &a, &b); + + sum = a + b; + + printf("Sum : %d", sum); + + return(0); +} diff --git a/SumOfTwoNumber b/SumOfTwoNumber new file mode 100644 index 00000000..666bd27d --- /dev/null +++ b/SumOfTwoNumber @@ -0,0 +1,10 @@ +#include +int main() +{ + int a, b, sum; + printf("\nEnter two no: "); + scanf("%d %d", &a, &b); + sum = a + b; + printf("Sum : %d", sum); + return(0); +} diff --git a/Swapnumbers b/Swapnumbers new file mode 100644 index 00000000..98a679ae --- /dev/null +++ b/Swapnumbers @@ -0,0 +1,21 @@ +#include + + int main() + +{ + +int a=10, b=20; + +printf("Before swap a=%d b=%d",a,b); + +a=a+b;//a=30 (10+20) + +b=a-b;//b=10 (30-20) + +a=a-b;//a=20 (30-10) + +printf("\nAfter swap a=%d b=%d",a,b); + +return 0; + +} diff --git a/Swapping of two number b/Swapping of two number new file mode 100644 index 00000000..5c7f9e45 --- /dev/null +++ b/Swapping of two number @@ -0,0 +1,21 @@ +#include +int main() { + double first, second, temp; + printf("Enter first number: "); + scanf("%lf", &first); + printf("Enter second number: "); + scanf("%lf", &second); + + // Value of first is assigned to temp + temp = first; + + // Value of second is assigned to first + first = second; + + // Value of temp (initial value of first) is assigned to second + second = temp; + + printf("\nAfter swapping, firstNumber = %.2lf\n", first); + printf("After swapping, secondNumber = %.2lf", second); + return 0; +} diff --git a/Swapping two Numbers b/Swapping two Numbers new file mode 100644 index 00000000..0977acb5 --- /dev/null +++ b/Swapping two Numbers @@ -0,0 +1,14 @@ +Swapping two Numbers using a Temporary Variable + +#include +#include + +void main() +{ + int x = 10, y = 15, temp; + temp = x; + x = y; + y = temp; + printf("x = %d and y = %d", x, y); + getch(); +} diff --git a/Table b/Table new file mode 100644 index 00000000..e48de691 --- /dev/null +++ b/Table @@ -0,0 +1,17 @@ +#include +#include +main() +{ + int n,end,i,mul; + printf("enter the number for which you want table:\n"); + scanf("%d",&n); + printf("enter the number till where \nyou want multiplication with %d :\n",n); + scanf("%d",&end); + printf("table of %d\n",n); + for(i=1;i<=end;i++) + { + mul=n*i; + printf("%d X %d = %d\n",n,i,mul); + } + getch(); +} diff --git a/Tac Toe game b/Tac Toe game new file mode 100644 index 00000000..521f347d --- /dev/null +++ b/Tac Toe game @@ -0,0 +1,284 @@ +#include +#include +#include +#include + +int board[10] = {2,2,2,2,2,2,2,2,2,2}; +int turn = 1,flag = 0; +int player,comp; + +void menu(); +void go(int n); +void start_game(); +void check_draw(); +void draw_board(); +void player_first(); +void put_X_O(char ch,int pos); +COORD coord= {0,0}; // this is global variable +//center of axis is set to the top left cornor of the screen +void gotoxy(int x,int y) +{ + coord.X=x; + coord.Y=y; + SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); +} + +void main() +{ + system("cls"); + menu(); + getch(); + +} + +void menu() +{ + int choice; + system("cls"); + printf("\n--------MENU--------"); + printf("\n1 : Play with X"); + printf("\n2 : Play with O"); + printf("\n3 : Exit"); + printf("\nEnter your choice:>"); + scanf("%d",&choice); + turn = 1; + switch (choice) + { + case 1: + player = 1; + comp = 0; + player_first(); + break; + case 2: + player = 0; + comp = 1; + start_game(); + break; + case 3: + exit(1); + default: + menu(); + } +} + +int make2() +{ + if(board[5] == 2) + return 5; + if(board[2] == 2) + return 2; + if(board[4] == 2) + return 4; + if(board[6] == 2) + return 6; + if(board[8] == 2) + return 8; + return 0; +} + +int make4() +{ + if(board[1] == 2) + return 1; + if(board[3] == 2) + return 3; + if(board[7] == 2) + return 7; + if(board[9] == 2) + return 9; + return 0; +} + +int posswin(int p) +{ +// p==1 then X p==0 then O + int i; + int check_val,pos; + + if(p == 1) + check_val = 18; + else + check_val = 50; + + i = 1; + while(i<=9)//row check + { + if(board[i] * board[i+1] * board[i+2] == check_val) + { + if(board[i] == 2) + return i; + if(board[i+1] == 2) + return i+1; + if(board[i+2] == 2) + return i+2; + } + i+=3; + } + + i = 1; + while(i<=3)//column check + { + if(board[i] * board[i+3] * board[i+6] == check_val) + { + if(board[i] == 2) + return i; + if(board[i+3] == 2) + return i+3; + if(board[i+6] == 2) + return i+6; + } + i++; + } + + if(board[1] * board[5] * board[9] == check_val) + { + if(board[1] == 2) + return 1; + if(board[5] == 2) + return 5; + if(board[9] == 2) + return 9; + } + + if(board[3] * board[5] * board[7] == check_val) + { + if(board[3] == 2) + return 3; + if(board[5] == 2) + return 5; + if(board[7] == 2) + return 7; + } + return 0; +} + +void go(int n) +{ + if(turn % 2) + board[n] = 3; + else + board[n] = 5; + turn++; +} + +void player_first() +{ + int pos; + + check_draw(); + draw_board(); + gotoxy(30,18); + printf("Your Turn :> "); + scanf("%d",&pos); + + if(board[pos] != 2) + player_first(); + + if(pos == posswin(player)) + { + go(pos); + draw_board(); + gotoxy(30,20); + //textcolor(128+RED); + printf("Player Wins"); + getch(); + exit(0); + } + + go(pos); + draw_board(); + start_game(); +} + +void start_game() +{ +// p==1 then X p==0 then O + if(posswin(comp)) + { + go(posswin(comp)); + flag = 1; + } + else if(posswin(player)) + go(posswin(player)); + else if(make2()) + go(make2()); + else + go(make4()); + draw_board(); + + if(flag) + { + gotoxy(30,20); + //textcolor(128+RED); + printf("Computer wins"); + getch(); + } + else + player_first(); +} + +void check_draw() +{ + if(turn > 9) + { + gotoxy(30,20); + //textcolor(128+RED); + printf("Game Draw"); + getch(); + exit(0); + } +} + +void draw_board() +{ + int j; + + for(j=9; j<17; j++) + { + gotoxy(35,j); + printf("| |"); + } + gotoxy(28,11); + printf("-----------------------"); + gotoxy(28,14); + printf("-----------------------"); + + for(j=1; j<10; j++) + { + if(board[j] == 3) + put_X_O('X',j); + else if(board[j] == 5) + put_X_O('O',j); + } +} + +void put_X_O(char ch,int pos) +{ + int m; + int x = 31, y = 10; + + m = pos; + + if(m > 3) + { + while(m > 3) + { + y += 3; + m -= 3; + } + } + if(pos % 3 == 0) + x += 16; + else + { + pos %= 3; + pos--; + while(pos) + { + x+=8; + pos--; + } + } + gotoxy(x,y); + printf("%c",ch); +} diff --git a/Tetris b/Tetris new file mode 100644 index 00000000..2a78fd42 --- /dev/null +++ b/Tetris @@ -0,0 +1,318 @@ + + + + + + + + + + + diff --git a/Tic tac toe b/Tic tac toe new file mode 100644 index 00000000..a2bbd5ab --- /dev/null +++ b/Tic tac toe @@ -0,0 +1,284 @@ +#include +#include +#include +#include + +int b[10] = {2,2,2,2,2,2,2,2,2,2}; +int t = 1,f = 0; +int p,c; + +void menu(); +void go(int n); +void start_game(); +void check_draw(); +void draw_board(); +void player_first(); +void put_X_O(char ch,int pos); +COORD coord= {0,0}; // this is global variable +//center of axis is set to the top left cornor of the screen +void gotoxy(int x,int y) +{ +coord.X=x; +coord.Y=y; +SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord); +} + +void main() +{ +system("cls"); +menu(); +getch(); + +} + +void menu() +{ +int choice; +system("cls"); +printf("\n--------MENU--------"); +printf("\n1 : Play with X"); +printf("\n2 : Play with O"); +printf("\n3 : Exit"); +printf("\nEnter your choice:>"); +scanf("%d",&choice); +t = 1; +switch (choice) +{ +case 1: +p = 1; +c = 0; +player_first(); +break; +case 2: +p = 0; +c = 1; +start_game(); +break; +case 3: +exit(1); +default: +menu(); +} +} + +int make2() +{ +if(b[5] == 2) +return 5; +if(b[2] == 2) +return 2; +if(b[4] == 2) +return 4; +if(b[6] == 2) +return 6; +if(b[8] == 2) +return 8; +return 0; +} + +int make4() +{ +if(b[1] == 2) +return 1; +if(b[3] == 2) +return 3; +if(b[7] == 2) +return 7; +if(b[9] == 2) +return 9; +return 0; +} + +int posswin(int p) +{ +// p==1 then X p==0 then O +int i; +int check_val,pos; + +if(p == 1) + check_val = 18; +else + check_val = 50; + +i = 1; +while(i<=9)//row check +{ + if(b[i] * b[i+1] * b[i+2] == check_val) + { + if(b[i] == 2) + return i; + if(b[i+1] == 2) + return i+1; + if(b[i+2] == 2) + return i+2; + } + i+=3; +} + +i = 1; +while(i<=3)//column check +{ + if(b[i] * b[i+3] * b[i+6] == check_val) + { + if(b[i] == 2) + return i; + if(b[i+3] == 2) + return i+3; + if(b[i+6] == 2) + return i+6; + } + i++; +} + +if(b[1] * b[5] * b[9] == check_val) +{ + if(b[1] == 2) + return 1; + if(b[5] == 2) + return 5; + if(b[9] == 2) + return 9; +} + +if(b[3] * b[5] * b[7] == check_val) +{ + if(b[3] == 2) + return 3; + if(b[5] == 2) + return 5; + if(b[7] == 2) + return 7; +} +return 0; +} + +void go(int n) +{ +if(t % 2) +b[n] = 3; +else +b[n] = 5; +turn++; +} + +void player_first() +{ +int pos; + +check_draw(); +draw_board(); +gotoxy(30,18); +printf("Your Turn :> "); +scanf("%d",&pos); + +if(b[pos] != 2) + player_first(); + +if(pos == posswin(player)) +{ + go(pos); + draw_board(); + gotoxy(30,20); + //textcolor(128+RED); + printf("Player Wins"); + getch(); + exit(0); +} + +go(pos); +draw_board(); +start_game(); +} + +void start_game() +{ +// p==1 then X p==0 then O +if(posswin(c)) +{ +go(posswin(c)); +f = 1; +} +else if(posswin(p)) +go(posswin(p)); +else if(make2()) +go(make2()); +else +go(make4()); +draw_board(); + +if(f) +{ + gotoxy(30,20); + //textcolor(128+RED); + printf("Computer wins"); + getch(); +} +else + player_first(); +} + +void check_draw() +{ +if(turn > 9) +{ +gotoxy(30,20); +//textcolor(128+RED); +printf("Game Draw"); +getch(); +exit(0); +} +} + +void draw_board() +{ +int j; + +for(j=9; j<17; j++) +{ + gotoxy(35,j); + printf("| |"); +} +gotoxy(28,11); +printf("-----------------------"); +gotoxy(28,14); +printf("-----------------------"); + +for(j=1; j<10; j++) +{ + if(b[j] == 3) + put_X_O('X',j); + else if(b[j] == 5) + put_X_O('O',j); +} +} + +void put_X_O(char ch,int pos) +{ +int m; +int x = 31, y = 10; + +m = pos; + +if(m > 3) +{ + while(m > 3) + { + y += 3; + m -= 3; + } +} +if(pos % 3 == 0) + x += 16; +else +{ + pos %= 3; + pos--; + while(pos) + { + x+=8; + pos--; + } +} +gotoxy(x,y); +printf("%c",ch); +} diff --git a/To Print a Rombus star pattern b/To Print a Rombus star pattern new file mode 100644 index 00000000..b84519e7 --- /dev/null +++ b/To Print a Rombus star pattern @@ -0,0 +1,21 @@ +#include + +int main() +{ + int n; + printf("Enter the number of rows"); + scanf("%d",&n); + for(int i=n;i>=1;i--) + { + for(int j=1;j<=i-1;j++) + { + printf(" "); + } + for(int k=1;k<=n;k++) + { + printf("*"); + } + printf("\n"); + } + return 0; +} diff --git a/Tower of Hanoi b/Tower of Hanoi new file mode 100644 index 00000000..cf5eae98 --- /dev/null +++ b/Tower of Hanoi @@ -0,0 +1,36 @@ +#include + + +// C recursive function to solve tower of hanoi puzzle + +void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) +{ + + if (n == 1) + + { + + printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod); + + return; + + } + + towerOfHanoi(n-1, from_rod, aux_rod, to_rod); + + printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod); + + towerOfHanoi(n-1, aux_rod, to_rod, from_rod); +} + + + +int main() +{ + + int n = 4; // Number of disks + + towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods + + return 0; +} diff --git a/TowerOfHanoi b/TowerOfHanoi new file mode 100644 index 00000000..dbc4f920 --- /dev/null +++ b/TowerOfHanoi @@ -0,0 +1,34 @@ +#include + + +void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) +{ + + if (n == 1) + + { + + printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod); + + return; + + } + + towerOfHanoi(n-1, from_rod, aux_rod, to_rod); + + printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod); + + towerOfHanoi(n-1, aux_rod, to_rod, from_rod); +} + + + +int main() +{ + + int n = 4; // Number of disks + + towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods + + return 0; +} diff --git a/Triplet of three numbers (a,b,c) b/Triplet of three numbers (a,b,c) new file mode 100644 index 00000000..11bf40bb --- /dev/null +++ b/Triplet of three numbers (a,b,c) @@ -0,0 +1,35 @@ +#include + + + +void main() + +{ + + float arr[4]; + + + + for(int i=0; i<3; i++) + + scanf("%f",&arr[i]); + + + + //To check wether the triplet is strictly increasing or decreasing or none. + + + + if(arr[0]>arr[1]&&arr[1]>arr[2]) + + printf("1"); + + else if(arr[2]>arr[1]&&arr[1]>arr[0]) + + printf("1"); + + else + + printf("0"); + +} diff --git a/Window Sliding Technique b/Window Sliding Technique new file mode 100644 index 00000000..53dd6dfa --- /dev/null +++ b/Window Sliding Technique @@ -0,0 +1,22 @@ +#include +using namespace std; +int maxSum(int arr[], int n, int k) +{ + int max_sum = INT_MIN; + for (int i = 0; i < n - k + 1; i++) { + int current_sum = 0; + for (int j = 0; j < k; j++) + current_sum = current_sum + arr[i + j]; + max_sum = max(current_sum, max_sum); + } + + return max_sum; +} +int main() +{ + int arr[] = { 1, 4, 2, 10, 2, 3, 1, 0, 20 }; + int k = 4; + int n = sizeof(arr) / sizeof(arr[0]); + cout << maxSum(arr, n, k); + return 0; +} diff --git a/Winshree b/Winshree new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/Winshree @@ -0,0 +1 @@ + diff --git a/add multiply divide and substract b/add multiply divide and substract new file mode 100644 index 00000000..7189cf76 --- /dev/null +++ b/add multiply divide and substract @@ -0,0 +1,21 @@ +#include +int main() +{ + int first, second, add, subtract, multiply; + float divide; + + printf("Enter two integers\n"); + scanf("%d%d", &first, &second); + + add = first + second; + subtract = first - second; + multiply = first * second; + divide = first / (float)second; //typecasting + + printf("Sum = %d\n", add); + printf("Difference = %d\n", subtract); + printf("Multiplication = %d\n", multiply); + printf("Division = %.2f\n", divide); + + return 0; +} diff --git a/add two numbers in c b/add two numbers in c new file mode 100644 index 00000000..18f9f81d --- /dev/null +++ b/add two numbers in c @@ -0,0 +1,14 @@ +#include +int main() { + + int number1, number2, sum; + + printf("Enter two integers: "); + scanf("%d %d", &number1, &number2); + + // calculating sum + sum = number1 + number2; + + printf("%d + %d = %d", number1, number2, sum); + return 0; +} diff --git a/addition b/addition new file mode 100644 index 00000000..18f9f81d --- /dev/null +++ b/addition @@ -0,0 +1,14 @@ +#include +int main() { + + int number1, number2, sum; + + printf("Enter two integers: "); + scanf("%d %d", &number1, &number2); + + // calculating sum + sum = number1 + number2; + + printf("%d + %d = %d", number1, number2, sum); + return 0; +} diff --git a/addition and subtraction b/addition and subtraction new file mode 100644 index 00000000..3b8b2d37 --- /dev/null +++ b/addition and subtraction @@ -0,0 +1,14 @@ +#include +int main() { +int a,b; + + printf("\tEnter two integers :"); + + scanf("%d%d", &a,&b); + + printf("\nSum of these two numbers are %d", a+b); + + printf("\nDiff of these two no. are %d", a-b); + +} + return0; diff --git a/addition no b/addition no new file mode 100644 index 00000000..6fe498d2 --- /dev/null +++ b/addition no @@ -0,0 +1,3 @@ +int main() { int x, y, z; +printf("Enter two numbers to add\n"); scanf("%d%d", &x, &y); +printf("Sum of the numbers = %d\n", z); diff --git a/additionoftwonumber b/additionoftwonumber new file mode 100644 index 00000000..18f9f81d --- /dev/null +++ b/additionoftwonumber @@ -0,0 +1,14 @@ +#include +int main() { + + int number1, number2, sum; + + printf("Enter two integers: "); + scanf("%d %d", &number1, &number2); + + // calculating sum + sum = number1 + number2; + + printf("%d + %d = %d", number1, number2, sum); + return 0; +} diff --git a/addnos b/addnos new file mode 100644 index 00000000..a1f72afe --- /dev/null +++ b/addnos @@ -0,0 +1,12 @@ + Program to Add two numbers in C + +#include +int main() +{ +int number1, number2, sum; +printf("Enter two integers: "); +scanf("%d %d", &number1, &number2); +sum = number1 + number2; +printf("%d + %d = %d", number1, number2, sum); +return 0; +} diff --git a/addoftwono b/addoftwono new file mode 100644 index 00000000..6135c5ae --- /dev/null +++ b/addoftwono @@ -0,0 +1,14 @@ +#include + +int main() { + int a, b, sum; + + printf("\nEnter two no: "); + scanf("%d %d", &a, &b); + + sum = a + b; + + printf("Sum : %d", sum); + + return(0); +} diff --git a/addtwodistance.c b/addtwodistance.c new file mode 100644 index 00000000..401af0d4 --- /dev/null +++ b/addtwodistance.c @@ -0,0 +1,38 @@ +// Program to add two distances (feet-inch) +#include +struct Distance +{ + int feet; + float inch; +} dist1, dist2, sum; + +int main() +{ + printf("1st distance\n"); + printf("Enter feet: "); + scanf("%d", &dist1.feet); + + printf("Enter inch: "); + scanf("%f", &dist1.inch); + printf("2nd distance\n"); + + printf("Enter feet: "); + scanf("%d", &dist2.feet); + + printf("Enter inch: "); + scanf("%f", &dist2.inch); + + // adding feet + sum.feet = dist1.feet + dist2.feet; + // adding inches + sum.inch = dist1.inch + dist2.inch; + + // changing to feet if inch is greater than 12 + while (sum.inch >= 12) + { + ++sum.feet; + sum.inch = sum.inch - 12; + } + + printf("Sum of distances = %d\'-%.1f\"", sum.feet, sum.inch); + return 0; diff --git a/advanced calculator CLI b/advanced calculator CLI new file mode 100644 index 00000000..79d546ac --- /dev/null +++ b/advanced calculator CLI @@ -0,0 +1,200 @@ +// Calculator example using C code +#include +#include +#include +#include + +#define KEY "Enter the calculator Operation you want to do:" + +// Function prototype declaration +void addition(); +void subtraction(); +void multiplication(); +void division(); +void modulus(); +void power(); +int factorial(); +void calculator_operations(); + +// Start of Main Program +int main() +{ + int X=1; + char Calc_oprn; + + // Function call + calculator_operations(); + + while(X) + { + printf("\n"); + printf("%s : ", KEY); + + Calc_oprn=getche(); + + switch(Calc_oprn) + { + case '+': addition(); + break; + + case '-': subtraction(); + break; + + case '*': multiplication(); + break; + + case '/': division(); + break; + + case '?': modulus(); + break; + + case '!': factorial(); + break; + + case '^': power(); + break; + + case 'H': + case 'h': calculator_operations(); + break; + + case 'Q': + case 'q': exit(0); + break; + case 'c': + case 'C': system("cls"); + calculator_operations(); + break; + + default : system("cls"); + + printf("\n**********You have entered unavailable option"); + printf("***********\n"); + printf("\n*****Please Enter any one of below available "); + printf("options****\n"); + calculator_operations(); + } + } +} + +//Function Definitions + +void calculator_operations() +{ + //system("cls"); use system function to clear + //screen instead of clrscr(); + printf("\n Welcome to C calculator \n\n"); + + printf("******* Press 'Q' or 'q' to quit "); + printf("the program ********\n"); + printf("***** Press 'H' or 'h' to display "); + printf("below options *****\n\n"); + printf("Enter 'C' or 'c' to clear the screen and"); + printf(" display available option \n\n"); + + printf("Enter + symbol for Addition \n"); + printf("Enter - symbol for Subtraction \n"); + printf("Enter * symbol for Multiplication \n"); + printf("Enter / symbol for Division \n"); + printf("Enter ? symbol for Modulus\n"); + printf("Enter ^ symbol for Power \n"); + printf("Enter ! symbol for Factorial \n\n"); +} + +void addition() +{ + int n, total=0, k=0, number; + printf("\nEnter the number of elements you want to add:"); + scanf("%d",&n); + printf("Please enter %d numbers one by one: \n",n); + while(k +#include +#include + +int checkcoprime(int num) +{ + int a,b,temp; + a=num; + b=26; + + while(b!=0) + { + temp=b; + b=a%b; + a=temp; + + } + + return a; + +} +main() +{ + int a,b,check,numstr[1000]; + char str[1000]; + printf("Enter a ( 1 to 25) both included which is co-prime with 26\n"); + scanf("%d",&a); + if(a>=1 && a<=25) + { + check=checkcoprime(a); + if(check!=1) + { + printf("Enter the one which is co-prime with 26\n"); + exit(0); + } + + } + else + { + printf("Enter between 1 and 25\n "); + exit(1); + } + + printf("Enter b ( 0 to 25) both included \n"); + scanf("%d",&b); + if(b<1 || a>25) + { + printf("Enter b between 0 to 25 \n"); + exit(3); + + } + + printf("Enter the string to be encypted: \n"); + fflush(stdin); + gets(str); + + // converting string into capital letters + + int i,j; + + for(i=0;ileft ) + int rightH = FindHeight(root->right ) + return max( leftH, rightH )+1 diff --git a/anke game in c language b/anke game in c language new file mode 100644 index 00000000..95c19c83 --- /dev/null +++ b/anke game in c language @@ -0,0 +1,179 @@ +Snake Game in c language + +check(); +end(); +win(); +int m[500],n[500],con=20; +clock_t start,stop; + void main(void) +{ + +int gd=DETECT,gm,ch,maxx,maxy,x=13,y=14,p,q,spd=100; + +initgraph(&gd,&gm,"..\bgi"); + +setcolor(WHITE); +settextstyle(3,0,6); +outtextxy(200,2," SNAKE 2 "); +settextstyle(6,0,2); +outtextxy(20,80," Use Arrow Keys To Direct The Snake "); +outtextxy(20,140," Avoid The Head Of Snake Not To Hit Any Part Of Snake +"); +outtextxy(20,160," Pick The Beats Untill You Win The Game "); +outtextxy(20,200," Press 'Esc' Anytime To Exit "); +outtextxy(20,220," Press Any Key To Continue "); +ch=getch(); +if(ch==27) exit(0); +cleardevice(); +maxx=getmaxx(); +maxy=getmaxy(); + +randomize(); + +p=random(maxx); +int temp=p%13; +p=p-temp; +q=random(maxy); +temp=q%14; +q=q-temp; + + + + start=clock(); +int a=0,i=0,j,t; +while(1) +{ + + setcolor(WHITE); + setfillstyle(SOLID_FILL,con+5); + circle(p,q,5); + floodfill(p,q,WHITE); + + if( kbhit() ) + { + ch=getch(); if(ch==0) ch=getch(); + if(ch==72&& a!=2) a=1; + if(ch==80&& a!=1) a=2; + if(ch==75&& a!=4) a=3; + if(ch==77&& a!=3) a=4; + } + else + { + if(ch==27 + ) break; + } + + if(i<20){ + m[i]=x; + n[i]=y; + i++; + } + + if(i>=20) + + { + for(j=con;j>=0;j--){ + m[1+j]=m[j]; + n[1+j]=n[j]; + } + m[0]=x; + n[0]=y; + + setcolor(WHITE); + setfillstyle(SOLID_FILL,con); + circle(m[0],n[0],8); + floodfill(m[0],n[0],WHITE); + + setcolor(WHITE); + for(j=1;j=5) spd=spd-5; else spd=5; + if(con>490) win(); + p=random(maxx); temp=p%13; p=p-temp; + q=random(maxy); temp=q%14; q=q-temp; + } + if(a==1) y = y-14; if(y<0) { temp=maxy%14;y=maxy-temp;} + if(a==2) y = y+14; if(y>maxy) y=0; + if(a==3) x = x-13; if(x<0) { temp=maxx%13;x=maxx-temp;} + if(a==4) x = x+13; if(x>maxx) x=0; + if(a==0){ y = y+14 ; x=x+13; } + } + + } + + +check(){ + int a; + for(a=1;a +void main() +{ + int num,armstrong=0,reminder,n; + printf("enter the number"); + scanf("%d",&num); + n=num; + while(n>0) + { + reminder=n%10; + armstrong=armstrong +(reminder*reminder*reminder); + n=n/10; + } + if(num==armstrong) + printf("%d is armstrong number",num); + else + printf("%d is not armstrong number",num); +} diff --git a/armstrong number b/armstrong number new file mode 100644 index 00000000..23d17e44 --- /dev/null +++ b/armstrong number @@ -0,0 +1,41 @@ +#include +#include +int main() { + int low, high, number, originalNumber, rem, count = 0; + double result = 0.0; + printf("Enter two numbers(intervals): "); + scanf("%d %d", &low, &high); + printf("Armstrong numbers between %d and %d are: ", low, high); + + // iterate number from (low + 1) to (high - 1) + // In each iteration, check if number is Armstrong + for (number = low + 1; number < high; ++number) { + originalNumber = number; + + // number of digits calculation + while (originalNumber != 0) { + originalNumber /= 10; + ++count; + } + + originalNumber = number; + + // result contains sum of nth power of individual digits + while (originalNumber != 0) { + rem = originalNumber % 10; + result += pow(rem, count); + originalNumber /= 10; + } + + // check if number is equal to the sum of nth power of individual digits + if ((int)result == number) { + printf("%d ", number); + } + + // resetting the values + count = 0; + result = 0; + } + + return 0; +} diff --git a/armstrongC b/armstrongC new file mode 100644 index 00000000..8655134e --- /dev/null +++ b/armstrongC @@ -0,0 +1,18 @@ +int main() +{ +int n,r,sum=0,temp; +printf("enter the number="); +scanf("%d",&n); +temp=n; +while(n>0) +{ +r=n%10; +sum=sum+(r*r*r); +n=n/10; +} +if(temp==sum) +printf("armstrong number "); +else +printf("not armstrong number"); +return 0; +} diff --git a/basicgameinc b/basicgameinc new file mode 100644 index 00000000..8dad81bb --- /dev/null +++ b/basicgameinc @@ -0,0 +1,509 @@ +#include +#include +#include +#include +#include +#define MAX 10 +#define A "P" +#define B "Q" +int getRandom(int low, int high); +int getValidInteger(int low, int high); +unsigned int playerRoll(int low, int high); + +void seed(void) +{ + srand(time(NULL)); +} +void space(unsigned int size) // create space +{ + printf(" "); +} +char getDisplayType(unsigned int index, unsigned int playerPosition, char playerName) // check the index and return character +{ + + if (playerName != '#') + { + if (index == playerPosition) + return playerName; + + if (index == 0) + return ('C'); + + if (index % 3 == 0) + if (index % 5 == 0) + if (index % 7 == 0) + return('G'); + else return ('L'); + else return ('W'); + + if (index % 5 == 0) + if (index % 7 == 0) + return('G'); + else return ('L'); + + if (index % 7 == 0) { + return('G'); + } + else return (' '); + } + + if (playerName == '#') + { + + if (index == 0) + return ('C'); + + if (index % 3 == 0) { + if (index % 5 == 0) { + if (index % 7 == 0) { + return('G'); + } + else return ('L'); + } + else return ('W'); + } + + if (index % 5 == 0) { + if (index % 7 == 0) { + return('G'); + } + else return ('L'); + } + if (index % 7 == 0) { + return('G'); + } + else + return (' '); + + + + } +} +void firstLine(unsigned int size) // create the upper line of the square for the first and last line +{ + int i; + + for (i = 0; i < size; i++) + { + printf(" ___ "); + } + printf("\n"); +} + +void secondLine(unsigned int size, unsigned int i, unsigned int playerPosition, char playerName) //create the inside and return the type for the first line +{ + int x, j; + for (j = 0; j < size; j++) + { + x = j; + printf("| %c |", getDisplayType(x, playerPosition, playerName)); + } + printf("\n"); +} + +void secondLine2nd(unsigned int size, unsigned int i, unsigned int playerPosition, char playerName) { //create the inside and return the type for the last line + int x, y, z; + y = 3 * (size - 1); + printf("| %c |", getDisplayType(y, playerPosition, playerName)); + for (i = 0; i < size - 2; i++) + { + z = 2 * (size - 1) + ((size - 2) - i); + printf("| %c |", getDisplayType(z, playerPosition, playerName)); + } + x = size + i; + printf("| %c |", getDisplayType(x, playerPosition, playerName)); + + + printf("\n"); +} + + +void thirdLine(unsigned int size) // create lower line for the first and last line +{ + int i; + + for (i = 0; i < size; i++) + { + printf("|___|"); + } + printf("\n"); +} +void upperrow(unsigned int size) // create the upper line for the square of the row +{ + int i; + printf(" ___"); + for (i = 0; i < size - 2; i++) { + space(size); + } + printf(" ___"); + printf("\n"); +} + +void lowerrow(unsigned int size) //create the lower line for the square of the row +{ + int i; + printf("|___|"); + for (i = 0; i < size - 2; i++) { + space(size); + } + + printf("|___|"); + printf("\n"); +} + +void middlerow(unsigned int size, unsigned int i, unsigned int playerPosition, char playerName) //create the inside and return the type for the square of the row +{ + int q, b, p; + b = i; + q = 3 * (size - 1) + ((size - 1) - i); + printf("| %c |", getDisplayType(q, playerPosition, playerName)); + for (b = 0; b < size - 2; b++) { + space(size); + } + p = size - 1 + i; + printf("| %c |", getDisplayType(p, playerPosition, playerName)); + printf("\n"); +} + + +char getValidCharacter(char a, char b) //make sure the character input is right// +{ + char c, choice; + do { + scanf_s("%c", &choice); + getchar(); + if ((choice != 'p' && choice != 'q' && choice != 'r' && choice != 's')) + { + printf("Invalid input, please try again: "); + } + } while (choice != 'p' && choice != 'q' && choice != 'r' && choice != 's'); + return choice; +} + +int getValidInteger(int low, int high) //validate the input number// +{ + int a; int choice; + do { + a = scanf_s("%d", &choice); + if (choice high) + { + printf("Invalid input, please try again: "); + } + + } while (choice < low || choice > high); + return choice; +} + +int getRandom(int low, int high) //get a rando number +{ + int a; + a = rand() % (high - low) + low; + + return a; +} + +unsigned int playerRoll(int low, int high) //prompt and output the roll +{ + int a = 1, b, c, d, e, choice; + do { + printf("\nyour turn, how many dice will you roll : "); + scanf_s("%d", &choice); + if (choice == 1) + { + b = getRandom(low, high); + //printf("b: \n"); + //scanf_s("%d", &b); + printf("You rolled %d\n", b); + printf("Advancing %d space\n", b); + a = 0; + return b; + } + else if (choice == 2) + { + c = getRandom(low, high); + //printf("c: "); + //scanf_s("%d", &c); + d = getRandom(low, high); + //printf("d: "); + //scanf_s("%d", &d); + b = c + d; + printf("You rolled %d %d\n ", c, d); + printf("Advancing %d space\n", b); + a = 0; + return b; + } + else if (choice == 3) { + c = getRandom(low, high); + d = getRandom(low, high); + e = getRandom(low, high); + b = c + d + e; + printf("You rolled %d %d %d\n ", c, d, e); + printf("Advancing %d space\n", b); + a = 0; + return b; + } + else + printf("Try again,"); + } while (a = 1); +} + +void winPrize(int playerPrizes[], unsigned int* prizeCount) //do the winprize function +{ + int i; + unsigned int prize; + prize = getRandom(10, 100); + printf("%d\n", prize); + if (*prizeCount < MAX) + { + playerPrizes[*prizeCount] = prize; + printf("you won a prize of %d\n", prize); + *prizeCount = *prizeCount + 1; + } + else + printf("Your inventory is full \n"); +} +void winGrandPrize(int playerPrizes[], unsigned int* prizeCount) // do the win grand prize function +{ + int i; + unsigned int prize; + prize = getRandom(100, 200); + printf("%d\n", prize); + if (*prizeCount < MAX) + { + playerPrizes[*prizeCount] = prize; + printf("you won a grand prize of %d\n", prize); + *prizeCount = *prizeCount + 1; + } + else + printf("Your inventory is full "); +} +int loseItem(int playerPrizes[], unsigned int *prizeCount) // do the loseitem fuction +{ + int i, j, k, r, ran = 2; + + if (*prizeCount == 0) + { + printf("Nothing happened,Move On\n"); + } + else + { + + ran = getRandom(0, *prizeCount); + playerPrizes[ran] = 0; + *prizeCount = *prizeCount - 1; + printf("you lost the prize"); + for (i = ran - 1; i < MAX; i++) //arange the array in order + for (j = i; j < MAX; j++) + if (playerPrizes[i] == 0) + { + k = playerPrizes[i]; + playerPrizes[i] = playerPrizes[j]; + playerPrizes[j] = k; + } + + } +} + + +void initPlayer(int *playerScore, int playerPrizes[], unsigned int *prizeCount, char *playerName, int *playerPosition) //do the initplayer function, set everything to 0 +{ + int i; + playerPrizes[MAX] = 0; + + *playerScore = 0; + printf("playerPrizes: %d\n", playerPrizes[MAX]); + *prizeCount = 0; + *playerPosition = 0; + printf("Enter Player ID: "); + scanf_s("%c", playerName); +} + + + + +void displayBoard(unsigned int size, unsigned int playerPosition, char playerName) //display the boardgame +{ + + int k, size1, loop; + float loop2, playerPosition1, size2; + //printf("player name in display board: %c\n", playerName); + //printf("%d\n", r); + //printf("%d", playerPosition); + playerPosition1 = (float)playerPosition; + size1 = (4 * (size - 1)); + + size2 = (float)size1; + //printf("size2: %.2f\n", size2); + // printf("playerPo1: %.2f\n", playerPosition1); + //playerPosition2 = float size; + //printf("playerPos: %d\n", playerPosition); + loop2 = playerPosition1 / size2; + + loop = trunc(loop2); + + + k = playerPosition - (4 * (size - 1))*loop; + + playerPosition = k; + + { + int i = 0; + if (size == 1) + { + printf(" ___ \n"); + printf(" | ? | \n"); + printf(" |___|"); + printf("\n"); + } + else { + + for (i = 0; i < size - 1; i++) + { + + if (i == 0) + { + firstLine(size); + secondLine(size, i, playerPosition, playerName); + thirdLine(size); + } + } + for (i = 1; i < size - 1; i++) + { + upperrow(size); + middlerow(size, i, playerPosition, playerName); + lowerrow(size); + } + for (i = size - 2; i < size - 1; i++) + { + firstLine(size); + secondLine2nd(size, i, playerPosition, playerName); + thirdLine(size); + } + } + } +} + +int checkout(int *playerScore, int playerPrizes[], unsigned int* prizeCount) //do the checkout +{ + int i; + for (i = 0; i < *prizeCount; i++) + *playerScore += playerPrizes[i]; + *prizeCount = 0; + printf("You check out for $%d score is now: $%d \n", *playerScore, *playerScore); + if (*playerScore >= 200) + { + return 1; + } + else + { + return 0; + } + +} + + +void playGame(unsigned int size, int *playerScore, int playerPrizes[], unsigned int *prizeCount, char *playerName, int* playerPosition) //play the game +{ + printf("playerName in playgame %c\n", *playerName); + //printf("%d\n",*prizeCount); + //printf("%d\n", *playerScore); + int i, l = 1; + while (l) + { + displayBoard(size, *playerPosition, *playerName); + + printf("Score: %d inventory (%d items): ", *playerScore, *prizeCount); + for (i = 0; i < *prizeCount; i++) { + printf("%d, ", playerPrizes[i]); + + } + + *playerPosition = *playerPosition + playerRoll(1, 6); + if (*playerPosition >= 4 * (size - 1)) + *playerPosition = *playerPosition - 4 * (size - 1); + //printf("player position in display %d\n", *playerPosition); + //printf("display type in play game: %c\n", getDisplayType(*playerPosition, *playerPosition, '#')); + //displayBoard(boardSize, playerPosition, playerName); + if (getDisplayType(*playerPosition, *playerPosition, '#') == 'G') + { + winGrandPrize(playerPrizes, prizeCount); + } + else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'W') + { + + winPrize(playerPrizes, prizeCount); + } + else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'L') + { + + loseItem(playerPrizes, prizeCount); + } + else if (getDisplayType(*playerPosition, *playerPosition, '#') == 'C') + { + + + if (checkout(playerScore, playerPrizes, prizeCount) == 1) + { + printf("You Win\n"); + l = 0; + } + } + else + printf("nothing happens, go again.\n"); + } +} + + + + + + + +int main(void) +{ + int i, l = 1; + char a, choice; + char c = '#'; + int playerScore; + int playerPrizes[MAX]; + unsigned int prizeCount; + char playerName; + unsigned int size; + unsigned int playerPosition; + printf("Welcome to CHECKOUT\n"); + while (l) { + printf("Main Menu\n"); + printf("p-(p)lay q-(q)uit r-inst(r)uctions s-HI(s)core: \n"); + choice = getValidCharacter('P', 'Q'); + if (choice == 'p') { + printf("Number of players is 1\n"); + initPlayer(&playerScore, playerPrizes, &prizeCount, &playerName, &playerPosition); + + printf("Enter board size: "); + scanf_s("%d", &size); + + playGame(size, &playerScore, playerPrizes, &prizeCount, &playerName, &playerPosition); + + + + getchar(); + + } + if (choice == 's') + { + printf("--\n"); + printf(" \\ "); + printf("_______\n"); + printf(" \\++++++|\n"); + printf(" \\=====|\n"); + printf(" 0--- 0\n"); + printf("HI SCORE: %d Player Name: %c \n", playerScore, playerName); + } + if (choice == 'q') + { + printf("dont go, I will miss you :("); + l = 0; + getchar(); + } + } +} diff --git a/biggernumber b/biggernumber new file mode 100644 index 00000000..44608557 --- /dev/null +++ b/biggernumber @@ -0,0 +1,24 @@ +/* C Program to Find Largest of Two numbers */ + +#include + +int main() { + int a, b; + printf("Please Enter Two different values\n"); + scanf("%d %d", &a, &b); + + if(a > b) + { + printf("%d is Largest\n", a); + } + else if (b > a) + { + printf("%d is Largest\n", b); + } + else + { + printf("Both are Equal\n"); + } + + return 0; +} diff --git a/bignumber b/bignumber new file mode 100644 index 00000000..e2a39f2e --- /dev/null +++ b/bignumber @@ -0,0 +1,16 @@ +#include +int main() +{ +//Fill the code +int num1, num2; +scanf(“%d %d”,&num1,&num2); +if(num1 > num2) +{ +printf(“%d is greater”,num1); +} +else +{ +printf(“%d is greater”,num2); +} +return 0; +} diff --git a/binStack.c b/binStack.c new file mode 100644 index 00000000..7e9a7e28 --- /dev/null +++ b/binStack.c @@ -0,0 +1,36 @@ +#include "stdio.h" +#include "stdlib.h" +struct xx +{ + int data; + struct xx *link; +}; +struct xx *sp=0,*q; +main() +{ + int n; + printf("Enter a decimal number :"); + scanf("%d",&n); + while(n>0) + { + if(n==0) + { + sp=(struct xx *)malloc(sizeof(struct xx)); + sp->data=n%2; + sp->link=0; + } + else + { + q=(struct xx *)malloc(sizeof(struct xx)); + q->link=sp; + sp=q; + sp->data=n%2; + } + n=n/2; + } + while(sp!=0) + { + printf("%d ",sp->data); + sp=sp->link; + } +} diff --git a/bubble sort in C b/bubble sort in C new file mode 100644 index 00000000..bee530d0 --- /dev/null +++ b/bubble sort in C @@ -0,0 +1,41 @@ + +#include + +void swap(int *xp, int *yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +// A function to implement bubble sort +void bubbleSort(int arr[], int n) +{ +int i, j; +for (i = 0; i < n-1; i++) + + // Last i elements are already in place + for (j = 0; j < n-i-1; j++) + if (arr[j] > arr[j+1]) + swap(&arr[j], &arr[j+1]); +} + +/* Function to print an array */ +void printArray(int arr[], int size) +{ + int i; + for (i=0; i < size; i++) + printf("%d ", arr[i]); + printf("\n"); +} + + +int main() +{ + int arr[] = {64, 34, 25, 12, 22, 11, 90}; + int n = sizeof(arr)/sizeof(arr[0]); + bubbleSort(arr, n); + printf("Sorted array: \n"); + printArray(arr, n); + return 0; +} diff --git a/bubble sorting b/bubble sorting new file mode 100644 index 00000000..a491d8db --- /dev/null +++ b/bubble sorting @@ -0,0 +1,32 @@ +#include + +int main(){ + + int count, temp, i, j, number[30]; + + printf("How many numbers are u going to enter?: "); + scanf("%d",&count); + + printf("Enter %d numbers: ",count); + + for(i=0;i=0;i--){ + for(j=0;j<=i;j++){ + if(number[j]>number[j+1]){ + temp=number[j]; + number[j]=number[j+1]; + number[j+1]=temp; + } + } + } + + printf("Sorted elements: "); + for(i=0;i + +int main(){ + + int count, temp, i, j, number[30]; + + printf("How many numbers are u going to enter?: "); + scanf("%d",&count); + + printf("Enter %d numbers: ",count); + + for(i=0;i=0;i--){ + for(j=0;j<=i;j++){ + if(number[j]>number[j+1]){ + temp=number[j]; + number[j]=number[j+1]; + number[j+1]=temp; + } + } + } + + printf("Sorted elements: "); + for(i=0;i +#include + +int main() +{ + +int i,j,len,v=1; +char str[100]; + +printf("enter string : "); +scanf("%[^\n]%c",str); + +len=strlen(str); + +printf("duplicates character found in the string are: "); + for(i=0;i +#include +void main() +{ + int no,factorial; + int fact(int no); + clrscr(); + printf("Enter a number: "); + scanf("%d",&no); + factorial=fact(no); + printf("Factorial=%d",factorial); + getch(); +} +int fact(int no) +{ + if(no==1) + return 1; + else + return(no*fact(no-1)); +} diff --git a/calculate the salary b/calculate the salary new file mode 100644 index 00000000..d6fc8072 --- /dev/null +++ b/calculate the salary @@ -0,0 +1,30 @@ +#include + +int main() +{ + char g; + int yos,qual,sal=0; + + printf("Enter Gender, Years of Service and Qualifications (0=Graduate, 1=Post-graduate):\n"); + scanf("%c%d%d",&g,&yos,&qual); + + if(g == 'm'&& yos >= 10 && qual == 1) + sal = 15000; + else if((g == 'm' && yos >=10 && qual == 0) || (g == 'm' && yos < 10 && qual == 1)) + sal = 10000; + else if(g == 'm' && yos < 10 && qual == 0) + sal = 7000; + else if(g == 'f' && yos >= 10 && qual == 1) + sal = 12000; + else if(g == 'f' && yos >= 10 && qual == 0) + sal = 9000; + else if(g == 'f' && yos < 10 && qual == 1) + sal = 10000; + else if(g == 'f' && yos < 10 && qual == 0) + sal = 6000; + + printf("\nSalary of Employee = %d\n",sal); + + return 0; + +} diff --git a/calculator b/calculator new file mode 100644 index 00000000..a8bd462a --- /dev/null +++ b/calculator @@ -0,0 +1,29 @@ +#include +int main() { + char operator; + double first, second; + printf("Enter an operator (+, -, *,): "); + scanf("%c", &operator); + printf("Enter two operands: "); + scanf("%lf %lf", &first, &second); + + switch (operator) { + case '+': + printf("%.1lf + %.1lf = %.1lf", first, second, first + second); + break; + case '-': + printf("%.1lf - %.1lf = %.1lf", first, second, first - second); + break; + case '*': + printf("%.1lf * %.1lf = %.1lf", first, second, first * second); + break; + case '/': + printf("%.1lf / %.1lf = %.1lf", first, second, first / second); + break; + // operator doesn't match any case constant + default: + printf("Error! operator is not correct"); + } + + return 0; +} diff --git a/celcius to farenheit b/celcius to farenheit new file mode 100644 index 00000000..628c03e7 --- /dev/null +++ b/celcius to farenheit @@ -0,0 +1,11 @@ +/*to convert celcius to farenheit*/ +#include + +int main(){ +float celcius,farenheit; +printf("enter the value of celcius:"); +scanf("%f",&celcius); +farenheit=(celcius*9/5)+32; +printf("the temperature in farenheit is:%f",farenheit); +return 0; +} diff --git a/celsius to fahrenheit b/celsius to fahrenheit new file mode 100644 index 00000000..729795b7 --- /dev/null +++ b/celsius to fahrenheit @@ -0,0 +1,19 @@ +#include + +int main() +{ + float celsius, fahrenheit; + + /* Input temperature in celsius */ + printf("Enter temperature in Celsius: "); + scanf("%f", &celsius); + + /* celsius to fahrenheit conversion formula */ + fahrenheit = (celsius * 9 / 5) + 32; + + printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit); + + return + +0; +} diff --git a/check leap year b/check leap year new file mode 100644 index 00000000..b945276c --- /dev/null +++ b/check leap year @@ -0,0 +1,27 @@ +#include +int main() { + int year; + printf("Enter a year: "); + scanf("%d", &year); + + // leap year if perfectly visible by 400 + if (year % 400 == 0) { + printf("%d is a leap year.", year); + } + // not a leap year if visible by 100 + // but not divisible by 400 + else if (year % 100 == 0) { + printf("%d is not a leap year.", year); + } + // leap year if not divisible by 100 + // but divisible by 4 + else if (year % 4 == 0) { + printf("%d is a leap year.", year); + } + // all other years are not leap year + else { + printf("%d is not a leap year.", year); + } + + return 0; +} diff --git a/check to prime no or not b/check to prime no or not new file mode 100644 index 00000000..4fba8ae0 --- /dev/null +++ b/check to prime no or not @@ -0,0 +1,27 @@ +#include +int main() { + int n, i, flag = 0; + printf("Enter a positive integer: "); + scanf("%d", &n); + + for (i = 2; i <= n / 2; ++i) { + + // condition for non-prime + if (n % i == 0) { + flag = 1; + break; + } + } + + if (n == 1) { + printf("1 is neither prime nor composite."); + } + else { + if (flag == 0) + printf("%d is a prime number.", n); + else + printf("%d is not a prime number.", n); + } + + return 0; +} diff --git a/check whether a no. is prime or not b/check whether a no. is prime or not new file mode 100644 index 00000000..ce336769 --- /dev/null +++ b/check whether a no. is prime or not @@ -0,0 +1,25 @@ +#include +int main() { + int n, i, flag = 0; + printf("Enter a positive integer: "); + scanf("%d", &n); + + for (i = 2; i <= n / 2; ++i) { + if (n % i == 0) { + flag = 1; + break; + } + } + + if (n == 1) { + printf("1 is neither prime nor composite."); + } + else { + if (flag == 0) + printf("%d is a prime number.", n); + else + printf("%d is not a prime number.", n); + } + + return 0; +} diff --git a/checkPalindrome b/checkPalindrome new file mode 100644 index 00000000..683bdd86 --- /dev/null +++ b/checkPalindrome @@ -0,0 +1,28 @@ +#include +int main() +{ + int num, reverse_num=0, remainder,temp; + printf("Enter an integer: "); + scanf("%d", &num); + + /* Here we are generating a new number (reverse_num) + * by reversing the digits of original input number + */ + temp=num; + while(temp!=0) + { + remainder=temp%10; + reverse_num=reverse_num*10+remainder; + temp/=10; + } + + /* If the original input number (num) is equal to + * to its reverse (reverse_num) then its palindrome + * else it is not. + */ + if(reverse_num==num) + printf("%d is a palindrome number",num); + else + printf("%d is not a palindrome number",num); + return 0; +} diff --git a/check_prime_number b/check_prime_number new file mode 100644 index 00000000..9d10a632 --- /dev/null +++ b/check_prime_number @@ -0,0 +1,26 @@ +#include + +int main() { + int loop, number; + int prime = 1; + + printf("Enter a number"); + scanf("%d", &number); + + if(number <= 1){ + prime = 0; + } + else{ + for(loop = 2; loop < number; loop++) { + if((number % loop) == 0) { + prime = 0; + } + } + } + + if (prime == 1) + printf("%d is prime number.", number); + else + printf("%d is not a prime number.", number); + return 0; +} diff --git a/checkpalindrome b/checkpalindrome new file mode 100644 index 00000000..683bdd86 --- /dev/null +++ b/checkpalindrome @@ -0,0 +1,28 @@ +#include +int main() +{ + int num, reverse_num=0, remainder,temp; + printf("Enter an integer: "); + scanf("%d", &num); + + /* Here we are generating a new number (reverse_num) + * by reversing the digits of original input number + */ + temp=num; + while(temp!=0) + { + remainder=temp%10; + reverse_num=reverse_num*10+remainder; + temp/=10; + } + + /* If the original input number (num) is equal to + * to its reverse (reverse_num) then its palindrome + * else it is not. + */ + if(reverse_num==num) + printf("%d is a palindrome number",num); + else + printf("%d is not a palindrome number",num); + return 0; +} diff --git a/cieser-cipher-encryption b/cieser-cipher-encryption new file mode 100644 index 00000000..5a9be319 --- /dev/null +++ b/cieser-cipher-encryption @@ -0,0 +1,85 @@ + +#include +void encryption(); +void decryption(); + +void main(){ + encryption(); + decryption(); +} + +void encryption() +{ + char message[100], ch; + int i, key; + + printf("Enter a message to encrypt: "); + gets(message); + printf("Enter key: "); + scanf("%d", &key); + + for(i = 0; message[i] != '\0'; ++i){ + ch = message[i]; + + if(ch >= 'a' && ch <= 'z'){ + ch = ch + key; + + if(ch > 'z'){ + ch = ch - 'z' + 'a' - 1; + } + + message[i] = ch; + } + else if(ch >= 'A' && ch <= 'Z'){ + ch = ch + key; + + if(ch > 'Z'){ + ch = ch - 'Z' + 'A' - 1; + } + + message[i] = ch; + } + } + + printf("Encrypted message: %s", message); + + return 0; +} + +void decryption() +{ + char message[100], ch; + int i, key; + + printf("Enter a message to decrypt: "); + gets(message); + printf("Enter key: "); + scanf("%d", &key); + + for(i = 0; message[i] != '\0'; ++i){ + ch = message[i]; + + if(ch >= 'a' && ch <= 'z'){ + ch = ch - key; + + if(ch < 'a'){ + ch = ch + 'z' - 'a' + 1; + } + + message[i] = ch; + } + else if(ch >= 'A' && ch <= 'Z'){ + ch = ch - key; + + if(ch < 'A'){ + ch = ch + 'Z' - 'A' + 1; + } + + message[i] = ch; + } + } + + printf("Decrypted message: %s", message); + + return 0; +} diff --git a/classes b/classes new file mode 100644 index 00000000..fd837724 --- /dev/null +++ b/classes @@ -0,0 +1,36 @@ +#include +using namespace std; + +class employee +{ + private: + int a , b , c; + + public: + int d , e; + void setdata(int a1, int b1, int c1); //Declaration + void getdata(){ + cout<<"THe value of a is "< This will throw error as a is private + lucky.d= 35; + lucky.e=45; + lucky.setdata(1,2,4); + lucky.getdata(); + return 0; +} diff --git a/coding template b/coding template new file mode 100644 index 00000000..1a98ed76 --- /dev/null +++ b/coding template @@ -0,0 +1,52 @@ +#include +using namespace std; +const int N=10000000; +//................................... +#define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); +#define ll long long int +#define ss second +#define ff first +#define pb push_back +#define pll pair +#define F(i,n) for(ll i=0;i +#define t_case ll test;cin>>test;while(test--) +#define all(a) a.begin(),a.end() +//................................... +bool cmp(ll x,ll y) {return x>y;} +ll min3(ll x,ll y,ll z) {return min(x,min(y,z));} +ll max3(ll x,ll y,ll z) {return max(x,max(y,z));} +ll powrec(ll a,ll n){if(n==0) return 1;ll res=powrec(a,n/2);if(n%2)return res*res*a;else return res*res;} +ll powloop(ll a,ll n){ll res=1;while(n>0){if(n&1)res=res*a;a=a*a;n>>=1;}return res;} +ll powmod(ll a,ll n,ll m){a%=m;ll res=1;while(n>0){if(n&1)res=res*a%m;a=a*a%m;n>>=1;}return res;} +//................................... +int main() +{ + + + t_case + { + ll n,x,p,k; + cin>>n>>x>>p>>k; + ll a[n]; + F(i,n) + { + cin>>a[i]; + } + sort(a,a+n); + ll c=0,flag=0; + F(i,300) + { + if(a[p-1]==x) + { + flag=1; break; + } + c++; + a[k-1]=x; + sort(a,a+n); + } + if(flag==1) cout< +#include + +int main() +{ + float principle, rate, time, CI; + + /* Input principle, time and rate */ + printf("Enter principle (amount): "); + scanf("%f", &principle); + + printf("Enter time: "); + scanf("%f", &time); + + printf("Enter rate: "); + scanf("%f", &rate); + + /* Calculate compound interest */ + CI = principle* (pow((1 + rate / 100), time)); + + /* Print the resultant CI */ + printf("Compound Interest = %f", CI); + + return 0; +} diff --git a/concatenate str b/concatenate str new file mode 100644 index 00000000..f913a334 --- /dev/null +++ b/concatenate str @@ -0,0 +1,24 @@ +#include +int main() { + char s1[100] = "programming ", s2[] = "is awesome"; + int length, j; + + // store length of s1 in the length variable + length = 0; + while (s1[length] != '\0') { + ++length; + } + + // concatenate s2 to s1 + for (j = 0; s2[j] != '\0'; ++j, ++length) { + s1[length] = s2[j]; + } + + // terminating the s1 string + s1[length] = '\0'; + + printf("After concatenation: "); + puts(s1); + + return 0; +} diff --git a/count_totalno_of_alphabet_digit_and_special_char b/count_totalno_of_alphabet_digit_and_special_char new file mode 100644 index 00000000..06578b9d --- /dev/null +++ b/count_totalno_of_alphabet_digit_and_special_char @@ -0,0 +1,43 @@ +#include +#include +#include + + +#define str_size 100 //Declare the maximum size of the string + +void main() +{ + char str[str_size]; + int alp, digit, splch, i; + alp = digit = splch = i = 0; + + + printf("\n\nCount total number of alphabets, digits and special characters :\n"); + printf("--------------------------------------------------------------------\n"); + printf("Input the string : "); + fgets(str, sizeof str, stdin); + + /* Checks each character of string*/ + + while(str[i]!='\0') + { + if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z')) + { + alp++; + } + else if(str[i]>='0' && str[i]<='9') + { + digit++; + } + else + { + splch++; + } + + i++; + } + + printf("Number of Alphabets in the string is : %d\n", alp); + printf("Number of Digits in the string is : %d\n", digit); + printf("Number of Special characters in the string is : %d\n\n", splch); +} diff --git a/countingsort b/countingsort new file mode 100644 index 00000000..32d25c01 --- /dev/null +++ b/countingsort @@ -0,0 +1,58 @@ +#include +#include + +#define WORD_SIZE 10 +#define POSITIONS 100 + +struct word { + int position; + char term[WORD_SIZE + 1]; +}; + +void counting_sort(char *sorted[], struct word words[], size_t size) { + int buckets[POSITIONS]; + int i; + + + for (i = 0; i < POSITIONS; i++) { + buckets[i] = 0; + } + + + for (i = 0; i < size; i++) { + buckets[words[i].position]++; + } + + + for (i = 1; i < POSITIONS; i++) { + buckets[i] += buckets[i - 1]; + } + + + for (i = size - 1; i >= 0; i--) { + sorted[--buckets[words[i].position]] = words[i].term; + } +} + +int main() { + int n, i, max; + char garbage[WORD_SIZE + 1]; + + scanf("%d", &n); + struct word words[n]; + char *sorted[n]; + + max = n / 2; + for (i = 0; i < max; i++) { + scanf("%d %10s", &words[i].position, garbage); + strcpy(words[i].term, "-"); + } + for (; i < n; i++) { + scanf("%d %10s", &words[i].position, words[i].term); + } + counting_sort(sorted, words, n); + for (i = 0; i < n; i++) { + printf("%s ", sorted[i]); + } + return 0; +} diff --git a/countvowels b/countvowels new file mode 100644 index 00000000..5e660fc1 --- /dev/null +++ b/countvowels @@ -0,0 +1,30 @@ +//PROGRAM TO COUNT VOWELS IN A STRING.. +#include +#include +#include +void main() +{ + char s1[30]; + int count=0,i; + clrscr(); + printf("Enter the sentence:"); + gets(s1); + for(i=0;i +int main() { + double n1, n2, n3; + printf("Enter three different numbers: "); + scanf("%lf %lf %lf", &n1, &n2, &n3); + + // if n1 is greater than both n2 and n3, n1 is the largest + if (n1 >= n2 && n1 >= n3) + printf("%.2f is the largest number.", n1); + + // if n2 is greater than both n1 and n3, n2 is the largest + if (n2 >= n1 && n2 >= n3) + printf("%.2f is the largest number.", n2); + + // if n3 is greater than both n1 and n2, n3 is the largest + if (n3 >= n1 && n3 >= n2) + printf("%.2f is the largest number.", n3); + + return 0; +} diff --git a/creatervikas b/creatervikas new file mode 100644 index 00000000..8f466115 --- /dev/null +++ b/creatervikas @@ -0,0 +1,31 @@ +#include +using namespace std; +int findSmallestElement(int arr[], int n){ + /* We are assigning the first array element to + * the temp variable and then we are comparing + * all the array elements with the temp inside + * loop and if the element is smaller than temp + * then the temp value is replaced by that. This + * way we always have the smallest value in temp. + * Finally we are returning temp. + */ + int temp = arr[0]; + for(int i=0; iarr[i]) { + temp=arr[i]; + } + } + return temp; +} +int main() { + int n; + cout<<"Enter the size of array: "; + cin>>n; int arr[n-1]; + cout<<"Enter array elements: "; + for(int i=0; i>arr[i]; + } + int smallest = findSmallestElement(arr, n); + cout<<"Smallest Element is: "< + +int main() +{ + int number, cube; + + printf(" \n Please Enter any integer Value : "); + scanf("%d", &number); + + cube = number * number * number; + + printf("\n Cube of a given number %d is = %d", number, cube); + + return 0; +} diff --git a/datatypes b/datatypes new file mode 100644 index 00000000..53e78bf9 --- /dev/null +++ b/datatypes @@ -0,0 +1,25 @@ +#include +int main() +{ + int a = 1; + char b ='G'; + double c = 3.14; + printf("Hello World!\n"); + + + printf("Hello! I am a character. My value is %c and " + "my size is %lu byte.\n", b,sizeof(char)); + + + printf("Hello! I am an integer. My value is %d and " + "my size is %lu bytes.\n", a,sizeof(int)); + + + printf("Hello! I am a double floating point variable." + " My value is %lf and my size is %lu bytes.\n",c,sizeof(double)); + + + printf("Bye! See you soon. :)\n"); + + return 0; +} diff --git a/decimaltobinary b/decimaltobinary new file mode 100644 index 00000000..246fccbe --- /dev/null +++ b/decimaltobinary @@ -0,0 +1,34 @@ +// C++ program to convert a decimal +// number to binary number + +#include +using namespace std; + +// function to convert decimal to binary +void decToBinary(int n) +{ + // array to store binary number + int binaryNum[32]; + + // counter for binary array + int i = 0; + while (n > 0) { + + // storing remainder in binary array + binaryNum[i] = n % 2; + n = n / 2; + i++; + } + + // printing binary array in reverse order + for (int j = i - 1; j >= 0; j--) + cout << binaryNum[j]; +} + +// Driver program to test above function +int main() +{ + int n = 17; + decToBinary(n); + return 0; +} diff --git a/digital-clock b/digital-clock new file mode 100644 index 00000000..2f2b4b76 --- /dev/null +++ b/digital-clock @@ -0,0 +1,47 @@ +/*C program to design a digital clock.*/ + +#include +#include //for sleep() function +#include +#include + +int main() +{ + int hour, minute, second; + + hour=minute=second=0; + + while(1) + { + //clear output screen + system("clear"); + + //print time in HH : MM : SS format + printf("%02d : %02d : %02d ",hour,minute,second); + + //clear output buffer in gcc + fflush(stdout); + + //increase second + second++; + + //update hour, minute and second + if(second==60){ + minute+=1; + second=0; + } + if(minute==60){ + hour+=1; + minute=0; + } + if(hour==24){ + hour=0; + minute=0; + second=0; + } + + sleep(1); //wait till 1 second + } + + return 0; +} diff --git a/duplicateelementsinarray b/duplicateelementsinarray new file mode 100644 index 00000000..300b1476 --- /dev/null +++ b/duplicateelementsinarray @@ -0,0 +1,17 @@ +#include + +int main() +{ + + int arr[] = {1, 2, 3, 4, 5,6,7,2}; + int length = sizeof(arr)/sizeof(arr[0]); + printf("Duplicate elements in given array: \n"); + //Searches for duplicate element + for(int i = 0; i < length; i++) { + for(int j = i + 1; j < length; j++) { + if(arr[i] == arr[j]) + printf("%d\n", arr[j]); + } + } + return 0; +} diff --git a/even or odd number b/even or odd number new file mode 100644 index 00000000..8a29813a --- /dev/null +++ b/even or odd number @@ -0,0 +1,20 @@ +/* Program to check whether the input integer number + * is even or odd using the modulus operator (%) + */ +#include +int main() +{ + // This variable is to store the input number + int num; + + printf("Enter an integer: "); + scanf("%d",&num); + + // Modulus (%) returns remainder + if ( num%2 == 0 ) + printf("%d is an even number", num); + else + printf("%d is an odd number", num); + + return 0; +} diff --git a/example to calculate income tax b/example to calculate income tax new file mode 100644 index 00000000..1e6dcd84 --- /dev/null +++ b/example to calculate income tax @@ -0,0 +1,17 @@ +/*to calculate income tax*/ +#include + +int main(){ +float tax=0, income; +printf("enter your income\n"); +scanf("%f",&income); + +if(income>=250000 && income<=500000){ + tax=tax+0.05*(income-250000); +} +if(income<=500000){ + tax=tax+0.20*(income-500000); +} +printf("your net income tax to be paid is %f\n",tax); +return 0; +} diff --git a/fabonaccisequence b/fabonaccisequence new file mode 100644 index 00000000..9b6f1264 --- /dev/null +++ b/fabonaccisequence @@ -0,0 +1,17 @@ +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) + { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/factorailno b/factorailno new file mode 100644 index 00000000..1663258b --- /dev/null +++ b/factorailno @@ -0,0 +1,12 @@ +#include +int main() +{ + int i,fact=1,number; + printf("Enter a number: "); + scanf("%d",&number); + for(i=1;i<=number;i++){ + fact=fact*i; + } + printf("Factorial of %d is: %d",number,fact); +return 0; +} diff --git a/factorial b/factorial new file mode 100644 index 00000000..846f0ed4 --- /dev/null +++ b/factorial @@ -0,0 +1,12 @@ + #include + int main() + { + int i,fact=1,number; + printf("Enter a number: "); + scanf("%d",&number); + for(i=1;i<=number;i++){ + fact=fact*i; + } + printf("Factorial of %d is: %d",number,fact); + return 0; + } diff --git a/factorial by recursion b/factorial by recursion new file mode 100644 index 00000000..4c4cbe7b --- /dev/null +++ b/factorial by recursion @@ -0,0 +1,22 @@ +#include +#include + +int fact(int); + +int main() { + int factorial, num; + + printf("Enter the value of num :"); + scanf("%d", &num); + + factorial = fact(num); + printf("Factorial is %d", factorial); + + return (0); +} + +int fact(int n) { + if (n == 0) { + return (1); + } + return (n * fact(n - 1)); diff --git a/factorial.c b/factorial.c new file mode 100644 index 00000000..07b09ea2 --- /dev/null +++ b/factorial.c @@ -0,0 +1,19 @@ +#include +int main() { + int n, i; + unsigned long long fact = 1; + printf("Enter an integer: "); + scanf("%d", &n); + + // shows error if the user enters a negative integer + if (n < 0) + printf("Error! Factorial of a negative number doesn't exist."); + else { + for (i = 1; i <= n; ++i) { + fact *= i; + } + printf("Factorial of %d = %llu", n, fact); + } + + return 0; +} diff --git a/factorial_prog b/factorial_prog new file mode 100644 index 00000000..07b09ea2 --- /dev/null +++ b/factorial_prog @@ -0,0 +1,19 @@ +#include +int main() { + int n, i; + unsigned long long fact = 1; + printf("Enter an integer: "); + scanf("%d", &n); + + // shows error if the user enters a negative integer + if (n < 0) + printf("Error! Factorial of a negative number doesn't exist."); + else { + for (i = 1; i <= n; ++i) { + fact *= i; + } + printf("Factorial of %d = %llu", n, fact); + } + + return 0; +} diff --git a/factors b/factors new file mode 100644 index 00000000..75f1ba15 --- /dev/null +++ b/factors @@ -0,0 +1,13 @@ +#include +int main() { + int num, i; + printf("Enter a positive integer: "); + scanf("%d", &num); + printf("Factors of %d are: ", num); + for (i = 1; i <= num; ++i) { + if (num % i == 0) { + printf("%d ", i); + } + } + return 0; +} diff --git a/febonacci series b/febonacci series new file mode 100644 index 00000000..e9f5157b --- /dev/null +++ b/febonacci series @@ -0,0 +1,17 @@ +# febonacci series +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/fibanocci_series b/fibanocci_series new file mode 100644 index 00000000..d3375464 --- /dev/null +++ b/fibanocci_series @@ -0,0 +1,16 @@ +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/fibonacci b/fibonacci new file mode 100644 index 00000000..d3375464 --- /dev/null +++ b/fibonacci @@ -0,0 +1,16 @@ +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/fibonacci no. b/fibonacci no. new file mode 100644 index 00000000..3a8309e9 --- /dev/null +++ b/fibonacci no. @@ -0,0 +1,16 @@ +#include + +int main(){ +int fib1=0,fib2=1,fib,n; +printf("enter the lmits:"); +scanf("%d",&n); + +for(int i=1;i<=n;i++){ + printf("%d\n",fib1); + + fib=fib1+fib2; + fib1=fib2; + fib2=fib; +} +return 0; +} diff --git a/find gcd b/find gcd new file mode 100644 index 00000000..b7e8ed2a --- /dev/null +++ b/find gcd @@ -0,0 +1,33 @@ +#include +#include + +int gcd(int a, int b) +{ + int r; + while(a) + { + r=b%a; + b=a; + a=r; + } + return b; +} +int gcd_(int* A, int N) +{ + int c=gcd(*A,*(A+1)); + int i,g; + for(i=1;i +int main() +{ + int num1, num2, flag_var, i, j; + + /* Ask user to input the from/to range + * like 1 to 100, 10 to 1000 etc. + */ + printf("Enter two range(input integer numbers only):"); + //Store the range in variables using scanf + scanf("%d %d", &num1, &num2); + + //Display prime numbers for input range + printf("Prime numbers from %d and %d are:\n", num1, num2); + for(i=num1+1; i +int main() { + int i, n; + float arr[100]; + printf("Enter the number of elements (1 to 100): "); + scanf("%d", &n); + + for (i = 0; i < n; ++i) { + printf("Enter number%d: ", i + 1); + scanf("%f", &arr[i]); + } + + for (i = 1; i < n; ++i) { + if (arr[0] < arr[i]) + arr[0] = arr[i]; + } + + printf("Largest element = %.2f", arr[0]); + + return 0; +} diff --git a/findingtickets b/findingtickets new file mode 100644 index 00000000..c35f8997 --- /dev/null +++ b/findingtickets @@ -0,0 +1,18 @@ +#include + +int main() +{ + int num_s,tickets,r; + printf("please enter number of rows!!"); + scanf("%d",&r); + printf("please enter number of seats!!"); + scanf("%d",&num_s); + printf("please enter number of tickets!!"); + scanf("%d",&tickets); + int n=r*num_s; + if(n>tickets) + printf("Opps!! tickets is less then number of seats:)"); + else + printf("%d %d",n,tickets-n); + +} diff --git a/flying_character b/flying_character new file mode 100644 index 00000000..db8b37f1 --- /dev/null +++ b/flying_character @@ -0,0 +1,75 @@ + +#include +#include +#include +#include + +/*will return character based on passed number*/ +char getCHAR(int n) +{ + char ok[]="~!@#$%^&*:;'\",{}|<>="; + return ok[n]; +} + +/*function to identify that any key hit by the keyboard*/ +int kbhit(void) +{ + struct termios oldt, newt; + int ch; + int oldf; + + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + oldf = fcntl(STDIN_FILENO, F_GETFL, 0); + fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); + + ch = getchar(); + + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + fcntl(STDIN_FILENO, F_SETFL, oldf); + + if(ch != EOF) + { + ungetc(ch, stdin); + return 1; + } + + return 0; +} + +/*move cursor on specified position on screen*/ +void gotoxy(int x,int y) +{ +printf("%c[%d;%df",0x1B,y,x); +} + +int main(void) { + int x,y; + long int i,j; + i=j=0; + + x=1; y=20; + system("clear"); + + /*program will run until any key hit*/ + while(!kbhit()) + { + x=rand()%((150-1+1)+1); + y=rand()%((50-1+1)+1); + gotoxy(x,y); + printf("%c", getCHAR(rand()%((20-1+1)+1))); fflush(stdout); + ++i; + if(i==10000) {++j; i=0;} + if(j==100000){ i=0;} + if(j<=100) + usleep(30000); + if(j<=1000) + usleep(3000); + if(j<=10000) + usleep(300); + } + return 0; +} +
diff --git a/gassbooking b/gassbooking new file mode 100644 index 00000000..323c2183 --- /dev/null +++ b/gassbooking @@ -0,0 +1,83 @@ +#include +#include +#include +#include +void data(){ + int press; + + int id; + char name[50]; + printf("\nThank you for comming :-)"); + + + + printf("\nEnter your valid id number: "); + scanf("%d",&id); + fflush(stdin); + printf("\nEnter your name: "); + gets(name); + printf("\n1.LPG big cylinders\n2.LPG mini cylinders\n3.acetylin cylinders\ndefault.cancel your orders"); + + + + printf("\nEnter your choice:"); + scanf("%d",&press); + switch(press){ + + case 1: + printf("----------------------------------------------------------------------------------"); + printf("\nLpg big cylinders"); + printf("\nyour choice lpg big cylinders\n"); + printf("Your id %d\n",id); + printf("your name: "); + puts(name); + printf("\nyour order is sucessful\nyour gas delivary within 24 days\n"); + printf("----------------------------------------------------------------------------------"); + break; + case 2: + printf("----------------------------------------------------------------------------------"); + printf("\nLpg mini cylinders"); + printf("\nyour choice mini cylinders\n"); + printf("Your id %d\n",id); + printf("your name: "); + puts(name); + printf("\nyour order is sucessful\nyour gas delivary within 24 days\n"); + printf("----------------------------------------------------------------------------------"); + break; + case 3: + printf("----------------------------------------------------------------------------------"); + printf("\nacetylin cylinders"); + printf("\nyour choice acetylin cylinders\n"); + printf("Your id %d\n",id); + printf("your name: "); + puts(name); + printf("\nyour order is sucessful \n your gas delivary within 24 days\n"); + printf("----------------------------------------------------------------------------------"); + break; + default: + printf("----------------------------------------------------------------------------------"); + printf("\ncancel your order "); + printf("\nyour order with name and id cancel sucessfully\n"); + printf("----------------------------------------------------------------------------------"); +} + +} + +int main() +{ + + char choice = 'y'; + + + + data(); + printf("\n\nyou want to continue yes or no\nif yes press y or no press n:"); + choice = getche(); + + }while(choice == 'y'); + + + return 0; + + +} diff --git a/gcd_of_2_numbers b/gcd_of_2_numbers new file mode 100644 index 00000000..efb2e9f5 --- /dev/null +++ b/gcd_of_2_numbers @@ -0,0 +1,33 @@ +// C program to find GCD of two numbers +#include + +// Recursive function to return gcd of a and b +int gcd(int a, int b) +{ + // Everything divides 0 + if (a == 0) + return b; + if (b == 0) + return a; + + // base case + if (a == b) + return a; + + // a is greater + if (a > b) + return gcd(a-b, b); + return gcd(a, b-a); +} + +// Driver program to test above function +int main() +{ + + int a , b ; + printf("Enter 2 numbers to find their gcd"); + scanf("%d%d",&a,&b); + printf("GCD of %d and %d is %d ", a, b, gcd(a, b)); + return 0; + +} diff --git a/graphicscarmove b/graphicscarmove new file mode 100644 index 00000000..a139a2a9 --- /dev/null +++ b/graphicscarmove @@ -0,0 +1,58 @@ +#include +#include + +// Function to draw moving car +void draw_moving_car(void) { + + int i, j = 0, gd = DETECT, gm; + + initgraph(&gd, &gm, ""); + + for (i = 0; i <= 420; i = i + 10) { + + // Set color of car as red + setcolor(RED); + + line(0 + i, 300, 210 + i, 300); + line(50 + i, 300, 75 + i, 270); + line(75 + i, 270, 150 + i, 270); + line(150 + i, 270, 165 + i, 300); + line(0 + i, 300, 0 + i, 330); + line(210 + i, 300, 210 + i, 330); + + // For left wheel of car + circle(65 + i, 330, 15); + circle(65 + i, 330, 2); + + // For right wheel of car + circle(145 + i, 330, 15); + circle(145 + i, 330, 2); + + // Line left of left wheel + line(0 + i, 330, 50 + i, 330); + + // Line middle of both wheel + line(80 + i, 330, 130 + i, 330); + + // Line right of right wheel + line(210 + i, 330, 160 + i, 330); + + delay(100); + + // To erase previous drawn car + // use cleardevice() function + cleardevice(); + } + + getch(); + + closegraph(); +} + +// Driver code +int main() +{ + draw_moving_car(); + + return 0; +} diff --git a/graterNumber b/graterNumber new file mode 100644 index 00000000..a3023d78 --- /dev/null +++ b/graterNumber @@ -0,0 +1,17 @@ +#include +int main() +{ + int a,b; + printf("Enter two values); + scanf("%d%d",&a,&b); + if(a>b) + { + return(a); + } + else + { + return(b); + } + + +} diff --git a/greatearno b/greatearno new file mode 100644 index 00000000..a8bd462a --- /dev/null +++ b/greatearno @@ -0,0 +1,29 @@ +#include +int main() { + char operator; + double first, second; + printf("Enter an operator (+, -, *,): "); + scanf("%c", &operator); + printf("Enter two operands: "); + scanf("%lf %lf", &first, &second); + + switch (operator) { + case '+': + printf("%.1lf + %.1lf = %.1lf", first, second, first + second); + break; + case '-': + printf("%.1lf - %.1lf = %.1lf", first, second, first - second); + break; + case '*': + printf("%.1lf * %.1lf = %.1lf", first, second, first * second); + break; + case '/': + printf("%.1lf / %.1lf = %.1lf", first, second, first / second); + break; + // operator doesn't match any case constant + default: + printf("Error! operator is not correct"); + } + + return 0; +} diff --git a/greatemo b/greatemo new file mode 100644 index 00000000..0ff23675 --- /dev/null +++ b/greatemo @@ -0,0 +1,22 @@ +#include + +int main() { + int a, b; + printf("Please Enter Two different values\n"); + scanf("%d %d", &a, &b); + + if(a > b) + { + printf("%d is Largest\n", a); + } + else if (b > a) + { + printf("%d is Largest\n", b); + } + else + { + printf("Both are Equal\n"); + } + + return 0; +} diff --git a/greater number b/greater number new file mode 100644 index 00000000..73c31935 --- /dev/null +++ b/greater number @@ -0,0 +1,15 @@ +#include +#include +int main() +{ + int a, b, big; + printf("Enter any two number: "); + scanf("%d%d", &a, &b); + if(a>b) + big=a; + else + big=b; + printf("\nBiggest of the two number is: %d", big); + getch(); + return 0; +} diff --git a/greater number c b/greater number c new file mode 100644 index 00000000..fa2d2b05 --- /dev/null +++ b/greater number c @@ -0,0 +1,22 @@ +#include +int main() +{ + int no1, no2; + printf("Insert two numbers:"); + scanf("%d %d",&no1, &no2); + + //Condition to check which of the two number is greater + //it will compare of number where number 1 is greater + if(no1 > no2) + printf("%d is greatest",no1); + + //where number 2 is greater + else if(no2 > no1) + printf("%d is greatest",no2); + + //for both are equal + else + printf("%d and %d are equal", no1, no2); + + return 0; +} diff --git a/greater of three numbers b/greater of three numbers new file mode 100644 index 00000000..9b6f073d --- /dev/null +++ b/greater of three numbers @@ -0,0 +1,13 @@ +int main() +{ + int num1,num2,num3; + printf("\nEnter value of num1, num2 and num3:"); + scanf("%d %d %d",&num1,&num2,&num3); + if((num1>num2)&&(num1>num3)) + printf("\n Number1 is greatest"); + else if((num2>num3)&&(num2>num1)) + printf("\n Number2 is greatest"); + else + printf("\n Number3 is greatest"); + return 0; +} diff --git a/greater of two number b/greater of two number new file mode 100644 index 00000000..73c31935 --- /dev/null +++ b/greater of two number @@ -0,0 +1,15 @@ +#include +#include +int main() +{ + int a, b, big; + printf("Enter any two number: "); + scanf("%d%d", &a, &b); + if(a>b) + big=a; + else + big=b; + printf("\nBiggest of the two number is: %d", big); + getch(); + return 0; +} diff --git a/greater_two_number b/greater_two_number new file mode 100644 index 00000000..236349e4 --- /dev/null +++ b/greater_two_number @@ -0,0 +1,16 @@ +#include +int main() +{ + +int num1, num2; +scanf(“%d %d”,&num1,&num2); +if(num1 > num2) +{ +printf(“%d is greater”,num1); +} +else +{ +printf(“%d is greater”,num2); +} +return 0; +} diff --git a/greaterno10 b/greaterno10 new file mode 100644 index 00000000..a8d92ee8 --- /dev/null +++ b/greaterno10 @@ -0,0 +1,20 @@ +#include + int main() { + int a[10]; + int i; + int greatest; + printf("Enter ten values:"); + //Store 10 numbers in an array + for (i = 0; i < 10; i++) { + scanf("%d", &a[i]); + } + //Assume that a[0] is greatest + greatest = a[0]; + for (i = 0; i < 10; i++) { +if (a[i] > greatest) { +greatest = a[i]; + } + } + printf(" + Greatest of ten numbers is %d", greatest + } diff --git a/greaternumber b/greaternumber new file mode 100644 index 00000000..21c18ea9 --- /dev/null +++ b/greaternumber @@ -0,0 +1,28 @@ +/* + * C program to find the biggest of three numbers + */ +#include + +void main() +{ + int num1, num2, num3; + + printf("Enter the values of num1, num2 and num3\n"); + scanf("%d %d %d", &num1, &num2, &num3); + printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3); + if (num1 > num2) + { + if (num1 > num3) + { + printf("num1 is the greatest among three \n"); + } + else + { + printf("num3 is the greatest among three \n"); + } + } + else if (num2 > num3) + printf("num2 is the greatest among three \n"); + else + printf("num3 is the greatest among three \n"); +} diff --git a/greaternumberofAorB b/greaternumberofAorB new file mode 100644 index 00000000..bc12d152 --- /dev/null +++ b/greaternumberofAorB @@ -0,0 +1,22 @@ +// C program to find the greatest of three numbers + +#include +int main() +{ +//Fill the code +int num1, num2, num3; +scanf(“%d %d %d”,&num1,&num2,&num3); +if(num1 > num2 && num1 > num3) +{ +printf(“%d is greater”,num1); +} +else if(num2 > num1 && num2 > num3) +{ +printf(“%d is greater”,num2); +} +else +{ +printf(“%d is greater”,num3); +} +return 0; +} diff --git a/greatest number b/greatest number new file mode 100644 index 00000000..8c571177 --- /dev/null +++ b/greatest number @@ -0,0 +1,20 @@ +#include +int main() { + double n1, n2, n3; + printf("Enter three different numbers: "); + scanf("%lf %lf %lf", &n1, &n2, &n3); + + // if n1 is greater than both n2 and n3, n1 is the largest + if (n1 >= n2 && n1 >= n3) + printf("%.2f is the largest number.", n1); + + // if n2 is greater than both n1 and n3, n2 is the largest + if (n2 >= n1 && n2 >= n3) + printf("%.2f is the largest number.", n2); + + // if n3 is greater than both n1 and n2, n3 is the largest + if (n3 >= n1 && n3 >= n2) + printf("%.2f is the largest number.", n3); + + return 0; +} diff --git a/greatest number 1 b/greatest number 1 new file mode 100644 index 00000000..5e4e2dd8 --- /dev/null +++ b/greatest number 1 @@ -0,0 +1,27 @@ + #include + #include + void main() + { + + int n1,n2,sum; + clrscr(); + + printf("\nEnter 1st number : "); + scanf("%d",&n1); + + printf("\nEnter 2nd number : "); + scanf("%d",&n2); + + if(n1 > n2) + printf("\n1st number is greatest."); + else + printf("\n2nd number is greatest."); + + getch(); + } + + Output : + + Enter 1st number : 115 + Enter 2nd number : 228 + 2nd number is greatest. diff --git a/greatestoftwonumbers b/greatestoftwonumbers new file mode 100644 index 00000000..51ec9ef3 --- /dev/null +++ b/greatestoftwonumbers @@ -0,0 +1,17 @@ +// C program to find largest of three numbers in C using nested if +#include +int main() +{ +int a,b,c; +printf("Enter three numbers : "); +scanf("%d %d %d", &a,&b,&c); +int max = 0; +if(a > b && a > c) +printf("\nThe largest among the three numbers is %d",a); +else if(b > a && b > c) +printf("\nThe largest among the three numbers is %d",b); +else +printf("\nThe largest among the three numbers is %d",c); +printf("\n"); +return 0; +} diff --git a/greatno b/greatno new file mode 100644 index 00000000..c8138701 --- /dev/null +++ b/greatno @@ -0,0 +1,15 @@ +int main() +{ +//Fill the code +int num1, num2; +scanf(“%d %d”,&num1,&num2); +if(num1 > num2) +{ +printf(“%d is greater”,num1); +} +else +{ +printf(“%d is greater”,num2); +} +return 0; +} diff --git a/greatorno b/greatorno new file mode 100644 index 00000000..35025350 --- /dev/null +++ b/greatorno @@ -0,0 +1,18 @@ +#include +int main() +{ + int num1,num2,num3; + + //Ask user to input any three integer numbers + printf("\nEnter value of num1, num2 and num3:"); + //Store input values in variables for comparsion + scanf("%d %d %d",&num1,&num2,&num3); + + if((num1>num2)&&(num1>num3)) + printf("\n Number1 is greatest"); + else if((num2>num3)&&(num2>num1)) + printf("\n Number2 is greatest"); + else + printf("\n Number3 is greatest"); + return 0; +} diff --git a/greternumber b/greternumber new file mode 100644 index 00000000..02fee784 --- /dev/null +++ b/greternumber @@ -0,0 +1,54 @@ +#include + + + +int main(void) + +{ + + + int num1,num2; + + printf("Enter two numbers:"); + scanf("%d %d",&num1,&num2); + + + if(num1>num2) + + + { + + printf("%d is greatest",num1); + + +} + + + else if(num2>num1) + + + { + + + printf("%d is greatest",num2); + + +} + + + else + + + { + + + printf("%d and %d are equal",num1,num2); + + + } + + + return 0; + + +} diff --git a/gretter new b/gretter new new file mode 100644 index 00000000..3b23501c --- /dev/null +++ b/gretter new @@ -0,0 +1,18 @@ +/ C program to find the greatest of two numbers + +#include +int main() +{ +//Fill the code +int num1, num2; +scanf(“%d %d”,&num1,&num2); +if(num1 > num2) +{ +printf(“%d is greater”,num1); +} +else +{ +printf(“%d is greater”,num2); +} +return 0; +} diff --git a/hacktoberfest b/hacktoberfest new file mode 100644 index 00000000..5ded750d --- /dev/null +++ b/hacktoberfest @@ -0,0 +1,6 @@ +#include +#include +void main() +{ +printf("Hacktober fest is going on!!"); +} diff --git a/hangman game b/hangman game new file mode 100644 index 00000000..d9843926 --- /dev/null +++ b/hangman game @@ -0,0 +1,87 @@ +#include +#include +#include + +void main() +{ +int i,j,c,count=0,ans=0,flag=0,*ptr; +char a[1][6]={"dogodo"}; + +char b[10],alpha; +char d='_'; +clrscr(); +c=strlen(&a[0][0]); +//printf("\n\t\t**************\n\n\t\t\t"); +printf("\n\n\t\t\t ** HANGMAN ** \n"); + printf("\n\t\t\t**************\t\t\t"); + printf("\n\t\t\t..............\n\n\t\t\t "); +for(j=0;j +int main() +{ + char ch, input[9], s[9]; + int i, len=0; + + printf("Enter your input bits:"); + scanf("%s",input); + + printf("Enter your key S (8 bits):"); + scanf("%s",s); + + printf("Your hash code is:"); + for(i=0; i<8; i++) + { + ch=input[i]^s[i]; + printf("%d",ch); + } + return 0; +} diff --git a/heapsort b/heapsort new file mode 100644 index 00000000..83d3d96e --- /dev/null +++ b/heapsort @@ -0,0 +1,48 @@ +#include + + void swap(int *a, int *b) { + int temp = *a; + *a = *b; + *b = temp; + } + + void heapify(int arr[], int n, int i) { + int largest = i; + int left = 2 * i + 1; + int right = 2 * i + 2; + + if (left < n && arr[left] > arr[largest]) + largest = left; + + if (right < n && arr[right] > arr[largest]) + largest = right; + if (largest != i) { + swap(&arr[i], &arr[largest]); + heapify(arr, n, largest); + } + } + void heapSort(int arr[], int n) { + for (int i = n / 2 - 1; i >= 0; i--) + heapify(arr, n, i); + for (int i = n - 1; i >= 0; i--) { + swap(&arr[0], &arr[i]); + heapify(arr, i, 0); + } + } + void printArray(int arr[], int n) { + for (int i = 0; i < n; ++i) + printf("%d ", arr[i]); + printf("\n"); + } + int main() { + int n; + scanf("%d",&n); + int arr[n]; + for(int i=0;i=0;i--) + heapify(arr,n,i); + printArray(arr, n); + heapSort(arr, n); + printArray(arr, n); + } diff --git a/helloc b/helloc new file mode 100644 index 00000000..bd384c49 --- /dev/null +++ b/helloc @@ -0,0 +1,6 @@ +#include +#include +void main() +{ +printf("Hello c ! Have a nice day:)"); +} diff --git a/hexadecimaltodecimal b/hexadecimaltodecimal new file mode 100644 index 00000000..e24680c4 --- /dev/null +++ b/hexadecimaltodecimal @@ -0,0 +1,51 @@ +// C++ program to convert hexadecimal to decimal +#include +#include +using namespace std; + +// Function to convert hexadecimal to decimal +int hexadecimalToDecimal(char hexVal[]) +{ + int len = strlen(hexVal); + + // Initializing base value to 1, i.e 16^0 + int base = 1; + + int dec_val = 0; + + // Extracting characters as digits from last character + for (int i=len-1; i>=0; i--) + { + // if character lies in '0'-'9', converting + // it to integral 0-9 by subtracting 48 from + // ASCII value. + if (hexVal[i]>='0' && hexVal[i]<='9') + { + dec_val += (hexVal[i] - 48)*base; + + // incrementing base by power + base = base * 16; + } + + // if character lies in 'A'-'F' , converting + // it to integral 10 - 15 by subtracting 55 + // from ASCII value + else if (hexVal[i]>='A' && hexVal[i]<='F') + { + dec_val += (hexVal[i] - 55)*base; + + // incrementing base by power + base = base*16; + } + } + + return dec_val; +} + +// Driver program to test above function +int main() +{ + char hexNum[] = "1A"; + cout << hexadecimalToDecimal(hexNum) << endl; + return 0; +} diff --git a/hollow diamond b/hollow diamond new file mode 100644 index 00000000..fa50fdae --- /dev/null +++ b/hollow diamond @@ -0,0 +1,43 @@ +#include +int main() +{ + int i, j, n; + scanf("%d", &n); + + // first half + + for(i = 0; i < n; i++) + { + for(j = 0; j < (2 * n); j++) + { + if(i + j <= n - 1) // upper left triangle + printf("*"); + else + printf(" "); + if((i + n) <= j) // upper right triangle + printf("*"); + else + printf(" "); + } + printf("\n"); + } + + // second half + + for(i = 0; i < n; i++) + { + for(j = 0; j < (2 * n); j++) + { + if(i >= j) //bottom left triangle + printf("*"); + else + printf(" "); + if(i >= (2 * n - 1) - j) // bottom right triangle + printf("*"); + else + printf(" "); + } + printf("\n"); + } + return 0; +} diff --git a/infix to postfix b/infix to postfix new file mode 100644 index 00000000..d51271b8 --- /dev/null +++ b/infix to postfix @@ -0,0 +1,110 @@ +#include +#include +#include +// Stack type +struct Stack +{ + int top; + unsigned capacity; + int* array; +}; +// Stack Operations +struct Stack* createStack( unsigned capacity ) +{ + struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack)); + if (!stack) + return NULL; + stack->top = -1; + stack->capacity = capacity; + stack->array = (int*) malloc(stack->capacity * sizeof(int)); + return stack; +} +int isEmpty(struct Stack* stack) +{ + return stack->top == -1 ; +} +char peek(struct Stack* stack) +{ + return stack->array[stack->top]; +} +char pop(struct Stack* stack) +{ + if (!isEmpty(stack)) + return stack->array[stack->top--] ; + return '$'; + } +void push(struct Stack* stack, char op) +{ + stack->array[++stack->top] = op; +} +// A utility function to check if the given character is operand +int isOperand(char ch) +{ + return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'); +} +// A utility function to return precedence of a given operator +// Higher returned value means higher precedence +int Prec(char ch) +{ + switch (ch) + { + case '+': + case '-': + return 1; + case '*': + case '/': + return 2; + case '^': + return 3; + } +return -1; +} +// The main function that converts given infix expression +// to postfix expression. +int infixToPostfix(char* exp) +{ + int i, k; + // Create a stack of capacity equal to expression size + struct Stack* stack = createStack(strlen(exp)); + if(!stack) // See if stack was created successfully + return -1 ; + for (i = 0, k = -1; exp[i]; ++i) + { + // If the scanned character is an operand, add it to output. + if (isOperand(exp[i])) + exp[++k] = exp[i]; + // If the scanned character is an ‘(‘, push it to the stack. + else if (exp[i] == '(') + push(stack, exp[i]); + // If the scanned character is an ‘)’, pop and output from the stack + // until an ‘(‘ is encountered. + else if (exp[i] == ')') + { + while (!isEmpty(stack) && peek(stack) != '(') + exp[++k] = pop(stack); + if (!isEmpty(stack) && peek(stack) != '(') + return -1; // invalid expression + else + pop(stack); + } + else // an operator is encountered + { + while (!isEmpty(stack) && Prec(exp[i]) <= Prec(peek(stack))) + exp[++k] = pop(stack); + push(stack, exp[i]); + } + } + // pop all the operators from the stack + while (!isEmpty(stack)) + exp[++k] = pop(stack ); + exp[++k] = '\0'; + printf( "%s", exp ); +} +// Driver program to test above functions +int main() +{ + char exp[]={ "a+b*(c^d-e)^(f+g*h)-i"}; + printf("OUTPUT IS:\n"); + infixToPostfix(exp); + return 0; + } diff --git a/int, float, double and char b/int, float, double and char new file mode 100644 index 00000000..1faedbc2 --- /dev/null +++ b/int, float, double and char @@ -0,0 +1,15 @@ +#include +int main() { + int intType; + float floatType; + double doubleType; + char charType; + + // sizeof evaluates the size of a variable + printf("Size of int: %ld bytes\n", sizeof(intType)); + printf("Size of float: %ld bytes\n", sizeof(floatType)); + printf("Size of double: %ld bytes\n", sizeof(doubleType)); + printf("Size of char: %ld byte\n", sizeof(charType)); + + return 0; +} diff --git a/ipcheck_in_linux b/ipcheck_in_linux new file mode 100644 index 00000000..7e0dcd94 --- /dev/null +++ b/ipcheck_in_linux @@ -0,0 +1,42 @@ + +#include +#include +#include + +#include +#include +#include +#include +#include + +int main() +{ + unsigned char ip_address[15]; + int fd; + struct ifreq ifr; + + /*AF_INET - to define network interface IPv4*/ + /*Creating soket for it.*/ + fd = socket(AF_INET, SOCK_DGRAM, 0); + + /*AF_INET - to define IPv4 Address type.*/ + ifr.ifr_addr.sa_family = AF_INET; + + /*eth0 - define the ifr_name - port name + where network attached.*/ + memcpy(ifr.ifr_name, "eth0", IFNAMSIZ-1); + + /*Accessing network interface information by + passing address using ioctl.*/ + ioctl(fd, SIOCGIFADDR, &ifr); + /*closing fd*/ + close(fd); + + /*Extract IP Address*/ + strcpy(ip_address,inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr)); + + printf("System IP Address is: %s\n",ip_address); + + return 0; +} +
diff --git a/larger b/larger new file mode 100644 index 00000000..fc8d4fac --- /dev/null +++ b/larger @@ -0,0 +1,15 @@ +#include +int main() +{ +int n1, n2; +scanf(“%d %d”,&n1,&n2); +if(n1 > n2) +{ +printf(“%d is larger”,n1); +} +else +{ +printf(“%d is larger”,n2); +} +return 0; +} diff --git a/leapYear b/leapYear new file mode 100644 index 00000000..7e74f2f1 --- /dev/null +++ b/leapYear @@ -0,0 +1,21 @@ +#include +int main() { + int year; + printf("Enter a year: "); + scanf("%d", &year); + +if (year % 400 == 0) { + printf("%d is a leap year.", year); + } + else if (year % 100 == 0) { + printf("%d is not a leap year.", year); + } + else if (year % 4 == 0) { + printf("%d is a leap year.", year); + } + else { + printf("%d is not a leap year.", year); + } + + return 0; +} diff --git a/leapyear.c b/leapyear.c new file mode 100644 index 00000000..7aa916e8 --- /dev/null +++ b/leapyear.c @@ -0,0 +1,19 @@ +#include +int main() { + int year; + printf("Enter a year: "); + scanf("%d", &year); + if (year % 400 == 0) { + printf("%d is a leap year.", year); + } + else if (year % 100 == 0) { + printf("%d is not a leap year.", year); + } + else if (year % 4 == 0) { + printf("%d is a leap year.", year); + } + else { + printf("%d is not a leap year.", year); + } + return 0; +} diff --git a/linked_list_deletion.c b/linked_list_deletion.c new file mode 100644 index 00000000..afe19f36 --- /dev/null +++ b/linked_list_deletion.c @@ -0,0 +1,164 @@ +Deletion of first node of singly linked list3 +Free the memory occupied by the first node. +Deletion of first node of singly linked list4 +Program to delete first node of Singly Linked List +/** + * C program to delete first node from Singly Linked List + */ + +#include +#include + + +/* Structure of a node */ +struct node { + int data; // Data + struct node *next; // Address +}*head; + + +void createList(int n); +void deleteFirstNode(); +void displayList(); + + + +int main() +{ + int n, choice; + + /* + * Create a singly linked list of n nodes + */ + printf("Enter the total number of nodes: "); + scanf("%d", &n); + createList(n); + + printf("\nData in the list \n"); + displayList(); + + printf("\nPress 1 to delete first node: "); + scanf("%d", &choice); + + /* Delete first node from list */ + if(choice == 1) + deleteFirstNode(); + + printf("\nData in the list \n"); + displayList(); + + return 0; +} + + +/* + * Create a list of n nodes + */ +void createList(int n) +{ + struct node *newNode, *temp; + int data, i; + + head = (struct node *)malloc(sizeof(struct node)); + + /* + * If unable to allocate memory for head node + */ + if(head == NULL) + { + printf("Unable to allocate memory."); + } + else + { + /* + * In data of node from the user + */ + printf("Enter the data of node 1: "); + scanf("%d", &data); + + head->data = data; // Link the data field with data + head->next = NULL; // Link the address field to NULL + + temp = head; + + /* + * Create n nodes and adds to linked list + */ + for(i=2; i<=n; i++) + { + newNode = (struct node *)malloc(sizeof(struct node)); + + /* If memory is not allocated for newNode */ + if(newNode == NULL) + { + printf("Unable to allocate memory."); + break; + } + else + { + printf("Enter the data of node %d: ", i); + scanf("%d", &data); + + newNode->data = data; // Link the data field of newNode with data + newNode->next = NULL; // Link the address field of newNode with NULL + + temp->next = newNode; // Link previous node i.e. temp to the newNode + temp = temp->next; + } + } + + printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n"); + } +} + + +/* + * Deletes the first node of the linked list + */ +void deleteFirstNode() +{ + struct node *toDelete; + + if(head == NULL) + { + printf("List is already empty."); + } + else + { + toDelete = head; + head = head->next; + + printf("\nData deleted = %d\n", toDelete->data); + + /* Clears the memory occupied by first node*/ + free(toDelete); + + printf("SUCCESSFULLY DELETED FIRST NODE FROM LIST\n"); + } +} + + +/* + * Displays the entire list + */ +void displayList() +{ + struct node *temp; + + /* + * If the list is empty i.e. head = NULL + */ + if(head == NULL) + { + printf("List is empty."); + } + else + { + temp = head; + while(temp != NULL) + { + printf("Data = %d\n", temp->data); // Print data of current node + temp = temp->next; // Move to next node + } + } +} diff --git a/m%n remainder 1.c b/m%n remainder 1.c new file mode 100644 index 00000000..e35522e2 --- /dev/null +++ b/m%n remainder 1.c @@ -0,0 +1,31 @@ +#include + + + +void main() + +{ + + int m,n; + + scanf("%d",&m); + + scanf("%d",&n); + + + + int rem = m%n; //% is used to find the remainder. + + + + if(rem==1) + + printf("1"); + + else + + printf("0"); + + + +} diff --git a/mapOfIndia b/mapOfIndia new file mode 100644 index 00000000..cedcfc01 --- /dev/null +++ b/mapOfIndia @@ -0,0 +1,18 @@ +main() + { + int a,b,c; + int count = 1; + + for (b = c = 10; + a = "- FIGURE?, UMKC,XYZHello Folks,\ + TFy!QJu ROo TNn(ROo)SLq SLq ULo+\ + UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\ + NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\ + HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\ + T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\ + Hq!WFs XDt!"[b+++21]; ) + for(; a-- > 64 ; ) + putchar ( ++c=='Z' ? c = c/ 9:33^b&1); + + return 0; + } diff --git a/maxinarray b/maxinarray new file mode 100644 index 00000000..49ffb980 --- /dev/null +++ b/maxinarray @@ -0,0 +1,35 @@ +#include + +using namespace std; + +#define SIZE 50 //Defining max size of array + +int main() + +{ + int array[SIZE]; + int i, max, min, size; + // Input size of the array + cout<<"Enter size of the array: "; + cin>>size; + // Input array elements + cout<<"\n Enter "<>array[i]; + // Assume first element as maximum + max = array[0]; + + //Find maximum in all array elements. + for(i=1; i max) + max = array[i]; + + } + // Print maximum element + cout<<"\nMaximum element =" << max << "\n"; + + return 0; + +} diff --git a/merge and quick sort b/merge and quick sort new file mode 100644 index 00000000..1625ed1e --- /dev/null +++ b/merge and quick sort @@ -0,0 +1,212 @@ +/* +Name: Hrithik Singh +*/ + + + +#include +#include +#define MAX 30 + +// Merges two subarrays of arr[]. +// First subarray is arr[l..m] +// Second subarray is arr[m+1..r] +int 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); + } +} + + + +void swap(int* a, int* b) +{ + int t = *a; + *a = *b; + *b = t; +} + +/* This function takes last element as pivot, places + the pivot element at its correct position in sorted + array, and places all smaller (smaller than pivot) + to left of pivot and all greater elements to right + of pivot */ +int partition (int arr[], int low, int high) +{ + int pivot = arr[high]; // pivot + int i = (low - 1); // Index of smaller element + + for (int j = low; j <= high- 1; j++) + { + // If current element is smaller than the pivot + if (arr[j] < pivot) + { + i++; // increment index of smaller element + swap(&arr[i], &arr[j]); + } + } + swap(&arr[i + 1], &arr[high]); + return (i + 1); +} + +/* The main function that implements QuickSort + arr[] --> Array to be sorted, + low --> Starting index, + high --> Ending index */ +void quickSort(int arr[], int low, int high) +{ + if (low < high) + { + /* pi is partitioning index, arr[p] is now + at right place */ + int pi = partition(arr, low, high); + + // Separately sort elements before + // partition and after partition + quickSort(arr, low, pi - 1); + quickSort(arr, pi + 1, high); + } +} + + +/* 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"); +} +//fuction for merge sort without recursion +int mergeSortwithout(int arr[],int size){ + int i=2,j=0; + while(isize){ + right=size-1; + } + int middle=(left + right )/2; + merge(arr,left,middle, right); + j=j+1; + } + i=i*2; + if(i>=size){ + i=i/2; + merge(arr,0,size-1,(i-1)); + i=size; + } + } +} +/* Driver program to test above functions */ +int main(){ + int n; + int choice; + printf("Enter the no. of elements in the list\n"); + scanf("%d",&n); + printf("Enter the elements\n"); + int arr[n]; + for(int i=0;i +using namespace std; + +void mergeArrays(int arr1[], int arr2[], int n1, + int n2, int arr3[]) +{ + int i = 0, j = 0, k = 0; + while (i>n1>>n2; + int arr1[n1],arr2[n2]; + for(int i=0;i>arr1[i]; + } + for(int i=0;i>arr2[i]; + } + + int arr3[n1+n2]; + mergeArrays(arr1, arr2, n1, n2, arr3); + + cout << "Array after merging into one" < +using namespace std; + +void findMultiples(int n){ + for(int i = 0; i <= n; i++) + if(i % 3 == 0 && i % 5 == 0) + cout << i << endl; +} + +int main() { + findMultiples(120); + + return 0; +} diff --git a/multiplication of 2 matrix b/multiplication of 2 matrix new file mode 100644 index 00000000..def03f7b --- /dev/null +++ b/multiplication of 2 matrix @@ -0,0 +1,82 @@ +#include + +// function to get matrix elements entered by the user +void getMatrixElements(int matrix[][10], int row, int column) { + + printf("\nEnter elements: \n"); + + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("Enter a%d%d: ", i + 1, j + 1); + scanf("%d", &matrix[i][j]); + } + } +} + +// function to multiply two matrices +void multiplyMatrices(int first[][10], + int second[][10], + int result[][10], + int r1, int c1, int r2, int c2) { + + // Initializing elements of matrix mult to 0. + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + result[i][j] = 0; + } + } + + // Multiplying first and second matrices and storing it in result + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { + for (int k = 0; k < c1; ++k) { + result[i][j] += first[i][k] * second[k][j]; + } + } + } +} + +// function to display the matrix +void display(int result[][10], int row, int column) { + + printf("\nOutput Matrix:\n"); + for (int i = 0; i < row; ++i) { + for (int j = 0; j < column; ++j) { + printf("%d ", result[i][j]); + if (j == column - 1) + printf("\n"); + } + } +} + +int main() { + int first[10][10], second[10][10], result[10][10], r1, c1, r2, c2; + printf("Enter rows and column for the first matrix: "); + scanf("%d %d", &r1, &c1); + printf("Enter rows and column for the second matrix: "); + scanf("%d %d", &r2, &c2); + + // Taking input until + // 1st matrix columns is not equal to 2nd matrix row + while (c1 != r2) { + printf("Error! Enter rows and columns again.\n"); + printf("Enter rows and columns for the first matrix: "); + scanf("%d%d", &r1, &c1); + printf("Enter rows and columns for the second matrix: "); + scanf("%d%d", &r2, &c2); + } + + // get elements of the first matrix + getMatrixElements(first, r1, c1); + + // get elements of the second matrix + getMatrixElements(second, r2, c2); + + // multiply two matrices. + multiplyMatrices(first, second, result, r1, c1, r2, c2); + + // display the result + display(result, r1, c2); + + return 0; +} diff --git a/multiplication of 2.12 and 3.88 b/multiplication of 2.12 and 3.88 new file mode 100644 index 00000000..b264583c --- /dev/null +++ b/multiplication of 2.12 and 3.88 @@ -0,0 +1,21 @@ +#include + +int main() +{ + float A, B, product = 0.0f; + + // Ask user to enter the two numbers + printf("Enter two floating numbers A and B : \n"); + + // Read two numbers from the user || A = 2.12, B = 3.88 + scanf("%f%f", &A, &B); + + // Calclulate the multiplication of A and B + // using '*' operator + product = A * B; + + // Print the product + printf("Product of A and B is: %f", product); + + return 0; +} diff --git a/multiplication_table b/multiplication_table new file mode 100644 index 00000000..7250ae24 --- /dev/null +++ b/multiplication_table @@ -0,0 +1,10 @@ +#include +int main() { + int n, i; + printf("Enter an integer: "); + scanf("%d", &n); + for (i = 1; i <= 10; ++i) { + printf("%d * %d = %d \n", n, i, n * i); + } + return 0; +} diff --git a/multiply b/multiply new file mode 100644 index 00000000..6aabbae8 --- /dev/null +++ b/multiply @@ -0,0 +1,14 @@ +#include +int main() { + double a, b, product; + printf("Enter two numbers: "); + scanf("%lf %lf", &a, &b); + + // Calculating product + product = a * b; + + // Result up to 2 decimal point is displayed using %.2lf + printf("Product = %.2lf", product); + + return 0; +} diff --git a/multiply two no. b/multiply two no. new file mode 100644 index 00000000..28de781a --- /dev/null +++ b/multiply two no. @@ -0,0 +1,13 @@ +#include +#include +void main() +{ +int one, two, multiply; +printf("Enter first number - "); +scanf("%d",&one); +printf("Enter second number - "); +scanf("%d",&two); +multiply = one * two; +printf("The multiplication of numbers %d and %d is %d",one,two,multiply); +getch(); +} diff --git a/multiplyoftwono b/multiplyoftwono new file mode 100644 index 00000000..6aabbae8 --- /dev/null +++ b/multiplyoftwono @@ -0,0 +1,14 @@ +#include +int main() { + double a, b, product; + printf("Enter two numbers: "); + scanf("%lf %lf", &a, &b); + + // Calculating product + product = a * b; + + // Result up to 2 decimal point is displayed using %.2lf + printf("Product = %.2lf", product); + + return 0; +} diff --git a/negativeNumber.c b/negativeNumber.c new file mode 100644 index 00000000..3beb72fb --- /dev/null +++ b/negativeNumber.c @@ -0,0 +1,18 @@ +// Program to display a number if it is negative + +#include +int main() { + int number; + + printf("Enter an integer: "); + scanf("%d", &number); + + // true if number is less than 0 + if (number < 0) { + printf("You entered %d.\n", number); + } + + printf("The if statement is easy."); + + return 0; +} diff --git "a/next \342\206\222\342\206\220 prev Matrix multiplication in C" "b/next \342\206\222\342\206\220 prev Matrix multiplication in C" new file mode 100644 index 00000000..736f5400 --- /dev/null +++ "b/next \342\206\222\342\206\220 prev Matrix multiplication in C" @@ -0,0 +1,49 @@ +#include +#include +int main(){ +int a[10][10],b[10][10],mul[10][10],r,c,i,j,k; +system("cls"); +printf("enter the number of row="); +scanf("%d",&r); +printf("enter the number of column="); +scanf("%d",&c); +printf("enter the first matrix element=\n"); +for(i=0;i + +int main() +{ + int Number, Reminder, Count=0; + + printf("\n Please Enter any number\n"); + scanf("%d", &Number); + + while(Number > 0) + { + Number = Number / 10; + Count = Count + 1; + } + + printf("\n Number of Digits in a Given Number = %d", Count); + return 0; +} diff --git a/numberSwap b/numberSwap new file mode 100644 index 00000000..1843c0cc --- /dev/null +++ b/numberSwap @@ -0,0 +1,18 @@ +//Forswaping two numbers. +#include + +int main() +{ + int x, y; + printf("Enter Value of x "); + scanf("%d", &x); + printf("\nEnter Value of y "); + scanf("%d", &y); + + int temp = x; + x = y; + y = temp; + + printf("\nAfter Swapping: x = %d, y = %d", x, y); + return 0; +} diff --git a/odd number b/odd number new file mode 100644 index 00000000..24ff1dfd --- /dev/null +++ b/odd number @@ -0,0 +1,14 @@ +#include +int main() { + int num; + printf("Enter an integer: "); + scanf("%d", &num); + + // True if num is perfectly divisible by 2 + if(num % 2 == 0) + printf("%d is even.", num); + else + printf("%d is odd.", num); + + return 0; +} diff --git a/odd or enen b/odd or enen new file mode 100644 index 00000000..dbbc9a12 --- /dev/null +++ b/odd or enen @@ -0,0 +1,16 @@ +#include +int main () +{ +int n; +printf("enter the value of n\n"); +scanf("%d",&n); +if (n%d==0): +{ +printf("%d is enen ,n); +} + else + { + printf("%d is odd ,n); +} + return 0; + } diff --git a/optimized implementation of bubble sort b/optimized implementation of bubble sort new file mode 100644 index 00000000..386b2f00 --- /dev/null +++ b/optimized implementation of bubble sort @@ -0,0 +1,31 @@ +#include +int main() +{ +int array[100], n, i, j, swap,flag=0; +printf("Enter number of elementsn"); +scanf("%d", &n); +printf("Enter %d Numbers:n", n); +for(i = 0; i < n; i++) +scanf("%d", &array[i]); +for(i = 0 ; i < n - 1; i++) +{ +for(j = 0 ; j < n-i-1; j++) +{ +if(array[j] > array[j+1]) +{ +swap = array[j]; +array[j] = array[j+1]; +array[j+1] = swap; +flag=1; +} +if(!flag) +{ +break; +} +} +} +printf("Sorted Array:n"); +for(i = 0; i < n; i++) +printf("%dn", array[i]); +return 0; +} diff --git a/orphanprocess.c b/orphanprocess.c new file mode 100644 index 00000000..fa5b0f0b --- /dev/null +++ b/orphanprocess.c @@ -0,0 +1,40 @@ +#include +#include +#include + +int main() +{ + // fork() Create a child process + + int pid = fork(); + if (pid > 0) + { + //getpid() returns process id + // while getppid() will return parent process id + printf("Parent process\n"); + printf("ID : %d\n\n",getpid()); + } + else if (pid == 0) + { + printf("Child process\n"); + // getpid() will return process id of child process + printf("ID: %d\n",getpid()); + // getppid() will return parent process id of child process + printf("Parent -ID: %d\n\n",getppid()); + + sleep(10); + + // At this time parent process has finished. + // So if u will check parent process id + // it will show different process id + printf("\nChild process \n"); + printf("ID: %d\n",getpid()); + printf("Parent -ID: %d\n",getppid()); + } + else + { + printf("Failed to create child process"); + } + + return 0; +} diff --git a/p for calculator b/p for calculator new file mode 100644 index 00000000..a8bd462a --- /dev/null +++ b/p for calculator @@ -0,0 +1,29 @@ +#include +int main() { + char operator; + double first, second; + printf("Enter an operator (+, -, *,): "); + scanf("%c", &operator); + printf("Enter two operands: "); + scanf("%lf %lf", &first, &second); + + switch (operator) { + case '+': + printf("%.1lf + %.1lf = %.1lf", first, second, first + second); + break; + case '-': + printf("%.1lf - %.1lf = %.1lf", first, second, first - second); + break; + case '*': + printf("%.1lf * %.1lf = %.1lf", first, second, first * second); + break; + case '/': + printf("%.1lf / %.1lf = %.1lf", first, second, first / second); + break; + // operator doesn't match any case constant + default: + printf("Error! operator is not correct"); + } + + return 0; +} diff --git a/palindrom b/palindrom new file mode 100644 index 00000000..cc065251 --- /dev/null +++ b/palindrom @@ -0,0 +1,18 @@ +#include +void main() +{ + int num,n,reverce=0,reminder; + printf("enter the number"); + scanf("%d",&num); + n=num; + while(n>0) + { + reminder=n%10; + reverce=(reverce*10)+reminder; + n=n/10; + } + if(num==reverce) + printf("palindrom"); + else + printf("not palindrome"); +} diff --git a/palindrome using while loop b/palindrome using while loop new file mode 100644 index 00000000..c18a0b80 --- /dev/null +++ b/palindrome using while loop @@ -0,0 +1,29 @@ +#include + +int main() +{ + int Number, Temp, Reminder, Reverse = 0; + + printf("\nPlease Enter any number to Check for Palindrome\n"); + scanf("%d", &Number); + + //Helps to prevent altering the original value + Temp = Number; + + while ( Temp > 0) + { + Reminder = Temp %10; + Reverse = Reverse *10+ Reminder; + Temp = Temp /10; + } + + printf("Reverse of entered number is = %d\n", Reverse); + + if ( Number == Reverse ) + printf("\n%d is Palindrome Number.\n", Number); + + else + printf("%d is not the Palindrome Number.\n", Number); + + return 0; +} diff --git a/palindromecheck b/palindromecheck new file mode 100644 index 00000000..2d0bd1c7 --- /dev/null +++ b/palindromecheck @@ -0,0 +1,18 @@ +int main() +{ +int n,r,sum=0,temp; +printf("enter the number="); +scanf("%d",&n); +temp=n; +while(n>0) +{ +r=n%10; +sum=(sum*10)+r; +n=n/10; +} +if(temp==sum) +printf("palindrome number "); +else +printf("not palindrome"); +return 0; +} diff --git a/pascaltriangle b/pascaltriangle new file mode 100644 index 00000000..f2764093 --- /dev/null +++ b/pascaltriangle @@ -0,0 +1,20 @@ + +#include +int main() { + int rows, coef = 1, space, i, j; + printf("Enter the number of rows: "); + scanf("%d", &rows); + for (i = 0; i < rows; i++) { + for (space = 1; space <= rows - i; space++) + printf(" "); + for (j = 0; j <= i; j++) { + if (j == 0 || i == 0) + coef = 1; + else + coef = coef * (i - j + 1) / j; + printf("%4d", coef); + } + printf("\n"); + } + return 0; +} diff --git a/passing address b/passing address new file mode 100644 index 00000000..ff18b664 --- /dev/null +++ b/passing address @@ -0,0 +1,22 @@ +#include +void swap(int *n1, int *n2); + +int main() +{ + int num1 = 5, num2 = 10; + + // address of num1 and num2 is passed + swap( &num1, &num2); + + printf("num1 = %d\n", num1); + printf("num2 = %d", num2); + return 0; +} + +void swap(int* n1, int* n2) +{ + int temp; + temp = *n1; + *n1 = *n2; + *n2 = temp; +} diff --git a/pattern programs b/pattern programs new file mode 100644 index 00000000..caad6b52 --- /dev/null +++ b/pattern programs @@ -0,0 +1,18 @@ +#include + +int main() +{ int i,j,n; +printf("enter n= "); +scanf("%d",&n); +for(i=1;i<=n;i++) +{ + for(j=1;j<=n;j++) + {if(i==j) + printf("\\"); + else if(i+j==n+1) + printf("/"); + else printf("*"); + } + printf("\n"); + +}} diff --git a/pattern1.c b/pattern1.c new file mode 100644 index 00000000..5dad2aa3 --- /dev/null +++ b/pattern1.c @@ -0,0 +1,27 @@ +#include +#include +int main() +{ +int n, x, y, k; +printf("Enter the number of rows to show number pattern: "); +scanf("%d",&n); +for(x =1; x <= n; x++) +{ +for(y =1; y <= n; y++) +{ +if(y <= x) +printf("%d",y); +else +printf(" "); +} +for(y = n; y >= 1;y--) +{ +if(y <= x) +printf("%d",y); +else +printf(" "); +} +printf("\n"); +} +return 0; +} diff --git a/polynomial_equation_solver b/polynomial_equation_solver new file mode 100644 index 00000000..67739849 --- /dev/null +++ b/polynomial_equation_solver @@ -0,0 +1,44 @@ +#include +#include + +float poly(float a[], int, float); + +int main() +{ + float x, a[10], y1; + int deg, i; + + printf("Enter the degree of polynomial equation: "); + scanf("%d", °); + + printf("Ehter the value of x for which the equation is to be evaluated: "); + scanf("%f", &x); + + for(i=0; i<=deg; i++) + { + printf("Enter the coefficient of x to the power %d: ",i); + scanf("%f",&a[i]); + } + + y1 = poly(a, deg, x); + + printf("The value of polynomial equation for the value of x = %.2f is: %.2f",x,y1); + + return 0; +} + +/* function for finding the value of polynomial at some value of x */ +float poly(float a[], int deg, float x) +{ + float p; + int i; + + p = a[deg]; + + for(i=deg;i>=1;i--) + { + p = (a[i-1] + x*p); + } + + return p; +} diff --git a/positiveInteger b/positiveInteger new file mode 100644 index 00000000..8e4c8798 --- /dev/null +++ b/positiveInteger @@ -0,0 +1,27 @@ +#include + +int exp(int,int); +int main() +{ + int a,b,s; + printf("Enter values for X and Y:"); + scanf("%d %d", &a,&b); + s=exp(a,b); + printf("%d",s); + return 0; +} +int exp(int x, int y){ + int res=1,a=x,b=y; + while(b!=0){ + if(b%2==0){ + a=a*a; + b=b/2; + } + else + { + res=res*a; + b=b-1; + } + } + return res; +} diff --git a/prime b/prime new file mode 100644 index 00000000..dbb53c1c --- /dev/null +++ b/prime @@ -0,0 +1,31 @@ +#include +int main() { + int n, i, flag = 0; + printf("Enter a positive integer: "); + scanf("%d", &n); + + for (i = 2; i <= n / 2; ++i) { + + // condition for non-prime + if (n % i == 0) { + flag = 1; + break; + } + } + + if (n == 1) { + printf("1 is neither prime nor composite."); + } + else { + if (flag == 0) + printf("%d is a prime number.", n); + else + printf("%d is not a prime number.", n); + } + + return 0; +} +Output + +Enter a positive integer: 29 +29 is a prime number. diff --git a/prime_or_not b/prime_or_not new file mode 100644 index 00000000..97eb4703 --- /dev/null +++ b/prime_or_not @@ -0,0 +1,33 @@ +#include + +int main(){ + int i,n,prime=1; + printf("Enter number"); + scanf("%d",&n); + + if(n==1){ + printf("1 is not a prime number"); + } + + else if(n==2){ + printf("2 is not a prime number"); + } + + else{ + for (i=2; i +using namespace std; +#define MAXN 100001 + +long long spf[MAXN]; +void sieve(){ + spf[1] = 1; + for(long long i=2;i getFactorization(long long x){ + vector ret; + while (x != 1){ + ret.push_back(spf[x]); + x = x / spf[x]; + } + return ret; +} + +int main(int argc, char const *argv[]){ + sieve(); + long long n{0}; + cin>>n; + vector p = getFactorization(n); + cout< +int main() { + int n, i, flag = 0; + printf("Enter a positive integer: "); + scanf("%d", &n); + + for (i = 2; i <= n / 2; ++i) { + + // condition for non-prime + if (n % i == 0) { + flag = 1; + break; + } + } + + if (n == 1) { + printf("1 is neither prime nor composite."); + } + else { + if (flag == 0) + printf("%d is a prime number.", n); + else + printf("%d is not a prime number.", n); + } + + return 0; +} +Output + +Enter a positive integer: 29 +29 is a prime number. diff --git a/primenumber1 b/primenumber1 new file mode 100644 index 00000000..b9e5215e --- /dev/null +++ b/primenumber1 @@ -0,0 +1,7 @@ +#include int main() +{ int n, i, flag = 0; printf("Enter a positive integer: "); +scanf("%d", &n); for (i = 2; i <= n / 2; ++i) { + // condition for non-prime if (n % i == 0) { flag = 1; break; } } +if (n == 1) { printf("1 is neither prime nor composite."); } +else { if (flag == 0) printf("%d is a prime number.", n); + else printf("%d is not a prime number.", n); } return 0; } diff --git a/print an integer b/print an integer new file mode 100644 index 00000000..907a4197 --- /dev/null +++ b/print an integer @@ -0,0 +1,14 @@ +#include +int main() { + int number; + + printf("Enter an integer: "); + + // reads and stores input + scanf("%d", &number); + + // displays output + printf("You entered: %d", number); + + return 0; +} diff --git a/print rainbow b/print rainbow new file mode 100644 index 00000000..12f35a5a --- /dev/null +++ b/print rainbow @@ -0,0 +1,21 @@ +#include +#include +#include +#include +void main() +{ +int gdriver = DETECT,gmode; +int x,y,i; +initgraph(&gdriver,&gmode,”C:\\Turboc3\\BGI”); +x=getmaxx()/2; +y=getmaxy()/2; +for(i=30;i<200;i++) +{ +delay(100); +setcolor(i/10); +arc(x,y,0,180,i-10); +} +getch(); +} + +//please sir accept my pull. diff --git a/printing pattern b/printing pattern new file mode 100644 index 00000000..70a1a944 --- /dev/null +++ b/printing pattern @@ -0,0 +1,13 @@ +#include +int main() { + int i, j, rows; + printf("Enter the number of rows: "); + scanf("%d", &rows); + for (i = 1; i <= rows; ++i) { + for (j = 1; j <= i; ++j) { + printf("* "); + } + printf("\n"); + } + return 0; +} diff --git a/product b/product new file mode 100644 index 00000000..9ddd14a1 --- /dev/null +++ b/product @@ -0,0 +1,16 @@ +Program to Multiply Two Numbers + +#include +int main() { + double a, b, product; + printf("Enter two numbers: "); + scanf("%lf %lf", &a, &b); + + + product = a * b; + + + printf("Product = %.2lf", product); + + return 0; +} diff --git a/prog.c b/prog.c new file mode 100644 index 00000000..5a1262e1 --- /dev/null +++ b/prog.c @@ -0,0 +1,19 @@ +#include +int main(){ +int n,i,m=0,flag=0; +printf("Enter the number to check prime:"); +scanf("%d",&n); +m=n/2; +for(i=2;i<=m;i++) +{ +if(n%i==0) +{ +printf("Number is not prime"); +flag=1; +break; +} +} +if(flag==0) +printf("Number is prime"); +return 0; + } diff --git a/program to check leap year b/program to check leap year new file mode 100644 index 00000000..f15b3d3e --- /dev/null +++ b/program to check leap year @@ -0,0 +1,22 @@ +#include +int main() { + int year; + printf("Enter a year: "); + scanf("%d", &year); + + // leap year if perfectly visible by 400 + if (year % 400 == 0) { + printf("%d is a leap year.", year); + } + else if (year % 100 == 0) { + printf("%d is not a leap year.", year); + } + else if (year % 4 == 0) { + printf("%d is a leap year.", year); + } + else { + printf("%d is not a leap year.", year); + } + + return 0; +} diff --git a/program to check vowel using switch case b/program to check vowel using switch case new file mode 100644 index 00000000..7516a44f --- /dev/null +++ b/program to check vowel using switch case @@ -0,0 +1,30 @@ +#include + +int main() +{ + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + + char ch; + printf("Input a Character : "); + scanf("%c", &ch); + + switch(ch) + { + case 'a': + case 'A': + case 'e': + case 'E': + case 'i': + case 'I': + case 'o': + case 'O': + case 'u': + case 'U': + printf("\n\n%c is a vowel.\n\n", ch); + break; + default: + printf("%c is not a vowel.\n\n", ch); + } + printf("\n\n\t\t\tCoding is Fun !\n\n\n"); + return 0; +} diff --git a/program to find out the greatest number among four numbers and also it should show whether there are two or three equal numbers b/program to find out the greatest number among four numbers and also it should show whether there are two or three equal numbers new file mode 100644 index 00000000..18327fe7 --- /dev/null +++ b/program to find out the greatest number among four numbers and also it should show whether there are two or three equal numbers @@ -0,0 +1,65 @@ + ++1 + +#include +using namespace std; + +// main function +int main(){ + + //create object and read the value and store it + double a,b,c,d; + cout<<"Enter number A: "; + cin>>a; + cout<<"Enter number B: "; + cin>>b; + cout<<"Enter number C: "; + cin>>c; + cout<<"Enter number D: "; + cin>>d; + cout< b && a > c && a > d) + { + cout<<"A is the greatest = "<a && b>c && b>d) + {cout<<"B is the greatest = "<a && c>b && c>d) + {cout<<"C is the greatest = "<a && d>b && d>c) + {cout<<"D is the greatest = "<b) + big=a; + else + big=b; + printf("\nBiggest of the two number is: %d", big); + getch(); + return 0; +} diff --git a/program to print integer b/program to print integer new file mode 100644 index 00000000..907a4197 --- /dev/null +++ b/program to print integer @@ -0,0 +1,14 @@ +#include +int main() { + int number; + + printf("Enter an integer: "); + + // reads and stores input + scanf("%d", &number); + + // displays output + printf("You entered: %d", number); + + return 0; +} diff --git a/qcircle.c b/qcircle.c new file mode 100644 index 00000000..f3ba40d5 --- /dev/null +++ b/qcircle.c @@ -0,0 +1,96 @@ +#include +#define max 10 +int queue[max]; +int front, rear; +void createQueue() +{ + front = rear = -1; +} +void insert() +{ + int num; + printf("Please enter the number to be inserted :\n"); + scanf("%d", &num); + if((front==0 && rear==max-1) || (front == rear+1)) + printf("Queue is FULL\n"); + else + { + if((front == -1) && (rear == -1)) + front = rear = 0; + else if((rear == max-1) && (front != -1)) + rear = 0; + else + rear++; + queue[rear] = num; + } +} +void DELETE() +{ + if((front == -1) && (rear == -1)) + printf("Queue is EMPTY\n"); + else + { + printf("The deleted element is %d\n", queue[front]); + if(front == rear) + front = rear = -1; + else if(front == max-1) + { + front = 0; + } + else + front++; + } +} +void peek() +{ + if((front == -1) && (rear == -1)) + printf("Queue is EMPTY\n"); + else + printf("The element at the top is %d\n", queue[front]); +} +void display() +{ + int i; + if((front == -1) && (rear == -1)) + printf("Queue is EMPTY\n"); + else if(front < rear) + { + for(i=front; i<=rear; i++) + printf("%d\t", queue[i]); + printf("\n"); + } + else + { + for(i=front; i<=(max-1); i++) + printf("%d\t", queue[i]); + for(i=0; i<=rear; i++) + printf("%d\t", queue[i]); + printf("\n"); + } +} +void main() +{ + createQueue(); + int ch; + do + { + printf("\nPlease enter the choice\n"); + printf("1: INSERT\n2: DELETE\n3: PEEK\n4: DISPALY\n5: QUIT\n"); + scanf("%d", &ch); + switch(ch) + { + case 1: + insert(); + break; + case 2: + DELETE(); + break; + case 3: + peek(); + break; + case 4: + display(); + break; + } + }while(ch != 5); +} diff --git a/qlinear.c b/qlinear.c new file mode 100644 index 00000000..8233c064 --- /dev/null +++ b/qlinear.c @@ -0,0 +1,75 @@ +#include +#define max 10 +int queue[max]; +int front, rear; +void createQueue() +{ + front = rear = -1; +} +void insert() +{ + int num; + printf("Please enter the number to be inserted :\n"); + scanf("%d", &num); + if(rear == max-1) + printf("Queue is FULL\n"); + else if((front == -1) && (rear == -1)) + front = rear = 0; + else + rear++; + queue[rear] = num; +} +void DELETE() +{ + if((front == -1) || (front > rear)) + printf("Queue is EMPTY\n"); + else + { + printf("The deleted element is %d\n", queue[front]); + front++; + } +} +void peek() +{ + if((front == -1) || (front > rear)) + printf("Queue is EMPTY\n"); + else + printf("The element at the top is %d\n", queue[front]); +} +void display() +{ + if((front == -1)||(front > rear)) + printf("Queue is EMPTY\n"); + else + { + int i; + for(i=front; i<=rear; i++) + printf("%d\t", queue[i]); + } +} +void main() +{ + createQueue(); + int ch; + do + { + printf("\nPlease enter the choice\n"); + printf("1: INSERT\n2: DELETE\n3: PEEK\n4: DISPALY\n5: QUIT\n"); + scanf("%d", &ch); + switch(ch) + { + case 1: + insert(); + break; + case 2: + DELETE(); + break; + case 3: + peek(); + break; + case 4: + display(); + break; + } + }while(ch != 5); +} diff --git a/quadilateral formula b/quadilateral formula new file mode 100644 index 00000000..27d84d29 --- /dev/null +++ b/quadilateral formula @@ -0,0 +1,144 @@ +// C++ implementation of above approach +#include +using namespace std; + +struct Point // points +{ + int x; + int y; +}; + +// determines the orientation of points +int orientation(Point p, Point q, Point r) +{ + int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y); + + if (val == 0) + return 0; + return (val > 0) ? 1 : 2; +} + +// check whether the distinct line segments intersect +bool doIntersect(Point p1, Point q1, Point p2, Point q2) +{ + int o1 = orientation(p1, q1, p2); + int o2 = orientation(p1, q1, q2); + int o3 = orientation(p2, q2, p1); + int o4 = orientation(p2, q2, q1); + + if (o1 != o2 && o3 != o4) + return true; + + return false; +} + +// check if points overlap(similar) +bool similar(Point p1, Point p2) +{ + + // it is same, we are returning false because + // quadrilateral is not possible in this case + if (p1.x == p2.x && p1.y == p2.y) + return false; + + // it is not same, So there is a + // possibility of a quadrilateral + return true; +} + +// check for collinearity +bool collinear(Point p1, Point p2, Point p3) +{ + int x1 = p1.x, y1 = p1.y; + int x2 = p2.x, y2 = p2.y; + int x3 = p3.x, y3 = p3.y; + + // it is collinear, we are returning false + // because quadrilateral is not possible in this case + if ((y3 - y2) * (x2 - x1) == (y2 - y1) * (x3 - x2)) + return false; + + // it is not collinear, So there + // is a possibility of a quadrilateral + else + return true; +} + +int no_of_quads(Point p1, Point p2, Point p3, Point p4) +{ + // ** Checking for cases where no quadrilateral = 0 ** + + // check if any of the points are same + bool same = true; + same = same & similar(p1, p2); + same = same & similar(p1, p3); + same = same & similar(p1, p4); + same = same & similar(p2, p3); + same = same & similar(p2, p4); + same = same & similar(p3, p4); + + // similar points exist + if (same == false) + return 0; + + // check for collinearity + bool coll = true; + coll = coll & collinear(p1, p2, p3); + coll = coll & collinear(p1, p2, p4); + coll = coll & collinear(p1, p3, p4); + coll = coll & collinear(p2, p3, p4); + + // points are collinear + if (coll == false) + return 0; + + //** Checking for cases where no of quadrilaterals= 1 or 3 ** + + int check = 0; + + if (doIntersect(p1, p2, p3, p4)) + check = 1; + if (doIntersect(p1, p3, p2, p4)) + check = 1; + if (doIntersect(p1, p2, p4, p3)) + check = 1; + + if (check == 0) + return 3; + return 1; +} + +// Driver code +int main() +{ + struct Point p1, p2, p3, p4; + // A =(0, 9), B = (-1, 0), C = (5, -1), D=(5, 9) + p1.x = 0, p1.y = 9; + p2.x = -1, p2.y = 0; + p3.x = 5, p3.y = -1; + p4.x = 5, p4.y = 9; + cout << no_of_quads(p1, p2, p3, p4) << endl; + + // A=(0, 9), B=(-1, 0), C=(5, -1), D=(0, 3) + p1.x = 0, p1.y = 9; + p2.x = -1, p2.y = 0; + p3.x = 5, p3.y = -1; + p4.x = 0, p4.y = 3; + cout << no_of_quads(p1, p2, p3, p4) << endl; + + // A=(0, 9), B=(0, 10), C=(0, 11), D=(0, 12) + p1.x = 0, p1.y = 9; + p2.x = 0, p2.y = 10; + p3.x = 0, p3.y = 11; + p4.x = 0, p4.y = 12; + cout << no_of_quads(p1, p2, p3, p4) << endl; + + // A=(0, 9), B=(0, 9), C=(5, -1), D=(0, 3) + p1.x = 0, p1.y = 9; + p2.x = 0, p2.y = 9; + p3.x = 5, p3.y = -1; + p4.x = 0, p4.y = 3; + cout << no_of_quads(p1, p2, p3, p4) << endl; + + return 0; +} diff --git a/quickSort b/quickSort new file mode 100644 index 00000000..6b80d2a3 --- /dev/null +++ b/quickSort @@ -0,0 +1,83 @@ +Live Demo +#include +#include + +#define MAX 7 + +int intArray[MAX] = {4,6,3,2,1,9,7}; + +void printline(int count) { + int i; + + for(i = 0;i < count-1;i++) { + printf("="); + } + + printf("=\n"); +} + +void display() { + int i; + printf("["); + + // navigate through all items + for(i = 0;i < MAX;i++) { + printf("%d ",intArray[i]); + } + + printf("]\n"); +} + +void swap(int num1, int num2) { + int temp = intArray[num1]; + intArray[num1] = intArray[num2]; + intArray[num2] = temp; +} + +int partition(int left, int right, int pivot) { + int leftPointer = left -1; + int rightPointer = right; + + while(true) { + while(intArray[++leftPointer] < pivot) { + //do nothing + } + + while(rightPointer > 0 && intArray[--rightPointer] > pivot) { + //do nothing + } + + if(leftPointer >= rightPointer) { + break; + } else { + printf(" item swapped :%d,%d\n", intArray[leftPointer],intArray[rightPointer]); + swap(leftPointer,rightPointer); + } + } + + printf(" pivot swapped :%d,%d\n", intArray[leftPointer],intArray[right]); + swap(leftPointer,right); + printf("Updated Array: "); + display(); + return leftPointer; +} + +void quickSort(int left, int right) { + if(right-left <= 0) { + return; + } else { + int pivot = intArray[right]; + int partitionPoint = partition(left, right, pivot); + quickSort(left,partitionPoint-1); + quickSort(partitionPoint+1,right); + } +} + +int main() { + printf("Input Array: "); + display(); + printline(50); + quickSort(0,MAX-1); + printf("Output Array: "); + display(); + printline(50); diff --git a/quick_sort b/quick_sort new file mode 100644 index 00000000..a27ad809 --- /dev/null +++ b/quick_sort @@ -0,0 +1,65 @@ +#include +#define size 10 +int a[size]; + +int partition(int beg, int end) +{ + int left, right, temp, loc, flag; + loc = left = beg; + right = end; + flag = 0; + while(flag != 1) + { + while((a[loc] <= a[right]) && (loc != right)) + right--; + if(loc == right) + flag = 1; + else if(a[loc] > a[right]) + { + temp = a[loc]; + a[loc] = a[right]; + a[right] = temp; + loc = right; + } + if(flag != 1) + { + while((a[loc] >= a[left]) && (loc != left)) + left--; + if(loc == left) + flag = 1; + else if(a[loc] < a[left]) + { + temp = a[loc]; + a[loc] = a[left]; + a[left] = temp; + loc = left; + } + } + } + return loc; +} + +void quick_sort(int beg, int end) +{ + int loc; + if(beg < end) + { + loc = partition(beg, end); + quick_sort(beg, loc-1); + quick_sort(loc+1, end); + } +} + +void main() +{ + int i, n; + printf("Plesae enter the total number of elements :\n"); + scanf("%d",&n); + printf("Please enter the elements of the array : \n"); + for(i=0; i +void swap(int* a, int* b) +{ + int t = *a; + *a = *b; + *b = t; +} +int partition (int arr[], int low, int high) +{ + int pivot = arr[high]; + int i = (low - 1); + + for (int j = low; j <= high- 1; j++) + { + if (arr[j] < pivot) + { + i++; + swap(&arr[i], &arr[j]); + } + } + swap(&arr[i + 1], &arr[high]); + return (i + 1); +} +void quickSort(int arr[], int low, int high) +{ + if (low < high) + { + int pi = partition(arr, low, high); + quickSort(arr, low, pi - 1); + quickSort(arr, pi + 1, high); + } +} +void printArray(int arr[], int size) +{ + int i; + for (i=0; i < size; i++) + printf("%d ", arr[i]); + printf("\n"); +} +int main() +{ + int n; + scanf("%d",&n); + int arr[n]; + for (int i=0;i +int main() { + int n, rev = 0, remainder; + printf("Enter an integer: "); + scanf("%d", &n); + while (n != 0) { + remainder = n % 10; + rev = rev * 10 + remainder; + n /= 10; + } + printf("Reversed number = %d", rev); + return 0; +} diff --git a/reverse_a_string b/reverse_a_string new file mode 100644 index 00000000..cce9f939 --- /dev/null +++ b/reverse_a_string @@ -0,0 +1,15 @@ +#include +#include +int main() +{ + char s[100]; + + printf("Enter a string to reverse\n"); + gets(s); + + strrev(s); + + printf("Reverse of the string: %s\n", s); + + return 0; +} diff --git a/reverse_digits b/reverse_digits new file mode 100644 index 00000000..57d7d952 --- /dev/null +++ b/reverse_digits @@ -0,0 +1,23 @@ +#include + +/* Iterative function to reverse digits of num*/ +int reversDigits(int num) +{ + int rev_num = 0; + while(num > 0) + { + rev_num = rev_num*10 + num%10; + num = num/10; + } + return rev_num; +} + +/*Driver program to test reversDigits*/ +int main() +{ + int num = 3217; + printf("Reverse of no. is %d", reversDigits(num)); + + getchar(); + return 0; +} diff --git a/reversestr b/reversestr new file mode 100644 index 00000000..32d4267f --- /dev/null +++ b/reversestr @@ -0,0 +1,16 @@ +#include +#include +int main() +{ + char s[100]; + + printf("Enter a string to reverse\n"); + gets(s); + + strrev(s); + + printf("Reverse of the string: %s\n", s); + + return 0; +} + diff --git a/reversing of string b/reversing of string new file mode 100644 index 00000000..7c6bb8fd --- /dev/null +++ b/reversing of string @@ -0,0 +1,27 @@ +#include +int main() +{ + char s[1000], r[1000]; + int begin, end, count = 0; + + printf("Input a string\n"); + gets(s); + + // Calculating string length + + while (s[count] != '\0') + count++; + + end = count - 1; + + for (begin = 0; begin < count; begin++) { + r[begin] = s[end]; + end--; + } + + r[begin] = '\0'; + + printf("%s\n", r); + + return 0; +} diff --git a/sachdevakh b/sachdevakh new file mode 100644 index 00000000..21dbe0ff --- /dev/null +++ b/sachdevakh @@ -0,0 +1,18 @@ +// C program to find the greatest of two numbers + +#include +int main() +{ +//Fill the code +int num1, num2; +scanf(“%d %d”,&num1,&num2); +if(num1 > num2) +{ +printf(“%d is greater”,num1); +} +else +{ +printf(“%d is greater”,num2); +} +return 0; +} diff --git a/selectionsort b/selectionsort new file mode 100644 index 00000000..14193c04 --- /dev/null +++ b/selectionsort @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +int main() { + + int n,i,j,min,c=0; + scanf("%d",&n); + int a[n]; + + for(i=1;i<=n;i++) + scanf("%d",&a[i]); + + for(i=1;i<=n-1;i++) + { + min = i; + for(j=i+1;j<=n;j++) + { + c++; + if (a[j] < a[min]) + { + min = j; + } + } + if(min!=i) + { + int temp=a[i]; + a[i]=a[min]; + a[min]=temp; + } + } + + for(i=1;i<=n;i++) + printf("%d ",a[i]); + printf("\n%d\n%d",c,n-1); + + return 0; +} diff --git a/sentence to a file b/sentence to a file new file mode 100644 index 00000000..76be8c17 --- /dev/null +++ b/sentence to a file @@ -0,0 +1,23 @@ +#include +#include + +int main() { + char sentence[1000]; + + // creating file pointer to work with files + FILE *fptr; + + // opening file in writing mode + fptr = fopen("program.txt", "w"); + + // exiting program + if (fptr == NULL) { + printf("Error!"); + exit(1); + } + printf("Enter a sentence:\n"); + fgets(sentence, sizeof(sentence), stdin); + fprintf(fptr, "%s", sentence); + fclose(fptr); + return 0; +} diff --git a/shutdown_using_c b/shutdown_using_c new file mode 100644 index 00000000..d8b95254 --- /dev/null +++ b/shutdown_using_c @@ -0,0 +1,11 @@ +// C program to shutdown in Linux +#include +#include + +int main() +{ + // Running Linux OS command using system + system("shutdown -P now"); + + return 0; +} diff --git a/simple game b/simple game new file mode 100644 index 00000000..d6a83acc --- /dev/null +++ b/simple game @@ -0,0 +1,208 @@ +#include +using namespace std; + +#define COMPUTER 1 +#define HUMAN 2 + +#define SIDE 3 // Length of the board + +// Computer will move with 'O' +// and human with 'X' +#define COMPUTERMOVE 'O' +#define HUMANMOVE 'X' + +// A function to show the current board status +void showBoard(char board[][SIDE]) +{ + printf("\n\n"); + + printf("\t\t\t %c | %c | %c \n", board[0][0], + board[0][1], board[0][2]); + printf("\t\t\t--------------\n"); + printf("\t\t\t %c | %c | %c \n", board[1][0], + board[1][1], board[1][2]); + printf("\t\t\t--------------\n"); + printf("\t\t\t %c | %c | %c \n\n", board[2][0], + board[2][1], board[2][2]); + + return; +} + +// A function to show the instructions +void showInstructions() +{ + printf("\t\t\t Tic-Tac-Toe\n\n"); + printf("Choose a cell numbered from 1 to 9 as below" + " and play\n\n"); + + printf("\t\t\t 1 | 2 | 3 \n"); + printf("\t\t\t--------------\n"); + printf("\t\t\t 4 | 5 | 6 \n"); + printf("\t\t\t--------------\n"); + printf("\t\t\t 7 | 8 | 9 \n\n"); + + printf("-\t-\t-\t-\t-\t-\t-\t-\t-\t-\n\n"); + + return; +} + + +// A function to initialise the game +void initialise(char board[][SIDE], int moves[]) +{ + // Initiate the random number generator so that + // the same configuration doesn't arises + srand(time(NULL)); + + // Initially the board is empty + for (int i=0; i +#include +void main() +{ + int p,n,r,si; + clrscr(); + printf("Enter Principle, Rate of interest & Time :"); + scanf("%d%d%d",&p,&r,&n); + si=(p*r*n)/100; + printf("Simple Interest is :%d",si); + getch(); +} diff --git a/sine_series b/sine_series new file mode 100644 index 00000000..6bf5d1dc --- /dev/null +++ b/sine_series @@ -0,0 +1,29 @@ +#include + +int main() +{ + int i, j, n, fact, sign = - 1; + float x, p, sum = 0; + + printf("Enter the value of x : "); + scanf("%f", &x); + printf("Enter the value of n : "); + scanf("%d", &n); + + for (i = 1; i <= n; i += 2) + { + p = 1; + fact = 1; + for (j = 1; j <= i; j++) + { + p = p * x; + fact = fact * j; + } + sign = - 1 * sign; + sum += sign * p / fact; + } + + printf("sin %0.2f = %f", x, sum); + + return 0; +} diff --git a/sleep_sort b/sleep_sort new file mode 100644 index 00000000..fb23ad86 --- /dev/null +++ b/sleep_sort @@ -0,0 +1,40 @@ +#include +#include +#include +void routine(void *a) +{ + int n = *(int *) a; // typecasting from void to int + + // Sleeping time is proportional to the number + // More precisely this thread sleep for 'n' milliseconds + Sleep(n); + + // After the sleep, print the number + printf("%d ", n); +} +void sleepSort(int arr[], int n) +{ + // An array of threads, one for each of the elements + // in the input array + HANDLE threads[n]; + + // Create the threads for each of the input array elements + for (int i = 0; i < n; i++) + threads[i] = (HANDLE)_beginthread(&routine, 0, &arr[i]); + + // Process these threads + WaitForMultipleObjects(n, threads, TRUE, INFINITE); + return; +} + +// Driver program to test above functions +int main() +{ + // Doesn't work for negative numbers + int arr[] = {34, 23, 122, 9}; + int n = sizeof(arr) / sizeof(arr[0]); + + sleepSort (arr, n); + + return(0); +} diff --git a/smallarge b/smallarge new file mode 100644 index 00000000..91f194d0 --- /dev/null +++ b/smallarge @@ -0,0 +1,46 @@ +#include + +int main() +{ + printf("\n\n\t\tStudytonight - Best place to learn\n\n\n"); + int a[50], size, i, big, small; + + printf("\nEnter the size of the array: "); + scanf("%d", &size); + + printf("\n\nEnter the %d elements of the array: \n\n", size); + for(i = 0; i < size; i++) + scanf("%d", &a[i]); + + big = a[0]; // initializing + /* + from 2nd element to the last element + find the bigger element than big and + update the value of big + */ + for(i = 1; i < size; i++) + { + if(big < a[i]) // if larger value is encountered + { + big = a[i]; // update the value of big + } + } + printf("\n\nThe largest element is: %d", big); + + small = a[0]; // initializing + /* + from 2nd element to the last element + find the smaller element than small and + update the value of small + */ + for(i = 1; i < size; i++) + { + if(small>a[i]) // if smaller value is encountered + { + small = a[i]; // update the value of small + } + } + printf("\n\nThe smallest element is: %d", small); + printf("\n\n\t\t\tCoding is Fun !\n\n\n"); + return 0; +} diff --git a/sorting of arrays b/sorting of arrays new file mode 100644 index 00000000..c9b39e85 --- /dev/null +++ b/sorting of arrays @@ -0,0 +1,52 @@ +#include + +void swap(int* xp, int* yp) +{ + int temp = *xp; + *xp = *yp; + *yp = temp; +} + +// Function to perform Selection Sort +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 + swap(&arr[min_idx], &arr[i]); + } +} + +// Function to print an array +void printArray(int arr[], int size) +{ + int i; + for (i = 0; i < size; i++) + printf("%d ", arr[i]); + printf("\n"); +} + +// Driver code +int main() +{ + int arr[] = { 0, 23, 14, 12, 9 }; + int n = sizeof(arr) / sizeof(arr[0]); + printf("Original array: \n"); + printArray(arr, n); + + selectionSort(arr, n); + printf("\nSorted array in Ascending order: \n"); + printArray(arr, n); + + return 0; +} diff --git a/square of a number b/square of a number new file mode 100644 index 00000000..6a11d55d --- /dev/null +++ b/square of a number @@ -0,0 +1,17 @@ +// by ankit pal +#include +#include + +int main() +{ + int number, Square; + + printf(" \n Please Enter any integer Value : "); + scanf("%d", &number); + + Square = number * number; + + printf("\n Square of a given number %d is = %d", number, Square); + + return 0; +} diff --git a/squarepogram b/squarepogram new file mode 100644 index 00000000..8cad1a4f --- /dev/null +++ b/squarepogram @@ -0,0 +1,19 @@ +#include + +double square(double num) +{ + return (num * num); +} +int main() +{ + int num; + double n; + printf("\n\n Function : find square of any number :\n"); + printf("------------------------------------------------\n"); + + printf("Input any number for square : "); + scanf("%d", &num); + n = square(num); + printf("The square of %d is : %.2f\n", num, n); + return 0; +} diff --git a/stack pop up program b/stack pop up program new file mode 100644 index 00000000..2d17f0a9 --- /dev/null +++ b/stack pop up program @@ -0,0 +1,82 @@ +#include +#include +#include + +#define MAX 5 //Maximum number of elements that can be stored + +int top=-1,stack[MAX]; +void push(); +void pop(); +void display(); + +void main() +{ + int ch; + + while(1) //infinite loop, will end when choice will be 4 + { + printf("\n*** Stack Menu ***"); + printf("\n\n1.Push\n2.Pop\n3.Display\n4.Exit"); + printf("\n\nEnter your choice(1-4):"); + scanf("%d",&ch); + + switch(ch) + { + case 1: push(); + break; + case 2: pop(); + break; + case 3: display(); + break; + case 4: exit(0); + + default: printf("\nWrong Choice!!"); + } + } +} + +void push() +{ + int val; + + if(top==MAX-1) + { + printf("\nStack is full!!"); + } + else + { + printf("\nEnter element to push:"); + scanf("%d",&val); + top=top+1; + stack[top]=val; + } +} + +void pop() +{ + if(top==-1) + { + printf("\nStack is empty!!"); + } + else + { + printf("\nDeleted element is %d",stack[top]); + top=top-1; + } +} + +void display() +{ + int i; + + if(top==-1) + { + printf("\nStack is empty!!"); + } + else + { + printf("\nStack is...\n"); + for(i=top;i>=0;--i) + printf("%d\n",stack[i]); + } +} diff --git a/stack,c b/stack,c new file mode 100644 index 00000000..d74fe973 --- /dev/null +++ b/stack,c @@ -0,0 +1,73 @@ +// C program for array implementation of stack +#include +#include +#include + +// A structure to represent a stack +struct Stack { + int top; + unsigned capacity; + int* array; +}; + +// function to create a stack of given capacity. It initializes size of +// stack as 0 +struct Stack* createStack(unsigned capacity) +{ + struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack)); + stack->capacity = capacity; + stack->top = -1; + stack->array = (int*)malloc(stack->capacity * sizeof(int)); + return stack; +} + +// Stack is full when top is equal to the last index +int isFull(struct Stack* stack) +{ + return stack->top == stack->capacity - 1; +} + +// Stack is empty when top is equal to -1 +int isEmpty(struct Stack* stack) +{ + return stack->top == -1; +} + +// Function to add an item to stack. It increases top by 1 +void push(struct Stack* stack, int item) +{ + if (isFull(stack)) + return; + stack->array[++stack->top] = item; + printf("%d pushed to stack\n", item); +} + +// Function to remove an item from stack. It decreases top by 1 +int pop(struct Stack* stack) +{ + if (isEmpty(stack)) + return INT_MIN; + return stack->array[stack->top--]; +} + +// Function to return the top from stack without removing it +int peek(struct Stack* stack) +{ + if (isEmpty(stack)) + return INT_MIN; + return stack->array[stack->top]; +} + +// Driver program to test above functions +int main() +{ + struct Stack* stack = createStack(100); + + push(stack, 10); + push(stack, 20); + push(stack, 30); + + printf("%d popped from stack\n", pop(stack)); + + return 0; +} diff --git a/stack.c b/stack.c new file mode 100644 index 00000000..300c9362 --- /dev/null +++ b/stack.c @@ -0,0 +1,71 @@ +#include + +int MAXSIZE = 8; +int stack[8]; +int top = -1; + +int isempty() { + + if(top == -1) + return 1; + else + return 0; +} + +int isfull() { + + if(top == MAXSIZE) + return 1; + else + return 0; +} + +int peek() { + return stack[top]; +} + +int pop() { + int data; + + if(!isempty()) { + data = stack[top]; + top = top - 1; + return data; + } else { + printf("Could not retrieve data, Stack is empty.\n"); + } +} + +int push(int data) { + + if(!isfull()) { + top = top + 1; + stack[top] = data; + } else { + printf("Could not insert data, Stack is full.\n"); + } +} + +int main() { + // push items on to the stack + push(5); + push(90); + push(12); + push(3); + push(1); + push(0); + + printf("Element at top of the stack: %d\n" ,peek()); + printf("Elements: \n"); + + // print stack data + while(!isempty()) { + int data = pop(); + printf("%d\n",data); + } + + printf("Stack full: %s\n" , isfull()?"true":"false"); //checking stack is full or not + printf("Stack empty: %s\n" , isempty()?"true":"false"); //checking stack is empty or not + + return 0; +} diff --git a/stack_implementation.c b/stack_implementation.c new file mode 100644 index 00000000..34fa4ca6 --- /dev/null +++ b/stack_implementation.c @@ -0,0 +1,125 @@ +//Stack implementation using static array + +#include +#define CAPACITY 5 //Pre-processor macro and change the size if you want +int stack[CAPACITY], top=-1 ; + +void push(int); +int pop(void); +int isFull(void); +int isEmpty(void); +void traverse(void); +void peek(void); + +void main() +{ + char ch; + long int item; + while(1) + { + printf("\n ---------------------- \n 1.Push an element \n 2.PoP element\n 3.Peek \n 4.Traverse\n 5.Quit \n ---------------------- \n"); + printf("Enter your choice:"); + scanf("%d",&ch); + + switch(ch) + { + case 1: + printf("\nEnter element to push:"); + scanf("%d",&item); + push(item); + break; + case 2: item = pop(); + if(item==0) + { + printf("Stack is underflow\n"); + } + else + { + printf("Popped item: %d\n",item); + } + break; + case 3: peek(); + break; + case 4: traverse(); + break; + case 5: exit(0); + default: printf("Invalid input\n\n"); + break; + } + } + +} +int isFull() +{ + if(top== CAPACITY-1) + { + return 1; + } + else + { + return 0; + } +} +int isEmpty() +{ + if(top==-1) + { + return 1; + } + else + { + return 0; + } +} +void push(int ele) +{ + if(isFull()) + { + printf("\n Stack is overflow...\n"); + } + else + { + top++; + stack[top] = ele; + printf("\n %d pushed into stack \n", ele); + } +} + +int pop() +{ + if(isEmpty()) + { + return 0; + } + else + { + return stack[top--]; + } +} +void peek() +{ + if(isEmpty()) + { + printf("\nStack is empty \n"); + } + else + { + printf("\n Peek element: %d \n",stack[top]); + } +} +void traverse() +{ + if(isEmpty()) + { + printf("\n Stack is empty \n"); + } + else + { + int i; + printf("\n Stack elements : \n"); + for(i=0;i<=top;i++) + { + printf("%d\n________________ \n",stack[i]); + } + } +} diff --git a/standard deviation b/standard deviation new file mode 100644 index 00000000..73cf1ed9 --- /dev/null +++ b/standard deviation @@ -0,0 +1,24 @@ +#include +#include +float calculateSD(float data[]); +int main() { + int i; + float data[10]; + printf("Enter 10 elements: "); + for (i = 0; i < 10; ++i) + scanf("%f", &data[i]); + printf("\nStandard Deviation = %.6f", calculateSD(data)); + return 0; +} + +float calculateSD(float data[]) { + float sum = 0.0, mean, SD = 0.0; + int i; + for (i = 0; i < 10; ++i) { + sum += data[i]; + } + mean = sum / 10; + for (i = 0; i < 10; ++i) + SD += pow(data[i] - mean, 2); + return sqrt(SD / 10); +} diff --git a/strassen's matrix multiplication b/strassen's matrix multiplication new file mode 100644 index 00000000..6d1e0e4e --- /dev/null +++ b/strassen's matrix multiplication @@ -0,0 +1,53 @@ +#include +#include +#include +int main() +{ + int a[10][10],b[10][10],c[10][10],i,j; + int m1,m2,m3,m4,m5,m6,m7,n; + clock_t st,et; + double ts; + printf("Enter the size of two matrices: "); + scanf("%d",&n); + for(i=0;i +#include + int main() + { + int i,l; + char s[101]; + scanf("%s",s); + l=strlen(s); + for(i=0; i + +int main() +{ + int a,b,sub; + + //Read value of a + printf("Enter the first no.: "); + scanf("%d",&a); + + //Read value of b + printf("Enter the second no.: "); + scanf("%d",&b); + + //formula of subtraction + sub= a-b; + printf("subtract is = %d\n", sub); + + return 0; +} diff --git a/subtract b/subtract new file mode 100644 index 00000000..135a5f94 --- /dev/null +++ b/subtract @@ -0,0 +1,20 @@ +#include + +int main() +{ + int a,b,sub; + + //Read value of a + printf("Enter the first no.: "); + scanf("%d",&a); + + //Read value of b + printf("Enter the second no.: "); + scanf("%d",&b); + + //formula of subtraction + sub= a-b; + printf("subtract is = %d\n", sub); + + return 0; +} diff --git a/sum of two no. b/sum of two no. new file mode 100644 index 00000000..18f9f81d --- /dev/null +++ b/sum of two no. @@ -0,0 +1,14 @@ +#include +int main() { + + int number1, number2, sum; + + printf("Enter two integers: "); + scanf("%d %d", &number1, &number2); + + // calculating sum + sum = number1 + number2; + + printf("%d + %d = %d", number1, number2, sum); + return 0; +} diff --git a/sumno b/sumno new file mode 100644 index 00000000..05f60192 --- /dev/null +++ b/sumno @@ -0,0 +1,14 @@ +#include + +int main() +{ + int a = 4; + float b = 8.5; + int c = 6; + + printf("the value of a is %d \n", a); + printf("the value of b is %f \n", b); + printf("sum of a and b is %f" , a + b); + + return 0; +} diff --git a/swapping of two numbers b/swapping of two numbers new file mode 100644 index 00000000..0bed3c7f --- /dev/null +++ b/swapping of two numbers @@ -0,0 +1,19 @@ + +// C program to swap two variables +#include + +int main() +{ + int x, y; + printf("Enter Value of x "); + scanf("%d", &x); + printf("\nEnter Value of y "); + scanf("%d", &y); + + int temp = x; + x = y; + y = temp; + + printf("\nAfter Swapping: x = %d, y = %d", x, y); + return 0; +} diff --git a/swaptwonumber b/swaptwonumber new file mode 100644 index 00000000..5c7f9e45 --- /dev/null +++ b/swaptwonumber @@ -0,0 +1,21 @@ +#include +int main() { + double first, second, temp; + printf("Enter first number: "); + scanf("%lf", &first); + printf("Enter second number: "); + scanf("%lf", &second); + + // Value of first is assigned to temp + temp = first; + + // Value of second is assigned to first + first = second; + + // Value of temp (initial value of first) is assigned to second + second = temp; + + printf("\nAfter swapping, firstNumber = %.2lf\n", first); + printf("After swapping, secondNumber = %.2lf", second); + return 0; +} diff --git a/the hollow square star pattern b/the hollow square star pattern new file mode 100644 index 00000000..f6bd1e9b --- /dev/null +++ b/the hollow square star pattern @@ -0,0 +1,21 @@ +int main() +{ + int n; + printf("Enter the number of rows"); + scanf("%d",&n); + for(int i=1;i<=n;i++) + { + for(int j=1;j<=n;j++) + { + if(i==1 ||i==n||j==1||j==n) + { + printf("*"); + } + else + printf(" "); + } + printf("\n"); + } + + return 0; +} diff --git a/to find armstrong numbers using recursion b/to find armstrong numbers using recursion new file mode 100644 index 00000000..aacfc8a5 --- /dev/null +++ b/to find armstrong numbers using recursion @@ -0,0 +1,35 @@ +#include +#include + +int main() +{ + int Number, Temp, Reminder, Times =0, Sum = 0; + + printf("\nPlease Enter number to Check for Armstrong \n"); + scanf("%d", &Number); + + //Helps to prevent altering the original value + Temp = Number; + while (Temp != 0) + { + Times = Times + 1; + Temp = Temp / 10; + } + + Temp = Number; + while( Temp > 0) + { + Reminder = Temp %10; + Sum = Sum + pow(Reminder, Times); + Temp = Temp /10; + } + + printf("\n Sum of entered number is = %d\n", Sum); + + if ( Number == Sum ) + printf("\n %d is Armstrong Number.\n", Number); + else + printf("\n %d is not a Armstrong Number.\n", Number); + + return 0; +} diff --git a/to find fibonacci series b/to find fibonacci series new file mode 100644 index 00000000..d3375464 --- /dev/null +++ b/to find fibonacci series @@ -0,0 +1,16 @@ +#include +int main() { + int i, n, t1 = 0, t2 = 1, nextTerm; + printf("Enter the number of terms: "); + scanf("%d", &n); + printf("Fibonacci Series: "); + + for (i = 1; i <= n; ++i) { + printf("%d, ", t1); + nextTerm = t1 + t2; + t1 = t2; + t2 = nextTerm; + } + + return 0; +} diff --git a/to find prime number b/to find prime number new file mode 100644 index 00000000..4fba8ae0 --- /dev/null +++ b/to find prime number @@ -0,0 +1,27 @@ +#include +int main() { + int n, i, flag = 0; + printf("Enter a positive integer: "); + scanf("%d", &n); + + for (i = 2; i <= n / 2; ++i) { + + // condition for non-prime + if (n % i == 0) { + flag = 1; + break; + } + } + + if (n == 1) { + printf("1 is neither prime nor composite."); + } + else { + if (flag == 0) + printf("%d is a prime number.", n); + else + printf("%d is not a prime number.", n); + } + + return 0; +} diff --git a/tower of hanoi b/tower of hanoi new file mode 100644 index 00000000..19cb8bb9 --- /dev/null +++ b/tower of hanoi @@ -0,0 +1,18 @@ +void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) +{ + if (n == 1) + { + printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod); + return; + } + towerOfHanoi(n-1, from_rod, aux_rod, to_rod); + printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod); + towerOfHanoi(n-1, aux_rod, to_rod, from_rod); +} + +int main() +{ + int n = 4; // Number of disks + towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods + return 0; +} diff --git a/treetransversal.c b/treetransversal.c new file mode 100644 index 00000000..833b3cc6 --- /dev/null +++ b/treetransversal.c @@ -0,0 +1,142 @@ +#include +#include + +struct node { + int data; + + struct node *leftChild; + struct node *rightChild; +}; + +struct node *root = NULL; + +void insert(int data) { + struct node *tempNode = (struct node*) malloc(sizeof(struct node)); + struct node *current; + struct node *parent; + + tempNode->data = data; + tempNode->leftChild = NULL; + tempNode->rightChild = NULL; + + //if tree is empty + if(root == NULL) { + root = tempNode; + } else { + current = root; + parent = NULL; + + while(1) { + parent = current; + + //go to left of the tree + if(data < parent->data) { + current = current->leftChild; + + //insert to the left + if(current == NULL) { + parent->leftChild = tempNode; + return; + } + } //go to right of the tree + else { + current = current->rightChild; + + //insert to the right + if(current == NULL) { + parent->rightChild = tempNode; + return; + } + } + } + } +} + +struct node* search(int data) { + struct node *current = root; + printf("Visiting elements: "); + + while(current->data != data) { + if(current != NULL) + printf("%d ",current->data); + + //go to left tree + if(current->data > data) { + current = current->leftChild; + } + //else go to right tree + else { + current = current->rightChild; + } + + //not found + if(current == NULL) { + return NULL; + } + } + + return current; +} + +void pre_order_traversal(struct node* root) { + if(root != NULL) { + printf("%d ",root->data); + pre_order_traversal(root->leftChild); + pre_order_traversal(root->rightChild); + } +} + +void inorder_traversal(struct node* root) { + if(root != NULL) { + inorder_traversal(root->leftChild); + printf("%d ",root->data); + inorder_traversal(root->rightChild); + } +} + +void post_order_traversal(struct node* root) { + if(root != NULL) { + post_order_traversal(root->leftChild); + post_order_traversal(root->rightChild); + printf("%d ", root->data); + } +} + +int main() { + int i; + int array[7] = { 27, 14, 35, 10, 19, 31, 42 }; + + for(i = 0; i < 7; i++) + insert(array[i]); + + i = 31; + struct node * temp = search(i); + + if(temp != NULL) { + printf("[%d] Element found.", temp->data); + printf("\n"); + }else { + printf("[ x ] Element not found (%d).\n", i); + } + + i = 15; + temp = search(i); + + if(temp != NULL) { + printf("[%d] Element found.", temp->data); + printf("\n"); + }else { + printf("[ x ] Element not found (%d).\n", i); + } + + printf("\nPreorder traversal: "); + pre_order_traversal(root); + + printf("\nInorder traversal: "); + inorder_traversal(root); + + printf("\nPost order traversal: "); + post_order_traversal(root); + + return 0; +} diff --git a/triangular patterns b/triangular patterns new file mode 100644 index 00000000..70a1a944 --- /dev/null +++ b/triangular patterns @@ -0,0 +1,13 @@ +#include +int main() { + int i, j, rows; + printf("Enter the number of rows: "); + scanf("%d", &rows); + for (i = 1; i <= rows; ++i) { + for (j = 1; j <= i; ++j) { + printf("* "); + } + printf("\n"); + } + return 0; +} diff --git a/ugly numbers b/ugly numbers new file mode 100644 index 00000000..11656cf1 --- /dev/null +++ b/ugly numbers @@ -0,0 +1,38 @@ +# include +# include + +int main() +{ +int n,x=0; + printf("Input an integer number: "); + scanf("%d",&n); + + if (n <= 0) { + printf("Input a correct number."); + } + while (n != 1) + { + if (n % 5 == 0) + { + n /= 5; + } + else if (n % 3 == 0) + { + n /= 3; + } + else if (n % 2 == 0) + { + n /= 2; + } + else + { + printf("It is not an ugly number.\n"); + x = 1; + break; + } + } + if (x==0) + { + printf("It is an ugly number.\n"); + } +} diff --git a/use of bitwise operators b/use of bitwise operators new file mode 100644 index 00000000..d41b5a26 --- /dev/null +++ b/use of bitwise operators @@ -0,0 +1,28 @@ +// C Program to demonstrate use of bitwise operators +#include +int main() +{ + // a = 5(00000101), b = 9(00001001) + unsigned char a = 5, b = 9; + + // The result is 00000001 + printf("a = %d, b = %d\n", a, b); + printf("a&b = %d\n", a & b); + + // The result is 00001101 + printf("a|b = %d\n", a | b); + + // The result is 00001100 + printf("a^b = %d\n", a ^ b); + + // The result is 11111010 + printf("~a = %d\n", a = ~a); + + // The result is 00010010 + printf("b<<1 = %d\n", b << 1); + + // The result is 00000100 + printf("b>>1 = %d\n", b >> 1); + + return 0; +} diff --git a/while loop b/while loop new file mode 100644 index 00000000..b8a6b9b3 --- /dev/null +++ b/while loop @@ -0,0 +1,15 @@ +// Print numbers from 1 to 5 + +#include +int main() +{ + int i = 1; + + while (i <= 5) + { + printf("%d\n", i); + ++i; + } + + return 0; +} diff --git a/zombieprocess.c b/zombieprocess.c new file mode 100644 index 00000000..4b04b014 --- /dev/null +++ b/zombieprocess.c @@ -0,0 +1,26 @@ +#include +#include +#include + +int main() +{ + // fork() creates child process identical to parent + int pid = fork(); + + // if pid is greater than 0 than it is parent process + // if pid is 0 then it is child process + // if pid is -ve , it means fork() failed to create child process + + // Parent process + if (pid > 0) + sleep(20); + + // Child process + else + { + exit(0); + } + + return 0; + +}