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/Add_to_numb b/Add_to_numb new file mode 100644 index 00000000..18f9f81d --- /dev/null +++ b/Add_to_numb @@ -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/Addfunction b/Addfunction index 0fbdfec3..8d3bdb44 100644 --- a/Addfunction +++ b/Addfunction @@ -1,10 +1,10 @@ void add(int,int); #include int main(){ -int a,b; +int num1,num2; printf("Enter two integers\n"); -scanf("%d %d",&a,&b); -add(a,b); +scanf("%d %d",&num1,&num2); +add(num1,num2); return 0; } void add(int x,int y){ 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 index d70e9993..2a5ec274 100644 --- a/Arithmatic Operations +++ b/Arithmatic Operations @@ -1,4 +1,6 @@ +#include #include + int main() { int a,b; 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 index a328ddc9..e7c71599 100644 --- a/Array +++ b/Array @@ -1,18 +1,19 @@ -// Program to take 5 values from the user and store them in an array +/ 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: "); + 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: "); + printf("Displaying integers:\n"); // printing elements of an array for(int i = 0; i < 5; ++i) { 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/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 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/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 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/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/Days Conversion b/Days Conversion new file mode 100644 index 00000000..22d11b02 --- /dev/null +++ b/Days Conversion @@ -0,0 +1,19 @@ +#include + +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/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 +#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 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/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 Series in c b/Fibonacci Series in c new file mode 100644 index 00000000..3b95d0c7 --- /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 +#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/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/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 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/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/How to Reverse a String using C programming Language b/How to Reverse a String using C programming Language deleted file mode 100644 index 16834b69..00000000 --- a/How to Reverse a String using C programming Language +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include -void main() -{ - int i, j, k; - char str[100]; - char rev[100]; - printf("Enter a string:\t"); - scanf("%s", str); - printf("The original string is %s\n", str); - for(i = 0; str[i] != '\0'; i++); - { - k = i-1; - } - for(j = 0; j <= i-1; j++) - { - rev[j] = str[k]; - k--; - } - printf("The reverse string is %s\n", rev); - getch(); -} diff --git a/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/Intersection point of 2 linked list b/Intersection point of 2 linked list new file mode 100644 index 00000000..000814a5 --- /dev/null +++ b/Intersection point of 2 linked list @@ -0,0 +1,121 @@ +// C program to get intersection point of two linked list +#include +#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/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/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/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/Maxof3 b/Maxof3 new file mode 100644 index 00000000..de3636be --- /dev/null +++ b/Maxof3 @@ -0,0 +1,22 @@ +#include +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< +#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/MergeOverlappingSubIntervals b/MergeOverlappingSubIntervals new file mode 100644 index 00000000..341f3c73 --- /dev/null +++ b/MergeOverlappingSubIntervals @@ -0,0 +1,62 @@ + +#include +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/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/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/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/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/Pattern Program in c b/Pattern Program in c new file mode 100644 index 00000000..04b9d849 --- /dev/null +++ b/Pattern Program in c @@ -0,0 +1,20 @@ +* +* * +* * * +* * * * +* * * * * + + +#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/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/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/README.md b/README.md index 6b71346c..71b4d610 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# cprogram +# cprogram - An Amazing Project # The CP is a framework of international education that incorporates the values of the IB into a unique programme addressing the needs of students engaged in career-related education. diff --git a/RECURSION b/RECURSION new file mode 100644 index 00000000..6e28dc48 --- /dev/null +++ b/RECURSION @@ -0,0 +1,25 @@ +#include +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/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 +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/Smart Calculator b/Smart Calculator new file mode 100644 index 00000000..8aed0821 --- /dev/null +++ b/Smart Calculator @@ -0,0 +1,48 @@ +# Program make a simple calculator + +# This function adds two numbers +def add(x, y): + return x + y + +# This function subtracts two numbers +def subtract(x, y): + return x - y + +# This function multiplies two numbers +def multiply(x, y): + return x * y + +# This function divides two numbers +def divide(x, y): + return x / y + + +print("Select operation.") +print("1.Add") +print("2.Subtract") +print("3.Multiply") +print("4.Divide") + +while True: + # Take input from the user + choice = input("Enter choice(1/2/3/4): ") + + # Check if choice is one of the four options + if choice in ('1', '2', '3', '4'): + num1 = float(input("Enter first number: ")) + num2 = float(input("Enter second number: ")) + + if choice == '1': + print(num1, "+", num2, "=", add(num1, num2)) + + elif choice == '2': + print(num1, "-", num2, "=", subtract(num1, num2)) + + elif choice == '3': + print(num1, "*", num2, "=", multiply(num1, num2)) + + elif choice == '4': + print(num1, "/", num2, "=", divide(num1, num2)) + break + else: + print("Invalid Input") diff --git a/Snake b/Snake new file mode 100644 index 00000000..a7ef96e1 --- /dev/null +++ b/Snake @@ -0,0 +1,150 @@ + + + + + + + + + + + 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/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 +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/Tetris b/Tetris new file mode 100644 index 00000000..2a78fd42 --- /dev/null +++ b/Tetris @@ -0,0 +1,318 @@ + + + + + + + + + + + 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/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/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/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/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/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_sort b/bubble_sort new file mode 100644 index 00000000..a491d8db --- /dev/null +++ b/bubble_sort @@ -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(){ +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/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/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/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 +#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/cprogram b/cprogram new file mode 100644 index 00000000..8c571177 --- /dev/null +++ b/cprogram @@ -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/cubeno b/cubeno new file mode 100644 index 00000000..41e0a437 --- /dev/null +++ b/cubeno @@ -0,0 +1,17 @@ +/* C Program to find Cube of a Number */ + +#include + +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/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/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 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_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/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/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/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/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/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/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/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 +#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/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/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/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 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_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/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/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/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/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_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 + +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 the greatest of two numbers in C b/program to find the greatest of two numbers in C new file mode 100644 index 00000000..73c31935 --- /dev/null +++ b/program to find the greatest of two numbers in C @@ -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/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/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/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 + +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/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,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/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 + +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/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/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/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; + +}