Create a program that inputs two integers and outputs the larger

I wrote this code and it only outputs the larger integer if the larger integer is the first number. Otherwise it will say press any key to continue.


#include <stdafx.h>
#include <iostream>
#include <conio.h>
#pragma warning(disable: 4996)
#include<string>
#include<stdlib.h>
#include<time.h>

using namespace std;


int main()
{
int num1, num2;
std::cout << "Enter first number:" << std::endl;
std::cin >> num1;
std::cout << "Enter second number:" << std::endl;
std::cin >> num2;

if (num1<num2)
{
cout << "Larger Integer:" << num2 << endl;
}
else
{
cout << "Larger Integer:" << num1 << endl;
}
system("pause");
return 0;
Please Always use Code Tags (the <> button in the format menu on the right).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdafx.h>
#include <iostream>
#include <conio.h>
#pragma warning(disable: 4996)
#include<string>
#include<stdlib.h>
#include<time.h>

using namespace std;


int main()
{
int num1, num2; 
std::cout << "Enter first number:" << std::endl;
std::cin >> num1;
std::cout << "Enter second number:" << std::endl;
std::cin >> num2;

if (num1<num2)
{
cout << "Larger Integer:" << num2 << endl;
}
else
{
cout << "Larger Integer:" << num1 << endl;
}
system("pause");
return 0;

Add a close curly bracket at the end and try it. Also not sure why you are including so many files when you really only need iostream and stdlib.h.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include<stdlib.h>

using namespace std;


int main()
{
    int num1, num2;
    std::cout << "Enter first number:" << std::endl;
    std::cin >> num1;
    std::cout << "Enter second number:" << std::endl;
    std::cin >> num2;
    
    if (num1<num2)
    {
        cout << "Larger Integer:" << num2 << endl;
    }
    else
    {
        cout << "Larger Integer:" << num1 << endl;
    }
return 0;
system("pause");
}
Last edited on
feeks wrote:
I wrote this code and it only outputs the larger integer if the larger integer is the first number. Otherwise it will say press any key to continue.


I believe that you mean that if the second number is greater than first then it should just display "Press any key to continue"

so here's my solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include<stdlib.h>

using namespace std;


int main()
{
    int num1, num2;
    std::cout << "Enter first number:" << std::endl;
    std::cin >> num1;
    std::cout << "Enter second number:" << std::endl;
    std::cin >> num2;
    
    if (num1>num2)
    {
        cout << "Larger Integer:" << num2 << endl;
    }
    else
    {
        cout << "Press any key to continue"<< endl;
    }
return 0;
system("pause");
}




PS: I agree with McNo, you don't need that many headers
Thanks! THe headers are just for my teacher. He said he needed them for compatability!
I want it to output the larger of the two integers entered but it only does that if the first number is larger than the second. So this is the outcome when the second number is higher.
"Enter first number:
1
Enter second number:
2
Press any key to continue . . ."

What I want it to say is
"Enter first number:
1
Enter second number:
2
Larger Integer:
2 "
The sample you provide contradicts what you are saying.
1. If num1 is larger, does the program output num1? (yes/no)
2. If num1 is larger, does the program output "Press any key to continue..."? (yes/no)
3. If num2 is larger, does the program output num2? (yes/no)
4. If num2 is larger, does the program output "Press any key to continue..."? (yes/no)
Topic archived. No new replies allowed.