rnuzzo
10-10-2008, 19:27
Eccomi di nuovo per un vostro parere:) .Questo è un programma che verifica se un numero di 5 cifre sia palindromo o meno:#include<iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int a = 0;
int n1 = 0;
int n2 = 0;
int n3 = 0;
int n4 = 0;
int n5 = 0;
cout << "Inserire un numero a 5 cifre (-1 per uscire): ";
cin >> a;
while (a != -1)
{
if (a > 99999)
{
cout << "\nIL NUMERO SUPERA LE 5 CIFRE\n"<< endl;
}
else
{
n1 = ( a / 10000);
n2 = ( a % 10000) / 1000;
n3 = ( a % 1000) / 100;
n4 = ( a % 100) / 10;
n5 = ( a % 10);
}
if (n1 == n5)
{
if (n2 == n4)
cout << "\nIl numero e' palindromo\n" << endl;
else
cout << "\nIl numero non e' palindromo\n" << endl;
}
else
cout << "\nIl numero non e' palindromo\n" << endl;
cout << "Inserire un numero a 5 cifre (-1 per uscire): ";
cin >> a;
}
system("PAUSE");
return 0;
}
Usando solo gli if - if else c'e un modo per scrivere una sola volta "Il numero non è palindromo"?
Non mi dispiacerebbe nemmeno un giudizio sul listato...:rolleyes: .Grazie
using std::cout;
using std::cin;
using std::endl;
int main()
{
int a = 0;
int n1 = 0;
int n2 = 0;
int n3 = 0;
int n4 = 0;
int n5 = 0;
cout << "Inserire un numero a 5 cifre (-1 per uscire): ";
cin >> a;
while (a != -1)
{
if (a > 99999)
{
cout << "\nIL NUMERO SUPERA LE 5 CIFRE\n"<< endl;
}
else
{
n1 = ( a / 10000);
n2 = ( a % 10000) / 1000;
n3 = ( a % 1000) / 100;
n4 = ( a % 100) / 10;
n5 = ( a % 10);
}
if (n1 == n5)
{
if (n2 == n4)
cout << "\nIl numero e' palindromo\n" << endl;
else
cout << "\nIl numero non e' palindromo\n" << endl;
}
else
cout << "\nIl numero non e' palindromo\n" << endl;
cout << "Inserire un numero a 5 cifre (-1 per uscire): ";
cin >> a;
}
system("PAUSE");
return 0;
}
Usando solo gli if - if else c'e un modo per scrivere una sola volta "Il numero non è palindromo"?
Non mi dispiacerebbe nemmeno un giudizio sul listato...:rolleyes: .Grazie