Beruflich Dokumente
Kultur Dokumente
#include<iostream.h> #include<conio.h> void primen(int num); void perfect(int num); int main() { int num; int prime,i; clrscr(); cout<<"Enter the limit:"; cin>>num; cout<<"\nPrime numbers are\n"; primen(num); cout<<endl<<"Perfect dividing numbers are\n"; perfect(num); getch(); return (0); } void primen(int num) { int i,prime; for(i=3;i<=num;i++) { prime=1; for(int n=2;n<i;n++) { if(i%n==0) { prime=0; break; } } if(prime==1) { cout<<i<<endl; } } } void perfect(int num) { int sum,i; for(int n=1;n<num;n++)
OUTPUT:
Enter the limit: 10 Prime numbers are 3 5 7
2. Program to print prime and perfect dividing numbers upto an integer N using class.
#include<iostream.h> #include<conio.h> class primeper { int num,prime,i,sum; public: void read(); void primen(); void perfect();
}; void primeper::read() { cout<<"Enter the limit:"; cin>>num; } void primeper::primen() { int i,prime; for(i=3;i<=num;i++) { prime=1; for(int n=2;n<i;n++) { if(i%n==0) { prime=0; break; } } if(prime==1) { cout<<i<<endl; } } }
void primeper::perfect() { int sum,i; for(int n=1;n<num;n++) { sum=0; for(i=1;i<n;i++) { if(n%i==0) { sum=sum+i; } } if(sum==n) { cout<<n<<endl; }
} } int main() { clrscr(); primeper ob; ob.read(); cout<<"\nPrime numbers are\n"; ob.primen(); cout<<endl<<"\nPerfect dividing numbers are\n"; ob.perfect(); getch(); return (0); }
OUTPUT:
Enter the limit: 50 Prime numbers are 3 5 7 11 13 17 19 23 29 31 37 41 43 47 Perfect dividing numbers are 6 28
char temp[20]; for(int i=0;i<n;i++) for(int j=0;j<n-1;j++) { if(strcmp(names[j],names[j+1])>0) { strcpy(temp,names[j]); strcpy(names[j],names[j+1]); strcpy(names[j+1],temp); } } }
OUTPUT:
Enter the number of names to be sorted: 8 Enter the names : Sowmya Sushma Veena Ramya Sneha Shoba Sowndarya Sreesha Names before sorting: Sow Sush Veena Ramya Sneha Shoba Sowndarya Sree Names after sorting: Ramya Shoba Sneha Sow Sowndarya Sree Sush Veena
void main() { clrscr(); sort_names objsort_names; objsort_names.get_names(); cout<<"\nNames before sorting:\n"; objsort_names.display(); objsort_names.sort(); cout<<"\n\nNames after sorting:\n"; objsort_names.display(); getch(); }
OUTPUT:
Enter the number of names to be sorted: 5 Enter the names : Jaya Gini Joshua Nimmi Sree Names before sorting: Jaya Gini Joshua Nimmi Sree Names after sorting: Gini Jaya Joshua Nimmi Sree
5. Program to find the sum and difference of two matrices of order MxN and PxQ.
#include<iostream.h> #include<conio.h> #include<string.h> void add(int arr1[20][20],int arr2[20][20],int row1,int col1); void sub(int arr1[20][20],int arr2[20][20],int row1,int col1); int main() { int arr1[20][20],arr2[20][20],temp,add1[20][20]; int row1,col1,row2,col2,m,i,j,n; clrscr(); cout<<"Enter the rows and columns of 1st matrix:"; cin>>row1>>col1; cout<<"Enter the rows and columns of 2nd matrix:"; cin>>row2>>col2; if(row1!=row2||col1!=col2) { cout<<"\nOperation not possible"; } else { cout<<endl<<"Enter Matrix 1"<<endl; for(i=0;i<row1;i++) for(j=0;j<col1;j++) cin>>arr1[i][j]; cout<<endl<<"Enter Matrix 2"<<endl; for(i=0;i<row2;i++) for(j=0;j<col2;j++) cin>>arr2[i][j]; cout<<"\nMatrix after addition\n"; add(arr1,arr2,row1,col1); cout<<"\nMatrix after subtraction\n"; sub(arr1,arr2,row1,col1); } getch(); return 0; }
void add(int arr1[20][20],int arr2[20][20],int row1,int col1) { int add1[20][20]; for(int i=0;i<row1;i++) for(int j=0;j<col1;j++) add1[i][j]=arr1[i][j]+arr2[i][j]; for(i=0;i<row1;i++)
{ for(int j=0;j<col1;j++) cout<<" "<<add1[i][j]; cout<<endl; } } void sub(int arr1[20][20],int arr2[20][20],int row1,int col1) { int add1[20][20]; for(int i=0;i<row1;i++) for(int j=0;j<col1;j++) add1[i][j]=arr1[i][j]-arr2[i][j]; for(i=0;i<row1;i++) { for(int j=0;j<col1;j++) cout<<" "<<add1[i][j]; cout<<endl; } }
OUTPUT 1:
Enter the rows and columns of 1st matrix: 2 2 Enter the rows and columns of 2nd matrix:2 2 Enter Matrix 1 7 8 9 6 Enter Matrix 2 4 5 6 3 Matrix after addition 11 13
15
OUTPUT 2:
Enter the rows and columns of 1st matrix: 2 3 Enter the rows and columns of 2nd matrix:3 2 Operation not possible