Site hosted by Angelfire.com: Build your free website today!

                                                     

 

 

Using functions:

The program will ask you for input of a 4-digit number.

Your program will run some tests on this number.

1)      Find the sum of the digits………………1234 results in 10

2)      Reverse the number……………………………1234 becomes 4321

3)      Count its factors………………………………………..4

4)      Print factor list……………………………………………...{1, 2, 617, 1234}

5)      Determine prime or composite……composite

6)      Determine if it is a palindrome……1234<>4321 (no)

7)      Quit

 

·         Your program will have a function for each of the 6 operations. You will also have a MENU function.

·         You will only have two variables in your program’s execution block: choice and number.

·         All other variables will be local to the function.

·         Have results printed out in the particular function doing that one operation.

Your program will start like this.

cout<<”enter a 4-digit number”;

cin>>number;

 do(

                                    menu();

                                      cin>>choice;

                                      switch(choice)

                                        {

                                                case 1: sum(number);

                                                            break;

                                                case 2: reverse(number);

                                                            break;

and so on…….

                                    }while(choice!=7);

 

the functions need a proto-type. This goes before void main( )

            void sum(int);

            void reverse(int);

 

Hints: Sum of digits. You need four variables. One each for the thousands, hundreds, tens, and units.

thous = number / 1000;

hun = (number – (thous * 1000)) / 100 and so on

 

Reverse. Use the logic of the sum function but now create a new number

                        reverse = units*1000+tens*100+…………….

Factors.

            Counting………..Have a loop check for divisibility counting as it goes

                        for (x = 1; x<=number; ++x)

                           {

                              if (number % x = = 0) c++;

            Printing factors……Just use the previous line but have a cout instead

                        Prime or composite: if (count = = 2) cout<<”prime”;

 else cout<<”composite”;