In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf())
For this exercise you should be able to write a logical expression (i.e., with logical operators) which checks if some integer x consists of exactly 5 digits. Ex: 30498 and -14004 are 5-digit numbers, while 1018, -2 and 46 are not.
Complete the intQ2(intQ2_input) function that takes an input integer parameter and returns 1 if the number is five digits and 0.
Answer;
Explanation:
Here is the function in Bold below which takes an integer input and then counts the number of digits present in that number.
If the digits are 5, it returns 1
Otherwise it returns 0.
Code:
#include <stdio.h>
int intQ2(int intQ2_input)
{
int digits = 0;
if(intQ2_input<0)
intQ2_input = -intQ2_input;
while(intQ2_input>0)
{
intQ2_input = intQ2_input/10;
digits = digits + 1;
}
if(digits==5)
return 1;
else
return 0;
}
int main()
{
printf("%d", intQ2(-14004));
return 0;
}
Output:
PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!
PLEASE COMMENT IF YOU NEED ANY HELP!
Get Answers For Free
Most questions answered within 1 hours.