Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Java File

The document discusses various Java programming concepts like taking input from command line, arithmetic, relational, logical, assignment, bitwise and unary operators, if-else conditional statements, switch case, loops, finding area of geometric shapes, and determining leap years. Examples of code implementing these concepts are provided.

Uploaded by

Sachin Chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java File

The document discusses various Java programming concepts like taking input from command line, arithmetic, relational, logical, assignment, bitwise and unary operators, if-else conditional statements, switch case, loops, finding area of geometric shapes, and determining leap years. Examples of code implementing these concepts are provided.

Uploaded by

Sachin Chauhan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Taking input from Command line argument

/* taking input from command line arguments */


class DEmo2{
    public static void main(String[] args) {
       
        System.out.println(args[0]+args[1]);
    int x = Integer.parseInt(args[0]);
    int y= Integer.parseInt(args[2]);
    System.out.println(x+y);
    }
}

OPERATORS :

1- Arithemic Operators
2- class Arithemic{
3-     public static void main(String[] args) {
4-         int a= 15,b=3;
5-         System.out.println(a+b);
6-         System.out.println(a-b);
7-         System.out.println(a*b);
8-         System.out.println(a/b);
9-         System.out.println(a%b);

2 –Relational Operators

Class Relational{
public static void main(String[] args) {
   System.out.println(a>b);
   System.out.println(a<b);
   System.out.println(a>=b);
   System.out.println(a<=b);
   System.out.println(a==b);
   System.out.println(a!=b);
}
}

3-Assignment Operators

class Assi {
    public static void main(String[] args) {
        int a=5,b=7;
        System.out.println(a);
        a=a+b;
        System.out.println(a);
        a+=b; /*a=a+b */
        System.out.println(a);
        a-=b;/*a=a-b; */
        System.out.println(a);
        a*=b;/*a=a*b   */
        System.out.println(a);
        a/=b;/*a=a/b */
        System.out.println(a);
        a%=b;/*a=a%b */
        System.out.println(a);
      }
}

4- Bitwise Operators

class Bitwise {
    public static void main(String[] args) {
        int a= 17,b=26;
int p=15;
        int r=-17;

        System.out .println(a&b);AND Operator


        System.out .println(a|b);OR Operator
        System.out .println(a^b); exclusive OR Operator
        System.out.println(~p);
        System.out.println(~r); // Bitwise Not Compliment
    }
}

5- Unary Operators

class Unary{
    public static void main(String[] args) {
        boolean a= true,b=false;
  int x=55;
        System.out.println(++x);
System.out.println(x++);

        System.out.println(!a);
        System.out.println(!b);
    }
}

6- Logical Operators:

class Logic{
    public static void main(String[] args) {
        boolean a=true;
       boolean b=false;
        System.out.println(a && a);
        System.out.println(a && b);
        System.out.println(b && a);
        System.out.println(b && b );

        System.out.println(a||a);
        System.out.println(a || b);
        System.out.println(b || a);
        System.out.println(b|| b );

    }}
7-Shift Operator:

class Shift{
    public static void main(String[] args) {
        int a=5;
        int b=3;
        System.out.println(a>>b);
       
        System.out.println(a<<b);
       
        System.out.println(a>>>b);
       
       
    }
}

Area of Circle:

import java.util.Scanner;

class Areaofcircle{
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

    float pi=3.14f;
    float r= sc.nextInt();
    float formula=pi*r*r;
    System.out.println(formula);
   
}
}

Area of triangle:

import java.util.Scanner;

class Triangle{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
    int h=sc.nextInt();
int b=sc.nextInt();
int area_of_triangle=h*b/2;
System.out.println(area_of_triangle);
    }
}

To Calculate Simple Intrest:


import java.util.Scanner;
class Simple_intrest{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int p=sc.nextInt();
        int r=sc.nextInt();
        int t=sc.nextInt();
        double formula=p*r*t/100;
        System.out.println(formula);
       
    }
}
Area of rectangle.

//    area of rectangle
import java.util.Scanner;
class Practise{
    public static void main(String[] args) {
         Scanner sc=new Scanner(System.in);

   int l=sc.nextInt();
   int b=sc.nextInt();
   int area_of_rectangle=l*b;
   System.out.println( area_of_rectangle );

Swapping of Variable without using 3rd Variable.

/* Swapping using without third variable  */


class Swapping{
public static void main(String[] args) {
    int a=10;
    int b=20;
    a=a+b;
    b=a-b;
    a=a-b;
    System.out.println(a);
    System.out.println(b);
}
}

Swapping of Variable using 3rd Variable.

class Swapping {
    public static void main(String[] args) {
  int a = 10;
        int b = 20;
        int temp = a;
        a = b;
        b = temp;
        System.out.println(a);
        System.out.println(b);
    }
}
Conditional Statements:-
1 if-else

class Condition {
    public static void main(String[] args) {
        int a=10;
        if(a>10)
        {
            System.out.println("hello sir");
        }
        else
        {
            System.out.println("hello maam");
        } }}

2nd Program

import java.util.Scanner;
class Condition1{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int num=sc.nextInt();
        if(num%2==0)
        {
            System.out.println("entered number is even");
        }
        else{
            System.out.println("entered number id odd");
        }}}
   
3rd Program

import java.util.Scanner;
class Condition1{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
int age=sc.nextInt();
    if(age>=18){
        System.out.println("person can vote");
  }
    else{
        System.out.println("person can not vote");
    }}}

4th Program

import java.util.Scanner;

class Normal_Vowel{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Your Alphabet:");
        char x=sc.next().charAt(0);
        if(x=='a'||x=='e'||x=='i'||x=='o'||x=='u'){
            System.out.println(x+"  Alphabet iss Vowel.");    
          }
          else
          {
            System.out.println(x+"Alphabet is not Vowel."); } }}
6th program

import java.util.Scanner;
class Condition1{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
   String vow=sc.nextLine();
   if(vow.equals("a") || vow.equals("e") || vow.equals("i") || vow.equals("o") || vow.equals("u"))
    {
        System.out.println(vow+" is vowel");
    }
    else{
        System.out.println(vow +" not vowel");
    }}}   
5th Program

import java.util.Scanner;
class June12{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
int a=50;
           int b=60,c=70;
           double percentage=a+b+c/3;
           if (percentage<32)
           System.out.println("u are fail");
           else if(percentage>=33||percentage<44)
           System.out.println("you are pass with third division");
           else if (percentage>=45||percentage<59)
           System.out.println("pass with second diviion");
          else if (percentage>=60)
          System.out.println("pass with third diision");
           else{
            System.out.println("fail");
           }

Calculator using elseif condition

import java.util.Scanner;

class Elseif{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("enter your symbol");
char c = sc.next().charAt(0);
        int a=10,b=20;
        if(c=='+')
        System.out.println("Sum"+(a+b));
else if(c=='-')
System.out.println("Sub"+(a-b));
else if(c=='*')
System.out.println("mul"+(a*b));
else if(c=='/')
System.out.println("div"+(a/b));
else
 System.out.println("you entered wrong symbol");
    }}
 Switch Condition:-

import java.util.Scanner;
class Switch {
   public static void main(String[] args) {
      Scanner sc =new Scanner(System.in);
      int c=sc.nextInt();
      int a=10,b=20;
        switch(c){
            case 1:
            System.out.println(a+b);
            break;
            case 2:
            System.out.println(a-b);
            break;
            case 3:
            System.out.println(a*b);
            break;
            case 4:
            System.out.println(a/b);
            break;
            default :
            System.out.println("Wrong Choice");
        } } }
Leap year:

import java.util.Scanner;

class Leap1{

    public static void main(String[] args) {


        Scanner sc= new Scanner(System.in);
        int ar=sc.nextInt();
        if (ar%400==0 || ar%100==0 && ar%4==0)
        System.out.println( "it is a leap year ");
   
    else{
        System.out .println("it is not a leap year");
       
    }
}
}

Leap Year with printing next leap year.

import java.util.Scanner;

class Leap{
    public static void main(String[] args) {
       
        Scanner sc= new Scanner(System.in);
        int a =sc.nextInt();
        if (a%400==0||a%100==0&&a%4==0)
        {System.out.println("entered number is leap year");
    }
   
       
        else {
            System.out.println("enter number is not leap year");
            int t=4-a%4;
            System.out.println("next leap year="+(a++));
        } }}

LOOPS:-

 While Loop:

To print the Table of 5.

import java.util.Scanner;

class June12{
    public static void main(String[] args) {
       
int a =5;
int i=0;
while(i<10)
{i++;
    System.out.println("5x"+i+"="+a*i);}}}
To print the Factorial of the given number.

import java.util.Scanner;
class Switch {
   public static void main(String[] args) {
      Scanner sc =new Scanner(System.in);

int a= sc.nextInt();
int i=1,f=1;
while(i<=a)
{
    f=f*i;
    i++;
}
System.out.println(f);
To check it is a Prime number or Not .

import java.util.Scanner;
class Switch {
   public static void main(String[] args) {

int i=2;
int a=13,c=0;
while(i<a)
{
    if(a%i==0)
        c++;
i++;
}
 if(c==0){
        System.out.println("prime");
    }
    else
    {
        System.out.println("not prime");
    }}}

Sum of digit:

import java.util.Scanner;
class Switch {
   public static void main(String[] args) {
   
int a =579;
    int s=0;  
    while(a>0){
        int r= a%10;
        s=s+r;
        // System.out.println(s);
        a=a/10;
System.out.println("sum of digits "+s);
    }}}
TO check number is Palindrome or NOT using while loop.

import java.util.Scanner;
class Switch {
   public static void main(String[] args) {

int a =121;
int t =a;
    int s=0;  
    while(a>0){
        int r= a%10;
        s=s*10+r;
        System.out.println(s);
        a=a/10;
    }
System.out.println(s);
if(s==t)
System.out.println("number polindrone");

else
System.out.println("number is not polindrone");

Product of digits using while loop.

import java.util.Scanner;
class Switch {
   public static void main(String[] args) {
   
int a =579;
    int s=1;  
    while(a>0){
        int r= a%10;
        s=s*r;
        // System.out.println(s);
        a=a/10;
System.out.println("sum of digits "+s);
    }}}
To print reverse number using while loop.

class Reverse{
   public static void main(String[] args) {

int a=321;
int s=0;
while(a>0){
    int r=a%10;
    s=s*10+r;
    a=a/10;
}
System.out.println(s);
}}
To print the armstrong number between 100 to 999.

class Arm{
    public static void main(String[] args) {
        int i=100;
        while(i<=999){
            int a=i,s=0;
            while(a>0)
            {
                int r=a%10;
                s=s+r*r*r;
                a=a/10;
            }
            if (s==i)
            {
                System.out.println(i);
            }
            i++;} }
Palindrome number between 10 to 100 using while loop.

class padli{
    public static void main(String[] args) {
        int i=10;
        while(i<100){
            int a=i,s=0;
           
            while(a>0){
                int r=a%10;
                s=s*10+r;
                a=a/10;

            }
            if(s==i)
            System.out.println(i);  
           i++;
        }
        }
    }
Check Entered number is Armstrong or not.
import java.util.Scanner;

class Armstrongwhile{
    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       int a=sc.nextInt();
       int s=0;
       int t=a;
       while(a>0){
        int r=a%10;
        s=s+r*r*r;
        a=a/10;

       }  
       if(s==t)
       System.out.println("Entered number is Armstrong: "+s);
       else
       System.out.println("Entered number is not Armstrong: "+s);}}

For Loop:-

Print the Table of the number.

class For_loop {
    public static void main(String[] args) {
int a= 5;
        for(int i=1;i<=10;i++)
        {
            System.out.println(a*i);
        }}}
Printing tha Factorail of the number.

class For_loop {
    public static void main(String[] args) {
 int a=5;
  int  f=1;
for(int i=1; i<=a;i++)
{
    f=f*i;
}
System.out.println(f);
}}
Printing the Reverse number.

class For_loop {
    public static void main(String[] args) {

int a=371;
int s=0;
for(; a>0;){
    int r=a%10;
    s=s*10+r;
    a=a/10;
}
System.out.println(s);
To check given number is Prime or Not using for loop between 1 to 50.

class For{
    public static void main(String[] args) {
        for(int i=1;i<50;i++)
        {
            int c=0;
            for(int j=2;j<i;j++)
            {
                if(i%j==0)
                c++;
            }
            if(c==0)
            System.out.println("it is a prime"+i);
            else
            System.out.println("it is not a prime"+i);
        }}}
To print the Armstrong number between 100 to 999 using for loop.

class Arm{
    public static void main(String[] args) {
        int i=100;
        while(i<=999){
            int a=i,s=0;
            while(a>0)
            {
                int r=a%10;
                s=s+r*r*r;
                a=a/10;
            }
            if (s==i)
            {
                System.out.println(i);
            }
            i++; 
} }}
Print the Palindrome number between 10 to100.

class Palidrome{
    public static void main(String[] args) {
        for (int i=10;i<100;i++)
        {
            int a=i,s=0;
            for( ;a>0 ; ){
                int r=a%10;
                s=s*10+r;
                a=a/10;

            }
            if(s==i)
            System.out.println(i);
        }
    }
}

3- Do while Loop:
To print the Armstrong number between 100 to 999 using Do while loop.

class Armst{
    public static void main(String[] args) {
        int i=100;
        do{
            int s=0,a=i;
            do{
               int r=a%10;
                s=s+r*r*r;
                a=a/10;
            }
            while(a>0);
            {
                if(s==i)
                System.out.println(i);
            }
       i++;
        }
        while(i<=999);
    }
}
Print the Palindrome number between 10 to100.

class Padl{
    public static void main(String[] args) {
        int i=10;

        do{
            int s=0;
            int a=i;
            do{
                int r=a%10;
                s=s*10+r;
                a=a/10;

            }
            while(a>0);
            {
                if(s==i)
                System.out.println(i);  

                i++;
            }

     }

        while(i<100);
    }
}
To check given number is Prime or Not using Do while loop

import java.util.Scanner;
class Prime_number{
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int i=2;
        int c=0;
        do{
            System.out.println("your entered  number is"+a);
        }
        while(i>a);
        {if(a%i==0)
           
            c++;
        i++;
        }
        if(c==0)
        System.out.println("it is a prime number ");
        else
        System.out.println("it is not a prime number"  );  }
    }
To check given number is Prime or Not using Do while loop between 1 to 50

class Questions {
    public static void main(String[] args) {
        int i = 1;
        do {

            int j = 2;
            int c = 0;
            // System.out.println("prime number between 1 to 50 "+i);
       
do{

    if (i % j == 0)
        c++;
    j++;
}
         
            while (j < i);

            if (c == 0)
                System.out.println(i);
            i++;
        }
        while(i<=50);

}}
Check given number is Armstrong or not using Do while loop.

class Do_WhileArm{
    public static void main(String[] args) {
       
        int s=0,a=370;
        int temp=a;
    do{
       int r=a%10;
        s=s+r*r*r;
        a=a/10;
    }
    while(a>0);
    {
        if(s==temp)
        System.out.println(s+" is Armstrong Number");
        else
        System.out.println(s+" is not Armstrong Number");}} }

Array:-

class Array1 {
    public static void main(String[] args) {
        int[] arr={21,45,25,36,56,78,95,65,44,52,63,98};

        System.out.println(arr[2]);
        System.out.println(arr[1]);

        // To print all elements of the array with while Loop

        int[] arr={12,45,63,52,41,74,85,96};
        int i=0;
        while(i<arr.length){
        System.out.println(arr[i]);
        i++;
        }

        // to print all elements using do while Loop

        int[] arr={21,45,25,36,56,78,95,65,44,52,63,98};
        int i=0;
        do{
        System.out.println(arr[i]);
        i++;
        }
        while(i<arr.length);

        // to print all elements using for LOOP

        int arr[]={12,45,63,8,59,65,22,45,75,52,63};
        for(int i=0;i<arr.length;i++){
        System.out.println(arr[i]);

        }

        // sum of array elements using for Loop

        int arr[]={12,2,15,4,5,21,548,4,15};
        int s=0;

        for(int i=0;i<=arr.length;i++){
        s+=arr[i];
        }
        System.out.println("sum of the elements="+s);

        // sum of numbers using while LOOP

        int arr[] = { 12, 2, 15, 4, 5, 21, 548, 4, 15 };


        int i = 0;
        int s = 0;
        while (i <arr.length) {
        s += arr[i];
        i++;

        }
        System.out.println("sumof the numbers= " + s);

        // sum of array using do while LOOP

        int arr[] = { 12, 2, 15, 4, 5, 21, 548, 4, 15 };


        int s=0;
        int i=0;
        do{
        s+=arr[i];
        i++;

        }while(i<arr.length);
        System.out.println("sum of numbers = "+s);

       
// sum of even numbers using for LOOP

        int arr[] = { 1,2,3,4,5,6,7,8,9};


        int s=0;

        for(int i=0; i<arr.length;i++){


        if(arr[i]%2==0){
        s+=arr[i];
        }

        }
        System.out.println("sum of even numbers= "+s );

       
// sum of even numbers using while LOOP

        int arr[] = { 1,2,3,4,5,6,7,8,9};


        int s=0;
        int i=0;
        while(i<arr.length){
        if(arr[i]%2==0){
        s+=arr[i];
        }
        i++;
        }
        System.out.println("sum of even = "+s);

        // sum of even number using do while loop

        int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int s = 0;
        int i = 0;
        do {
            if (arr[i] % 2 == 0) {
                s += arr[i];
            }
            i++;

        } while (i < arr.length);


        System.out.println("sum of even numbers= "+s);

// sum of odd number in array with for loop

 
int arr[] = { 1,2,3,4,5,6,7,8,99};
        int s=0;

        for(int i=0; i<arr.length;i++){


        if(arr[i]%2==1){
        s+=arr[i];
        }

        }
        System.out.println("sum of odd numbers= "+s );

// sum of odd numbers of array using while loop

 int arr[] = { 1,2,3,4,5,6,7,8,9};


        int s=0;
        int i=0;
        while(i<arr.length){
        if(arr[i]%2==1){
        s+=arr[i];
        }
        i++;
        }
        System.out.println("sum of odd = "+s);

// sum of odd numbers of array using do while loop


int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int s = 0;
        int i = 0;
        do {
            if (arr[i] % 2 == 1) {
                s += arr[i];
            }
            i++;

        } while (i < arr.length);


        System.out.println("sum of odd numbers= "+s);

// sum of prime number using for loop


int [] arr={1,3,5,7,9};
int s=0;
for (int i=0;i<arr.length;i++){
    int a =arr[i];
    int c=0;
   
   for(int j=2;j<a;j++){
     if(a%j==0)
     c++;
    }
   
   
    if (c==0 && a!=1){
       s+=arr[i];
   System.out.println("prime number"+s);
}
}

sum of prime number using while loop

int [] arr={1,3,5,7,9};
int s=0;
int i=0;
while(i<arr.length){
    int a =arr[i];
    int c=0;
   int j=2;
   while(j<a){
     if(a%j==0)
     c++;
     j++;
    }
   
   
    if (c==0){
        s+=arr[i];
    }
    System.out.println(arr[i]);
    i++;
}
System.out.println("prime number"+s);

// sum of prime number using do while


int [] arr={1,3,5,7,9,7,2};
int s=0;
int i=0;
do{
     int a =arr[i];
    int c=0;
   int j=2;
   do{
     if(a%j==0)
     c++;
     j++;
    }
    while(j<a);
    if (c==0){
       s+=arr[i];
    }
    i++;
}
while(i<arr.length);
   
System.out.println("prime number"+s);
   

    }
}

2D Array:-

import java.util.Scanner;

class Array2d {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int [] arr=new int[10];
        for(int i=0;i<10;i++){
        arr[i]=sc.nextInt();

        }
        for( int j=0;j<10;j++){
        System.out.println(arr[j]);

        }

        // to print 2d array taking user input

        int[]arr =new int[5];


        int s=0;
        for ( int i = 0; i <5; i++) {

        arr[i]=sc.nextInt();

        }
        for( int i=0;i<5;i++){
        System.out.print(" "+arr[i]);

        }
        // to print the sum of 1D array by user input

        int[]arr =new int[5];


        int s=0;
        for ( int i = 0; i <5; i++) {

        arr[i]=sc.nextInt();

        }
        for( int i=0;i<5;i++){
        s+=arr[i];

        }
        System.out.print(" "+s);

        // to print 2D array using user input

        int[][]arr =new int[3][3];


        int s=0;
        for ( int i = 0; i <3; i++) {
        for(int j=0;j<3;j++){

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

        }
        }
        for( int i=0;i<3;i++){
        for(int j=0;j<3;j++){
       
        System.out.print(" "+arr[i][j]);
        }
        System.out.println();
        }

        // to print the sum of2D array taking user input

        int[][]arr =new int[3][3];


        int s=0;
        for ( int i = 0; i <3; i++) {
        for(int j=0;j<3;j++){

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

        }
        }
        for( int i=0;i<3;i++){
        for(int j=0;j<3;j++){
        s+=arr[i][j];
        }
        System.out.println();
        }
        System.out.print(" "+s);

        // to print the sum of prime number in 2D array

        int[][] arr = new int[3][3];


        int s = 0;
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
               
                arr[i][j] = sc.nextInt();
               
            }
        }
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
               
                int c = 0;
                // System.out.print(" \t"+arr[i][j]);
                int a = arr[i][j];
                for (int k = 2; k < a; k++) {
                    if (a % k == 0)
                        c++;
                        System.out.println(a);
                }
                if (c == 0 && a != 1) {
                    s += arr[i][j];
                }

            }
           
            System.out.println();
        }
        System.out.println("\t"+s);

    }
}

Array:-

class Array2 {
    public static void main(String[] args) {

        print all elements of 2D array

        int[][] arr = { {4,8,6 }, { 4,8,9 }, { 4,8,9 } };


        for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
        System.out.print("\t i=" + i + ", j=" + j);// 00,01,02 this print the index of array
        System.out.print(" \t"+arr[i][j]);
        }
        System.out.println();

        }

        print all elements  using while loop

        int[][] arr = { { 4, 8, 6 }, { 4, 8, 9 }, { 4, 8, 9 } };
        int i = 0;
        while (i < 3) {
            int j = 0;
            while (j < 3) {
               
                System.out.print(" \t" + arr[i][j]);

                j++;
            }
            System.out.println();
            i++;
        }

print all the  elements using do while:

int[][] arr = { { 4, 8, 6 }, { 4, 8, 9 }, { 4, 8, 9 } };
        int i = 0;
        do{
            int j = 0;
do{
    System.out.print(" \t" + arr[i][j]);

                j++;
}while(j<3);

i++;
System.out.println();}

        while(i < 3);

        int[][] arr = { {4,8,6 }, { 4,8,9 }, { 4,8,9 } };


        for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
        }
        System.out.println();}

// to print the prime number from the matrix

        int arr[][]={{2,9,1},{1,2,3},{8,5,8}};
   
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                int a=arr[i][j];
                int c=0;
                for(int k=2;k<a;k++){
                    if(a%k==0)
                    c++;
                }
                if(c==0 &&a!=1)
                System.out.println(a);
            }
        }

// To print the sum of prime number in matrix

int arr[][]={{2,9,1},{1,2,3},{8,5,8}};
        int s=0;
        for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                int a=arr[i][j];
                int c=0;
                for(int k=2;k<a;k++){
                    if(a%k==0)
                    c++;
                }
                if(c==0 &&a!=1)
                s+=a;
            }
        }
        System.out.println(s);
Program on MATRIX:-

class Matrix {
    public static void main(String[] args) {

        Print sum of left diagonal of the matrix

        int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        int s=0;
        for (int i = 0; i < 3; i++)
         {
        for (int j = 0; j < 3; j++)
         {
        System.out.print("\t" + arr[i][j]);
        if (i == j)
        System.out.print(" " + arr[i][j]);
        s=s+arr[i][j];

        }
        System.out.println();

        }
        System.out.println(" Sum of left diag of the matrix:"+s);

    Print sum of right diagonal of the given matrix

        int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        int s = 0;
        for (int i = 0; i < 3; i++)
         {
            for (int j = 0; j < 3; j++)
         {
                System.out.print( "\t" + arr[i][j] );
                if (i+j==2)
                s = s + arr[i][j];
            }
            System.out.println();
        }

        System.out.println("sum of right diag of  the matrix :" + s);

        Print sum of 2nd row of matrix

          int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        int s = 0;
        for (int i = 0; i < 3; i++)
         {
            for (int j = 0; j < 3; j++)
        {
                System.out.print("\t" + arr[i][j]);
                if (i ==1)
                s = s + arr[i][j];

            }
            System.out.println();

        }
        System.out.println("sum of 2nd row of matrix "+s);

        //Print sum of 2nd column of a matrix

         int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        int s=0;
        for (int i=0; i<3; i++) {
            for (int j=0; j<3; j++) {
                System.out.print("\t"+arr[i][j]);
                if (j==1)
                s=s+arr[i][j];

            }
            System.out.println();

        }

        System.out.println("sum of 2nd column of matrix "+s);

    }

You might also like