hello,

I just wanted your input on this program(can it be more efficent?...and so on ). It's a simple program. It reads 5 integers and checks if its an palindrom or not. As you can see I am a beginner.

here is my code:

#include<iostream>
//#include<string>
//#include<ctime>
//#include<cstdlib>
//#include<
using namespace std;

int main()
{
	int arry[5];//holds 5 integers;
	int checker(0);//checkes if the list os an palindrom;

	for( int i=0; i<5; i++)
	{
		cout<<"Enter a number ";
		cin>>arry[i];
	}
					
	for(int i=0,j=4;((i>=0 && i<2) && (j<=4 && j>2 ));i++,j--)
		
	{
		if(i!=j)
		{
			if(arry[i]==arry[j])
			checker++;
			
		}
	}

	if(checker==2)
				cout<<"Your number is an palindrome"<<endl;

	else cout<<"Your number is not a palindrome"<<endl;
			
	return 0;
}

The test portion of your comparison for loop can be simplified a lot. It need not test that i >= 0, you just set it to 0. You don't need to test the j index at all. When either of the indexes has reached the midpoint, stop. You also don't need the test for i != j inside the loop body, it won't ever happen if your loop control is correct.
So, your for loop becomes

for(int i=0, j=4;  i<2; i++, j--)
{
  //do the comparison
}
//or
int i = 0, j = 4;
while( i < j )
{
  //do the comparison
  i++;
  j--;
}

I have four concerns with your code, in decreasing order of importance.

1) Your input requires five integers separated by spaces. What do you expect to happen if the user enters 26 27 88 72 62? Better to load a single integer (if you're happy to be limited to only checking values that can be stored in an int) or read a string of digits without spaces and then see if you have a palindrome by using string operations.

2) The for loop construct

for(int i=0,j=4;((i>=0 && i<2) && (j<=4 && j>2 ));i++,j--)

can be simplifed considerably. With i initialised to zero, and only ever incremented, it will always be >= 0 ... no need to check that. Similarly, with j initialised to 4 and only ever decremented, you're guaranteed that it will always be <= 4.

3) If you need the value of i before incrementing to be available, then use the i++ operation. However, if you don't need to access the original value, use ++i instead. Similar comment for pre and post decrement. Your code is using post-increment and post-decrement unnecessarily. [Technically, a high-quality optimising compiler be unfussed by the difference, but there is no point in code that relies on side-effects being optimised away - and some coding standards explicitly disallow such things, as it interferes with code verification activities].

4) It is no need to have comments that state the obvious. A declaration "int arry[5];" does not need a comment that solemnly informs us that it holds 5 integers.

Well, it's too narrow and a little obscure code ;)
I agree with previous posts.
>can it be more efficent?
What's your effectiveness criteria?

bool Palindrome(const char* p)
{
    if (!p) return false;
    if (!*p)return true;
    const char* pend = p;
    while (pend[1])
        ++pend;
    while (p < pend && *p == *pend)
        ++p,--pend;
    return p >= pend;
}
inline
bool Palindrome(const std::string& s) {
    return Palindrome(s.c_str());
}
// another name because of (0) arg...
bool isPalindrome(unsigned long n)
{
    std::string s;
    while (n) {
        s += "0123456789"[n%10];
        n /= 10;
    }
    return Palindrome(s);
}

int main()
{
    std::string line;
    std::cout << "Welcome to Palindromania(TM)\n\n";
    while (std::cout << "Type a line (empty to quit): ",
        getline(std::cin,line) && line.size()) 
    {
        std::cout << "It's "
        << (Palindrome(line)?"":"NOT ")
        << "a palindrome\n";
        line.clear();
    }
    return 0;
}

Well, it's too narrow and a little obscure code ;)
I agree with previous posts.
>can it be more efficent?
What's your effectiveness criteria?

Don't confuse efficiency with effectiveness. They are different beasts, who often do not serve the same master

Don't confuse efficiency with effectiveness. They are different beasts, who often do not serve the same master

Not if efficiency is defined as competence in achieving the intended effect. This is a definition that may be found in english language dictionaries. ;)

But, seriously, ArkM is right: increasing efficiency (or increasing anything for that matter) implies having a quantitative measure that depends on your objectives.

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.