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

Java Lec-19 Programs On Array.6c72454

The document provides code examples for two Java programs using arrays. The first program creates an array of size 5, reads and stores integers from user input, and prints the array. The second program reads 10 integers into an array, calculates the sum and average, and prints the results. Both examples were written by Atul Kabra to teach Java array concepts in a lecture.

Uploaded by

Mayur Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java Lec-19 Programs On Array.6c72454

The document provides code examples for two Java programs using arrays. The first program creates an array of size 5, reads and stores integers from user input, and prints the array. The second program reads 10 integers into an array, calculates the sum and average, and prints the results. Both examples were written by Atul Kabra to teach Java array concepts in a lecture.

Uploaded by

Mayur Patil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Course: Java Programming, Prepared By: Atul Kabra, 9422279260

Java Lecture-19
Topic: Programs on Array
1) Write a program to create an array of size 5 and then read and print the
numbers of an array.
import java.util.*;

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

int [] a = new int[5]; //create an array


int i;

System.out.println("Enter "+ a.length+" numbers ");

for(i=0;i<a.length;i++) //read the numbers in array


a[i]=in.nextInt();

System.out.println("\nArray Elements are...");


for( int x : a) //printing array number using for-each loop
System.out.println(x);
}
}
Output:

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Java Programming, Prepared By: Atul Kabra, 9422279260

2. Write a program to find the sum and average of 10 integers.

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

int [] a = new int[10];


int sum=0,i;
float avg;

System.out.println("Enter "+a.length+" numbers ");


for(i=0;i<a.length;i++)
{
a[i]=in.nextInt();
sum=sum+a[i];
}
avg=(float)sum/a.length;

System.out.println("Sum = "+sum);
System.out.println("Average = "+avg);
}
}
Output:

Course: Java Programming, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260

You might also like