0% found this document useful (0 votes)
13 views

Python programs

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Python programs

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

STATEMENTS

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 value of N (N <= 10): ");


scanf("%d", & n);
printf("Enter the elements of Matrix-A: \n");

for (i = 0; i < n; i++) {


for (j = 0; j < n; j++) {
scanf("%d", & a[i][j]);
}
}

printf("Enter the elements of Matrix-B: \n");

for (i = 0; i < n; i++) {


for (j = 0; j < n; j++) {
scanf("%d", & b[i][j]);
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
c[i][j] = 0;
for (k = 0; k < n; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
printf("The product of the two matrices is: \n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", c[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the value of N (N <= 10): 2
Enter the elements of Matrix-A:
22
22
Enter the elements of Matrix-B:
22
22
Product of the two matrices is :
8 8
8 8
POINTER
PROGRAM:
#include<stdio.h>
int main()
{
int num;
int*pnum;
pnum=&num;
num=100;
printf("\nusing variable num");
printf("value of num:%d\naddress of num:%u",num,&num);
printf("\nusing pointer variable:\n");
printf("\n value of num:%d\naddress of num :%u\n", * pnum
, pnum );
return 0;
}
OUTPUT:
using variable num
value of num:100
address of num:6487572
using pointer variable
value of num:100
address of num:6487572
STRUCTURES
PROGRAM:
#include<stdio.h>
void main ()
{
char names[2][10],dummy;
int roll_numbers[2],i;
float marks[2];
for (i=0;i<3;i++)
{

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

Printing the Student details...


Arun 90 91.000000
Varun 91 56.000000
Sham 89 69.000000
STACK USING ARRAY IMPLEMENTATION
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define CAPACITY 5
int stack[CAPACITY], top=-1;
void push(int);
int pop();
int isFull();
int isEmpty();
void peek();
void traverse();
int main()
{
int ch, item;
while(1)
{
printf("\n 1. Push");
printf("\n 2.Pop");
printf("\n 3.Peek");
printf("\n 4.Traverse");
printf("\n 5.Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter element to push");
scanf("%d",&item);
push(item);
break;
case 2:
item=pop();
if (item==0)
{
printf("\n Stack is underflow");
}
else
{
printf("\nPoped item is %d",item);
}
break;
case 3:
peek();
break;
case 4:
traverse();
break;
case 5:
exit(0);
default:
printf("\n Invalid Input");
}
}
getch();
}
void push(int ele)
{
if (isFull())
{
printf("\n Stack is Overflow");
}
else
{
top++;
stack[top] = ele;
printf("%d is pushed into the stack",ele);
}
}
int isFull()
{
if(top == CAPACITY-1)
{
return 1;
}
else
{
return 0;
}
}
int pop()
{
if(isEmpty())
{
printf("\n Stack is underflow");
}
else
{
return stack[top--];
}
}
int isEmpty()
{
if (top== -1)
{
return 1;
}
else
{
return 0;
}
}
void peek()
{
if(isEmpty())
{
printf("\n Stack is underflow");
}
else
{
printf("\n Peek element is %d", stack[top]);
}
}
void traverse()
{
if(isEmpty())
{
printf("\n Stack is underflow");
}
else
{
int i;
printf("\n Stack elements are:");
for(i=0;i<=top; i++)
printf("%d\n",stack[i]);
}
}

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>

void insertionSort(int arr[], int n)

int i, key, j;

for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;
}

void printArray(int arr[], int n)

int i;

for (i = 0; i < n; i++)

printf("%d ", arr[i]);

printf("\n");

int main()

int arr[] = { 12, 11, 13, 5, 6 };

int n = sizeof(arr) / sizeof(arr[0]);

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:

How many elements are u going to enter?: 5


Enter 5 elements: 88 0 -9 98 27
Order of Sorted elements: -9 0 27 27 88 98
MERGE SORT
PROGRAM:
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merging(int low, int mid, int high) {
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++)
{
if(a[l1] <= a[l2])
b[i] = a[l1++];
else
b[i] = a[l2++];
}
while(l1 <= mid)
b[i++] = a[l1++];
while(l2 <= high)
b[i++] = a[l2++];
for(i = low; i <= high; i++)
a[i] = b[i];
}
void sort(int low, int high) {
int mid;
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
} else {
return;
}
}
int main() {
int i;
printf("List before sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
sort(0, max);
printf("\nList after sorting\n");
for(i = 0; i <= max; i++)
printf("%d ", a[i]);
}
OUTPUT:
List before sorting
10 14 19 26 27 31 33 35 42 44 0
List after sorting
0 10 14 19 26 27 31 33 35 42 44
MERGE SORT
PROGRAM:
#include<stdio.h>
#define size 7
int arr[size];
void init()
{
int i;
for(i = 0; i < size; i++)
arr[i] = -1;
}

void insert(int value)


{
int key = value % size;
if(arr[key] == -1)
{
arr[key] = value;
printf("%d inserted at arr[%d]\n", value,key);
}
else
{
printf("Collision : arr[%d] has element %d already!\n",key,arr[ke
y]);
printf("Unable to insert %d\n",value);
}
}
void del(int value)
{
int key = value % size;
if(arr[key] == value)
arr[key] = -1;
else
printf("%d not present in the hash table\n",value);
}
void search(int value)
{
int key = value % size;
if(arr[key] == value)
printf("Search Found\n");
else
printf("Search Not Found\n");
}
void print()
{
int i;
for(i = 0; i < size; i++)
printf("arr[%d] = %d\n",i,arr[i]);
}
int main()
{
init();
insert(10);
insert(4);
insert(2);
insert(3);

printf("Hash table\n");
print();
printf("\n");

printf("Deleting value 10..\n");


del(10);
printf("After the deletion hash table\n");
print();
printf("\n");

printf("Deleting value 5..\n");


del(5);
printf("After the deletion hash table\n");
print();
printf("\n");
printf("Searching value 4..\n");
search(4);
printf("Searching value 10..\n");
search(10);

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.

You might also like