You are on page 1of 3

Assignment 6

Q1:
import java.util.Scanner;

public class Assignment6 {


public static void main(String [] args) {
Scanner s = new Scanner(System.in);
int positive = 0;
int negative = 0;
double total = 0;
int count = 0;
double average;
int number;
System.out.println("Enter number");

do { number = s.nextInt();
total += number;
count++;
if(number > 0){
positive++;
}
else if(number < 0){
negative++;
}
}
while(number != 0);

average = total / count;

System.out.println("The number of positives is "+ positive);


System.out.println("The number of negatives is "+ negative);
System.out.println("The total is "+ total);
System.out.println("The average is "+ average);

}
Q2:

Algorithm:
Start
Enter product number
Enter quantity sold
Total price 1= price 1of number *quantity sold
Total price 2= price 2of number *quantity sold
Total price 3= price 3of number *quantity sold
Total price 4= price 4of number *quantity sold
Total price 5= price 5of number *quantity sold

Print total price


End

Code;
import java.util.Scanner;

public class Assignment6 二 {


public static void main (String [] args) {
Scanner s = new Scanner(System.in);
double totalprice = 0;

while (true) {
System.out.println("price of product 1 is RM2.98");
System.out.println("price of product 2 is RM4.5");
System.out.println("price of product 3 is RM9.98");
System.out.println("price of product 4 is RM4.49");
System.out.println("price of product 5 is RM6.87");
System.out.println("-9999 out of programm");
System.out.println("Enter product number");
int p= s.nextInt();
if(p == -9999)
break;}
System.out.println("Enter quantity sold");
int q = s.nextInt();
switch('p') {
case 1:
totalprice+=2.98*q;
break;
case 2:
totalprice+=4.50*q;
break;
case 3:
totalprice+=9.98*q;
break;
case 4:
totalprice+=4.49*q;
break;
case 5:
totalprice+=6.87*q;
break;}
System.out.println("The total retail value of all
products sold: $"+totalprice);

You might also like