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

Assignment No 3 Solutions

This document contains solutions to 3 algorithm questions. It proposes algorithms to find the greatest of three numbers, calculate the factorial of a given number, and calculate the sum of even numbers between 1-20. For the greatest of three numbers, it uses conditional statements to compare each number and output the greatest. For factorials, it multiplies the input number by descending integers. For the even number sum, it uses a loop to add even numbers up to 20.

Uploaded by

Raja Zulqurnain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Assignment No 3 Solutions

This document contains solutions to 3 algorithm questions. It proposes algorithms to find the greatest of three numbers, calculate the factorial of a given number, and calculate the sum of even numbers between 1-20. For the greatest of three numbers, it uses conditional statements to compare each number and output the greatest. For factorials, it multiplies the input number by descending integers. For the even number sum, it uses a loop to add even numbers up to 20.

Uploaded by

Raja Zulqurnain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

REF # L1F12BSCS2063

Section: b

NAME: Zulqurnain jutt

Assignment No 3 Solutions:
Q1:
Features:

This program will identify the greatest of three numbers.


Will automatically identify if any three values or any 2 values will be equal.

Algorithm:
Requirements:
Three numbers, +ve or ve
Scope:
Find Greater of three given numbers
Inputs:
1) Three numbers
Steps:
1) Take three numbers (+ve or ve ) from user.
2) Compare every number individually with every other number using
conditional operator.
3) If 1st number is a , 2nd is b and 3rd is c then if a >b ,a>c then a is greater of all.
4) If b>a , b>c then b is Greater.
5) If c>a , c>b then c is greater of all.
6) And if a=b=c then all are equal.
7) And if a=b , a is not equal to c then a and b are equal accordingly.
8) And if b=c and b is not equals a then b and c are equal.
9) And if c=a and c is not equals b then c is equals a.
10)
Implement these conditions using conditional statements or loops.

Output:
e.g.

REF # L1F12BSCS2063

Section: b

NAME: Zulqurnain jutt

a = 45, b= 2, c= 3
a>b, a>c
45.2

, 45>3

1and1=1 (true)
-----------------------b>a, b>c
2>45, 2>3
0and0=0(false)
-------------------------c>a, c>b
3>45, 3>2
0and1=0(false)
So the number entered 45 is Greater of all

Q2:
Algorithm:
Requirements:
Number +ve or ve .
Scope:
Find the factorial of given number.
Input:
1) Get a number n from the user.
Steps:
1) Take the number n from the user.
2) Multiply number n with its smaller number which is a (where a=n-1). So
m=n*a

REF # L1F12BSCS2063

Section: b

NAME: Zulqurnain jutt

3) And next is to multiply m with number b (where b=a-1). And answer is


again assigned to m , and m will be multiplied to (a-1) value this process will
carry on until a-1 =1 .
4) Then the last value of m will is the called the factorial of Value n.
5) When a-1 = 1 then display message that factorial of n is equals to m. ( !n =
m)
Output:
e.g.
n = 4, a = n-1 = 4-1 =3 , m = n*a = 4*3=12
b = a-1= 3-1= 2, m = m*b =12*2 = 24
c = a-1 = 2-1 =1, a -1=1 (satisfied)
Factorial of 4 is 24.

Q3:
Algorithm:
Requirements:
Natural number 1 -20
Even numbers are multiple of 2.
+ve numbers
Scope:
Find sum of even numbers b/w 1-20
Inputs:
Take 1 20 numbers
Steps:
1) Take variable a equals 2
2) Take variable b equals 0
3) Now make a loop of which (count = count + a) and at the end of loop put a =
a+2.
4) Condition of the loop will be if a>22 iteration should stops.
5) Now count variable will work as adder of even numbers, and a will be used
as an iteration handler.
6) When a>22 the iteration stops and display Sum of 1-20 even numbers are
count.

REF # L1F12BSCS2063

Section: b

NAME: Zulqurnain jutt

Output:
Condition: a >22 iteration stops.
a = 2, count =count + a = 0+2= 2, a = a+2 = 2+2 = 4
a = 4, count = count +a = 2+2 =4, a = a+2 = 4+2 =6
.
.
a =18, count = count +a = 72 + 18 = 90, a =a+2 =18+2 =20
a = 20, count =count +a = 90 + 20 = 120, a = a+2 = 20+2 =22
a =22, (condition satisfied) iteration stops.

Sum of 1-20 even numbers are 120

Ref# L1F12BSCS2063
Name: Zulqurnain jutt

You might also like