Professional Documents
Culture Documents
3
#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(); }
7. 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(); }
8. 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(); }
9. 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 = ";
5
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(); }
11. Program to find the sum of the series : 1-2+34+........+(-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;
6
getch(); }
12. 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(); }
7
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(); }
14. 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(); }
15. 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();
8
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(); }
16.
#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(); }
17.
#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(); }
18. 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(); }
19. 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(); }
10
long fact=1; for(int i=1;i<=n;i++) fact=fact*i; cout<<"The factorial of the number "<<n<<" is = "<<fact; getch(); }
21.
#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(); }
22.
#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(); }
23.
11
{ 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(); }
24. 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(); }
25. Program to swap the values of two variables using temporary variable
#include <iostream.h> #include <conio.h> void main() { clrscr(); int a,b,temp;
12
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;
26.
#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 = ";
27.
#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;
13
decimal=decimal+term*digit; term=term*2; } cout<<"The decimal equivalent of the binary number is "<<decimal; getch(); }
29.
#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(); }
30. Program to swap the values of two variables without using a temporary variable
14
#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;
31.
#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(); }
32.
#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(); }
15
33.
#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(); }
34.
#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(); }
35.
#include <iostream.h> #include <conio.h> #include <math.h> void main() { int num,rem=1; cout<<"Enter a number = "; cin>>num;
16
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(); }
36.
#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(); }
37. 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];
17
cout<<"The sum is :"<<sum; getch(); }
38. 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(); }
39.
#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(); }
40. Program to find the H.C.F and L.C.M of two numbers using do-while loop
#include <iostream.h> #include <conio.h>
18
#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 = ";
41. 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(); }
19
float sum=0; for(i=0;i<10;i++) sum+=pow(-1,i)*a[i]*a[i]; cout<<"The sum is :"<<sum; getch(); }
43.
#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(); }
44.
#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(); }
20
#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(); }
21
cout<<a[i]<<setw(5); getch(); }
22
{ 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(); }
49. 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(); }
50. 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;
23
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(); }
51.
#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(); }
52.
#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 : ";
24
cout<<d3.feet<<" feet "<<d3.inches<<" inches"; getch(); }
53.
#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(); }
54. 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) {
25
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(); }
55.
#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(); }
56.
#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;
26
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(); }
57.
#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)
27
{ 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(); }
58.
#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++)
28
{ 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(); }
59. 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]; }
29
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(); }
60.
#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(); }