You are on page 1of 29

PROGRAMMING IN C/C++/MATLAB MUSTAK MONDAL 12408406 DEPT.

OF MATHEMATICS JIITU, NOIDA

/* Program to find the sum two integers */


#include <iostream.h> #include <conio.h> void main() { int a,b; clrscr(); cout<<"Enter the value of a = "; cin>>a; cout<<"Enter the value of b = "; cin>>b; int c; c=a+b; cout<<"The sum is = "; cout<<c; getch(); }

/* Program to find the absolute value of an integer */


#include <iostream.h> #include <math.h> void main() { int a; cout<<"Enter the value of the integer a = "; cin>>a; int b; b=abs(a); cout<<"The absolute value of the integer is = "; cout<<b; }

/* Program to find the absolute value of a real number */


#include <iostream.h> #include <math.h> #include <conio.h> void main() { clrscr(); float a; cout<<"Enter the value of the real number a = "; cin>>a; float b; b=fabs(a); cout<<"The absolute value of the real number is = "; cout<<b; getch(); }

/* Program for the setw() command */


#include <iostream.h> #include <iomanip.h>

#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 for area of a circle and to understand defining a constant */


#include <iostream.h> #include <math.h> #include <conio.h> const float PI = 3.14159;/*Value of PI can't be changed inside the program*/ void main() { float r,area; cout<<"Enter the radius of the circle r = "; cin>>r; area=PI*r*r; cout<<"The area of the circle is = "; cout<<area; getch(); }

/* Program to find the sum of 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=sum+i; /* sum+=i; */ } cout<<"The sum of the first "<<n<<" natural numbers is = "<<sum; 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 */


#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+1) { sum+=pow(-1,i+1)*i; /* sum=sum+pow(-1,i+1)*i; */ } cout<<"Sum of : 1-2+3-4+......+(-1)^"<<(n+1)<<"*"<<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;

cout<<"Sum of : 1-2+3-4+......+(-1)^"<<(n+1)<<"*"<<n<<" is = "<<sum; getch(); }

/* 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(); }

/* Program to understand the increment operator (prefix/postfix) */


#include <iostream.h> #include <math.h> #include <conio.h> void main()

{ 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(); }

/* Program for remainder operator */


#include <iostream.h> #include <math.h> #include <conio.h> void main() { clrscr(); int a,b,c; cout<<"Enter the dividend (integer) a = "; cin>>a; cout<<"Enter the divisor (integer) b = "; cin>>b; c=a%b; /* Real numbers can't be used in remainder operator (%) */ cout<<"The remainder is = "; cout<<c; getch(); }

/* Program to find the factorial of an integer*/


#include <iostream.h> #include <conio.h> void main() { clrscr(); int n; cout<<"Enter the value of n = "; cin>>n; long int fact=1; for(int i=1;i<=n;i++) fact=fact*i; cout<<"The factorial of "<<n<<" is = "<<fact;

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(); }

/* Program to find the factorial of an integer using for loop*/


#include <iostream.h> #include <conio.h> void main() { clrscr(); int n;

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(); }

/* Program to find the sum of : 1+x+x^2+x^3+x^4+x^5 */


#include <iostream.h> #include <conio.h> void main() { clrscr(); float x,sum=1; cout<<"Enter the value of x = "; cin>>x; float term=1; int i; for(i=1;i<=5;i++) { term=term*x; sum=sum+term; cout<<"\n i= "<<i<<"\n Term = "<<term<<"\n Sum = "<<sum; getch(); } cout<<"\n The sum of the series is = "<<sum; getch(); }

/* Program to find the sum : 1+(1/2!)+...+(1/n!)*/


#include <iostream.h> #include <conio.h> void main() { clrscr(); int i,n; float term,sum=1.0; long fact=1; cout<<"Enter the value of n = "; cin>>n; for(i=2;i<=n;i++) { fact=fact*i; term=1.0/fact; sum=sum+term; } cout<<"The sum of the series is = "<<sum; getch(); }

/* Program to find the sum : 1+(x/1!)+(x^2/2!)+...+(x^n/n!)*/


#include <iostream.h>

#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(); }

/* Program to find exp(x), taking upto the terms > AC=0.00005*/


#include <iostream.h> #include <conio.h> #include <math.h> void main() { int i=0; float term=1,x; float sum=0; float num=1.0; long den=1; float AC=0.00005; cout<<"Enter the value of x = "; cin>>x; do { sum=sum+term; i=i+1; num=num*x; den=den*i; term=num/den; }while(fabs(term)>AC); cout<<"The value of the exponential is = "<<sum; getch(); }

/* Program to swap the values of two variables using temporary variable*/


#include <iostream.h> #include <conio.h> void main() {

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;

/* Program to find HCF and LCM of two integers*/


#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; rem=b%a; while(rem!=0) { b=a; a=rem; rem=b%a; } cout<<"The highest common y=x/a; cout<<"The L.C.M of a and getch(); }

a = "; b = ";

factor of a and b is = "<<a<<endl; b is = "<<y;

/* Program to convert a decimal number to binary equivalent*/


#include <iostream.h> #include <conio.h> #include <math.h> void main() { clrscr(); int digit,decimal=0; long binary; int term=1; cout<<"Enter a binary number = "; cin>>binary; for(long i=binary;i>0;i=i/10)

{ digit=i%10; decimal=decimal+term*digit; term=term*2; } cout<<"The decimal equivalent of the binary number is "<<decimal; getch(); }

/* Program to convert a decimal number to equivalent binary number*/


#include <iostream.h> #include <conio.h> #include <math.h> void main() { clrscr(); int digit,decimal; long binary=0; long term=1; cout<<"Enter a decimal number = "; cin>>decimal; for(int i=decimal;i>0;i=i/2) { digit=i%2; binary=binary+term*digit; term=term*10; } cout<<"The binary equivalent of the decimal number is "<<binary; getch(); }

/* Program to enter an integer until it is divisible by 3*/


#include <iostream.h> #include <conio.h> #include <math.h> void main() { clrscr(); int a,rem; do { cout<<"Enter an integer = "; cin>>a; rem=a%3; }while(rem != 0); cout<<"The integer "<<a<<" is divisible by 3"; 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;

/* Program to display an integer e.g 123 as 1,2,3 */


#include <iostream.h> #include <conio.h> #include <math.h> void main() { clrscr(); int a; cout<<"Enter the value of a = "; cin>>a; while(a!=0) { cout<<a%10<<","; a=a/10; } getch(); }

/* Program to reverse an integer e.g 123 as 321*/


#include <iostream.h> #include <conio.h> #include <math.h> void main() { clrscr(); int a,sum=0,rem; cout<<"Enter the value of a = "; cin>>a; while(a!=0) { rem=a%10; sum=sum*10+rem; a=a/10; } cout<<"The reverse number is "<<sum; getch(); }

/* Program to check whether an integer is even or odd*/


#include <iostream.h> #include <conio.h> #include <math.h> void main() { clrscr(); int num,rem; cout<<"Enter the number = "; cin>>num; rem=num%2; if(rem == 0) cout<<"The number "<<num<<" is even"; else cout<<"The number "<<num<<" is odd"; getch(); }

/* Program to find the maximum of n numbers*/


#include <iostream.h> #include <conio.h> #include <math.h> void main() { clrscr(); int a,b,n,max; cout<<"Enter the number of values n = "; cin>>n; cout<<"Enter the first value = "; cin>>a; for(int i=1;i<n;i++) { cout<<"Enter the "<<(i+1)<<" th value = "; cin>>b; if(a>b) max=a; else max=b; a=max; } cout<<"The maximum is = "<<a; getch(); }

/* Program to check for primality of an integer */


#include <iostream.h> #include <conio.h> #include <math.h> void main() { int num,rem=1; cout<<"Enter a number = "; cin>>num;

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 check the primality of an integer */


#include <iostream.h> #include <conio.h> #include <math.h> void main() { clrscr(); int n,rem,flag=0; cout<<"Enter the number n = "; cin>>n; for(int i=2;i<n;i++) { rem=n%i; if(rem == 0) { flag=1; break; } } if(flag == 1) cout<<"The number is not prime"; else cout<<"The number is prime"; getch(); }

/*Program to find the sum of squares of n natural numbers using array*/


#include <iostream.h> #include <conio.h> void main() { clrscr(); int i,n=10; float a[10]; for(i=0;i<10;i++) a[i]=i+1; float sum=0; for(i=0;i<10;i++) sum+=a[i]*a[i];

cout<<"The sum is :"<<sum; getch(); }

/*Program to find the sum of squares of n natural numbers using array*/


#include <iostream.h> #include <conio.h> void main() { clrscr(); int i,n=10; float a[10]; for(i=0;i<10;i++) a[i]=i+1; float sum=0; for(i=0;i<10;i++) sum+=a[i]*a[i]; cout<<"The sum is :"<<sum; getch(); }

/*Program to find the H.C.F of n numbers */


#include <iostream.h> #include <conio.h> #include <math.h> void main() { clrscr(); int a,b,n,rem; cout<<"Enter the number of values n = "; cin>>n; cout<<"Enter the first value = "; cin>>a; for(int i=1;i<n;i++) { cout<<"Enter the "<<(i+1)<<" th value = "; cin>>b; rem=b%a; while(rem !=0) { b=a; a=rem; rem=b%a; } } cout<<"The H.C.F is "<<a<<endl; 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 = ";

factor of a and b is = "<<a<<endl; b is = "<<y;

/*Program to find the sum of squares of n natural numbers using array*/


#include <iostream.h> #include <conio.h> void main() { clrscr(); int i,n=10; float a[10]; for(i=0;i<10;i++) a[i]=i+1; float sum=0; for(i=0;i<10;i++) sum+=a[i]*a[i]; cout<<"The sum is :"<<sum; getch(); }

/*Program to find the sum of 1^2-2^2+3^2-....-10^2 using array*/


#include <iostream.h> #include <conio.h> #include <math.h> void main() { clrscr(); int i,n=10; float a[10]; for(i=0;i<10;i++) a[i]=i+1;

float sum=0; for(i=0;i<10;i++) sum+=pow(-1,i)*a[i]*a[i]; cout<<"The sum is :"<<sum; getch(); }

/*Program to find the maximum of n numbers using array*/


#include <iostream.h> #include <conio.h> const int MAX=10; void main() { clrscr(); int i,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 max=a[0]; for(i=1;i<n;i++) if(max<a[i]) max=a[i]; cout<<"The maximum of "<<n<<" numbers is : "<<max; getch(); }

/*Program to find the minimum of n numbers using array*/


#include <iostream.h> #include <conio.h> const int MAX=10; void main() { clrscr(); int i,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 min=a[0]; for(i=1;i<n;i++) if(min>a[i]) min=a[i]; cout<<"The minimum of "<<n<<" numbers is : "<<min; getch(); }

/*Program to arrange the elements of an array of n numbers in descending order*/

#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(); }

/*Program to arrange the elements of an array of n numbers in ascending order*/


#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 ascending order is : "; for(i=0;i<n;i++)

cout<<a[i]<<setw(5); getch(); }

/*Program to arrange the elements of an array of n numbers in ascending order*/


#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=j+1;i<n;i++) if(a[j]>a[i]) { temp=a[j]; a[j]=a[i]; a[i]=temp; } cout<<"The numbers in ascending order is : "; for(i=0;i<n;i++) cout<<a[i]<<setw(5); getch(); }

/*Program to arrange the elements of an array of n numbers in descending order*/


#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=j+1;i<n;i++) if(a[j]<a[i])

{ 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(); }

/*Program to reverse the elements of an array of n numbers 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],b[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;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 and access a structure variable*/


#include <iostream.h> #include <conio.h> struct distance { int feet; float inches; }; void main() { clrscr(); distance d1; d1.feet=5;/*cin>>d1.feet;*/ d1.inches=6.8;/*cin>>d1.inches;*/ cout<<"Your input distance is : "; cout<<d1.feet<<" feet "<<d1.inches<<" inches"; getch(); }

/*Program to add two distances using structure variable*/


#include <iostream.h> #include <conio.h> struct distance { int feet; float inches; }; void main() { clrscr(); distance d1,d2,d3; cout<<"Enter the first distance in feet and inches : "; cin>>d1.feet>>d1.inches; cout<<"Enter the second distance in feet and inches : "; cin>>d2.feet>>d2.inches; d3.feet=d1.feet+d2.feet; d3.inches=d1.inches+d2.inches; if(d3.inches>=12) { d3.inches-=12; d3.feet++; } cout<<"The addition of the two distances is : ";

cout<<d3.feet<<" feet "<<d3.inches<<" inches"; getch(); }

/*Program to define a structure variable for date*/


#include <iostream.h> #include <conio.h> struct date { int day,month,year; }; void main() { clrscr(); date x; cout<<"Enter the date in the form (day month year) : "; cin>>x.day>>x.month>>x.year; cout<<"The date you have entered is : "; cout<<x.day<<"/"<<x.month<<"/"<<x.year; 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(); }

/*Program to store a 2D array and print the same*/


#include <iostream.h> #include <conio.h> #include <iomanip.h> const int MAX=10; void main() { clrscr(); int A[MAX][MAX],i,j,m=2,n=3; for(i=0;i<m;i++) for(j=0;j<n;j++) { cout<<"Enter the element A["<<i<<"]["<<j<<"] = "; cin>>A[i][j]; } cout<<"The 2D array/matrix you have entered is : "<<endl; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cout<<setw(5)<<A[i][j]; } cout<<endl; } getch(); }

/*Program to add two matrices*/


#include <iostream.h> #include <conio.h> #include <iomanip.h> #include <process.h> const int MAX=10; void main() { clrscr(); int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX],i,j,m,n,p,q; cout<<"Enter the order of the first matrix mXn : "; cin>>m>>n; cout<<"Enter the order of the second matrix pXq : "; cin>>p>>q;

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(); }

/*Program to multiply two matrices*/


#include <iostream.h> #include <conio.h> #include <iomanip.h> #include <process.h> const int MAX=10; void main() { clrscr(); int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX],i,j,k,m,n,p,q; cout<<"Enter the order of the first matrix mXn : "; cin>>m>>n; cout<<"Enter the order of the second matrix pXq : "; cin>>p>>q; if(n==p)

{ 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(); }

/*Program to multiply a scalar to a matrix*/


#include <iostream.h> #include <conio.h> #include <iomanip.h> const int MAX=10; void main() { clrscr(); int A[MAX][MAX],B[MAX][MAX],i,j,m,n; cout<<"Enter the order of the matrix mXn : "; cin>>m>>n; cout<<"Enter the matrix : "; for(i=0;i<m;i++) for(j=0;j<n;j++)

{ 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(); }

/*Program to produce an identity matrix*/


#include <iostream.h> #include <conio.h> #include <iomanip.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; for(i=0;i<n;i++) for(j=0;j<n;j++) { if(i==j) A[i][j]=1; else A[i][j]=0; } for(i=0;i<n;i++) { for(j=0;j<n;j++) { cout<<A[i][j]<<setw(5); } cout<<endl; } getch(); }

You might also like