Skip to main content
/** To print diagonal elements of a matrix and its Sum*/

import java.util.Scanner;

public class DiagonalElements

{

    public static void main(String [] args)

    {

        int i,j;

        final int SIZE =3;   

        Scanner input = new Scanner(System.in);

        int [][] arr = new int [SIZE][SIZE];
       
        System.out.println("Enter Array Elements");       

        for(i =0; i<3;i++ )

        {

            for(j=0;j<3;j++)

            {

                arr[i][j] = input.nextInt();

            }

        }       

         System.out.println("Entered Array");

                for(i=0;i<3;i++)

                {

                System.out.println();

                        for(j=0;j<3;j++)

                          {

                                        System.out.print(arr[i][j]+"\t");

                        }

                }

                System.out.println();


        System.out.println("Diagonal Elements");

        int sum =0;

        for(i=0;i<3;i++)

        {

        System.out.println();

            for(j=0;j<3;j++)

            {

                if(i==j||i+j==2)
               
                {

                    System.out.print(arr[i][j]+"\t");

                    sum = sum + arr[i][j];               

                }
           
                else

                    System.out.print(" \t");   
            }

        }

        System.out.println(" sum of diagonal elements =" + sum);

    }

}


Comments

Popular posts from this blog

Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables‐a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named get Invoice Amount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities.

Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables‐a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named get Invoice Amount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities. import java.io.*; class Invoice {     BufferedReader br = new BufferedReader(new InputStreamReader(System.i...

Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. In addition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information about the book). You should use this keyword in member methods and constructor. Write a test application named BookTest to create an array of object for 30 elements for class Book to demonstrate the class Book's capabilities.

Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. In addition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information about the book). You should use this keyword in member methods and constructor. Write a test application named BookTest to create an array of object for 30 elements for class Book to demonstrate the class Book's capabilities. import java.io.*; class Book {     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));     String book_name;     int isbn;     String author;     String publisher; ...

Program to find the power of a number without using inbuilt function.

import java.io.*; class PowerOfANumber {     public static void main(String [] args){         int x=5,y=5;         System.out.println(power(x,y));     }     public static int power(int x , int y){         if(y==0)             return 1;         else if(y%2==0)             return power(x,y/2)*power(x,y/2);         else             return x*power(x,y/2)*power(x,y/2);     } }