Program to check numbers inputed

Write your question here.

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 :/ :/
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  
#include <iostream>

using namespace 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, const char * 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.


Topic archived. No new replies allowed.