Posts

Showing posts from October, 2023

PP-#

#1st   class Employee: 'Common base class for all employees' empCount = 0 def init (self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print ("Total Employee %d" % Employee.empCount) def displayEmployee(self): print ("Name : ", self.name, ", Salary: ", self.salary) #emp1 = Employee("Zara", 2000) #emp2 = Employee("Manni", 5000) emp1.displayEmployee() emp2.displayEmployee() print ("Total Employee %d" % Employee.empCount) Here “self” is a instance and “name” is a argument #2nd return values (add 2 numbers) class Employee: 'Common base class for all employees'  empCount = 0 def init (self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print ("Total Employee %d" % Employee.empCount) def displayEmployee(self): print ("Name : ", self.name, ", Salary: ", self.salary) emp1 = Employee...

(PP)write a python program to reverse the string.

Image
def rev_func(string):     revstr = ' '     for x in string:         revstr = x + revstr     return revstr string = str(input()) print('Original String: ', string) print('Reverse String: ', rev_func(string)) OUTPUT:-

(PP)write a python program to check whether the string is palindrome or not.

Image
def isPalindrome(string):     if(string == string[::-1]):         return "The string is a palindrome."     else:         return "The string is not a palindrome."  #Enter input string  string = input("Enter string: ")  print(isPalindrome(string)) 

(PP)write a program to accept a number from the user and count its number of divisors in python.

Image
n=int(input("Enter an integer:")) print("The divisors of the number are:") for i in range(1,n+1):     if(n%i==0):         print(i)

(DS-7)

  7.Write a program for inorder, postorder and preorder traversal of tree. #include<stdio.h> #include<malloc.h> struct node { int data; struct node *left; struct node *right; }; struct node *root=NULL; struct node *create(struct node*); struct node *display(struct node*); void preorder(struct node *temp); void postorder(struct node *temp); void inorder(struct node *temp); void main() { int choice,val,count,min,max; //clrscr(); do { printf("\n MAIN MENU\n"); printf("1.Create a Binary Tree\n"); printf("2.Display a Binary Tree\n"); printf("3.EXIT\n"); printf("Enter Your Choice : "); scanf("%d", &choice); printf("\n\n"); switch(choice) { case 1:root =create(root); break; case 2:root =display(root); break; case 3:break; } }while(choice!=3); } struct node *create(struct node *root) { struct node *newnode=NULL,*temp=NULL,*parent=NULL; in...

(DS-8)

8A. Write a program to insert the element into maximum heap. #include<stdio.h> #include<conio.h> #define SIZE 30 int a[SIZE], n; void maxheapify(int a[],int i,int n1); void buildheap(int a[],int n1); void swap(int i,int j); int main() { int i,j; printf("\nenter the number of elements: "); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter a value:"); scanf("%d",&a[i]); } buildheap(a,n); for(j=0;j<n;j++) { printf("\t%d",a[j]); } printf("\n"); } void buildheap(int a[],int n1) { int i,j; for(i=(n1/2)-1;i>=0;i--) { maxheapify(a,i,n1); } } void maxheapify(int a[],int i,int n1) { int max,l,r; max=i; l=(2*i)+1; r=(2*i)+2; if(l<n1&&r<n1){ if(a[l]>a[max]) { max=l; } if(a[r]>a[max]) { max=r; } } else if(l<n1&&r>=n1) { if(a[l]>a[max]) { max=l; } } else if(l>=n1&&r<n1) { if(a[r]>a[max]) { max=r; } } if(i!=max) { swap(i,max); maxheapify(a,max,n1); } } void swa...

(DS-6)

6A.Write a program to implement merge sort. #include<stdio.h> void mergesort(int a[],int i, int j); void merge(int a[],int i1,int j1,int i2,int j2); int main() { int a[30],n,i;   printf("Enter No of Elements : ");   scanf("%d", &n);   printf("Enter Array Elements : ");     for(i=0;i<n;i++)   scanf("%d", &a[i]);     mergesort(a,0,n-1);   printf("\n Sorted Array is : ");   for(i=0;i<n;i++)   printf("\t %d",a[i]); return 0; } void mergesort(int a[],int i, int j) { int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid);  mergesort(a,mid+1,j);   merge(a,i,mid,mid+1,j);  } } void merge(int a[],int i1,int j1,int i2,int j2) { int temp[50];    int i,j,k; i=i1;    j=i2;   k=0; while(i<=j1 && j<=j2)   { if(a[i]<a[j]) { temp[k++]=a[i++]; } else { temp[k++]=a[j++];...

(DS-5)

  5A.Write a program to implement bubble sort.   #include<stdio.h> int main() { int a[10],i,j,temp,n; printf("\nEnter no. of elements for list:"); scanf("%d",&n); printf("\nEnter %d integers :\n",n); for(i=0 ; i<n ; i++) { scanf("%d",&a[i]); } for(i=0; i<n ; i++) { for(j=i+1 ; j<n ; j++) { if(a[i] > a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("\nBubble Sorted list in ascending order is :"); for(i=0 ; i<n ; i++) { printf("\t %d",a[i]); } } 5B.Write a program to implement selection sort. #include<stdio.h> int main() { int a[20],n,i,j,position,t; printf("\nEnter no. of elements for list:"); scanf("%d",&n); printf("\nEnter %d integers :\n",n); for(i=0 ; i<n ; i++) { scanf("%d",&a[i]); } for(i=0 ; i<(n-1) ; i++)  { position=i; for(j=i+1 ; ...

(DS-0)

  1A.Write a program to store the elements in 1-D array and perform the operations like searching, sorting and reversing the elements. [Menu Driven] #include<stdio.h> void create(); void print(); void sort(); void search(); void reverse(); int a[5],n,i; int main() { int ch; do { printf("\n 1.create \n 2.print \n 3.sort \n 4.search \n 5.reverse \n 6.exit \n"); printf("\n Enter an option :"); scanf("%d",&ch); switch(ch) { case 1:create(); break; case 2:print(); break; case 3:sort(); break; case 4:search(); break; case 5:reverse(); break; case 6:break; } }while(ch!=6); return 0; } void create() { printf("\n Enter the no of element :"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n Enter element for index %d :",i); scanf("%d",&a[i]); } } void print() { printf("\n The array is :"); for(i=0;i<n;i++)...