This research aims to design a modified P algorithm for the efficient tracking of maximum power point (MPPT) for standalone and grid-connected systems. The proposed research work modifies the P algorithm for the dc-dc converter where the fixed step size P algorithm is translated into variable step size with the help of ant colony optimization (ACO) to generate optimal parameters for the PID controller to generate a variable step size in the P algorithm. This variable step size is dependent upon the error that is the difference between the generated power and desired power. By doing this it improves the efficiency of the P algorithm and its limitations are overcome. Furthermore, the PV is extended to connect with a grid where the inverter is controlled by a fuzzy logic controller (FLC) so that the combined structure of variable P and fuzzy helps to achieve MPP efficiently. The robustness of the proposed work is compared with other state-of-the-art controllers to justify the effectiveness of the proposed work. Finally, a stability test of the system is carried out to verify the overall stability of the power system.