Python programs
Python programs
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
printf("name:xxxxxxx");
printf("\nreg.no:yyyyyy");
getch();
}
OUTPUT:
name:xxxxxxx
reg.no:yyyyyy
EXPRESSIONS
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
int a=5,b=10,x;
x=a+b;
x=a-b;
printf("\n sum of x=%d",a+b);
printf("\nsub of x=%d",a-b);
getch();
}
OUTPUT:
sum of x=15
sub of x=-5
DECISION MAKING
PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{int i=10;
if(i>15)
{
printf("\n10 is less than 15");
}
printf("\n I am not in if");
getch();
}
OUTPUT:
I am not in if
ITERATIVE STATEMENT
PROGRAM:
#include<stdio.h>
int main(){
int i;
for(i=1;i<=5;i++)
{
printf("\nkeerthana");
return 0;
}
}
OUTPUT:
keerthana
keerthana
keerthana
keerthana
keerthana
FUNCTIONS
PROGRAM:
#include<stdio.h>
void swap(int *,int *);
int main()
{
int n1,n2;
printf("\n\n Function : swap two numbers using function :\n");
printf("------------------------------------------------\n");
printf("Input 1st number : ");
scanf("%d",&n1);
printf("Input 2nd number : ");
scanf("%d",&n2);
printf("Before swapping: n1 = %d, n2 = %d ",n1,n2);
swap(&n1,&n2);
printf("\nAfter swapping: n1 = %d, n2 = %d \n\n",n1,n2);
return 0;
}
void swap(int *p,int *q)
{ int tmp;
tmp = *p;
*p=*q;
*q=tmp;
}
OUTPUT:
Function : swap two numbers using function :
------------------------------------------------
Input 1st number : 2
Input 2nd number : 4
Before swapping: n1 = 2, n2 = 4
After swapping: n1 = 4, n2 = 2
ARRAY IMPLEMENTATION
PROGRAM:
#include<stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10], n, i, j, k;
printf("Enter the name, roll number, and marks of the student %d",i
+1);
scanf("%s %d %f",&names[i],&roll_numbers[i],&marks[i]);
scanf("%c",&dummy);
}
printf("Printing the Student details ...\n");
for (i=0;i<3;i++)
{
printf("%s %d %f\n",names[i],roll_numbers[i],marks[i]);
}
}
OUTPUT:
Enter the name, roll number, and marks of the student 1Arun 90 91
Enter the name, roll number, and marks of the student 2Varun 91 56
Enter the name, roll number, and marks of the student 3Sham 89 69
OUTPUT:
1. Push
2.Pop
3.Peek
4.Traverse
5.Exit
Enter your choice:1
Enter element to push 45
45 is pushed into the stack
QUEUE USING ARRAY IMPLEMENTATION
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define CAPACITY 5
int queue[CAPACITY], front=0, rear=0;
void enque();
void deque();
void traverse();
int main()
{
int ch;
while(1)
{
printf("\n 1. Enque");
printf("\n 2.Deque");
printf("\n 3.Traverse");
printf("\n 4.Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
enque();
break;
case 2:
deque();
break;
case 3:
traverse();
break;
case 4:
exit(0);
default:
printf("\n Invalid Input");
}
}
getch();
}
void enque()
{
if (CAPACITY == rear)
{
printf("\n Queue is Overflow");
}
else
{
int ele ;
printf("\n Enter the element to insert:");
scanf("%d",&ele);
queue[rear]=ele;
rear++;
}
}
void deque()
{
int i;
if(front == rear)
{
printf("\n Queue is underflow");
}
else
{
printf("\n Deleted element is %d",queue[front]);
for(i=0;i<rear;i++)
{
queue[i] = queue[i+1];
}
rear--;
}
}
void traverse()
{
int i;
if(front == rear)
{
printf("\n Queue is underflow");
}
else
{
printf("\n Queue elements are:");
for(i=front;i<rear; i++)
printf("%d\n",queue[i]);
}
}
OUTPUT:
1. Enque
2.Deque
3.Traverse
4.Exit
Enter your choice:1
Enter the element to insert:5
STACK USING LINKED LIST
IMPLEMENTATION
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* link;
};
struct node* top=NULL;
void push();
void pop();
void traverse();
int main()
{
int ch;
while(1)
{
printf("\n 1. Push");
printf("\n 2.Pop");
printf("\n 3.Traverse");
printf("\n 4.Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
traverse();
break;
case 4:
exit(0);
default:
printf("\n Invalid Input");
}
}
getch();
}
void push()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("\n Enter node data");
scanf("%d",&temp->data);
temp->link = top;
top = temp;
}
void pop()
{
struct node* temp;
if(top == NULL)
{
printf ("\n No elements in the stack");
}
else
{
temp = top;
printf("\n Poped item is %d ",temp->data);
top = top->link;
temp ->link = NULL;
free(temp);
}
}
void traverse()
{
struct node* temp;
if (top == NULL)
{
printf("\n Stack is underflow");
}
else
{
temp = top;
while (temp!= NULL)
{
printf("\n %d",temp->data);
temp = temp->link;
}
}
}
OUTPUT:
1. Push
2.Pop
3.Traverse
4.Exit
Enter your choice:1
Enter node data 2
1. Push
2.Pop
3.Traverse
4.Exit
Enter your choice:1
Enter node data4
1. Push
2.Pop
3.Traverse
4.Exit
Enter your choice: 2
Poped item is 4
QUEUE USING LINKED LIST
IMPLEMENTATION
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* link;
};
struct node* front=NULL;
struct node* rear=NULL;
void enque();
void deque();
void traverse();
int main()
{
int ch;
while(1)
{
printf("\n 1. Enque");
printf("\n 2. Deque");
printf("\n 3.Traverse");
printf("\n 4.Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
enque();
break;
case 2:
deque();
break;
case 3:
traverse();
break;
case 4:
exit(0);
default:
printf("\n Invalid Input");
}
}
getch();
}
void enque()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("\n Enter node data");
scanf("%d",&temp->data);
temp->link = NULL;
if (front ==NULL && rear==NULL)
{
front=rear=temp;
}
else
{
rear->link=temp;
rear=temp;
}
}
void deque()
{
struct node* temp;
if(front == NULL && rear== NULL)
{
printf ("\n No elements in the queue");
}
else
{
temp = front;
printf("\n Deleted item is %d ",temp->data);
front = front->link;
temp ->link = NULL;
free(temp);
}
}
void traverse()
{
struct node* temp;
if (front == NULL && rear == NULL)
{
printf("\n Queue is underflow");
}
else
{
temp = front;
while (temp!= NULL)
{
printf("\n %d",temp->data);
temp = temp->link;
}
}
}
OUTPUT:
1. Enque
2. Deque
3.Traverse
4.Exit
Enter your choice:1
Enter node data 3
1. Enque
2. Deque
3.Traverse
4.Exit
Enter your choice:1
Enter node data 2
1. Enque
2. Deque
3.Traverse
4.Exit
Enter your choice:2
Deleted item is 3
BINARY TREE
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left, *right;
};
struct node* create();
void preorder();
void inorder();
void postorder();
int main()
{
struct node* root;
int ch;
while(1)
{
printf("\n 1. Create");
printf("\n 2. Inorder");
printf("\n 3.Preorder");
printf("\n 4.PostOrder");
printf("\n 5.Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
root=0;
root=create();
break;
case 2:
printf("\n Inorder Traverse:");
inorder(root);
break;
case 3:
printf("\n Preorder Traverse:");
preorder(root);
break;
case 4:
printf("\n Postorder Traverse:");
postorder(root);
break;
case 5:
exit(0);
default:
printf("\n Invalid Input");
}
}
getch();
}
struct node* create()
{
int x;
struct node* newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n Enter node data (-1 for no node)");
scanf("%d",&x);
if(x==-1)
{
return 0;
}
newnode->data=x;
printf("\n Enter left child of %d",x);
newnode->left=create();
printf("\n Enter right child of %d",x);
newnode->right=create();
return newnode;
}
void preorder(struct node* root)
{
if(root==0)
{
return;
}
printf("\t%d ",root->data);
preorder(root->left);
preorder(root->right);
}
void inorder(struct node* root)
{
if(root==0)
{
return;
}
inorder(root->left);
printf("\t%d ",root->data);
inorder(root->right);
}
void postorder(struct node* root)
{
if(root==0)
{
return;
}
postorder(root->left);
postorder(root->right);
printf("\t%d ",root->data);
}
OUTPUT:
1.create.
2.inorder.
3.preorder.
4.postorder.
5.exit.
Enter your choice(-1 for no node) 2
Enter left child of 2
Enter your choice(-1 for no node)-1
Enter right child of 2
BINARY SEARCH TREE
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* left, *right;
};
struct node* root=NULL;
void insert();
void delete();
void preorder();
void inorder();
void postorder();
void main()
{
struct node* root;
int ch;
while(1)
{
printf("\n 1. Insert");
printf("\n 2. Delete");
printf("\n 2. Inorder");
printf("\n 3.Preorder");
printf("\n 4.PostOrder");
printf("\n 5.Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
delete();
break;
case 3:
printf("\n Inorder Traverse:");
inorder(root);
break;
case 4:
printf("\n Preorder Traverse:");
preorder(root);
break;
case 5:
printf("\n Postorder Traverse:");
postorder(root);
break;
case 6:
exit(0);
default:
printf("\n Invalid Input");
}
}
getch();
}
void insert()
{
int d;
struct node* temp, *parent;
temp=(struct node*)malloc(sizeof(struct node));
printf("\n Enter data to insert:");
scanf("%d",&d);
temp->data=d;
temp->left=NULL;
temp->right=NULL;
parent=root;
if(root==NULL)
{
root=temp;
}
else
{
struct node* curr;
curr=root;
while(curr)
{
parent=curr;
if(temp->data > curr->data)
{
curr=curr->right;
}
else
{
curr=curr->left;
}
}
}
if(temp->data > parent->data)
{
parent->right=temp;
}
else
{
parent->left=temp;
}
}
void delete()
{
struct node* curr, *parent,*temp;
int d;
temp=(struct node*)malloc(sizeof(struct node));
printf("\n Enter data to delete:");
scanf("%d",&d);
temp->data=d;
temp->left=NULL;
temp->right=NULL;
parent=root;
if(root==NULL)
{
printf("\n No node in the tree:");
}
else
{
struct node* curr;
curr=root;
while(curr)
{
parent=curr;
if(temp->data > curr->data)
{
curr=curr->right;
}
else
{
curr=curr->left;
}
}
}
// Deletion of leaf node
if(curr == parent->right)
{
parent->right = NULL;
}
else
{
parent->left = NULL;
}
free(curr);
// Deletion of node having one child
else if(curr->left != NULL)
{
if(curr == parent->right)
{
parent->right = curr->left;
}
}
curr->left = NULL;
free(curr);
else if(curr->right != NULL)
{
if(curr == parent->right)
{
parent->right = curr->right;
}
}
curr->left = NULL;
free(curr);
else if(curr->right != NULL)
{
if(curr == parent->left)
{
parent->left = curr->right;
}
}
curr->right = NULL;
free(curr);
else if(curr->left != NULL)
{
if(curr == parent->left)
{
parent->left = curr->left;
}
}
curr->left = NULL;
free(curr);
// Deletion of node having two children’s – Least Element in a right
sub tree
else if(curr->left != NULL && curr->right != NULL)
{
struct node* t1, *t2;
t1 = curr->right;
if(t1->left == NULL && t1->right == NULL)
{
curr->data = t1->data;
curr->right = NULL;
free(t1);
}
if (t1->right != NULL && t1->left != NULL)
{
curr->data = t1->data;
curr->right = t1->right;
t1->right = NULL;
free(t1);
}
}
void preorder(struct node* temp)
{
if(temp==0)
{
return;
}
printf("\t%d ",temp->data);
preorder(temp->left);
preorder(temp->right);
}
void inorder(struct node* temp)
{
if(temp==0)
{
return;
}
inorder(temp->left);
printf("\t%d ",temp->data);
inorder(temp->right);
}
void postorder(struct node* temp)
{
if(temp==0)
{
return;
}
postorder(temp->left);
postorder(temp->right);
printf("\t%d ",temp->data);
}
OUTPUT:
1.create.
2.delete.
3.inorder.
4.postorder.
5.preorder.
6.search and replace.
7.exit.
Enter choice:1
Enter new element:75
Root is 75.
INSERTION SORT
PROGRAM:
#include <math.h>
#include <stdio.h>
int i, key, j;
key = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
}
int i;
printf("\n");
int main()
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
OUTPUT:
5 6 11 12 13
QUICK SORT
PROGRAM:
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
OUTPUT:
printf("Hash table\n");
print();
printf("\n");
return 0;
}
OUTPUT:
10 inserted at arr[3]
4 inserted at[4]
2 inserted at[2]
Collision:arr[3] has element 10 already!
Unable to insert 3
Hash table
arr[0]=-1
arr[1]=-1
arr[2]=2
arr[3]=10
arr[4]=4
arr[5]=-1
arr[6]=-1
deleting value 10…
after deletion hash table
arr[0]=-1
arr[1]=-1
arr[2]=2
arr[3]=-1
arr[4]=4
arr[5]=-1
arr[6]=-1
deleting value 5…..
5 not present in tha hash table
Searching value 4…
Search found
Searching value 10…
Search not found.
FILES
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct bfile{
int n1,n2,n3;
};
void main(){
FILE *fileptr;
fileptr = fopen("first.txt","w");
if(fileptr== NULL){
printf("error!");
}
else{
printf("SUCESS");
}
char str[30];
printf("\n Enter data for file :");
gets(str);
fprintf(fileptr,"%s",str);
fclose(fileptr);
fileptr = fopen("first.txt","r");
if(fileptr==NULL){
printf("Error");
}
char s1[30];
fscanf(fileptr,"%s",&s1);
printf("The values are :%s %s \n ",s1);
fclose(fileptr);
struct bfile num;
fileptr = fopen("binfile.bin","wb");
if(fileptr==NULL){
printf("ERROR");
}
int n;
for(n = 1; n < 5; ++n)
{
num.n1 = n;
num.n2 = 5*n;
num.n3 = 5*n + 1;
fwrite(&num, sizeof(struct bfile), 1, fileptr);
}
fclose(fileptr);
fileptr = fopen("binfile.bin","rb");
if(fileptr == NULL){
printf("ERROR");
}
for(n = 1; n < 5; ++n)
{
fread(&num, sizeof(struct bfile), 1, fileptr);
printf("\n n1: %d\tn2: %d\tn3: %d\n", num.n1, num.n2,
num.n3);
}
fseek(fileptr,sizeof(struct bfile),SEEK_END);
printf("\n n1: %d\tn2: %d\tn3: %d\n", num.n1, num.n2,
num.n3);
fclose(fileptr);
}
OUTPUT:
SUCCESS
Enter data for file xxxxxxxxx
The values of xxxxxxxxx
n1:1 n2:5 n3:6
n1:7 n2:2 n3:4
n1:8 n2:9 n3:6
n1:4 n2:3 n3:4
n1:2 n2:1 n3:2.