Member Avatar for Zaina jee

what is an algorithm for a program that reads 4 integers (not in array) and print them without using loop

Recommended Answers

All 5 Replies

Well, you could use if-else if-else statements, but it will be a very long & messy code because of the permutation of the value in each variable.

It depends on your definition of loop. For example, if you create a detached recordset and read the integer values into it, you could then use the Sort method to rearrange the values. Your program would use no loops (as long as you brute-forced the input), however, the Sort method would undoubtedly have a loop hidden inside. Same thing with using QuickSort.

Although, strictly speaking, your problem definition does not mention sorting (only the thread title does). Your actual problem definition says you only have to read and write the numbers.

Psuedo codeish:

Get four numbers: a, b, c, d
if a is greater than b, swap a and b
if b is greater than c, swap b and c
if c is greater than d, swap c and d
if a is greater than b, swap a and b
if b is greater than c, swap b and c
if a is greater than b, swap a and b

Not very complicated.

Member Avatar for Zaina jee

Is it possible to do it without swapping numbers? Because our teacher has asked us to do without it.

UNLESS u use the switch case contruct
if a>b
display a
else dsplay b

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.