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

Assignment 2: Nadiah Binti Zainol Abidin

The document contains the assignments and solutions for 3 programming problems. The first problem asks the user to develop a system that takes 10 numbers as input and finds the largest and smallest numbers. The solution uses an array to store the numbers and loops to find the max and min. The second problem is to check if a 5 digit input is a palindrome. The solution reverses the string and compares it to the original. The third problem prints a diamond shape of stars of increasing then decreasing size using nested for loops.

Uploaded by

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

Assignment 2: Nadiah Binti Zainol Abidin

The document contains the assignments and solutions for 3 programming problems. The first problem asks the user to develop a system that takes 10 numbers as input and finds the largest and smallest numbers. The solution uses an array to store the numbers and loops to find the max and min. The second problem is to check if a 5 digit input is a palindrome. The solution reverses the string and compares it to the original. The third problem prints a diamond shape of stars of increasing then decreasing size using nested for loops.

Uploaded by

syarinad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PENGATURCARAAN II

ASSIGNMENT 2

NADIAH BINTI ZAINOL ABIDIN


E20131007127

PN ROHAIZAH BINTI ABDUL WAHIB

Questions :

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.

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.

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("*");
}

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

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

You might also like