Showing posts with label Example. Show all posts
Showing posts with label Example. Show all posts

September 7, 2011

Example 10

Task:
Create a program that ask for input integers n with the following provisions:
if  n = 5
Output :
***  *
     * *
*****
* *
* ***


Code:
for row <-- 1 to n do
{
  for column <-- 1 to n do
  {
    if (row = 1) and (column <= N div 2 + 1) then
    {
      output("*");
    } else if (row = n) and (column >= n div 2 + 1) then
    {
      output("*");
    } else if (column = 1) and (row >= n div 2 + 1) then
    {
      output("*");
    } else if (column = n) and (row <= n div 2 + 1) then
    {
      output("*");
    } else if (column = n div 2 + 1) or (row = n div 2 + 1) then
    {
      output("*");
    } else
    {
      output(" ");
    }
    output("\n"); /*new line*/
  }
}

September 5, 2011

Example 9

Task:
Create a program that ask for input integers n with the following provisions:
if  n = 5


Output : 
*       *
  *   *
    *
  *   *
*       *
 Code:
for row <-- 1 to N do
{
  for column <-- 1 to N do
  {
    if (row = column) or (row + column = N + 1) then
    {
      output("*");
    } else {
      output(" ");
    }
    output("\n"); /* new line */
  }
}



September 3, 2011

Example 8

Task:
Create a program that ask for input integers n with the following provisions:
if  n = 5


Output :
   *
   *
*****
   *
   *

Code:
for row <-- 1 to N do
{
  for column <-- 1 to N do
  {
    if (row = N div 2 + 1) or (column = N div 2 + 1) then
    {
      output("*");
    } else {
      output(" ");
    }
    output("\n"); /* new line */
  }
}

September 2, 2011

Example 7

Task:
Create a program to calculate the number of words.
Example:
Sentence: learn algorithm
Word count: 2


Code:
input sentence

isAnyWord<-F 
num<-0
for a<-1 to Length(sentence), a++
{
  if sentence[a]!= ' ' then isAnyWord <- T //if not space
  if sentence[a] = ' ' then //if space
  {
    if isAnyWord = T then //if previously there was a word, num++
    { 
      num++
      isAnyWord = F
    } 
  }
}

September 1, 2011

Example 6

Task:
Prompted to enter a user id and password if id and password is correct then the user will view the first entry is required, otherwise the process is repeated from the beginning to user id and password are correct.
Code:
id,password : string

begin
repeat
input id 
input password
   until 
   id = 'username' and  password = '123'
   print('You have entered the main page')
end.

August 31, 2011

Example 5

Task:
Create programs that require the input of a string. Example: If input = hello
Output:  

hello  
e     l
l      l
l     e
olleh

Code:

 //print "hello" first line
 for a<-1 to Length(sentence),a++
 {
   print sentence[a]
   print newline
 }
 //print the second line of until before the last line
 for a<-2 to Length(sentence)
 {
 print sentence[a]
 for b<-1 to Length(sentence)-2{print space}
 print sentence[Length(sentence)+1-a]
 print newline
 }
 //print "olleh" last line
 for a<-Length(sentence) downto 1,a-
 {
   print sentence [a]
 }

August 29, 2011

Example 4

Task:
Make a program  that require input an integer n:
Example:
If n = 3
Output:
1
2
3
2
1

If n = 4
Output:

1
2
3
4
3
2
1


Code:
input n
For a <- 1 to n do a++
{
  print a + newline
}

For a <- n-1 downto 1 do a--
{
  print a + newline
}

August 28, 2011

Example 3

Task:
Create a program that require input an integer n.
example output:
if n = 5
*****
*****
*****
*****
*****

if n = 3
***
***
***

Code :
input n
For a <- 1 to n
{
  For b <- 1 to n
  {
    print "*"
  }
}

August 26, 2011

Example 2

Task:
Make coding that print an even number of 1 to 100.

Code: 
for n <-- 1 to 100
{
  if (n mod 2) = 0 then print n
}

August 25, 2011

Example 1

Task:
Create an algorithm to display numbers 1-100 provided that:

  • Multiples of 3 is replaced with fizz

  • Multiples of 5 is replaced with  buzz

  • Multiples of 3 & 5 be replaced with fizzbuzz
Code :
for num 1 to 100
if mod 3*5 print fizzbuzz
else if mod 3 print fizz
else if mode 5 print buzz
else cetak num