Professional Documents
Culture Documents
#include <conio.h> void main() { clrscr(); int a,b,c; cout<<"Enter the value of the integer a = "; cin>>a; cout<<"Enter the value of the integer b = "; cin>>b; cout<<"Enter the value of the integer c = "; cin>>c; cout<<"Generally the display of the integers is : "; cout<<a<<b<<c; cout<<endl; cout<<"Using setw() command the display of the integers is : "; cout<<setw(5)<<a<<setw(5)<<b<<setw(5)<<c; getch(); }
/* Program to find the sum of the squares of the first n natural numbers */
#include <iostream.h> #include <math.h> #include <conio.h> void main() { clrscr(); int n; int sum=0; cout<<"Enter the value of n = "; cin>>n; for(int i=1;i<=n;i++) { sum+=i*i; /* sum=sum+i*i; */ } cout<<"Sum of the squares of the first "<<n<<" natural numbers="<<sum; getch(); }
/* Program to find the sum of the odd natural numbers less than or equal to n */
#include <iostream.h> #include <math.h> #include <conio.h> void main() { clrscr(); int n; int sum=0; cout<<"Enter the value of n = "; cin>>n; for(int i=1;i<=n;i=i+2) { sum+=i; /* sum=sum+i; */ } cout<<"Sum of the odd natural numbers <= "<<n<<" is = "<<sum; getch(); }
/* Program to find the sum of the even natural numbers less than or equal to n */
#include <iostream.h> #include <math.h> #include <conio.h> void main() { clrscr(); int n; int sum=0; cout<<"Enter the value of n = ";
cin>>n; for(int i=2;i<=n;i=i+2) { sum+=i; /* sum=sum+i; */ } cout<<"Sum of the even natural numbers <= "<<n<<" is = "<<sum; getch(); }
/* Program to find the sum of the series : 1-2+3-4+........+(1)^(n+1)*n */ /* 1-2+3-4+..........+(-1)^(n+1)*n = (1+3+.....upto odd natural number <= n) -(2+4+.....upto even natural number <= n) */
#include <iostream.h> #include <math.h> #include <conio.h> void main() { clrscr(); int n,sum; int sum1=0,sum2=0; cout<<"Enter the value of n = "; cin>>n; for(int i=1;i<=n;i=i+2) { sum1+=i; /* sum1=sum1+i; */ } for(int j=2;j<=n;j=j+2) { sum2+=j; /* sum2=sum2+j; */ } sum=sum1-sum2;
/* Program to find the outputs of some expressions having relational operators : >, <, >=, <=, ==, != */
#include <iostream.h> #include <math.h> #include <conio.h> void main() { clrscr(); int a1,a2,a3,a4,a5; int a,b; cout<<"Enter the value of the integer a = "; cin>>a; cout<<"Enter the value of the integer b = "; cin>>b; cout<<"The results are : "<<'\n'<<'\n'; a1=(a==b); if(a1==0) cout<<"The statement (a==b) is FALSE"<<'\n'; else cout<<"The statement (a==b) is TRUE"<<'\n'; a2=(a<b); if(a2==0) cout<<"The statement (a<b) is FALSE"<<'\n'; else cout<<"The statement (a<b) is TRUE"<<'\n'; a3=(b<=5); if(a3==0) cout<<"The statement (b<=5) is FALSE"<<'\n'; else cout<<"The statement (b<=5) is TRUE"<<'\n'; a4=(3<a); if(a4==0) cout<<"The statement (3<a) is FALSE"<<'\n'; else cout<<"The statement (3<a) is TRUE"<<'\n'; a5=(a%2); if(a5==0) cout<<"The statement (a%2) is FALSE"<<'\n'; else cout<<"The statement (a%2) is TRUE"<<'\n'; getch(); }
{ clrscr(); int a,b,a1,b1; cout<<"Enter the value of the integer a = "; cin>>a; cout<<"Enter the value of the integer b = "; cin>>b; cout<<'\n'; cout<<"For prefix increment operator : "<<'\n'; a1=++a; cout<<"The value of a1=++a="<<a1<<'\n'; cout<<"The value of a is = "<<a<<'\n'; cout<<'\n'; cout<<"For postfix increment operator : "<<'\n'; b1=b++; cout<<"The value of b1=b++="<<b1<<'\n'; cout<<"The value of b is = "<<b; getch(); }
/* Program to understand the decrement operator (prefix/postfix) */ /* increment and decrement operators works on both integer and floating point numbers */
#include <iostream.h> #include <math.h> #include <conio.h> void main() { clrscr(); float a,b,a1,b1; cout<<"Enter the value of the integer a = "; cin>>a; cout<<"Enter the value of the integer b = "; cin>>b; cout<<'\n'; cout<<"For prefix decrement operator : "<<'\n'; a1=--a; cout<<"The value of a1=--a="<<a1<<'\n'; cout<<"The value of a is = "<<a<<'\n'; cout<<'\n'; cout<<"For postfix decrement operator : "<<'\n'; b1=b--; cout<<"The value of b1=b--="<<b1<<'\n'; cout<<"The value of b is = "<<b; getch(); }
/* Program to find the outputs of some expressions having increment or decrement operators or both */
#include <iostream.h> #include <math.h> #include <conio.h> void main()
{ clrscr(); int a,b,c,d,a1,b1,c1,d1; cout<<"Enter the value of the integer a = "; cin>>a; cout<<"Enter the value of the integer b = "; cin>>b; cout<<"Enter the value of the integer c = "; cin>>c; cout<<"Enter the value of the integer d = "; cin>>d; cout<<'\n'; cout<<"The results are : "<<'\n'; a1=(++a)*(++a); cout<<"The value of a1=(++a)*(++a)= "<<a1<<'\n'; b1=(++b)*(b++); cout<<"The value of b1=(++b)*(b++)= "<<b1<<'\n'; c1=(++c)*(--c); cout<<"The value of c1=(++c)*(--c)= "<<c1<<'\n'; d1=(++d)*(d--); cout<<"The value of d1=(++d)*(d--)= "<<d1; getch(); }
getch(); }
/* Program to find the sum of first n natural numbers using while loop*/
#include <iostream.h> #include <conio.h> void main() { clrscr(); int n,i; cout<<"Enter the value of n = "; cin>>n; int sum=0; i=1; while(i<=n) { sum+=i; i++; } cout<<"The sum of first "<<n<<" natural numbers is "<<sum; getch(); }
/* Program to find the sum of first n natural numbers using do-while loop*/
#include <iostream.h> #include <conio.h> void main() { clrscr(); int n,i; cout<<"Enter the value of n = "; cin>>n; int sum=0; i=0; do { sum=sum+i; i++; } while(i<=n); cout<<"The sum of first "<<n<<" natural numbers is = "<<sum; getch(); }
cout<<"Enter the value of n = "; cin>>n; long fact=1; for(int i=1;i<=n;i++) fact=fact*i; cout<<"The factorial of the number "<<n<<" is = "<<fact; getch(); }
#include <conio.h> void main() { clrscr(); int n; cout<<"Enter the value of n = "; cin>>n; float sum=1.0,term,num=1.0,x; long den=1; cout<<"Enter the value of x = "; cin>>x; for(int i=1;i<=n;i++) { num=num*x; den=den*i; term=num/den; sum=sum+term; } cout<<"The sum of the series is = "<<sum; getch(); }
clrscr(); int a,b,temp; cout<<"Enter the value of cin>>a; cout<<"Enter the value of cin>>b; temp=a; a=b; b=temp; cout<<"The new value of a cout<<"The new value of b getch(); }
a = "; b = ";
is = "<<a<<endl; is = "<<b<<endl;
a = "; b = ";
{ digit=i%10; decimal=decimal+term*digit; term=term*2; } cout<<"The decimal equivalent of the binary number is "<<decimal; getch(); }
/* Program to swap the values of two variables without using a temporary variable*/
#include <iostream.h> #include <conio.h> void main() { clrscr(); int a,b; cout<<"Enter the value of cin>>a; cout<<"Enter the value of cin>>b; a=a+b; b=a-b; a=a-b; cout<<"The new value of a cout<<"The new value of b getch(); }
a = "; b = ";
is = "<<a<<endl; is = "<<b<<endl;
int i=2; while(i<=num-1 && rem !=0) { rem=num%i; if(rem == 0) { cout<<"The number is not prime"; } i=i+1; } if(i == num) cout<<"The number is prime"; getch(); }
/* Program to find the H.C.F and L.C.M of two numbers using do-while loop */
#include <iostream.h> #include <conio.h>
#include <math.h> void main() { clrscr(); int a,b; int x,y; cout<<"Enter the value of cin>>a; cout<<"Enter the value of cin>>b; x=a*b; int rem=a; a=b; do { b=a; a=rem; rem=b%a; }while(rem!=0); cout<<"The highest common y=x/a; cout<<"The L.C.M of a and getch(); }
a = "; b = ";
#include <iostream.h> #include <conio.h> #include <iomanip.h> const int MAX=10; void main() { clrscr(); int i,j,n; float a[MAX]; cout<<"Enter the number of variables = "; cin>>n; cout<<"Enter "<<n<<" values : "; for(i=0;i<n;i++) cin>>a[i]; float temp; for(j=0;j<n-1;j++) for(i=0;i<n-1;i++) if(a[i]<a[i+1]) { temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } cout<<"The numbers in descending order is : "; for(i=0;i<n;i++) cout<<a[i]<<setw(5); getch(); }
cout<<a[i]<<setw(5); getch(); }
{ temp=a[j]; a[j]=a[i]; a[i]=temp; } cout<<"The numbers in descending order is : "; for(i=0;i<n;i++) cout<<a[i]<<setw(5); getch(); }
/*Program to reverse the elements of an array of n numbers without using an another array*/
#include <iostream.h> #include <conio.h> #include <iomanip.h> const int MAX=10; void main() { clrscr(); int i,j,n; float a[MAX]; cout<<"Enter the number of variables = "; cin>>n; cout<<"Enter "<<n<<" values : "; for(i=0;i<n;i++) cin>>a[i]; float temp; for(i=0;i<n/2;i++) { temp=a[i]; a[i]=a[n-i-1]; a[n-i-1]=temp; } cout<<"The reversed array is : "; for(i=0;i<n;i++) cout<<a[i]<<setw(5); getch(); }
cout<<"Enter "<<n<<" values : "; for(i=0;i<n;i++) cin>>a[i]; float temp; for(i=0;i<n;i++) b[n-i-1]=a[i]; cout<<"The reversed array is : "; for(i=0;i<n;i++) cout<<b[i]<<setw(5); getch(); }
/*Program to define a structure for complex numbers and to perform the operations :(1) addition, (2) multiplication, (3) division and (4) print*/
#include <iostream.h> #include <conio.h> struct complex { float real,imag; }; void main() { clrscr(); complex x1,x2,x3,x4,x5; cout<<"Enter the first complex number in the form (real imag) : "; cin>>x1.real>>x1.imag; cout<<"Enter the second complex number in the form (real imag) : "; cin>>x2.real>>x2.imag; x3.real=x1.real+x2.real; x3.imag=x1.imag+x2.imag; cout<<"The addition of the two complex numbers you have entered is : "; cout<<x3.real<<"+i"<<x3.imag<<endl; x4.real=x1.real*x2.real-x1.imag*x2.imag; x4.imag=x1.real*x2.imag+x1.imag*x2.real; cout<<"The multiplication of the two complex numbers you have entered is : "; cout<<x4.real<<"+i"<<x4.imag<<endl; if(x2.real*x2.real+x2.imag*x2.imag>0) {
cout<<"The division x1/x2 is possible and the division is : "; x5.real=(x1.real*x2.real+x1.imag*x2.imag)/(x2.real*x2.real+x2.ima g*x2.imag); x5.imag=(x2.real*x1.imagx2.imag*x1.real)/(x2.real*x2.real+x2.imag*x2.imag); cout<<x5.real<<"+i"<<x5.imag; } else cout<<"Division is not possible"; getch(); }
if(m==p && n==q) { cout<<"Sum of the matrices is possible"; } else { cout<<"Sum of the two matrices is not possible"; exit(0); } cout<<"Enter the first matrix : "; for(i=0;i<m;i++) for(j=0;j<n;j++) { cout<<"Enter the element A["<<i<<"]["<<j<<"] = "; cin>>A[i][j]; } cout<<"Enter the second matrix : "; for(i=0;i<p;i++) for(j=0;j<q;j++) { cout<<"Enter the element B["<<i<<"]["<<j<<"] = "; cin>>B[i][j]; } for(i=0;i<m;i++) { for(j=0;j<n;j++) { C[i][j]=A[i][j]+B[i][j]; } } cout<<"The matrix after addition is : "<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<setw(5)<<C[i][j]; } cout<<endl; } getch(); }
{ cout<<"Product of the matrices is possible"; } else { cout<<"Product of the two matrices is not possible"; exit(0); } cout<<"Enter the first matrix : "; for(i=0;i<m;i++) for(j=0;j<n;j++) { cout<<"Enter the element A["<<i<<"]["<<j<<"] = "; cin>>A[i][j]; } cout<<"Enter the second matrix : "; for(i=0;i<p;i++) for(j=0;j<q;j++) { cout<<"Enter the element B["<<i<<"]["<<j<<"] = "; cin>>B[i][j]; } for(i=0;i<m;i++) { for(j=0;j<q;j++) { C[i][j]=0; for(k=0;k<n;k++) C[i][j]+=A[i][k]+B[k][j]; } } cout<<"The matrix after multiplication is : "<<endl; for(i=0;i<m;i++) { for(j=0;j<q;j++) { cout<<setw(5)<<C[i][j]; } cout<<endl; } getch(); }
{ cout<<"Enter the element A["<<i<<"]["<<j<<"] = "; cin>>A[i][j]; } for(i=0;i<m;i++) { for(j=0;j<n;j++) { B[i][j]=2*A[i][j]; } } cout<<"The matrix after scalar multiplication is : "<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<setw(5)<<B[i][j]; } cout<<endl; } getch(); }
/*Program to find the sum of the principle diagonal, upper principal diagonal, lower principal diagonal, cross diagonal elements of a square matrix*/
#include <iostream.h> #include <conio.h> const int MAX=10; void main() { clrscr(); int A[MAX][MAX],i,j,n; cout<<"The order of the matrix nXn : Enter n "; cin>>n; cout<<"Enter the matrix : "; for(i=0;i<n;i++) for(j=0;j<n;j++) { cout<<"Enter the element A["<<i<<"]["<<j<<"] = "; cin>>A[i][j]; } float sum1=0; for(i=0;i<n;i++) { sum1+=A[i][i]; } cout<<"The sum of the principal diagonal elements is : "<<sum1; getch(); float sum2=0; for(i=0;i<n-1;i++) { sum2+=A[i][i+1]; }
cout<<"The sum of the upper principal diagonal elements is : "<<sum2; getch(); float sum3=0; for(i=1;i<n;i++) { sum3+=A[i+1][i]; } cout<<"The sum of the lower principal diagonal elements is : "<<sum3; getch(); float sum4=0; for(i=0;i<n;i++) { sum4+=A[i][n-1-i]; } cout<<"The sum of the cross diagonal elements is : "<<sum4; getch(); }