Sorting 3 Numbers and Even/Odd Questions

In summary: He will need to use the cin object and the >> operator to get the input, and he will need to use the cout object and the << operator to output the result. Also, he will need to declare three variables to store the input, and he will need to use a conditional statement (if/else) to compare the variables and output them in the correct order.In summary, the conversation is about a request for help in writing a program that takes three numbers as input and outputs them in ascending order using C++. The use of the cmath header and the modulo operator to determine even and odd numbers is also discussed. The conversation concludes with a solution provided by a forum member.
  • #1
new c++er
4
0
hello everyone
i want your expert and your help please
i have to write a program that takes three numbers as input and write them in ascending order.
Sample Program Run:
Enter three numbers: 4 8 3
Numbers in ascending order: 3 4 8
how can i do it ?!:cry:
and i want to ask you does the cmath knows the diffrence between even numbers and odd numbers ?!

please consider that am a very first level in c++ and i need your help :blushing::cry:


thanks all
 
Physics news on Phys.org
  • #2


new c++er said:
hello everyone
i want your expert and your help please
i have to write a program that takes three numbers as input and write them in ascending order.
Sample Program Run:
Enter three numbers: 4 8 3
Numbers in ascending order: 3 4 8
how can i do it ?!:cry:
and i want to ask you does the cmath knows the diffrence between even numbers and odd numbers ?!

please consider that am a very first level in c++ and i need your help :blushing::cry:


thanks all
That's concerns...

What have you tried to do? The rules of the forum say that you need to make an attempt at a solution before anyone can help you. See Homework Help here: https://www.physicsforums.com/showthread.php?t=5374.

What's "the cmath?" How would it help to know whether the numbers are even or odd? That's not anything that's relevant in this problem.
 
Last edited:
  • #3
i'll check the link now :) !
but abt the cmath its another question not realted to the first one :D!
 
  • #4
i haven't tried anything for honosty!
 
  • #5
cmath is the header math.h. You don't need any of the functions whose prototypes are in math.h if all you want to do is determine whether a number is even or odd. The modulo or remainder operator (%) is all you need. You don't need to include any headers to use it.

This operator takes two operands, and returns the remainder when the first operand is divided by the second.
For example 5 % 2 == 1, and 8 % 2 == 0.
 
  • #6
wt abt the acending thingy?!
 
  • #7
Take a stab at it and then show me what you've tried.
 
  • #8
#!perl
print "Enter three numbers: ";
print "Numbers in ascending order: ".join(" ",sort {$a <=> $b} (split /\D+/,<>))."\n";

DaveE
 
  • #9
davee123 said:
#!perl
print "Enter three numbers: ";
print "Numbers in ascending order: ".join(" ",sort {$a <=> $b} (split /\D+/,<>))."\n";

DaveE
That's not going to be much help to the OP since he will be writing C++ code.
 

FAQ: Sorting 3 Numbers and Even/Odd Questions

How do you sort 3 numbers in ascending order?

To sort 3 numbers in ascending order, you need to compare each number to the other two numbers and place them in order from smallest to largest. For example, if the numbers are 5, 2, and 9, you would first compare 5 and 2 and swap them if necessary. Then, you would compare 5 and 9 and swap them if necessary. This would result in the numbers being in the order of 2, 5, and 9.

How do you determine if a number is even or odd?

To determine if a number is even or odd, you can divide the number by 2. If the remainder is 0, then the number is even. If the remainder is 1, then the number is odd.

How do you sort even and odd numbers separately?

To sort even and odd numbers separately, you can create two separate lists or arrays - one for even numbers and one for odd numbers. Then, you can loop through the original list of numbers and add each number to the appropriate list based on whether it is even or odd.

Can you use a sorting algorithm to sort even and odd numbers?

Yes, you can use a sorting algorithm to sort even and odd numbers by modifying the algorithm to only compare and swap even or odd numbers. For example, in a bubble sort, you would only compare and swap even numbers or only compare and swap odd numbers depending on which numbers you want to sort first.

What is the most efficient way to sort 3 numbers and determine if they are even or odd?

The most efficient way to sort 3 numbers and determine if they are even or odd is to use a conditional statement to compare each number to the other two numbers and place them in order. Then, you can use the remainder operator to determine if a number is even or odd. This method does not require any additional data structures and can be done in a single pass through the numbers.

Similar threads

Replies
1
Views
1K
Replies
10
Views
2K
Replies
7
Views
2K
Replies
2
Views
2K
Replies
4
Views
1K
Replies
4
Views
8K
Replies
12
Views
1K
Replies
8
Views
2K
Back
Top