0% found this document useful (0 votes)
3 views

Algo Examples 3

The document provides examples of algorithms using loops for various tasks including printing even numbers, generating multiplication tables, calculating sums and factorials, checking for prime numbers, and finding the greatest common divisor. It also includes a guessing game and generating Fibonacci series up to a user-defined limit. Each example outlines the steps in a clear and structured manner.

Uploaded by

hinar0337
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Algo Examples 3

The document provides examples of algorithms using loops for various tasks including printing even numbers, generating multiplication tables, calculating sums and factorials, checking for prime numbers, and finding the greatest common divisor. It also includes a guessing game and generating Fibonacci series up to a user-defined limit. Each example outlines the steps in a clear and structured manner.

Uploaded by

hinar0337
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Algorithm Examples # 3 (Loops)

1. Print Even Numbers (1 to 10):

1. Initialize a variable number to 1.


2. while number is less than or equal to 10:
if number is even (divisible by 2 with no remainder):
Print the value of number.
Increment number by 1.

2. Print Multiplication Table (of a number):

1. Get a number from the user and store it in number.


2. Initialize a variable multiplier to 1.
3. while multiplier is less than or equal to 10:
Print the result of number multiplied by multiplier.
Increment multiplier by 1.

3. Calculate Sum of Digits (of a number):

1. Get a number from the user and store it in number.


2. Initialize a variable sum to 0.
3. while number is greater than 0:

 Extract the last digit of number using modulo operator (%) and store it
in digit.
 Add digit to sum.
 Remove the last digit from number by integer division (/) and store the
result back in number.

4. Print the value of sum.

4. Sum of Natural Numbers:

Step 1: Take an input number.


Step 2: Initialize variables to hold the total sum and the current number.
Step 3: Set the total sum to 0.
Step 4: Set the current number to 1.
Step 5: While the current number is less than or equal to the input number:
5.1: Add the current number to the total sum.
5.2: Increment the current number by 1.
Step 6: Print the total sum.

5. Power Calculation:
Step 1: Take base and exponent as input.
Step 2: Initialize a result variable to 1.
Step 3: Initialize a counter variable to 0.
Step 4: While the counter is less than the exponent:
4.1: Multiply the result by the base.
4.2: Increment the counter by 1.
Step 5: Print the result.

6. Calculate Factorial (of a number):

1. Get a number from the user and store it in number.


2. Initialize a variable factorial to 1.
3. while number is greater than 0:
o Multiply factorial by number.
o Decrement number by 1.
4. Print the value of factorial.

7. Prime Number Check:

Step 1: Take an input number.


Step 2: Initialize a divisor variable to 2.
Step 3: While the divisor is less than the input number:
3.1: If the input number is divisible by the divisor:
3.1.1: Print "Not Prime" and exit the loop.
3.2: Increment the divisor by 1.
Step 4: If the loop completes without finding any divisor other than 1 and the
input number, print "Prime".

8. Greatest Common Divisor (GCD) Calculation:

Step 1: Take two input numbers.


Step 2: Initialize variables to hold the smaller and larger numbers.
Step 3: While the smaller number is not equal to 0:
3.1: Calculate the remainder of the larger number divided by the
smaller number.
3.2: Assign the value of the smaller number to the larger number.
3.3: Assign the value of the remainder to the smaller number.
Step 4: Print the value of the larger number, which represents the GCD.

9. Guessing Game (number between 1 and 50):

1. Generate a random number between 1 and 50 (store in secret_number).


2. Initialize a variable guess to 0.
3. while guess is not equal to secret_number:
o Get a guess from the user and store it in guess.
o If guess is less than secret_number, print "Too low, guess again!".
o If guess is greater than secret_number, print "Too high, guess again!".
4. Print "Congratulations, you guessed the number!"

10. print the Fibonacci series up to a user input number:

Step 1: Take an input number from the user and assign it to a variable 'limit'.
Step 2: Initialize variables 'a' and 'b' to store the first two numbers of the
Fibonacci series as 0 and 1, respectively.
Step 3: Print the first two numbers of the Fibonacci series, which are 0 and 1.
Step 4: Initialize a loop counter variable 'i' to 2 to keep track of the current
position in the Fibonacci series.
Step 5: While 'i' is less than or equal to 'limit':
5.1: Calculate the next Fibonacci number by adding 'a' and 'b' and assign
it to a new variable 'next_number'.
5.2: Print the value of 'next_number'.
5.3: Update the values of 'a' and 'b' for the next iteration:
- Assign the value of 'b' to 'a'.
- Assign the value of 'next_number' to 'b'.
5.4: Increment the loop counter 'i' by 1.
Step 6: End the loop.
Step 7: Exit.

You might also like