I was asked to write program that checks numbers inputed by a user. It basically checks whether the number inputed is a perfect number, or a prime or palindromic. i have done the coding how ever when i run the code i always get zero for the value of the inputed number whcih was supposed to be checked.. can anyone show me where i went wrong and what to do cause im really stuck :/ :/
#include <iostream>
usingnamespace std;
void prime ()
{
int sum =0 , num;
if (num % 2 == 0)
cout << num << "is a prime number" <<endl;
else
cout << num << "is not a prime number" <<endl;
}
void palindromic ()
{
int a=0,reverse=0,num;
for(int i=1;num!=0;i++)
{
a=num%10;
num=num/10;
reverse=num+(reverse*10);
}
if(reverse==a)
{
cout<<a<<" is a Palindrome Number";
}
else
{
cout<<a<<"is NOT a Palindrome Number";
}
}
void perfect ()
{
int sum =0, num;
for (int i = 1; i <= num/2; ++i)
{
if (num % i == 0)
sum +=i;
}
if(sum==num)
cout << num << "is a perfect number" << endl;
else
cout << num << "is not a perfect number" <<endl;
}
int main(int argc, constchar * argv[])
{
int num, choose;
cout << "***This program checks if a postivie integer is***"<< endl;
cout << "***a prime number, a palindromic number and a perfect number***"<<endl;
cout <<"Press any key to start the program..." << endl;
cin.ignore();
//cin.get();
cout << "Please enter a positive interger to start the checking" << endl;
cin >> num;
if (num <= 0)
{
cout << "The input is not positive, please enter a positive integer: " <<endl;
cin >> num;
}
else
{
cout << "Checking option:" <<endl;
cout << "1. Is it a prime number?" <<endl;
cout << "2. Is it a palindromic number?" <<endl;
cout << "3. Is it a perfect number?" << endl;
cin >>choose;
switch(choose)
{
case 1:
prime ();
break;
case 2:
palindromic ();
break;
case 3:
perfect ();
break;
default: cout << "You have entered an invalid selection." <<endl;
}
}
}
Line 65: num is a local variable. You don't pass it as an argument to any of your three functions? How are those functions supposed to lnow the value?
Line 9,20,42: num is again a local variable. These are different variables from the one declared at line 65. The local num is uninitialized? What to you think its value is? Hint: It's garbage. All of your calculations depending on num are going to be Garbage In. Garbage Out.