Skip to content

Commit 849ac30

Browse files
Add files via upload
0 parents  commit 849ac30

39 files changed

+3150
-0
lines changed

1 gcd.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include<stdio.h>
2+
int gcd(int,int);
3+
void main()
4+
{
5+
int a,b,result;
6+
printf("enter any two numbers");
7+
scanf("%d %d",&a,&b);
8+
result=gcd(a,b);
9+
printf("gcd=%d",result);
10+
}
11+
int gcd(int a,int b)
12+
{
13+
int r;
14+
r=a%b;
15+
if(r==0)
16+
{
17+
return b;
18+
}
19+
else
20+
{
21+
return gcd(b,r);
22+
}
23+
}

2 multiply without.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include<stdio.h>
2+
int mul(int x,int y)
3+
{
4+
if(y==0)
5+
{
6+
return 0;
7+
}
8+
else if(y>0)
9+
{
10+
return (x+mul(x,y-1));
11+
}
12+
else if(y<0)
13+
{
14+
return -mul(x,-y);
15+
}
16+
}
17+
int main()
18+
{
19+
int a,b,result;
20+
printf("enter two numbers\n");
21+
scanf("%d %d",&a,&b);
22+
result=mul(a,b);
23+
printf("%d",result);
24+
}

3 without division.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include<stdio.h>
2+
int division(int,int);
3+
void main()
4+
{
5+
int a,b,result;
6+
printf("enter two numbers\n");
7+
scanf("%d %d",&a,&b);
8+
result=division(a,b);
9+
printf("%d",result);
10+
}
11+
int division(int a,int b)
12+
{
13+
if(a-b<0)
14+
return 0;
15+
else if(a-b==0)
16+
return 1;
17+
else return division(a-b,b)+1;
18+
}
19+

4 power of 2num.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include<stdio.h>
2+
int power(int,int);
3+
void main()
4+
{
5+
int b,p,result;
6+
printf("enter base\n");
7+
scanf("%d",&b);
8+
printf("enter power\n");
9+
scanf("%d",&p);
10+
if(p>=0)
11+
{
12+
result=power(b,p);
13+
printf("%d^%d=%d",b,p,result);
14+
}
15+
if(p<0)
16+
{
17+
result=power(b,-p);
18+
printf("%d^%d=1/%d",b,p,result);
19+
}
20+
}
21+
int power(int b,int p)
22+
{
23+
if(p!=0)
24+
{
25+
return (b*power(b,p-1));
26+
}
27+
else
28+
{
29+
return 1;
30+
}
31+
}

5 palinfrome or not.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<stdio.h>
2+
void main()
3+
{
4+
int n,num ;
5+
printf("enter any number\n");
6+
scanf("%d",&n);
7+
num=reverse(n);
8+
if(n==num)
9+
{
10+
printf("palindrome");
11+
}
12+
else
13+
{
14+
printf("not palidrome");
15+
}
16+
}
17+
int reverse(int n)
18+
{
19+
static sum=0;
20+
if(n!=0)
21+
{
22+
sum=sum*10+(n%10);
23+
reverse(n/10);
24+
}
25+
else
26+
{
27+
return sum;
28+
}
29+
}

6 tower of hanoi.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include<stdio.h>
2+
void toh(int n,char s,char d,char au)
3+
{
4+
if(n==1)
5+
{
6+
printf("move disk %d from %c to %c\n",n,s,d);
7+
return;
8+
}
9+
toh(n-1,s,au,d);
10+
printf("move disk %d from %c to %c\n",n,s,d);
11+
toh(n-1,au,d,s);
12+
}
13+
void main()
14+
{
15+
int n;
16+
printf("enter number of dics\n");
17+
scanf("%d",&n);
18+
toh(n,'A','C','B');
19+
}
20+

ARRAY REPRESENTATION OF STACK.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#define MAX 10
4+
int top=-1,stack[MAX],x,i;
5+
void push();
6+
void pop();
7+
void display();
8+
void peek();
9+
void main()
10+
{
11+
int ch;
12+
while(1)
13+
{
14+
printf("Stack Operations:\n");
15+
printf("1.Push\n");
16+
printf("2.Pop\n");
17+
printf("3.Display\n");
18+
printf("4.peek\n5.exit\n");
19+
printf("enter your choice=\n");
20+
scanf("%d",&ch);
21+
switch(ch)
22+
{
23+
case 1:
24+
push();
25+
break;
26+
case 2:
27+
pop();
28+
break;
29+
case 3:
30+
display();
31+
break;
32+
case 4:
33+
peek();
34+
break;
35+
case 5:
36+
exit(0);
37+
break;
38+
default:
39+
printf("choose right option\n");
40+
}
41+
}
42+
}
43+
void push()
44+
{
45+
if(top>=MAX)
46+
{
47+
printf("STACK is overe flow\n");
48+
}
49+
else
50+
{
51+
printf("enter the element to push=\n");
52+
scanf("%d",&x);
53+
top++;
54+
stack[top]=x;
55+
}
56+
}
57+
void pop()
58+
{
59+
if(top<=-1)
60+
{
61+
printf("Stack is underflow\n");
62+
63+
}
64+
else
65+
{
66+
printf("The popped element is= %d\n",stack[top]);
67+
top--;
68+
}
69+
70+
}
71+
void display()
72+
{
73+
if(top>=0)
74+
{
75+
printf("The elements in the stack are=");
76+
for(i=top;i>=0;i--)
77+
{
78+
printf("\n%d",stack[i]);
79+
}
80+
}
81+
else
82+
{
83+
printf("The stack is empty\n");
84+
}
85+
}
86+
void peek()
87+
{
88+
printf("The Top element in the stack is=%d\n",stack[top]);
89+
90+
}
91+

BST.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include<stdio.h>
2+
#include<conio.h>
3+
#include<stdlib.h>
4+
struct node
5+
{
6+
struct node *left;
7+
int data;
8+
struct node *right;
9+
};
10+
struct node *root=NULL,*parent;
11+
void insert(int val)
12+
{
13+
struct node *temp,*current,*parent;
14+
temp=(struct node*)malloc(sizeof(struct node));
15+
temp->left=NULL;
16+
temp->data=val;
17+
temp->right=NULL;
18+
if(root==NULL)
19+
{
20+
root=temp;
21+
}
22+
else
23+
{
24+
current=root;
25+
while(current)
26+
{
27+
parent=current;
28+
if(val>current->data)
29+
{
30+
current=current->right;
31+
}
32+
else
33+
{
34+
current=current->left;
35+
}
36+
}
37+
if(val>parent->data)
38+
{
39+
parent->right=temp;
40+
}
41+
else { parent->left=temp; } } } void preorder(struct node *tree) { if(tree!=NULL) { printf("%d\t",tree->data); preorder(tree->left);
42+
preorder(tree->right); } } void inorder(struct node *tree) { if(tree!=NULL) { inorder(tree->left); printf("%d\t",tree->data); inorder(tree->right); } } void postorder(struct node *tree) { if(tree!=NULL) { postorder(tree->left); postorder(tree->right); printf("%d\t",tree->data); } } void delete(struct node *current,int val) { while(val!=current->data) { parent=current;
43+
if(val>current->data) { current=current->right; } else { current=current->left; } } if(current->left==NULL&&current->right==NULL) { if(parent->right==current) { parent->right=NULL; } else { parent->left=NULL; } } else if(current->left==NULL&&current->right!=NULL) { if(parent->right==current) { parent->right=current->right;
44+
} else { parent->left=current->right; } current->right=NULL; free(current); } else if(current->right==NULL&&current->left!=NULL) { if(parent->right==current) { parent->right=current->left; } else { parent->left=current->left; } current->left=NULL; free(current); } else if(current->left!=NULL&&current->right!=NULL) { struct node *min,*prev=NULL; min=current->right; if(min->left==NULL) {
45+
parent->right=min; min->left=current->left; current->right=NULL; current->right=NULL; free(current); } else { while(min->left!=NULL) { prev=min; min=min->left; } prev->left=min->right; parent->right=min; min->left=current->left; min->right=current->right; current->right=NULL; current->left=NULL; free(current); } } } int countn(struct node *tree) {
46+
int c=1; if(tree==NULL) { return 0; } else { c=c+countn(tree->left); c=c+countn(tree->right); return c; } } void nthnode(struct node *tree,int n) { static int count=0; if(tree==NULL) { return; } if(count<=n) { nthnode(tree->left,n); count++; if(count==n) { printf("\n%d",tree->data); }
47+
nthnode(tree->right,n); } } void small() { struct node *current=root; while(current->left!=NULL) { current=current->left; } printf("\n%d",current->data); } void max() { struct node *current=root; while(current->right!=NULL) { current=current->right; } printf("\t%d",current->data); } void at_level(struct node* temp,int desired,int current) { if(temp) { if(desired==current) printf("%d\t",temp->data);
48+
else { at_level(temp->left,desired,current+1); at_level(temp->right,desired,current+1); } } }
49+
50+
51+
void main() { int op,val,ele,count,n,c,l; while(1) { printf("\n**MENU**"); printf("\n1.create"); printf("\n2.preorder traversal"); printf("\n3.inorder traversal"); printf("\n4.postorder"); printf("\n5.delete"); printf("\n6.count"); printf("\n7.nthnode in inorder"); printf("\n8.small"); printf("\n9.max"); printf("\n10.nodes at nth level"); printf("\nenter option"); scanf("%d",&op);
52+
switch(op) { case 1: printf("\nenter data"); scanf("%d",&val); insert(val); break; case 2: preorder(root); break; case 3: inorder(root); break; case 4: postorder(root); break; case 5: printf("\nenter value"); scanf("%d",&ele); delete(root,ele); break; case 6: c=countn(root); printf("\ncount:%d",c); break; case 7:
53+
printf("\nenter n value"); scanf("%d",&n); count=0; nthnode(root,n); break; case 8: small(); break; case 9: max(); break; case 10: printf("enter n value\n"); scanf("%d",&l); at_level(root,l,0); break; } } }

0 commit comments

Comments
 (0)