M105 Exercise 05 Solution
M105 Exercise 05 Solution
Exercise 1:
What is the exact output of the following pieces of code?
a)
int[] array = {9, 5, 8, 1, 11, 3, 10};
array[0] = 2;
array[1]++;
array[2] = array[0] + array[1];
array[3] = array[2-1];
array[4] = array.length;
array[5] = ++array[0];
array[6] = array[0]++;
for (int i = 0; i < array.length; i++)
System.out.println(array[i]);
The output:
4
6
8
6
7
3
3
b)
int[] grades = {30, 90, 80, 50, 70};
for (int i = 0; i < grades.length; i+=2)
System.out.println(grades[i]);
The output:
30
80
70
c)
int[] grades = {30, 90, 80, 50, 70};
for (int i = grades.length-1; i >=0; i--)
System.out.println(grades[i]);
The output:
70
50
80
90
1
30
Exercise 2:
Write a Java program that creates and initializes an array of integers. Then it finds and prints the
minimum odd value in this array with its index. Assume that all the values < 1,000,000.
The Solution:
public class Test {
public static void main(String[] args){
int x[]={5,7,2,10,9,3};
int min=999999;
int index=-1;
for(int i=0;i<x.length;i++){
if(x[i]%2!=0 && x[i]<min){
min=x[i];
index=i;
}
}
System.out.printf("The minimum odd value in the array is: %d, its index is: %d\n", min, index);
}
}
Exercise 3:
Write a Java program that creates and initializes an array of integers. Then it finds and prints the
average of the positive numbers in this array.
The Solution:
public class Test {
public static void main(String[] args){
int x[]={5,7,3,-10,9,-3};
int sum=0, count=0;
for(int i=0;i<x.length;i++){
if(x[i]>0){
sum = sum + x[i];
count++;
}
}
double avg=(double)sum/count;
System.out.printf("The average of the positive number is: %.2f\n",avg);
}
}
2
Exercise 4:
Write a Java program that creates an eleven-element array of integers. Then it computes and stores
the factorial of the numbers from 0 to 10 in this array. (0! = 1)
The Solution:
public class Test {
public static void main(String[] args){
int x[]= new int [11];
x[0]=1;
for(int i=1;i<x.length;i++){
int fact=1;
for(int k=1;k<=i;k++){
fact=fact*k;
}
x[i]=fact;
}
for(int i=0;i<x.length;i++)
System.out.printf("The factorial of %d is: %d\n",i,x[i]);
}
}
Exercise 5:
Write a Java program that creates and initializes an array of integers. Then it reads from the user a
number and search for that number in the array. If the number is found, the program should print
its index (the first index is found in); otherwise a suitable message should be printed.
The Solution:
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int x[]= {7,3,5,11,2,4};
boolean found=false;
int index=-1;
System.out.print("Enter a number: ");
int num=input.nextInt();
for(int i=0;i<x.length;i++){
if(x[i]==num){
found=true;
index=i;
break;
}
}
if(found)
System.out.printf("%d is found at index: %d\n",num,index);
else
System.out.printf("%d is not found\n",num);
}
}
3
Exercise 6:
Write a Java program that creates and initializes an array of integers. Then create another array
with the same size. After that, the program should copy all the elements of the first array inside the
second array.
The Solution:
public class Test {
public static void main(String[] args){
int x[]= {7,3,5,11,2,4};
int y[]= new int[x.length];
for(int i=0;i<x.length;i++){
y[i]=x[i];
}
// test ///////////////
for(int i=0;i<y.length;i++)
System.out.println(y[i]);
}
}
Exercise 7:
Write a Java program that prompts the user to enter a word then prints the number of vowels in
this word. (Vowels are the letters 'a', 'e', 'i', 'o' and 'u')
The Solution:
import java.util.Scanner;
public class Test{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("Enter a word: ");
String word=input.next();
int count=0;
char c;
for(int i=0;i<s.length();i++){
c= s.charAt(i);
if(c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
count++;
}