Sunteți pe pagina 1din 12

FTSC 3103

PROGRAMMING

LAB EXERCISE 6

Name: Muhammad Nur Hakimi Bin Abd Jalil


No.Matrix: 16FA03028
Lect: Siti Azrehan Bt Aziz
Example 1a
public class example1a {
public static void main(String[] args) {

int y;
int x = 1;
int total = 0;

while (x <= 10)


{
y = x * x;
System.out.println(y);
total +=y;
++x;
}
System.out.printf("Total is %d\n", total);
}
}

Example 1b
import java.util.Scanner;
public class Lab6_Example1b {

public static void main(String[] args) {

int data;
int sum = 0;
Scanner MyInput = new Scanner (System.in);

System.out.print("Enter an int value: ");


data = MyInput.nextInt();

while (data != 0)
{
sum += data;
System.out.print("Enter an int value, the program exits if the input is 0: ");
data = MyInput.nextInt();
}
System.out.println("The sum is "+sum);

}
}
EXAMPLE 2A
import java.util.Scanner;

public class Lab6_Example2a {

public static void main(String[] args) {

Scanner input = new Scanner (System.in);

int a, b, length, width;

System.out.print("Enter the length of the square:");


length = input.nextInt();

System.out.print("Enter the width of the square:");

width = input.nextInt();

for(a=1;a<=length;a++)

{
System.out.println();
for(b=1;b<=width;b++)

{
System.out.print("*");

}
}

}
EXAMPLE 2B

public class Lab6_Example2b {

public static void main(String[] args) {

for(int row = 1; row<6; row++)

for(int column = 1; column < 6-row; column++)


System.out.print(" ");

for(int num = row; num>=1; num--)


System.out.print(num);

for(int num = 2; num<=row; num++)

System.out.print(num);

System.out.println("");
}
}

}
EXAMPLE 3A

public class Lab6_Example3a {

public static void main(String[] args) {

int sum = 0;

int item = 0;

do
{

item ++;
sum += item;

}while (item < 5);

System.out.println("The sum is "+ sum);


}

}
EXERCISE 1
import java.util.Scanner;

public class Lab6_Exercise1 {

public static void main(String[] args) {

Scanner input = new Scanner (System.in);

int a, b, length, width;

System.out.print("Enter the length of the square:");


length = input.nextInt();

System.out.print("Enter the width of the square:");

width = input.nextInt();

for(a=1;a<=length;a++)
{

System.out.println();
for(b=1;b<=width;b++)

System.out.print("@");

}
}

}
}
EXERCISE 2
public class Lab_6 {

public static void main(String[] args) {

int miles;

double kilo = 1.609;

double km = 0;

System.out.println("Miles Kilometers");

System.out.println("-----------------------------");

for(miles=1;miles<=10;miles++)

km = km + kilo ;
System.out.println(miles+" "+km);

System.out.println("-----------------------------");
}
}

}
CONCLUSION
In this lab exercise, I have learned how to write coding using statement and while in this program.
Hope this lab will help me in better future to write better code.

S-ar putea să vă placă și