Today we learnt new things called array. finally we seen Array.
An array is a collection of similar types of data. For example, if we want to store the names of 100 people then we can create an array of the string type that can store 100 names. String[] array = new String[100]; Here, the above array cannot store more than 100 names.
Program for printing the same number using while and for loop.
package demo;
public class Array {
public static void main(String[] args) {
int[]age= {12,13,14,15,16};
// int i=0;
System.out.print("{");
/*while(i<age.length) {
System.out.print(age[i]+",");
i++;
}*/
// System.out.print("}");
for(int i=0;i<age.length;i++) {
System.out.print(age[i]+",");
}
System.out.print("}");
}
}
output:
{12,13,14,15,16,}
program for adding the given numbers;
package demo;
public class arraysum {
public static void main(String arg[]) {
int[]age= {11,12,14,15,17};
int b=0;
for(int i=0;i<age.length;i++) {
b=b+age[i];
}
System.out.print(b);
}
}
output:
69
Program for displaying the sum of given odd number:
package demo;
public class Arrayodd {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[]age= {11,12,13,14,15};
int b=0;
for(int i=0;i<age.length;i++) {
if(i%2!=0) {
b=b+age[i];
}
}
System.out.println("odd"+b);
}
}
output;
odd26
program for finding the single number in the given number:
package demo;
public class Array2 {
public static void main(String args[]) {
int[]age= {11,12,13,14,15};
for(int i=0;i<age.length;i++) {
if(age[i]==13) {
System.out.println(“yes”);
}
else {
System.out.println("no");
}
}
}
}
output:
no
no
yes
no
no