C++ Programming: Please Help! Assume you are a programmer in a company X. You ar
ID: 643407 • Letter: C
Question
C++ Programming:
Please Help!
Assume you are a programmer in a company X. You are asked by the company to write a function that computes how many digits with values less than or equal to 5 and how many digits with values greater than 5 are there in a given positive integer. Your function MUST take an integer parameter of long type and return the number of digits that have values equal or less than 5 and the number of digits that have values greater than 5. You also need to write a main program to test your function.
You MUST NOT define any global variable.
An example for running the program is as below:
Enter an integer number: 362408547
Number of digits between 0 and 5 is: 6
Number of digits between 6 and 9 is: 3
Note: The maximum value for an integer of type long is (2,147,483,647). Your number should be less than the maximum value for C++ to be able to handle it. If you would like to test with a bigger number, you can define the number of long long type. It can hold a number up to a maximum value of (9,223,372,036,854,775,807).
Explanation / Answer
include <iostream>
using namespace std;
void fun(long n)
{ long i;
long x=n;
int count=0,count1=0;
while(x>0)
{
i=x%10;
x=x/10;
if(i>0 && i<=5)
{count=count+1;
}
if(i>=6 && i<=9)
{
count1=count1+1;
}
};
cout<< "less than 6 ";
cout<<count;
cout<<endl;
cout<<"greater than ";
cout<<count1;
cout<<endl;
}
int main(void) {
fun(123456);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.