Create a c++ program that accepts 5 integers in ascending order without using array or looping. Pure else and if statements.

Dani AI

Generated

You can meet the brief exactly — no arrays, no loops — by reading five separate integers and using only if/else checks. As pointed out, the forum should not do homework for someone else; 's hardcoded example illustrates why a clear spec matters. The example below uses real input and only if/else logic so it fits the stated constraints and is practical to run.

#include <iostream>
using namespace std;

int main() {
    int a, b, c, d, e;
    cout << "Enter 5 integers in ascending order:\n";
    if (cin >> a >> b >> c >> d >> e) {
        if (a <= b && b <= c && c <= d && d <= e) {
            cout << "Accepted: " << a << " " << b << " " << c << " " << d << " " << e << "\n";
        } else {
            cout << "Rejected: values are not in ascending order.\n";
        }
    } else {
        cout << "Invalid input.\n";
    }
    return 0;
}

Notes:

  • <= implements non-decreasing (duplicates allowed). Use < between each pair for strictly increasing (no duplicates).
  • cin >> a >> b >> c >> d >> e accepts whitespace-separated integers (spaces or newlines). It fails if a non-integer is entered or fewer than five values are provided; the example checks that.
  • To fail fast while still avoiding loops/arrays, read one value at a time and chain if checks after each read (reject immediately on the first violation). If the real task is to sort five numbers without loops/arrays, use a fixed sequence of pairwise compare-and-swap statements (a small sorting network) implemented with if and temporary swaps.

Recommended Answers

All 4 Replies

I just need it badly. I am going to fail my subject if i could not pass it until tommorow morning. Please help

Please help. Is there anyone here that can program that one? Please answer me. Thank you

No one is going to do your homework for you. If you hadn't waited to the last minute you could have posted your code and asked for help.

Using strictly your requirement this appears to be a 2 line exercise. Nowhere did you write the user input the values with cin or much else. You wrote the program accepts the 5 integers. This is a good example where the specification means a lot more than some folk think it does. The spec you supplied did not demand user input. So this is pretty easy and I worry that you need to spend time on your class work.

#include <iostream>

using namespace std;

int main()
{
  int a=1,b=2,c=3,d=4,e=5;
  cout << a << b << c << d << e << endl; 
  return 0;
}
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.