You are on page 1of 6

PROGRAMMING II

ASSIGNMENT 2
Control Statement and Array

NAMA

NO. KAD MATRIK

NAMA PENSYARAH

CHOW YEE HUI

E20141010065

Pn. Rohaizah

Questions :

1. Develop a system that will find the largest and the smallest number among 10
numbers given by the user. User can test the system as many as he/she want (Use
do..while statement). Use -1 to sexit the system. For each time user test the system,
ask the user to input ten(10) float/double number and store it using array. The system
then will find and display the largest and the smallest number among them.
[10 marks]
Coding :
package a2;
import java.util.*;
public class s1 {
public static void main(String [] args){
double number [] = new double [10];
double max=0;
double min=0;
int response;
Scanner sc = new Scanner (System.in);
do {
for (int y=0 ; y<10 ; y++)
{
System.out.println("Enter number : ");
number [y] = sc.nextDouble();
if (number[y] >= max)
max = number[y];
if (number[y] <= min)
min = number[y];
}
min =number[0];
for(int y=1; y<10 ; y++){
if(number[y] <=min)
min=number[y];
}
System.out.println("Your numbers chose : ");
for(int y=1; y<10 ; y++){
System.out.println(number[y] + " ");
System.out.println();
System.out.println("The largest number : " + max);
System.out.println("The smallest number : " + min);

}
System.out.println();
System.out.print("Enter any number to repeat. (-1 to exit) : ");
response=sc.nextInt();
}while (response!=-1);
System.out.println("System terminated.");
}
}
Output :
Enter number :
5
Enter number :
4
Enter number :
7
Enter number :
14
Enter number :
45
Enter number :
4
Enter number :
4
Enter number :
4
Enter number :
47.5
Enter number :
50
Your numbers chose :
4.0
The largest number : 50.0
The smallest number : 4.0
7.0
The largest number : 50.0
The smallest number : 4.0
14.0
The largest number : 50.0
The smallest number : 4.0
45.0
The largest number : 50.0
The smallest number : 4.0
4.0
The largest number : 50.0
The smallest number : 4.0
4.0

The largest number : 50.0


The smallest number : 4.0
4.0
The largest number : 50.0
The smallest number : 4.0
47.5
The largest number : 50.0
The smallest number : 4.0
50.0
The largest number : 50.0
The smallest number : 4.0
Enter any number to repeat. (-1 to exit) : -1
System terminated.

2.

Develop a java class for palindrome. A palindrome is a number or text phrase that
reads the same backward as forward. For example, each of the following five
characters are palindrome: katak, kakak. Write a program that reads in a five-digit
character and determines whether its a palindrome or not.
[10 marks]

Coding :
package a2;
import java.util.*;
public class s2 {
public static void main(String [] args)
{
String original, reverse = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter a 5 digits characters to check if it is a palindrome");
original = sc.nextLine();
int length = original.length();
for ( int i = length - 1; i >= 0; i-- )
reverse = reverse + original.charAt(i);
if (original.equals(reverse))
System.out.println("palindrome.");
else
System.out.println("not a palindrome.");

}
}
Output :
Enter a 5 digits characters to check if it is a palindrome
kakak
palindrome.
Enter a 5 digits characters to check if it is a palindrome
comel
not a palindrome.

3. Write a program that prints the following diamond shape of *.


*
***
*****
*******
*********
*******
*****
***
*
[10 marks]
Coding :
package a2;
public class s3 {
public static void main(String[] args){
for (int x= 1;x<10; x+=2) {
for (int y = 0; y < 9 - x / 2; y++) {
System.out.print(" ");
}
for (int y = 0; y<x; y++) {
System.out.print("*");
}
}

System.out.print("\n");

for (int x = 7; x > 0; x -= 2) {


for (int y = 0; y < 9 - x / 2; y++) {
System.out.print(" ");
}
for (int y = 0; y < x; y++) {
System.out.print("*");
}

}
}

Output :
*
***
*****
*******
*********
*******
*****
***
*

System.out.print("\n");

You might also like