1.//Program to find whether a given number is prime
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n; cout<<"Enter the number\n"; cin>>n; int i=2; while(i<=n-1) { if(n%i==0) { cout<<n<<" isnt prime\n"; break; } i++; } if(i==n) cout<<n<<" is a prime"; getch(); }
2.//Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + 1/5 +1/n #include<iostream.h>
#include<conio.h>
void main()
{
int i,n;
float sum=0;
cout<<"Enter the value of n ";
cin>>n;
for(i=1;i<=n;i++)
sum += 1.0/i;
cout<<"\nSum : "<<sum;
getch();
}
//Strings 1.//Program to find number of vowels in given string #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); char str[100]; cout<<"Enter string\n"; gets(str); int nv=0; for(int i=0;str[i]!='\0';++i)
if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[ i]=='U'||str[i]=='i'||str[i]=='I') ++nv; cout<<"No. of vowels "<<nv; getch();
}
2.//Program to display string from backward #include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main( )
{
clrscr( );
char str[80];
cout<<"Enter a string:";
gets(str);
for(int l=0; str[l]!='\0';++l); //Loop to find the length of the string
for(int i=l-1;i>=0;--i) //Loop to display the string backwards
cout<<\nThe string in backward is <<str[i];
getch();
}
//1-D Arrays 1.//Program to input 2 arrays and merge them into one #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10],b[10],c[20],m,n,i,j; cout<<"Enter the size of first array"; cin>>m; cout<<"\nEnter elements"; for(i=0;i<m;++i) cin>>a[i]; cout<<"\nEnter the size of second array"; cin>>n; cout<<"\nEnter elements"; for(j=0;j<n;++j) cin>>b[j]; for(i=0;i<m;++i) //Copying a in c c[i]=a[i]; for(j=0;j<n;++j) c[i+j]=b[j]; //Copying b in c cout<<"\nThe merged array is\n"; for(i=0;i<m+n;++i) cout<<c[i]<<'\t'; getch(); } 2.//Program to display largest and smallest element in 1-d array #include<iostream.h> #include<conio.h> void main() { int Arr[100],n,small,large; cout<<"Enter number of elements you want to insert "; cin>>n; cout<<"\nEnter elements "; for(i=0;i<n;++i) { cin>>Arr[i]; } small=Arr[0]; large=Arr[0];
for(i=1;i<n;i++) { if(Arr[i]<small) small=Arr[i]; if(Arr[i]>large) large=Arr[i]; } cout<<"\nLargest element is :"<<large; cout<<"\nSmallest element is :"<<small; getch(); } //2-D ARRAYS 1.//PROGRAM TO COUNT ODD ELEMENTS IN 2-D ARRAY #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[10][10],b[10][10],c[10][10],cod=0,p,q,i,j; cout<<"Enter rows and columns of first array"; cin>>p>>q; cout<<"\nEnter the elements"; for(i=0;i<p;++i) { for(j=0;j<q;++j) cin>>a[i][j]; } for(i=0;i<p;++i) { for(j=0;j<q;++j) if(a[i][j]%2==1) ++cod; } cout<<"\nNo. of odd elements "<<cod; getch(); } 2.//Program to input matrix & display its transpose #include<iostream.h> #include<conio.h> void main() { int a[10][10],b[10][10],i,j,p,q; cout<<"Enter rows and columns"; cin>>p>>q; cout<<"\nEnter the elements"; for(i=0;i<p;++i) { for(j=0;j<q;++j) cin>>a[i][j]; } cout<<"\nThe array is"; for(i=0;i<p;++i) { for(j=0;j<q;++j) cout<<a[i][j]<<'\t'; cout<<endl; } for(i=0;i<p;++i) { for(j=0;j<q;++j) b[j][i]=a[i][j]; } cout<<"\nThe transpose is"; for(i=0;i<q;++i) { for(j=0;j<p;++j) cout<<b[i][j]<<'\t'; cout<<endl; }
getch();
}
//Functions 1.//Program to print area and perimeter of a circle #include<iostream.h> #include<conio.h> void cal(float R) { cout<<The area of circle is<<3.14*R*R; cout<<\nThe perimeter of circle is<<2*3.14*R; } void main() { clrscr(); float r; cout<<Enter the radius of the circle; cin>>r; cal(r); getch(); }
2.//Program to Display greater of 3 numbers #include<iostream.h> #include<conio.h> void comp(int A,int B,int C) { if(A>B && A>C) { cout<<A<<is greatest; } else if(B>C) { cout<<B<<is greatest; } else cout<<c<<is greatest; } void main() { int i,j,k; cout<<Enter 3 numbers you want to compare; cin>>i>>j>>k; comp(i,j,k); }