Hi, I am beginner in programing of C++. I have a trouble with my code. I recived
ID: 3903986 • Letter: H
Question
Hi, I am beginner in programing of C++. I have a trouble with my code. I recived many error messeages that say "the definiton of local function is not correct ". (also I recived other error messages) and I am feeling it hard to complete only by myself .
The purpose of this program is that it repeatedly reads a natural number (n>= 1) from keyboard (if the input number is <=0, then it is terminated ),
and the following menu is displayed.
Could you correct my source code for me ? Probably, my code is messy, because I have been confusedwith functions of program.
What I want to do in this program is that reads a number and then display about characters of the number. for example, if an user put 8 and then press 5, the program must responce "the number is even " to the user.
#include
using namespace std;
bool isPrime(unsigned int);
void displayMultiple(unsigned int, unsigned int );
void naturalFactors(unsigned int);
void additiveReverse(unsigned int);
bool evenOdd(unsigned int);
void reverse(unsigned int);
int main()
{
unsigned int number;
cout << "This prgoram repeatedly reads a natural number : ";
cin >> number;
while (number >= 1)
{
cout << "Press 1 to check if th input number is prime. ";
cout << "Press 2 to display all the natural multiples of the input number in range r. ";
cout << "Press 3 to display all the natural factors of the inpu number. ";
cout << "Press 4 to display the addictive revers of the number. ";
cout << "Press 5 to check if the number is even or odd. ";
cout << "Press 6 to see the summation of digits. ";
cout << "Press 7 to see the reverse of the number. ";
int choice;
cin >> choice;
switch (choice) {
case 1: {
bool result = isPrime(number);
if (result == true)
cout << "this number is prime ";
else
cout << "this number is not prime ";
}
break;
case 2: {
unsigned int range;
cin >> range;
displayMultiple(number, range);
}
break;
case 3: {
naturalFactors(number);
}
break;
case 4: {
additiveReverse(number);
}
break;
case 5: {
bool result = displayMultipe(number);
if (result == true)
cout << "this number is even";
else
cout << "this number is odd ";
}
break;
case 6: {
summation(number);
}
break;
case 7: {
reverse(number);
}
break;
default: cout << " You did not entered in the range of 1 to 8";
break;
}
system("pause");
return 0;
}
}
// No1: check whether the natural number is prime .
bool isPrime (unsigned int number )
{
int count = 0;
for (unsigned int a = 1; a <= number; a++)
{
if (number % a == 0)
{
count++;
}
}
if (count == 2)
{
return true;
}
else
{
return false;
}
}
void displayMultiple(unsigned int, unsigned int)
{
}
// No2: display all the natural multiples of the inpit number in rnge r
void displayMultipe (unsigned int number, unsigned int r)
{
int i = 1;
unsigned int multiple = number;
while (multiple <= r)
{
unsigned int multipes = i * number;
cout << multiple;
i++;
}
}
// No.3 display all the natural factors of the imput number
void naturalFactors(unsigned int number)
{
unsigned int i = 1;
while (number >= i)
{
if (number % i == 0)
cout << i << endl;
i++;
}
}
// No.4 display the additive reverse of the number
void additiveReverse(unsigned int number)
{
int multiple = -1 * number;
cout << multiple << endl;
}
// No.5 to check if the number is even or odd
bool evenOdd(unsigned int number)
{
if ( number % 2 == 0)
{
return true;
}
else
{
return false;
}
}
// No.6 to see the sumation ofdigits
void summation (unsigned int number)
{
int sum = 0;
while (number != 0)
{
int remainder = number % 10;
sum = sum + remainder;
number = number / 10;
}
cout << sum;
}
// No.7 to display the reverse of the number
void reverse(unsigned int number)
{
int number, reversedNumber = 0, remainder;
cout << "Enter an integer: ";
cin >> number;
while (number != 0)
{
remainder = number % 10;
reversedNumber = reversedNumber * 10 + remainder;
number /= 10;
}
cout << "Reversed Number = " << reversedNumber;
}
Explanation / Answer
#include <iostream>
using namespace std;
bool isPrime(unsigned int);
void displayMultiple(unsigned int, unsigned int);
void naturalFactors(unsigned int);
void additiveReverse(unsigned int);
bool evenOdd(unsigned int);
void reverse(unsigned int);
void summation(unsigned int);
int main() {
unsigned int number;
while (true)
{
cout <<endl << endl<< "This prgoram repeatedly reads a natural number : ";
cin >> number;
cout << endl << endl << "Press 1 to check if th input number is prime. ";
cout << "Press 2 to display all the natural multiples of the input "
"number in range r. ";
cout << "Press 3 to display all the natural factors of the inpu "
"number. ";
cout << "Press 4 to display the addictive revers of the number. ";
cout << "Press 5 to check if the number is even or odd. ";
cout << "Press 6 to see the summation of digits. ";
cout << "Press 7 to see the reverse of the number. ";
int choice;
cout << endl << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1: {
bool result = isPrime(number);
if (result == true)
cout << "this number is prime ";
else
cout << "this number is not prime ";
break;
}
case 2: {
unsigned int range;
cout << "Enter range: ";
cin >> range;
displayMultiple(number, range);
break;
}
case 3: {
naturalFactors(number);
break;
}
case 4: {
additiveReverse(number);
break;
}
case 5: {
bool result = evenOdd(number);
if (result == true)
cout << "this number is even";
else
cout << "this number is odd ";
break;
}
case 6: {
summation(number);
break;
}
case 7: {
reverse(number);
break;
}
default:
cout << " You did not entered in the range of 1 to 8";
}
}
system("pause");
return 0;
}
// No1: check whether the natural number is prime .
bool isPrime(unsigned int number)
{
int count = 0;
for (unsigned int a = 1; a <= number; a++)
{
if (number % a == 0)
{
count++;
}
}
if (count == 2)
{
return true;
}
else
{
return false;
}
}
// No2: display all the natural multiples of the inpit number in rnge r
void displayMultiple(unsigned int number, unsigned int r)
{
int i = 1;
while (i <= r)
{
if(number % i == 0)
cout << i << " ";
i++;
}
cout << endl;
}
// No.3 display all the natural factors of the imput number
void naturalFactors(unsigned int number)
{
unsigned int i = 1;
while (number >= i)
{
if (number % i == 0)
cout << i << endl;
i++;
}
}
// No.4 display the additive reverse of the number
void additiveReverse(unsigned int number)
{
int multiple = -1 * number;
cout << multiple << endl;
}
// No.5 to check if the number is even or odd
bool evenOdd(unsigned int number)
{
if (number % 2 == 0)
{
return true;
}
else
{
return false;
}
}
// No.6 to see the sumation ofdigits
void summation(unsigned int number)
{
int sum = 0;
while (number != 0)
{
int remainder = number % 10;
sum = sum + remainder;
number = number / 10;
}
cout << sum;
}
// No.7 to display the reverse of the number
void reverse(unsigned int number)
{
int reversedNumber = 0, remainder;
while (number != 0)
{
remainder = number % 10;
reversedNumber = reversedNumber * 10 + remainder;
number /= 10;
}
cout << "Reversed Number = " << reversedNumber;
}
/*SAMPLE OUTPUT
This prgoram repeatedly reads a natural number : 1234
Press 1 to check if th input number is prime.
Press 2 to display all the natural multiples of the input number in range r.
Press 3 to display all the natural factors of the inpu number.
Press 4 to display the addictive revers of the number.
Press 5 to check if the number is even or odd.
Press 6 to see the summation of digits.
Press 7 to see the reverse of the number.
Enter choice: 7
Reversed Number = 4321
This prgoram repeatedly reads a natural number : 1234
Press 1 to check if th input number is prime.
Press 2 to display all the natural multiples of the input number in range r.
Press 3 to display all the natural factors of the inpu number.
Press 4 to display the addictive revers of the number.
Press 5 to check if the number is even or odd.
Press 6 to see the summation of digits.
Press 7 to see the reverse of the number.
Enter choice: 1
this number is not prime
This prgoram repeatedly reads a natural number : 1234
Press 1 to check if th input number is prime.
Press 2 to display all the natural multiples of the input number in range r.
Press 3 to display all the natural factors of the inpu number.
Press 4 to display the addictive revers of the number.
Press 5 to check if the number is even or odd.
Press 6 to see the summation of digits.
Press 7 to see the reverse of the number.
Enter choice: 6
10
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.