Problem Solving Assignment 1 PDF
Problem Solving Assignment 1 PDF
PROBLEM SOLVING
Name: Lakshya Tripathi
Batch: 49
SAP ID: 500127054
Email ID: [email protected]
Question 1. Create an algorithm and a flowchart that will output for G.C.D
Input: Two integers n1 and n2.
Output: The GCD of the two numbers n1 and n2.
Algorithm:
1: Start with the two input integers n1 and n2.
2: While n2 is not equal to 0:
• Calculate the remainder of n1 divided by n2, denoted as r.
• Set n1 equal to n2 and n2 equal to r.
3: When n2 becomes 0, the value of n1 will be the GCD of the original n1 and
n2.
4: Return the value of n1 as the GCD.
Flowchart:
Question 2. Create an algorithm and a flowchart that will output the factorial of a
given number.
Input: A non-negative integer n.
Output: The factorial of n, denoted as n!
Algorithm:
1: Start
2: Input a non-negative integer n.
3: If n is less than 0, display an error message and exit.
4: If n is 0 or 1, the factorial is 1.
Set factorial to 1.
Go to step 7.
5: Initialize
• factorial = 1
• i =1
6: If i<=n, calculate factorial = factorial * i.
7: Increase the value of i by 1 i.e., i=i+1.
8: Go to step 6, otherwise go to step 9.
9: The value of factorial is the factorial of n.
10: Output the value of factorial.
11: End.
Flowchart:
Question 3. Create an algorithm and a flowchart that will output the Fibonacci
series up to a given number.
Input: An integer n.
Output: Fibonacci series with number of terms equal to n.
Algorithm:
1: Start
2: Initialize variables:
• a: Initialize to 0 (first term)
• b: Initialize to 1 (second term)
• i: Initialize to 1 (loop counter)
3: Read the number of terms to generate as n.
4: While i is less than or equal to n, repeat steps 5-7.
5: Print the value of 'a'.
6: Update 'a' to 'b' and 'b' to 'a + b'.
7: Increment i by 1.
8: End
Flowchart:
Question 4. Create an algorithm and a flowchart that will output all the prime
numbers between 2 numbers.
Input: Two numbers, start and end, entered by the user. start is the lower limit,
and end is the upper limit.
Output: Print all prime numbers between start and end.
Algorithm:
1: Read the lower limit start from the user.
3: Initialize an empty list to store the prime numbers, let's call it prime numbers.
If number is less than 2, set is prime to False (as prime numbers are
greater than 1).
For each integer i in the range from 2 to the square root of number: If
number is divisible by i, set is prime to False and break the loop.
If is prime is still True after the loop in step 4(111), add number to the
prime numbers list.
5: Print the prime numbers list, which contains all prime numbers between start
and end.
Flowchart: