cse1001: programming and problem solving homework sheet 1

21
CSE1001: Programming and Problem Solving Homework Sheet 1 Write the PAC, Pseudo code and draw the flowchart for the following problems: 1. Add two numbers entered by user. 2. Multiply two numbers entered by the user using loop. 3. Find the largest among three different numbers entered by user. 4. Find all the roots of a quadratic equation ax 2 +bx+c=0 5. Find the Fibonacci series till term 1000. 6. Find whether given number is Prime or not 7. Find the first 100 prime numbers. 8. Find whether given number is Armstrong number or not 9. Calculating factorial value of given number 10. Check if two given numbers are Coprime or not 11. Convert from cms to feet and inches.

Upload: others

Post on 18-Dec-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE1001: Programming and Problem Solving Homework Sheet 1

CSE1001: Programming and Problem Solving

Homework Sheet 1

Write the PAC, Pseudo code and draw the flowchart for the following problems:

1. Add two numbers entered by user. 2. Multiply two numbers entered by the user using loop. 3. Find the largest among three different numbers entered by user. 4. Find all the roots of a quadratic equation ax2+bx+c=0 5. Find the Fibonacci series till term ≤ 1000. 6. Find whether given number is Prime or not 7. Find the first 100 prime numbers. 8. Find whether given number is Armstrong number or not 9. Calculating factorial value of given number 10. Check if two given numbers are Coprime or not 11. Convert from cms to feet and inches.

Page 2: CSE1001: Programming and Problem Solving Homework Sheet 1

Add two numbers entered by user

Pseudocodeflowchart startummmmmmm

I Get two numbers

Input two Compute sumnumbers sum number l t

1 number 2

add two nom store sumbers display sum

1Displayresultsum

1Stop

PACmummum

Data Processing Output SolutionAlternatives

Numbers Add numi Display Assign nami

som num2 asnuml num2 and

constantsnum2 equate theobtained answer Assign mum

to a variable numz as

input valuessum

Page 3: CSE1001: Programming and Problem Solving Homework Sheet 1

2 Multiply two numbers entered by the user using

loop

Flowchartmmmmmmmm start

1input twonumbers num num2

1i I pom0

tis NO

arenum NO isnuml NO nomine 7 Dl num2 7 tire num2 mm2ve ve tve

k 5fuses YES

c

v

if ie NOnumz y Output STOP

SUM

YES

50Mt nvm l

Iite l

Page 4: CSE1001: Programming and Problem Solving Homework Sheet 1

B

tif iEnum NO Output STOP

sum

YES

Sumterrum2

tit l

C

u

if iEnumz Output stopsum

YES

Sumterruml

u

ite I

I

Page 5: CSE1001: Programming and Problem Solving Homework Sheet 1

D

tnk l numlm2 I num2

Iif ienz Output stop

sum

1Sunt n I

tit 1

PseudocodemmmmmmmmGet two numbers

Assign i I sum 0

Add either of thenumbers to sum and increment

Repeat until i is equal to other number

c SototionData Processing Output

MotiveAlternatives

Assign it sum 0 and check Declare numiTwonumbersif i less than or equal to Display mm2 as

M either of the numbers If gum constants

m2yes then add the other

Declarenumbnumz as

number and increment i input values

and repeat until i becomes simplyuse

equal to the number operator

Page 6: CSE1001: Programming and Problem Solving Homework Sheet 1

3 Find the largest among three numbers entered

by user

flowchart startmmmm

1Input a b c

v

a b NO b c No outputc

islargest

IES HESa c Output b u

is largest StopYESu u

output stopa islargest

1Stop

pseudocodemmmmmmmm

Get three numbersb c

Check if a b and a c

If true display a is largest

check if b a and b C

If true display b is largest

Else display c is largest

Page 7: CSE1001: Programming and Problem Solving Homework Sheet 1

PACmmmm

SolutionData Processing Output

Alternatives

Three Check which print largest Check if a ba c It truetheir

numbers number is numberprint a is largest

a b c largest similarity check forb and cDivide by 2continuously untilquotient reaches 1Track the number ofloops using a variable

E Assign degree

yt t ChummodDThe number withthe highest degreewill be the largestnumber

Page 8: CSE1001: Programming and Problem Solving Homework Sheet 1

Find all the roots of a quadratic equation

ax2tbxtCPseudocodelowchart start minnow

mmmmmmmm get a b cI

Inputa b c Compotebtb andza

b Eac and assign1Evaluate bt Eat Za

and

assign it Foa Evaluate it to N and K2

b F and assignit Display x and R2zato Xz

1Outputx K2

1Stop

PACmm Solution

Data Processing OutputAlternatives

Three Evaluate roots Display a xz a Evaluateusingnumbers of a formula

quadraticAbcc 24104 1equationaa2tbn aCeo 7422 Ea

calculate we can solve thesebtnbT.ae and two to get miszastorethe valuesin two variablesal plz

Page 9: CSE1001: Programming and Problem Solving Homework Sheet 1

S Find all the Fibonacci series till

term 1000

Pseudo CodeFlowchart Start mmtmmmm mmmmmm

Initialize i asI

Create an array ai 0 afAssign value 0

t 1 to 1st and 2ndACO 0 a 131

element of the

array respectivelyif i722 No

and afi Eiow agYY liftAssign a value to

YESevery element

ofaci aci Itai 2 agg suchthat

sum of1 it is the

the previoustwo

displayafielements

Increment i andit _I repeat antil a two

t

Page 10: CSE1001: Programming and Problem Solving Homework Sheet 1

PACmtg

Data Processing Output Alternativesolutions

Array af Assign a Display Fibonaccivalue to series with

a iTEioo0everyace terms untilsuch that 1000

af af Dtaci 2

untilUci C 1000

Page 11: CSE1001: Programming and Problem Solving Homework Sheet 1

6 Find whether the given number is prime or

not

flowchart pseudocodemmmm mummieswww start

Get number1 compute remainder

input nvm2 of number when it1 is divided by alli 2 the numbers less

than ThumI

NO If remainder Oit icsqn it is Stopnung prime after any divisionfYES then non prime

pr nvm1 If remainder 0ifor all divisions1

no then it is primeit Pr O

YESnotprime

1stop

Page 12: CSE1001: Programming and Problem Solving Homework Sheet 1

PACmmmm

Data Processing Output Solution Alternatives

Number Check if there Print prime 1 Divide nvm by numbersor

rum is a factor from 2 to inurn andnot prime if for all the divisionsof norm

the remainder is nonzero then nvm is

prime2 Instead of Z tonum are divide from2 to nm 12

3 Instead of tonom are divide from2 to 5mm

Page 13: CSE1001: Programming and Problem Solving Homework Sheet 1

7 Find the first 100 prime numbers

Pseudocodeflowchart unmanningstart Assign i l j 21mm

tUsing a loop

increment

print 2i till 100

t using another loopi l 5 2 nvm 3 check if anumberi'S

primeIif No Print all primeiCloo numbers

LYES STOP

TNO if cu JLnvmprintnvm LYES

it Noi it mum1 j 0

YES j jtlv

Num tt

v

j 2

Page 14: CSE1001: Programming and Problem Solving Homework Sheet 1

PACmmmm

Output SolutionData Processing Alternatives

we can initializeprint first first 100a variable n

100 prime primenumbers that would

numbers allow the user

to print howmany everprime numbershe she desires

To check if a

number is prime ornot we use the same

Ngo as we used

for

Page 15: CSE1001: Programming and Problem Solving Homework Sheet 1

g Find whether given number is an Armstrong

Flowchart start pseudicod.cmmum mummum

I Get num

assign tempenumsum 0Sum O

Compute Some Sum fryI toxinput n 1 103 4 10mum

fs

repeat until nvm zo

temp _nvm

ttemp0

NOcomputedigit_tempt10temp_templioSumtecdigitxidigitix

digit

hurry50mLNO

tyresprint numis print nvmnot an is an armstrongnoarmstrongno

Istop

Page 16: CSE1001: Programming and Problem Solving Homework Sheet 1

PACmmmm

SolutionData Processing Output

Alternatives

A number Check if num num is an Divide and assignhim is an Armstrong Armstrong no nom to numho

number or not whilst assigningCoD the remainder toi e check ifmum can be rum is not a variable digitexpressed as the an armstrong cube digit andcubes of its number add it to a

variable sum under

digits every iteration

If some neum

then it is an

Armstrong number

Take inputfrom user digitwise and thusobtain thedesirednumber bymultiplyingwith 10th

powers Cube

digits andcompare bothIf they areequal then itis an Armstrognumber

I

Page 17: CSE1001: Programming and Problem Solving Homework Sheet 1

9 Calculating factorial of a given number

Flowchartminnow start

tinput nvm

tface l

ti nvm NO pyjittaagtorial stop

YESfact i

it 1

Pseudocode Get nvmummmmmmmAssign i 1 fuel

compute fac fact i

increment i and repeat the loop until

i nvm

Display face

Page 18: CSE1001: Programming and Problem Solving Homework Sheet 1

PACmmmm

SolutionData Processing Output

Alternatives

A number compute The factorial Assign i I Sae l

num the of the number compute fac fac ifactorial of is face Increment i by la Repeat until inumber mum When i mum

exit from theloop and displayface

mm could be

assigned as a

constantmum could bean input value

Page 19: CSE1001: Programming and Problem Solving Homework Sheet 1

0 Check if two numbers are coprime or not

Flowchart Pg mdemummies mmmm

start get min

compote gcdcmmIinput in n assign it to a variabl

check

1 it check LYES print co prime

gcdca.by No

elsedisplaym n are u print not coprimecoprime displaymen are

not coprime

u

stop

PACmmmm

OutputSolution

Data Processing Alternatives

Two numbers Check it in n m n are Use godtunchFind factors

in n are coprime coprime to bothnumbers

using a looprn n are and store

not coprime the factorsin an arrayComparethe

arrays forboth numbers

Page 20: CSE1001: Programming and Problem Solving Homework Sheet 1

if there is acommon element

then the numbersare not coprime

Page 21: CSE1001: Programming and Problem Solving Homework Sheet 1

1 Convert from cms to feet and inches

PseudocodeFlowchart mnememmmmmmmm get length in Cms km

Assign variables Irn1ftstart

compute 1ft 1cm130.481

Compute Lin_Com130.4832input lengthin Cms output length is

htt ft

t and 1in inches

1ft 1cm130.48

Itin Clem1.30487 2.5

1output lengthis 1ft feet and1in

inches

1STOP

SolutionData Processing Output

Alternatives

Input length Convertoms Display length km can be a

to ft and in ft and fixed constantin Cms

inches inches km can be auser definedvalue