0% found this document useful (0 votes)
16 views

Program File

The document contains code snippets demonstrating various Java programming concepts including: 1) Command line arguments - Code shows how to pass command line arguments to a Java program and access them in the main method. 2) Identifiers - Code defines valid and invalid identifier names in Java. 3) Operators - Code shows examples of arithmetic, assignment, logical, bitwise, unary, shift, relational, and ternary operators. 4) Conditional statements - Code provides if/else examples to check even/odd numbers, voting eligibility, vowels, and student grades. Methods to check for leap years are also demonstrated.

Uploaded by

Sachin Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Program File

The document contains code snippets demonstrating various Java programming concepts including: 1) Command line arguments - Code shows how to pass command line arguments to a Java program and access them in the main method. 2) Identifiers - Code defines valid and invalid identifier names in Java. 3) Operators - Code shows examples of arithmetic, assignment, logical, bitwise, unary, shift, relational, and ternary operators. 4) Conditional statements - Code provides if/else examples to check even/odd numbers, voting eligibility, vowels, and student grades. Methods to check for leap years are also demonstrated.

Uploaded by

Sachin Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Command line argument :

/* Command line Argument */

class Demo{
    public static void main(){
        System.out.println("Hello java");
        System.out.println(Integer.parseInt(kvch[0]) +
Integer.parseInt(kvch[1]));

       

    }
}

/*COmmand line Arguments */

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

/* Command line Argument */

class Demo{
    public static void main(){
        System.out.println("Hello java");
        System.out.println(Integer.parseInt(kvch[0]) +
Integer.parseInt(kvch[1]));

       

    }
}

Identifiers:
/* Identifiears */

import java.util.Scanner;
class Demo4{
    public static void main(String[]args){
        int a=10, b=20;
        int c=a+b;

        int _a=10;// identifire to give name we can only use underscore or


andoperator
        int $a=10;
        int %a=10; //It give Error
    }
}

Operators:

Taking user input:

/* Taking user input  */

import java.util.Scanner;

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

System.out.println("Enter first number");


int a = ob.nextInt();

System.out.println("Enter second number");


int b = ob.nextInt();
int c=a+b;
System.out.println("The sum of Two number"+c);

}
}

/*  Asignment And Airthmeatic Operators */

class OperatorEx{
    public static void main(String[]args){
      int a=10,b=3;

 //Airthmeatic Operators
        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);

//Asignment Operators
        int c=10,d=7;
        System.out.println(c);
        c=c+d;
        System.out.println(c);
        c+=d;
        System.out.println(c);
        c-=d;
        System.out.println(c);
        c*=d;
        System.out.println(c);
        c/=d;
        System.out.println(c);
        c%=d;
        System.out.println(c);
    }
}
class OperatorEx1{
    public static void main(String[]args){
        boolean a = true, b=false;

// //Logical Operators
        System.out.println(a&&b);
        System.out.println(b||a);
       

// Bitwise Operators
        int c=17,d=26;
        System.out.println(c&d);
        System.out.println(c|d);
        System.out.println(c^d);

//Unary Operators
        int e=15, f=-17;
        System.out.println(~e);
        System.out.println(~f);// in it the value is increases by 1 and the sign
get changed
        System.out.println(!a);

//Shift Operators
        int g=3, h=5;;
        System.out.println(g<<h);
         System.out.println(36>>3);
        System.out.println(35>>3);

  // Relational Operators
        int x=10, y=20;
        System.out.println(x<y);      
        System.out.println(x>y);      
        System.out.println(x<=y);      
        System.out.println(x>=y);      
        System.out.println(x==y);      
        System.out.println(x!=y);      
       
    }
}
Areas :
/* Operators  */

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

// //Area of Circle
        // double Area,r;
        // System.out.println("Enter the Radius of Circle");
        //  r = ob.nextDouble();
        //  Area =3.14*r*r;
        // System.out.println("The area of Circle is: "+Area);

//Area of reactangle

        // double rec , l,b;


        // System.out.println("Enter the lenght of rectangle ");
        //  l = ob.nextDouble();
        // System.out.println("Enter the breath of rectange ");
        //  b= ob.nextDouble();
        //  rec= l*b;
        //  System.out.println("The area of rectangle is "+rec);

//Area of triangle

        // double tri, h,b;


        // System.out.println("Enter the Heigh of Triangle");
        // h=ob.nextDouble();
        // System.out.println("Enter the Base of triangle ");
        // b = ob.nextDouble();

        // tri= (h*b)/2;
        // System.out.println("The Area of triangle is "+ tri);

 // Calculation of Simple Intrest


        // double SI, p,r,t;
        // System.out.println("Enter the p");
        // p=ob.nextDouble();
        // System.out.println("Enter the r");
        // r=ob.nextDouble();
        // System.out.println("Enter the t");
        // t=ob.nextDouble();
        // SI=(p*r*t)/100;
        // System.out.println("The Si is "+ SI);

 //Swapping with third variable

        // System.out.println("Enter the value of a");


        // int a= ob.nextInt();
        // System.out.println("Enter the second number");
        // int b = ob.nextInt();
        // int temp=a;
        // a=b;
        // b=temp;
        // System.out.println("Value of a  "+a);
        // System.out.println("Value of b  "+b);

 //Swapping without Third Variable

        System.out.println("Enter the value of a");


        int a= ob.nextInt();
        System.out.println("Enter the second number");
        int b = ob.nextInt();

        a=a+b;
        b=a-b;
        a=a-b;
        System.out.println("The value of a : "+a);
        System.out.println("The value of b : "+b);
   
    }
}
Conditional Statement:
 Performing addition, subtraction, multiplication and division by entering
their sign
import java.util.Scanner;

import javax.lang.model.util.ElementScanner14;

public class ConditionalEx2 {


    public static void main(String[] args) {

// Basic if else program

  int a = 20;

        if (a > 20) {
            System.out.println("Hello Java");
        } else {
            System.out.println("Number is smaller the 20");
        }

// Performing addition, subtraction, multiplication and division by entering


their sign  

        int a = 10, b = 5;

        Scanner ob = new Scanner(System.in);

        System.out.println("Enter the Sign Which you Want");


        String c = ob.nextLine();

        if (c.equals("+"))
            System.out.println("Sum=" + (a + b));
        else if (c.equals("-"))
            System.out.println("Sub=" + (a - b));
        else if (c.equals("*"))
            System.out.println("Mul=" + (a * b));
        else if (c.equals("/"))
            System.out.println("Div=" + (a / b));
        else {
            System.out.println("Sorry......");
        }
    }

}
If else programs:
/* if else Conditional Statement */

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

        Scanner ob = new Scanner(System.in);

//Even odd Number

        System.out.println("Enter the number");


        double c = ob.nextDouble();

       
        if(c %2 ==0 ){
            System.out.println("The number is Even");
        }
        else{
            System.out.println("The number is odd ");
        }

   

       

// //Person can vote or note

        int age;
        System.out.println("Enter the age");
        age= ob.nextInt();
        if(age>18){
            System.out.println("The Person can Vote");
        }
        else{
            System.out.println("Cann't Vote");
        }

       
// Enter Character is vowel or not

        String a="b";
        if(a=="a"||a=="e"||a=="o"||a=="u")
        System.out.println(a+" is vowel");
        else
        System.out.println(a+" is not Vowel");

//Finding entered alphabate is vowel or not :


         
        String a= ob.nextLine();
        if(a.equals("a")||a.equals("e")||a.equals("o")||a.equals("u"))
        System.out.println(a+" is vowel");
        else
        System.out.println(a+" is not Vowel");

    }
}

 Finding grade of a student using his/her marks


import java.rmi.server.SocketSecurityException;
import java.util.Scanner;

import javax.lang.model.util.ElementScanner14;

public class Grade {


    public static void main(String[] args) {
        float avg;
        Scanner ob = new Scanner(System.in);

        System.out.println("Enter the FIrst SUb Marks");


        int a = ob.nextInt();
        System.out.println("Enter the Second Sub marks");
        int b = ob.nextInt();
        System.out.println("Enter the Third sub Marks");
        int c = ob.nextInt();

         avg= (a+b+c)/3;
         if(avg>=60){
         System.out.println("First Division");
         }

         else if (avg>=45 && avg<60){


         System.out.println("Second Division");
         }

         else if(avg>=33 && avg<45){


         System.out.println("Third");
         }

         else{
            System.out.println("Fail");
         }

    }
}

Finding Leap year or not:


import java.util.Scanner;

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

        Scanner ob = new Scanner(System.in);


        int yr;
        System.out.println("Enter the Year");
         yr = ob.nextInt();

        if(yr%400==0 && yr%100==0  || yr%100!=0 &&  yr%4==0){


            System.out.println("Leap year");
        }
     
        else {
            System.out.println(" Not aLeap year");
        }
    }
}
Finding next leap year:
import java.util.Scanner;

class FindingNextLeapyr{
    public static void main(String[] args) {
        Scanner ob = new Scanner(System.in);
        System.out.println("Enter the Year");
        int yr = ob.nextInt();
 
    if(yr%4==0){
        System.out.println("Leap Yr");
    }
    else{
        System.out.println("Not a leap year");

    }
   
    int temp = 4-(yr%4);
    System.out.println("Next leap year = "+ (yr+temp));
    }
}

 Switch conditional Statement


public class SwitchCase {
    public static void main(String[] args) {
        int a= 10, b= 5,c=1;
        switch(1){
            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 Choise");

        }
    }
   
}

While Loop
 Basic program
public class whileLoopEx2 {
    public static void main(String[] args) {
        int i=2, a=9;
        while(i<a)
        {
            if(a%i==0)
            System.out.println("hello java");
            i++;
        }
    }
   
}
 Printing the table of entered number:
import java.util.Scanner;

public class Table {


    public static void main(String[] args) {

        Scanner ob =new Scanner(System.in);

        System.out.println("Enter the Table Number");


        int a = ob.nextInt();
        int i=0;
        while(i<=10)
        {
            i++;
            System.out.println(a+"*"+i +"=" +(a*i));
           
        }
    }
   
}
 Factorial of a given number:
public class Factorial {
    public static void main(String[] args) {
        int a=5;
        int i= 1, f=1;
        while(i<=a)
        {
            f=f*i;
            System.out.println(f);
            i++;
        }
        System.out.println("Factorila "+f);

    }
   
}

 Finding the given number is prime or not:


import java.util.Scanner;

public class PrimeNumber {


    public static void main(String[] args) {
        Scanner ob = new Scanner(System.in);
        int a = ob.nextInt();
        int i=2;
       
        int c=0;

        while(i<a){
            if(a%i==0)
            c++;
            i++;
        }
//System.out.println(c);
        if(c==0){
        System.out.println("Numebr is Prime");
        }
        else{
            System.out.println("Number is not Prime");
        }
    }
   
}

 Finding sum of digits of a number:


import java.util.Scanner;

public class SumOfDigits {


    public static void main(String[] args) {
        Scanner ob = new Scanner(System.in);
        System.out.println("Enter the Number");
        int n = ob.nextInt();
        int s=0;
        while(n>0){
            int rem=n%10;
            s=s+rem;
           
            n=n/10;
        }
        System.out.println("Sum of Digits of a number = "+s);
    }
   
}

 Finding product if digits of a given number:


import java.util.Scanner;

public class ProductOfDigits {


    public static void main(String[] args) {
        Scanner ob = new Scanner(System.in);
        System.out.println("Enter the Number");
        int n = ob.nextInt();
        int s=1;
        while(n>0){
            int rem=n%10;
            s=s*rem;
           
            n=n/10;
        }
        System.out.println("Product of Digits of a number = "+s);
    }
   
}

public class Loop {


    public static void main(String[] args) {
        int i=11;
        while(i>0){
            System.out.println(i);
            // i++;
            // i=i+2
            // i=i*2
            // i--;
            // i=i-2;
            // i=i/2;
            i=i%2;
            break;
        }
    }
   
}

 For loop

import javax.lang.model.util.ElementScanner14;

public class ForLoopEx {


    public static void main(String[] args) {
 // Printing number from 1 to 10
        // for(int i=1; i<=10;i++){
        // System.out.println(i);
        // }
 // Printing prime number between 1 to 10
        // for(int i=1; i<=10;i=i+2){
        // System.out.println(i);
        // }
// Printing even number between 1 to 10
        // for(int i=1; i<=10;i=i*2){
        // System.out.println(i);
        // }
// printing number from 10 to 1 in reverse order
        // for(int i=10; i>=0;i--){
        // System.out.println(i);
        // }
// Printing even number etween 10 to 0 in reverse order
        // for(int i=10; i>0;i=i-2){
        // System.out.println(i);
        // }
 //
        // for(int i=10; i>0;i=i/2){
        // System.out.println(i);
        // }
//
        // for(int i=10; i>0;i=i%2){
        // System.out.println(i);
        // }

// Printing the Table of 5


        // int a=5;
        // for(int i=1;i<=10;i++){
        // System.out.println(a*i);
        // }

// ---> Printing the factorial of given number


        // int a=5;
        // int f=1;
        // for(int i=1;i<=a;i++)
        // {
        // f=f*i;
        // }
        // System.out.println("Factorial "+f);

// ---> Printing the given number is Prime Number or not

        // int a = 8;
        // int c = 0;
        // for (int i = 2; i < a; i++) {
        // if (a % i == 0)
        // c++;
        // }
        // if (c == 0)
        // System.out.println("Prime Number ");
        // else
        // System.out.println("Note Prime Number");

// Printing sum of digits of given number


        // int n = 193;
        // int s = 0;
        // for (int i = n; i > 0; i = i / 10) {
        // int rem = i % 10;
        // s = s + rem;
        // n=n/10; // this is in for loop
        // }
        // System.out.println(s);

// Prining the sum of digits of given number


        // int a=129;
        // int s=0;
        // for( ;a>0;a=a/10){
        // int rem=a%10;
        // s=s+rem;
        // }
        // System.out.println("Sum of Digits by for loop " +s);

//finding sum 0f digits of a number


        // int a=129;
        // int s=0;
        // for( ;a>0;){
        // int rem=a%10;
        // s=s+rem;
        // a=a/10;
        // }
        // System.out.println("Sum of Digits by for loop " +s);

       
// Finding given number is armstrong or not
       
        // int n = 153;        
        // int temp = n;
        // int s = 0;
        // for (; n > 0;) {
        // int rem = n % 10;
        // s = s + rem * rem * rem;
        // // System.out.println(s);
        // n = n / 10;
        // }
        // // System.out.println(s);
        // if (s == temp) {
        // System.out.println("Armstrong Number");
        // } else {
        // System.out.println("Not Armstrong");
        // }

//Printing prime number between 1 to 50

        for (int i = 1; i <= 50; i++) {


            int c = 0;
            for (int n = 2; n < i; n++) {
                if (n % i == 0)
                    c++;
                }
                if (c == 0)
                    System.out.println(" prime number between 1-50 are " + i);
                      i++;
            }
        }
    }
   
                   
   

 Do while loop

 Printing the given number is Armstrong or not


import java.util.Scanner;

// import javax.lang.model.util.ElementScanner14;

public class DowhileArmstrong {


    public static void main(String[] args) {
        int i = 154;
        int s = 0;
        int a = i;
        do {

            int r = a % 10;
            s = s + r * r * r;
            a = a / 10;

        } while (a > 0);

        if (s == i) {
            System.out.println("Armstrong number");
        } else {
            System.out.println("Not Armstrong");
        }

    }
}

 Printing the given number is prime or not


public class DowhilePrimeNumber {
    public static void main(String[] args) {
        int a=7;
        int i=2;
        int c=0;

        do{
            if(a%i==0)
            c++;
            i++;
        }while(i<a);
        if(c==0)
        System.out.println(a+" is prime number");
        else
        System.out.println(a+" is not a prime number");
    }
   
}
 Printing the reverse number of given number
class DoWhileReverseNumber{
    public static void main(String[] args) {
       
        // int a=123;
        // int s=0;
        // while(a>0){
        //     int rem=a%10;
        //     s=s*10+rem;
        //     a=a/10;
        // }
        // System.out.println(s);

        int a=345;
        int s=0;
        do{
            int rem=a%10;
            s=s*10+rem;
            a=a/10;
        }while(a>0);
        System.out.println("The reverse number is "+s);
    }
}

 Printing the table of given number


import java.util.Scanner;

public class DowhileTable {


    public static void main(String[] args) {
       
        int a=4;
        int i=0;
       
        do{
            System.out.println(a+"*"+i +"=" +(a*i));
            i++;

        }
        while(i<=10);
    }
   
}

 Printing the Armstrong number between 100 to 900 with for loop, while
loop, do-while loop

public class ArmstrongNumberEx {


    public static void main(String[] args) {

// Printing the prime number between 100 to 900 with for loop

        // for (int i = 100; i <= 900; i++) {


        //     int s = 0;
        //     int a=i;
        //     for ( ; a>0; ) {
            //         int rem = a % 10;
            //         s = s + rem * rem * rem;
            //         a=a/10;
            //     }
            //     if(s==i)
            //     System.out.println(s);
            // }

           
 // Printing the prime number between 100 to 900 with while loop
       
            // int i=100;
            // while(i<=900){
            //     int a=i,s=0;
            //     while(a>0){
            //         int rem=a%10;
            //         s=s+rem*rem*rem;
            //         a=a/10;
            //     }
            //     if(s==i)
            //      System.out.println(s);
            //     i++;
            // }

  // Printing the prime number between 100 to 900 with do while loop
       
       
            int i=100;
            do{
                int s=0 ,a=i;
                do{
                    int rem=a%10;
                    s=s+rem*rem*rem;
                    a=a/10;
                }while(a>0);
                if(s==i){
                    System.out.println(i);
                   
                }
             
                   i++;
                }while(i<=999);
            }
        }
               

 Printing the palindrom number using for loop, while loop and do while
loop
import java.util.Scanner;

public class PalindromNumber {


    public static void main(String[] args) {

//printing the Palindrom number between 10 to 100 using while loop

        // int i = 10;
        // System.out.println("The Palindrom numbers are ");
        // while (i < 100) {
        // int s = 0;
        // int a = i;
        // while (a > 0) {
        // int r = a % 10;
        // s = s * 10 + r;
        // a = a / 10;
        // }
        // if (s == i)
        // System.out.println(i);
        // i++;
        // }

//printing the Palindrom number between 10 to 100 using do-while loop


         
        // int i = 10;
        // System.out.println("The Palindrom numbers are ");
        // do {
        //     int s = 0, 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);

//printing the Palindrom number between 10 to 100 using for loop


       
        System.out.println("The Palindrom numbers are ");
        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);
        }
    }
}

 Printing the Prime number using for loop, while loop and do while loop
public class PrimeNumberExss {
    public static void main(String[] args) {

 // --->Prime Number

       
        //Prime number by while loop
        int i = 1;
        while (i <= 50) {
            int j = 2;
            int c = 0;
            while (j < i) {
                if (i % j == 0)
                    c++;
                j++;
            }
            if (c == 0)
                System.out.println(i);
            i++;
        }

       
//prime number by do-while loop
        // int i = 1;
        // do {
        //     int c = 0;
        //     int j = 2;
        //     while (j < i) {
        //         if (i % j == 0) {
        //             c++;
        //             j++;
        //         }
        //         if (c == 0)
        //             System.out.println(i);
        //         i++;
        //     }
        // } while (i <= 50);

//Prime number by For loop


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

    }
}
 Reverse number using for loop , while loop and do while loop
class DoWhileReverseNumber{
    public static void main(String[] args) {
 
 //Reverse number using while loop

        // int a=123;
        // int s=0;
        // while(a>0){
        //     int rem=a%10;
        //     s=s*10+rem;
        //     a=a/10;
        // }
        // System.out.println(" The reverse number is "+s);

 //Reverse number using do while loop

        // int a=345;
        // int s=0;
        // do{
        //     int rem=a%10;
        //     s=s*10+rem;
        //     a=a/10;
        // }while(a>0);
        // System.out.println("The reverse number is "+s);

//Reverse number using for loop


       
         int s=0;
        for(int a=345; a>0; ){
            int rem=a%10;
            s=s*10+rem;
            a=a/10;
        }
        System.out.println("The reverse number is "+s);
    }
}
 Array

import java.util.Locale.IsoCountryCode;
import java.util.Scanner;

public class ArrayEx {


    public static void main(String[] args) {

        // int sum = 0;
        // int arr[] = { 12,22,31,21,23,45,64,56,89 };
        // for (int i = 0; i < arr.length; i++) {
        // System.out.println(arr[i]); //to print array
        // if(arr[i]%2==0) // even num
        // if(arr[i]%2==1) // odd number
        // if(i%2==1) // sum odd index number
        // if(i%2==0) // sum even index number
        // sum += arr[i];

        // int a=arr[i];
        // int c=0;
        // for(int j=2;j<a;j++){
        // if(a%j==0)
        // c++;
        // }
        // if(c==0){
        // System.out.println("The prime number in the array are : "+arr[i]);
        // sum+=arr[i];
        // }
        // }
        // System.out.println("Sum of array : "+ sum);
        // System.out.println("Sum of even number of array : "+ sum);
        // System.out.println("Sum of odd number of array : "+ sum);
        // System.out.println("Sum of odd index number of array : "+ sum);
        // System.out.println("Sum of even index number of array : " + sum);
        // System.out.println("Sum of prime number of array : "+sum);

// //print the array index

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


        // for (int j = 0; i < 3; j++) {
        // System.out.println("\t i= " + i + ",j=" + j);
        // }
        // System.out.println();
        // }
// //printing the array with for loop
        // int arr[]={1,2,3,4,5,6,7,8,9};

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


        // System.out.println(arr[i]);

// // printing the array with while loop


        // int arr[]={ 2,3,4,5,5,3,5,36,2};
        // int i=0;
        // while(i<arr.length){
        // System.out.println(arr[i]);
        // i++;
        // }

// // Printing the array with do whil loop

        // int arr[] = { 2, 4, 5, 34, 432, 54, 23 };


        // int i = 0;
        // do {
        // System.out.println(arr[i]);
        // i++;
        // } while (i < arr.length);

 // //printing the sum of array with while loop

        // int arr[]={ 12,23,3,1,43,45,12,98};


        // int s=0;
        // int i=0;
        // while(i<arr.length){
        // // System.out.println(arr[i]);
        // s+=arr[i];
        // i++;
        // }
        // System.out.println(s);

 // // printing the sum of array with do while

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


        // int s=0;
        // int i=0;
        // do{
        // s+=arr[i];
        // i++;
        // }while(i<arr.length);
        // System.out.println(s);

 // // Print the sum array with for loop


        // int arr[]={2,1,3,4,33,54,5,4,3};

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

        // }
        // System.out.println(" The sum of array "+s);

// // Printing the sum of even number with while loop

        // int arr[]={2,3,4,3,2,45,3,2,4,3,32};
        // int s=0;
        // int i=0;
        // while(i<arr.length){`
        // if(arr[i]%2==0)
        // s+=arr[i];
        // i++;
        // }
        // System.out.println(s);

// // printing the sum of even number with do while

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

        // } while (i < arr.length);


        // System.out.println("Sum of even number in array "+s);

// // printing the sum of even number of array with for loop


        // int arr[]={1,2,1,3,2,34,4,23,2};
        // 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 number"+s);

// // printing the sum of prime number with while loop


        // int arr[] = { 1, 2, 3, 2, 8, 80, 90, 3, 3, 2, 2 };
        // int s = 0;
        // int i = 0;
        // while (i < arr.length) {
        // int a = arr[i], 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("sum of prime number "+s);

// printing the sum of prime number with do while loop


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

// // printing the sum of prime number with for loop


        // int arr[] = { 1, 2, 3, 4, 3, 2, 4, 1, 3, 32, 4 };
        // int s = 0;
        // for (int i = 0; i < arr.length; i++) {
        //     int a = arr[i], c = 0;
        //     for (int j = 2; j < a; j++) {
        //         if (a % j == 0)
        //             c++;
        //     }
        //     if (c == 0) {
        //         // System.out.println(arr[i]);
        //         s += arr[i];
        //     }
        // }
        // System.out.println("sum of prime number " + s);

// // printing array by user input

        // Scanner ob= new Scanner(System.in);


        // int []arr=new int [10];
        // for(int i=0;i<10;i++){
        // arr[i]=ob.nextInt();
        // }
        // for(int j=0; j<10; j++){
        // System.out.print(arr[j]);
        // }
        // System.out.println();

        // }

        // }

 // printing sum ofarray by user input

        Scanner ob = new Scanner(System.in);


        int[] arr = new int[25];
        int s = 0;
        System.out.println("Enter the range of array");
        int r = ob.nextInt();
        for (int i = 0; i < r; i++) {
            arr[i] = ob.nextInt();
            s += arr[i];
        }

        for (int j = 0; j < r; j++) {


            System.out.print("Array element  are  " + arr[j]);

        }

        System.out.println();

        System.out.println("Sum of array " + s);


    }
}

 2D Array

import java.util.Scanner;

import javax.sound.sampled.SourceDataLine;
import javax.swing.event.SwingPropertyChangeSupport;

public class TwoDarray {


    public static void main(String[] args) {
        // int arr[][]={ {1,2,3}, {4,5,6},{7,8,9}};
        // System.out.println(arr[0][0]);
        // System.out.println(arr[0][1]);
        // System.out.println(arr[0][2]);
        // System.out.println(arr[1][0]);
        // System.out.println(arr[1][1]);
        // System.out.println(arr[1][2]);

// // printing the element of array in for of matrix

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

// // printing the element of array with while of matrix

        // int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        // int i = 0;
        // while (i < 3) {
        // int j = 0;
        // while (j < 3) {
        // System.out.print("\t" + arr[i][j]); // printing element of array
        // j++;
        // }
        // System.out.println();
        // i++;
        // }

 // // printing the element of array with while of matrix

        // int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        // int i = 0;
        // do {
        // int j = 0;
        // do {
        // System.out.print("\t" + arr[i][j]); // printing element of array
        // j++;
        // } while (j < 3);
        // System.out.println();
        // i++;
        // }while (i < 3);

// // sum of element of array with for loop


        // int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        // int sum = 0;
        // for (int i = 0; i < 3; i++) {
            // for (int j = 0; j < 3; j++) {
            // System.out.print("\t"+arr[i][j]); // printing element of array
            // sum+=arr[i][j];// Sum of array
            // }
            // System.out.println();
            // }
            // System.out.println( " sum of 2D array :"+sum);

 // // sum of element of array with while loop


   
            // int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
            // int sum = 0;
            // int i = 0;
            // while (i < 3) {
            // int j = 0;
            // while (j < 3) {
            // System.out.print("\t" + arr[i][j]); // printing element of array
            // sum += arr[i][j];// Sum of array
            // j++;
            // }
            // System.out.println();
            // i++;
            // }
            // System.out.println(" sum of 2D array :" + sum);

// // sum of element of array with do while loop

        // int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        // int sum = 0;
        // int i = 0;
        // do {
        // int j = 0;
        // do {
        // System.out.print("\t" + arr[i][j]); // printing element of array
        // sum += arr[i][j];// Sum of array
        // j++;
        // } while (j < 3);
        // System.out.println();
        // i++;
        // }while (i < 3);
        // System.out.println(" sum of 2D array :" + sum);

// sum of prime number element in matrix with for loop

        // int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        // int sum = 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) {
        // System.out.print("\t"+a);
        // sum += arr[i][j];
        // }
        // System.out.println();
        // }
        // }
        // System.out.println("Sum of Prime number of array element "+sum); //
correcet

// to find the element in 2D array

        // int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        // int n = 15, c = 0;
        // for (int i = 0; i < 3; i++)
        // {
        // for (int j = 0; j < 3; j++)
        // {
        // System.out.println("\t" + arr[i][j]);
        // if (arr[i][j] == n)
        // c++;
        // }
        // System.out.println();
        // }
        // if (c==0)
        // System.out.println("Number Not found ");
        // else
        // System.out.println("Number found ");

// // printing the array by user input of 3x3


        // Scanner ob= new Scanner(System.in);
        // int [] [] arr=new int [3][3];
        // for(int i=0;i<3;i++){
        // for(int j=0;j<3;j++){
        // arr[i][j]=ob.nextInt();
        // }
        // }
        // for(int i=0;i<3;i++){
        // for(int j=0;j<3;j++){
        // System.out.print("\t"+arr[i][j]);
        // }
        // System.out.println();
        // }

// // printing the sum of diagonal of array by user input of 3x3


        // Scanner ob= new Scanner(System.in);
        // int [] [] arr=new int [3][3];
        // for(int i=0;i<3;i++){
        // for(int j=0;j<3;j++){
        // arr[i][j]=ob.nextInt();
        // }
        // }
        // int s=0;
        // for(int i=0;i<3;i++){
        // for(int j=0;j<3;j++){
        // if(i==j){
        // System.out.print("\t"+arr[i][j]);
        // s+=arr[i][j];
        // }
        // }
        // System.out.println();
        // }
        // System.out.println(s);

// // printing the array and prime number sum of array

        // Scanner ob = new Scanner(System.in);


        // int[][] arr = new int[3][3];
        // for (int i = 0; i < 3; i++) {
        // for (int j = 0; j < 3; j++) {
        // arr[i][j] = ob.nextInt();
        // }
        // System.out.println();
        // }
        // System.out.println("the element of array are ");
        // for(int i=0;i<3;i++){
        // for(int j=0; j<3;j++){
        // System.out.print("\t"+arr[i][j]);
        // }
        // System.out.println();
        // }
// System.out.println("the prime numbers are ");

        // 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) {
        // System.out.print("\t" + a);
        // s += arr[i][j];
        // }
        // // System.out.println();

        // }
        // System.out.println();
        // }
        // System.out.println(" the sum of prime number is : " + s);

// searching the element of array by taking from user


        Scanner ob = new Scanner(System.in);
        int[][] arr = new int[3][3];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                arr[i][j] = ob.nextInt();
            }
        }

        int s = 0;

        int c = 0;
        int in1 = 0, in2 = 0;

        System.out.println("Enter the number we want to find in array");


        int n = ob.nextInt();
        for (int i = 0; i < 3; i++) {

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


                if (arr[i][j] == n) {
                    in1 = i;
                    in2 = j;
                    c++;

                }

            }
            // System.out.println();
        }
        if (c == 0) {
            System.out.println("Number not found ");
        } else {

            System.out.println("number  found");
        }
        System.out.println("Element found at i " + in1 + " j " + in2 + " th
position.");
        // System.out.println(s);

    }
}

 Matrix

public class Matrix2DArray {


    public static void main(String[] args) {
//Printing sum of left diagonal

        // 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 diagonal of matrix"+s);
//Printing sum of right diagonal

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


12 } };
        // 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 diagonal of matrix " + s);

//Printing sum of second row of matrix

        //   int arr[][] = { { 1, 4, 9 }, { 4, 7, 6 }, { 7, 2, 1 } };
        // 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  second row of matrix " + s);

//Printing sum of second column of a matrix

         int arr[][] = { { 1, 4, 9 }, { 4, 7, 6 }, { 7, 2, 1 } };
        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  second col of matrix " + s);

    }

You might also like