Skip to main content

Arrays Introduction | Java Complete Placement Course | Lecture 10

Java - Introduction to Programming

Lecture 10



Arrays In Java


Arrays in Java are like a list of elements of the same type i.e. a list of integers, a list of booleans etc. 

  1. Creating an Array (method 1) - with new keyword

int[] marks = new int[3];

marks[0] = 97;

marks[1] = 98;

marks[2] = 95;


  1. Creating an Array (method 2)

int[] marks = {98, 97, 95};


  1. Taking an array as an input and printing its elements.

import java.util.*;

 

public class Arrays {

   public static void main(String args[]) {

       Scanner sc = new Scanner(System.in);

       int size = sc.nextInt();

       int numbers[] = new int[size];

 

       for(int i=0; i<size; i++) {

           numbers[i] = sc.nextInt();

       }

 

       //print the numbers in array

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

           System.out.print(numbers[i]+" ");

       }

   }

}

 





Homework Problems


  1. Take an array of names as input from the user and print them on the screen.

import java.util.*;

 

public class Arrays {

   public static void main(String args[]) {

      Scanner sc = new Scanner(System.in);

      int size = sc.nextInt();


      String names[] = new String[size];

 

      //input

      for(int i=0; i<size; i++) {

          names[i] = sc.next();

      }

     

      //output

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

           System.out.println("name " + (i+1) +" is : " + names[i]);

       }

      

   }

}

 



  1. Find the maximum & minimum number in an array of integers. 

[HINT : Read about Integer.MIN_VALUE & Integer.MAX_VALUE in Java]

import java.util.*;

 

public class Arrays {

   public static void main(String args[]) {

      Scanner sc = new Scanner(System.in);

      int size = sc.nextInt();

      int numbers[] = new int[size];

 

      //input

      for(int i=0; i<size; i++) {

          numbers[i] = sc.nextInt();

      }

 

      int max = Integer.MIN_VALUE;

      int min = Integer.MAX_VALUE;

     

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

           if(numbers[i] < min) {

               min = numbers[i];

           }

           if(numbers[i] > max) {

               max = numbers[i];

           }

       }

 

       System.out.println("Largest number is : " + max);

       System.out.println("Smallest number is : " + min);

      

   }

}

 




  1. Take an array of numbers as input and check if it is an array sorted in ascending order.

Eg : { 1, 2, 4, 7 } is sorted in ascending order.

       {3, 4, 6, 2} is not sorted in ascending order.

import java.util.*;

 

public class Arrays {

   public static void main(String args[]) {

      Scanner sc = new Scanner(System.in);

      int size = sc.nextInt();

      int numbers[] = new int[size];

 

      //input

      for(int i=0; i<size; i++) {

          numbers[i] = sc.nextInt();

      }

 

      boolean isAscending = true;

     

       for(int i=0; i<numbers.length-1; i++) { // NOTICE numbers.length - 1 as termination condition

           if(numbers[i] > numbers[i+1]) { // This is the condition for descending order

               isAscending = false;

           }

       }

 

       if(isAscending) {

           System.out.println("The array is sorted in ascending order");

       } else {

           System.out.println("The array is not sorted in ascending order");

       }

      

   }

}

 

 

Comments

Popular posts from this blog

Conditional Statements | if, If-else, "if" and "if else", Switch Break | Complete Java Placement Course | Lecture 3

                                    Java - Introduction to Programming Lecture 3 Conditional Statements Conditional statements are a fundamental concept in programming. They are used to make decisions based on certain conditions, allowing the program to perform different actions depending on the input or data it receives. In this article, we'll take a closer look at conditional statements in programming and how they can be used effectively. What are conditional statements? Conditional statements, also known as "if-then" statements, are used in programming to execute certain code if a condition is met. In other words, the program will perform a particular action if a particular condition is true. For example, a program may check if a user has ent...

Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.

Enter 3 numbers from the user & make a function to print their average. To find the average of three numbers, you need to add the three numbers together and then divide the sum by three. Here's how you can write a function in C++, C, Python, and Java to take three numbers from the user and calculate their average: JAVA import java . util .*; public class Solutions {     public static void main ( String args []) {        Scanner sc = new Scanner ( System . in );        int a = sc . nextInt ();        int b = sc . nextInt ();        int c = sc . nextInt ();          int average = ( a + b + c ) / 3 ;        System . out . println ( average );    }    }   C++: // c++ #include <iostream> using namespace std ;...

Recursion in One Shot | Theory + Question Practice + Code | Level 1 - Easy

JAVA Recursion Class 1 (Codes) Q1. Print numbers from 5 to 1. public static void printNumbers(int n) { if(n == 0) { return; } System.out.println(n); printNumbers(n-1); } Q2. Print numbers from 1 to 5. public static void printNumbers(int n) { if(n == 6) { return; } System.out.println(n); printNumbers(n+1); } Q3. Print the sum of first n natural numbers. class Recursion1 { public static void printSum(int n, int sum) { if(n == 0) { System.out.println(sum); return; } sum += n; printSum(n-1, sum); } public static void main(String args[]) { printSum(5, 0); } } Q4. Print factorial of a number n. class Recursion1 { public static void printFactorial(int n, int fact) { if(n == 0) { System.out.println(fact); return; } fact *= n; printFactorial(n-1, fact); } public static void main(String args[]) { printFactorial(5, 1); } } Q5. Print the fibonacci sequence till nth term. class Recursion1 { public static void printFactorial(int a, int b, int n) { if(n == 0) { return; } System.out.println(a); printF...