checkGreatestFactor
Error Debugging
checkGreatestFactor
Problem Statement
You are required to fix all logical errors in the given Code. You can Compile & Run to check the compilation/execution status of the program. You can use printf to debug your code. The submitted code should be logically/syntactically correct and pass all testcases. Do not write the main() function as it is not required.
Code Approach
For this question, you will need to correct the given implementation. We do not expect you to modify the approach or incorporate any additional library methods
The function checkGreatestFactor(int num) accepts an integer num as an input and is supposed to return the highest factor that is less than num.
It uses another function calculateFactor(int inputNumber) for calculating the factors of a number.
The function checkGreatestFactor looks fine but gives a compilation error.
Your task is to fix the program so that it passes all the test cases.
Test Cases
Test Case 1
Input
234
Expected Return Value
Test Case 2
Input
89
Expected Return Value
1
Error Debugging
int calculateFactor(int inputNumber);int checkGreatestFactor(num){if(num==0)return 0;elsereturn (calculateFactor(num));}int calculateFactor(int inputNumber){int i=0;maxFactor=0;for(i=0,i<=inputNumber/2,i++){if(inputNumber%i==0)maxFactor=i;}return maxFactor;}
Solution
int calculateFactor(int inputNumber);int checkGreatestFactor(int num){if(num==0)return 0;elsereturn (calculateFactor(num));}int calculateFactor(int inputNumber){int i=0,maxFactor=0;for(i=1;i<=inputNumber/2;i++){if(inputNumber%i==0)maxFactor=i;}return maxFactor;}//Published by PKJCODERS
Comments