Presentation is loading. Please wait.

Presentation is loading. Please wait.

Count and add list of numbers From user input and from file.

Similar presentations


Presentation on theme: "Count and add list of numbers From user input and from file."— Presentation transcript:

1 Count and add list of numbers From user input and from file

2 Count: From user input criteria to stop: reading negative number Dim Count as integer, Num as integer Count=0 Num=val(inputbox(“ Enter a number: “)) Do while (Num>=0) Count=Count+1 Num=val(inputbox(“ Enter a number: “)) Loop Print “The total numbers read is: “ & Count

3 Add: From user input criteria to stop: reading negative number Dim Sum as integer, Num as integer Sum=0 Num=val(inputbox(“ Enter a number: “)) Do while (Num>0) Sum=Sum+Num Num=val(inputbox(“ Enter a number: “)) Loop Print “The sum of the numbers read is: “ & Sum

4 Count: From file (note: counts all numbers in the file) Dim Count as integer, Num as integer Open “data.txt” For Input As #1 Count=0 Do while Not EOF(1) Input #1, Num Count=Count+1 Loop Print “The total numbers read is: “ & Count

5 Add: From file (note: adds all numbers in the file) Dim Sum as integer, Num as integer Open “data.txt” For Input As #1 Sum=0 Do while Not EOF(1) Input #1, Num Sum=Sum+Num Loop Print “The sum of the numbers read is: “ & Sum

6 Exercises: –Write a code to find the average for a list of numbers for the previous two case: Read from user input Read from file –Write a code to find the number of the odd and even numbers in a list of numbers for the same two cases.


Download ppt "Count and add list of numbers From user input and from file."

Similar presentations


Ads by Google