Creating a program from flowchart

Post on 08-May-2015

8.108 views 0 download

Transcript of Creating a program from flowchart

How to create a program

A simple guide to help you better understand the process

1.Example program

Create a program that produces the following-

1.Input a number

2.Find out if the number is a multiple of 3

2.Create a flowchart

So if the output has more than one answer, it would be conditional instructions so we must include a decision box within the flowchart.

3.Creating the flowchart

To help us with coding, we should create variable names for the data we will input.

So if the instruction is to input a number then we will call it var numb.

*variables are case-sensitive and cannot include spaces or punctuation marks.

4.The flowchart

The code<html>

<body>

<script type="text/javascript">

var numb = prompt("enter number to check if it is a multiple of 3","");

numb= parseInt(numb);

var remainder=numb%3;

if(remainder%3!=0)

{

document.write("no");

}

else

{

document.write("yes");

}

</script>

</body>

</html>

5.Using the flowchart to help create code in javascript

<html>

<body>

<script type="text/javascript">

5.Using the flowchart to help use code in javascript

Here we create the code so the program will ask the user to enter a number and we assign the variable to a function.

var numb = prompt("enter number to check if it is a multiple of 3","");

numb= parseInt(numb);

5.Using the flowchart to help use code in javascript

This % is a mathematical operator which will present a remainder if a number is divided. The if statement checks if a code divides by 3 with a remainder.

var remainder=numb%3;

if(remainder%3!=0)

5.Using the flowchart to help use code in javascript

The “!=0” states to check if the remainder is not equal to 0 since a multiple would divide and have no remainder.

var remainder=numb%3;

if(remainder%3!=0)

5.Using the flowchart to help use code in javascript

So if code divides by 3 with a remainder, the if statement will produce output that reads “no” meaning the number entered in the program is not a multiple of 3. If there is any other number that does divide by 3 with no remainder it will display yes.

{

document.write("no");

}

else

{

document.write("yes");

}

5.Using the flowchart to help use code in javascript

We then end the program with this-

</script>

</body>

</html>

5.Using the flowchart to help use code in javascript

We then end the program with this-

</script>

</body>

</html>

Summary

Create a flowchart to help you when coding a program.

Remember that variables usually begin with “var” and are case-sensitive.

Document.write is used to produce an output. Assign a variable using parseInt I.e var age =

parseInt (age.)

*If a code doesn't work, you can check online to see what could be wrong with the code.