Program in PHP to find the sum of first 20 even number

  1. <?php
  2. //FrequenTech program in PHP to find the sum of first 20 even numbers
  3. //Declaration of class
  4. class AddEvenNumber
  5. {
  6. //Declaring three required private INT variables
  7. private $i = 0;
  8. private $j = 0;
  9. private $addVar = 0;
  10. //public function to perform operation
  11. public function evenNum(){
  12. while ($this->j<20){
  13. if($this->i%2 == 0){
  14. $this->addVar+= $this->i;
  15. $this->j++;
  16. }
  17. $this->i++;
  18. }
  19. return $this->addVar;
  20. }
  21. }
  22. $addResult = new AddEvenNumber();
  23. $result = $addResult->evenNum();
  24. echo "Addition of first 20 Even Number is: ".$result;
  25. OUTPUT:
  26. Addition of first 20 Even Number is: 380

Grover Harleen

Grover Harleen

Harleen Grover is running a growing web and mobile development company. He always focuses to learn something new and grow and share his knowledge with others same time. you can send him your questions also related to programming and Technology and He will always try to provide the Best outputs to you.

One thought on “Program in PHP to find the sum of first 20 even number

  1. Generally I do not learn post on blogs, but I would like to say that this write-up very forced me to take a look at and do it! Your writing style has been amazed me. Thanks, quite great post. Dorothee Mano Maxy

Leave a Reply

Your email address will not be published. Required fields are marked *