# Exercise 1

Pseudo Code

Share

### Write an algorithm to print Hello World

``````
Main()
Begin
Print: “Hello World”;
End
``````

### Write an algorithm to enter your name and print a greeting message

``````
/*
name is a variable used to store the name entered by user
*/
Main()
Begin
Print: “Hi ”, name;
End
``````

Note! Message “Hi” and variable name is separated by comma ,

### Write an algorithm to print the sum of two numbers

``````
/*
x and y will store the input and
sum will store the result
*/
SumOf2Num()
Begin
Set sum = x + y;
Print: sum;
End
``````

### Write an algorithm to print from 1 to 10

``````
Print1To10()
Begin
for i = 1 to 10 by 1 do
Print: i and go to new line;
endfor
End
``````
Note! You can use small sentences like “go to new line” in your instruction

### Write an algorithm to print all the even numbers from 1 to 100

``````
/*
Even no. between 1 to 100 starts from 2 and goes up to 100.
So we’ll use a for loop,
start it from 2 and increment i by 2 till we reach 100
*/
PrintEven()
Begin
for i = 2 to 100 by 2 do
Print: i and go to new line;
endfor
End
``````

### Write an algorithm to print all the odd numbers from 1 to 100

``````
/*
Odd no. between 1 to 100 starts from 1 and goes up to 99. So we’ll use a for loop, start it from 1 and increment i by 2 till we reach 99
*/
PrintOdd()
Begin
for (i = 1; i <= 100; i = i + 2)
Print: i and go to new line;
endfor
End
``````

Note! In this case we have used C style for loop

### Write an algorithm to find the biggest element in a 1D array of n elements

``````
/*
Let a[0:n-1] be a 1D array of n elements.
We’ll use big to store the biggest element of the array.
Array is of n elements so starting index = 0 and ending index = n-1
*/
FindBig()
Begin
Set big = a;
for i = 1 to n-1 by 1 do
if a[i]>big then
Set big = a[i];
endif
endfor
Print: big;
End
``````

### Write an algorithm to find the Cube of Odd numbers and Square of Even numbers from 1 to n where n is provided by user

``````
/*
Algorithm starts from the Main module.
Variable n stores the user input.
*/
Main()
Begin
for i = 1 to n by 1 do
if i%2==0 then
Call Square(i);
else
Call Cube(i);
endif
endfor
End

/*
Square module will store value passed by Main module in x and print the square
*/
Square(x)
Begin
Print: x*x and go to new line;
End

/*
Cube module will store value passed by Main module in x and print the cube
*/
Cube(x)
Begin
Print: x*x*x and go to new line;
End
``````

### Write an algorithm to print the factorial of a number n entered by user

Factorial of n is represented as n! where
n! = 1*2*3*…*(n-2)*(n-1)*n
Example
3! = 1*2*3 = 6

``````
/*
Algorithm starts here.
Variable n stores the user input while x stores the result.
*/
Main()
Begin
Set x = Call Fact(n);
Print: x;
End

/*
Fact module will store value passed by Main module in num and
will return the factorial of num back to Main
*/
Fact(num)
Begin
Set f = 1;
for i = 2 to num by 1 do
Set f = f * i;
endfor
return f;
End
``````

### Write an algorithm to find Simple Interest. User will provide the value of Principal, Rate and Time

``````
/*
Variable p, r and t will store the principal,
rate and time respectively while si will store the result.
We are using the formula
si=(p*r*t)/100
*/
SimpleInterest()
Begin
Read: p, r, t;
Set si=(p*r*t)/100;
Print: “Simple Interest = ”, si;
End
``````

Share