Welcome To Uday Satya Blog

11-20 PROGRAMS



11   Program to produce the conversion table for dollar and rupees using CLA

class dollarcla
{
public static void main(String s[])
{
int n,d;
n=Integer.parseInt(s[0]);
System.out.println("Dollars\t\tRupees" );
for(int i=1;i<=n;i++)
{
d=Integer.parseInt(s[i]);
System.out.println(d+"\t=\t"+(d*45) );
}
}
}

OUTPUT :

java dollarcla 4 1 34 36 37
Dollars         Rupees
1       =       45
34      =       1530
36      =       1620
37      =       1665


12  Program to print area  of right angled triangle using CLA

class AreaTri
{
public static void main(String s[])
{
int a,b,area;
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);
area=a*b/2;
System.out.println("Area of Right angled Triangle = "+area);
}
}

OUTPUT :

java AreaTri 5 4

Area of Right angled Triangle = 10


13  Program to print all the elements in an array using CLA

class matrixcla
{
public static void main(String s[])
{
int n,m;
n=Integer.parseInt(s[0]);
System.out.println("The length of the matrix is: " +n);
System.out.println("The elements of the matrix are:");
for(int i=1;i<=n;i++)
{
m=Integer.parseInt(s[i]);
System.out.println(m);
}
}
}

OUTPUT :

java matrixcla 5 10 34 36 37 99
The length of the matrix is: 5
The elements of the matrix are:
10
34
36
37
99


14  Program to find the sum of all the elements in an array

class Sum
{
public static void main(String args[])
{
double nums[]={1.1,1.2,1.3,1.4,1.5};
double result=0;
System.out.println("The Elements of Array are :");
for(int i=0;i<5;i++)
            {
            System.out.println(nums[i]);
            result=result+nums[i];
            }
System.out.println("Sum is " + result);
}
}

OUTPUT :

The Elements of Array are :
1.1
1.2
1.3
1.4
1.5
Sum is 6.5


15  Program to print all the elements in a 2D array

class Matrix
{
public static void main(String s[]) 
{
int a[][]= {{1,2,3},{4,5,6}};
System.out.println("Number of Row= " + a.length);
System.out.println("Number of Column= " + a[1].length);
System.out.println("The elements of the matrix are : ");
for(int i = 0; i
{
for(int j = 0; j < a[1].length; j++)
{
System.out.print(" "+ a[i][j]);
} 
System.out.println();
}
}}

OUTPUT :

Number of Row= 2
Number of Column= 3
The elements of the matrix are :
 1 2 3
 4 5 6
16  Program to find the sum of two matrices

class addm
{
public static void main(String args[])
{
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int b[][]={{1,1,1},{0,1,0},{0,0,1}};
int c[][]= new int[3][3];
int i,j;
for(i=0;i<3;i++)
            for( j=0;j<3;j++)
                        c[i][j]=a[i][j]+b[i][j];
for(i=0;i<3;i++)
            {
            for(j=0;j<3;j++)
                        System.out.print(+c[i][j]+ "  ");
            System.out.println();
            }
}
}
                                   
OUTPUT :

2  3  4
4  6  6
7  8  10         


17  Program to find the difference of two matrices using CLA

class Matrixcla {
public static void main(String s[])  {
int a[][]=new int[2][2];
int b[][]=new int[2][2];
int x,y,k=0;
System.out.println("First Matrix : ");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
x=Integer.parseInt(s[k]);
a[i][j]=x;
k++;
System.out.print(" "+ a[i][j]); } 
System.out.println(); } 
System.out.println("Second Matrix : ");
for(int i = 0; i <2; i++) {
for(int j = 0; j <2; j++) {
y=Integer.parseInt(s[k]);
b[i][j]=y;
k++;
System.out.print(" "+b[i][j]); } 
System.out.println(); }
System.out.println("Difference of two matrix : ");
for(int i = 0; i <2; i++) {
for(int j = 0; j <2; j++)
System.out.print(" "+(a[i][j]-b[i][j]));
System.out.println();
}}}

OUTPUT :
java Matrixcla 6 7 8 9 5 5 5 5
First Matrix :
 6 7
 8 9
Second Matrix :
 5 5
 5 5
Difference of two matrix :
 1 2
 3 4
18  Program to print the UPPER and LOWER triangle of a matrix
class UppLow{
public static void main(String arsg[]) {
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
System.out.println("the given matrix:");
for(int i=0;i<3;i++)  {
for(int j=0;j<3;j++) 
System.out.print(" "+a[i][j]);
System.out.println(); }
System.out.println("upper triangle of matrix:");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(i<=j)
System.out.print(a[i][j] + " ");
else
System.out.print("  "); }
System.out.println(); }
System.out.println("lower triangle of matrix:");
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(i>=j)
System.out.print(a[i][j] + " ");
else
System.out.print("  "); }
System.out.println();
}}}

OUTPUT :
The given matrix:
1 2 3
4 5 6
7 8 9
Upper triangle of matrix:
1 2 3
  5 6
    9
Lower triangle of matrix:
1
4 5
7 8 9
19  Program to find the trace of a matrix

class Trace
{
public static void main(String args[])
{
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int i,j,sum=0;
System.out.println("The given matrix : ");
for(int i = 0; i <3; i++)
            {
            for(int j = 0; j <3; j++)
            System.out.print(a[i][j] + " "); 
            System.out.println();
            }
for(i=0;i<3;i++)
            {
            for(j=0;j<3;j++)
                        if(i==j)
                           sum=sum+a[i][j];
            }
System.out.println("Trace is : " +sum);
}
}
                                   
OUTPUT :

The given matrix :
1 2 3
4 5 6
7 8 9
Trace is : 15            


20  Program to find the transpose of a matrix

class Transpose
{
public static void main(String args[])
{
int a[][]={{1,2,3},{4,5,6},{7,8,9}};
int i,j;
System.out.println("The given matrix : ");
for(int i = 0; i <3; i++)
            {
            for(int j = 0; j <3; j++)
            System.out.print(a[i][j] + " "); 
            System.out.println();
            }
System.out.println("Transpose of Matrix : ");
for(i=0;i<3;i++)
            {
            for(j=0;j<3;j++)
                        System.out.print(a[j][i]+ "  ");
            System.out.println();
            }
}
}
           
OUTPUT :

The given matrix :
1  2  3
4  5  6
7  8  9
Transpose of Matrix :
1  4  7
2  5  8
3  6  9  

0 comments:

Post a Comment