Write a PL/SQL program using WHILE loop for calculating the average of the numbers entered by user. Stop the entry of numbers whenever the user enters the number 0.
I have tried below code in SQL developer in sample scott user. I have tried many variations of trying to get input but after 2 inputs it throws the error.
Sorry if not understood or not posted in a proper way as it is my first post on this site.
DECLARE
num NUMBER := 0; -- number entered by the user
sum NUMBER := 0; -- sum of all entered numbers
count NUMBER := 0; -- count of entered numbers
average NUMBER := 0; -- average of entered numbers
BEGIN
WHILE num != 0 LOOP
DBMS_OUTPUT.PUT_LINE('Enter a number (enter 0 to exit): ');
num := &input_number; -- Prompt the user to enter a number
IF num != 0 THEN
sum := sum + num; -- add the entered number to the sum
count := count + 1; -- increment the count of entered numbers
END IF;
END LOOP;
-- Calculate the average
IF count > 0 THEN
average := sum / count;
DBMS_OUTPUT.PUT_LINE('Average: ' || average);
ELSE
DBMS_OUTPUT.PUT_LINE('No numbers entered!');
END IF;
END;
After that getting below error in SQL developer.
old:DECLARE
num NUMBER := 0; -- number entered by the user
sum NUMBER := 0; -- sum of all entered numbers
count NUMBER := 0; -- count of entered numbers
average NUMBER := 0; -- average of entered numbers
BEGIN
WHILE num != 0 LOOP
DBMS_OUTPUT.PUT_LINE('Enter a number (enter 0 to exit): ');
num := # -- Prompt the user to enter a number
IF num != 0 THEN
sum := sum + num; -- add the entered number to the sum
count := count + 1; -- increment the count of entered numbers
END IF;
END LOOP;
-- Calculate the average
IF count > 0 THEN
average := sum / count;
DBMS_OUTPUT.PUT_LINE('Average: ' || average);
ELSE
DBMS_OUTPUT.PUT_LINE('No numbers entered!');
END IF;
END;
new:DECLARE
num NUMBER := 0; -- number entered by the user
sum NUMBER := 0; -- sum of all entered numbers
count NUMBER := 0; -- count of entered numbers
average NUMBER := 0; -- average of entered numbers
BEGIN
WHILE num != 0 LOOP
DBMS_OUTPUT.PUT_LINE('Enter a number (enter 0 to exit): ');
num := 4; -- Prompt the user to enter a number
IF num != 0 THEN
sum := sum + num; -- add the entered number to the sum
count := count + 1; -- increment the count of entered numbers
END IF;
END LOOP;
-- Calculate the average
IF count > 0 THEN
average := sum / count;
DBMS_OUTPUT.PUT_LINE('Average: ' || average);
ELSE
DBMS_OUTPUT.PUT_LINE('No numbers entered!');
END IF;
END;
Error starting at line : 1 in command -
DECLARE
num NUMBER := 0; -- number entered by the user
sum NUMBER := 0; -- sum of all entered numbers
count NUMBER := 0; -- count of entered numbers
average NUMBER := 0; -- average of entered numbers
BEGIN
WHILE num != 0 LOOP
DBMS_OUTPUT.PUT_LINE('Enter a number (enter 0 to exit): ');
num := # -- Prompt the user to enter a number
IF num != 0 THEN
sum := sum + num; -- add the entered number to the sum
count := count + 1; -- increment the count of entered numbers
END IF;
END LOOP;
-- Calculate the average
IF count > 0 THEN
average := sum / count;
DBMS_OUTPUT.PUT_LINE('Average: ' || average);
ELSE
DBMS_OUTPUT.PUT_LINE('No numbers entered!');
END IF;
END;
Error report -
ORA-06550: line 12, column 18:
PLS-00103: Encountered the symbol "+" when expecting one of the following:
(
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action: