Thread: Number swap C++

  1. #1
    Registered User GrafixFairy's Avatar
    Join Date
    Mar 2014
    Posts
    45

    Number swap C++

    I received help here from experts nice enough to help me out so i am posting all my assignments.. Maybe someone will find the useful and leave from them.

    Basic, for beginners.

    question: Develop a program that prompts the user to enter two (2) numbers. The program will display the numbers based on these conditions:
    a. if the first number is less than the second number the program will perform a swap operation on these two numbers and display them, otherwise

    b. the program will display the numbers as entered.

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main(){
        
        //Declaration of variables
        int number1 = 0, number2 = 0, temp = 0;;
        
        cout <<"Enter Two Numbers: ";
        cin >> number1 >> number2;
        
        if(number1 < number2){ /*Conditional statement to check if 
                               the first number is smaller than the second number*/
            
            /*Swap operations*/
            temp = number1;
            number1 = number2;
            number2 = temp;                                    
                   
        }//end if
        
       
            /*Output statements for the first and second number*/ 
           cout <<"The first number is " <<number1 <<endl;
           cout <<"The second number is " <<number2 <<endl;    
    
        
      system("pause");  
      return 0;  
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You should check out std::swap.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User GrafixFairy's Avatar
    Join Date
    Mar 2014
    Posts
    45
    I was not allowed to use it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 02-21-2014, 02:10 PM
  2. swap
    By indrajit_muk in forum C Programming
    Replies: 4
    Last Post: 12-22-2009, 05:25 AM
  3. off by one and help with swap
    By jrb47 in forum C++ Programming
    Replies: 1
    Last Post: 11-11-2006, 07:45 PM
  4. swap()? (g++)
    By jafet in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2006, 04:59 PM
  5. Swap a bit
    By mr_nice! in forum C Programming
    Replies: 7
    Last Post: 03-01-2004, 03:15 AM