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 

117

 Test Case 2 

 Input 

89

 Expected Return Value 

1

 Error Debugging 

int calculateFactor(int inputNumber);
int checkGreatestFactor(num)
{
if(num==0)
return 0;
else
return (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;
else
return (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

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Maximum Goods Transported

Mango Distribution

Swap and Reverse the String

Electrostatic

RAT CHEESE MAZE

Commands