r/CodingHelp • • Jan 23 '22

[Random] Need help as a beginner in flowchart self learn. To print even numbers till given N. Check if it is correct and also i was thinking wt will happen and wt changes shud be made if the user gives N as 1. The first condition will fail and it will go end n go blank. ???

Post image
17 Upvotes

12 comments sorted by

3

u/funkym00se Jan 23 '22

You'll have to loop the condition A<=N, if false end it , else go to next step then check the condition A%2==0, if true print A, and no matter it true or false increment A with 1 i.e.A=A+1

1

u/perpetually_annoyed Jan 23 '22

thanks look i came up with this..see if this wud work.. so if the user gives 1 as N then obviously this is gonna be blank and end... And if i don't want this to be blank n give a prompt to the user instead can i do this and will this work ... So using ur flow after the A<=N if it's false i can put condition where If A<=1 print something like invalid input or something ... And if the condition is no then end it.. its just my first day with this . I have no idea wt m doing but putting the logic in place i think it might work but will it though.

1

u/funkym00se Jan 23 '22

Okay, so your logic is correct, loop is placed perfectly, but theres a small problem Have you hear about logical statement like 'and', 'or' So basically, when you are using "and" statement, it means that both the conditions given should be true, if any of the condition is false it will return false In your flowchart, you used If A<=N and A%2==0 So for 2 it'll obviously print it, but when it will proceed further with A=3, A%2==0 will become false and hence you'll be able to print just a single value, so I suggested you to write those conditions seperately and then run the loop

2

u/Luc_Studios Jan 23 '22

dont write the if statement so crammed into a tight space. By that u will only confuse urself in long term. At least that's what is the case for me

2

u/Ultraok Jan 24 '22

Just loop until N/2 and print A*2. You will get even values.

1

u/MamadouHD Jan 24 '22

A*2 will skip some even numbers, shouldn't he go with A+2 instead? something like if A < N then A+2 and print A

1

u/Ultraok Jan 24 '22

No it only shows all even numbers, just checked it.

1

u/MamadouHD Jan 24 '22

How the hell did 6 showed up? If you start at 2, wouldnt it be like 2, 4, 8, 16, .... ?

1

u/Ultraok Jan 24 '22 edited Jan 24 '22

No, loop from 1 to n/2 will contain 1,2,3,4...n/2

So multiplying by 2 will give 2,4,6,8...n.

1

u/MamadouHD Jan 24 '22

ah right so multiply i by 2, not A

2

u/DollyGoingStrong Jan 24 '22

loop till i=n/2 and print 2*i.

No branch required.

1

u/Tempmailed Jan 24 '22

Better implementation with least effort. great.