0% found this document useful (0 votes)
4 views9 pages

Cs 2205 Programming Assignment Unit 7

The document outlines programming assignments for a Web Programming course, specifically focusing on PHP. It includes tasks such as writing a program to sum integers from 0 to 50, checking voter eligibility based on age, and determining if a number is prime. Each task is accompanied by example code snippets demonstrating the required functionality.

Uploaded by

kuyembehj05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Cs 2205 Programming Assignment Unit 7

The document outlines programming assignments for a Web Programming course, specifically focusing on PHP. It includes tasks such as writing a program to sum integers from 0 to 50, checking voter eligibility based on age, and determining if a number is prime. Each task is accompanied by example code snippets demonstrating the required functionality.

Uploaded by

kuyembehj05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Cs 2205 programming assignment unit 7

Web Programming 1 (proctored course) (University of the


People)

Scan to open on Studocu

Downloaded by Simeon Dilac Kuyembeh


Studocu is not sponsored or endorsed by any college or university

Downloaded by Simeon Dilac Kuyembeh


1. Write a program in PHP using a ‘for’ loop to add all the integers between 0 and 50 and
display the total.

Solution:

/*

Program 1: Write a program in PHP using a ‘for’ loop to add all the integers between 0 and 50 and
display the total.

*/

<?php // starting php here

$y = 0; //variable y is used to hold the previous number and then add the current number to y to
update it.

For ($x = 0; $x <= 50; $x++) { // for loop to count the numbers from 0 to 50

//echo “x number is: $x and y number is: $y \n”; //to check the values

$y+=$x; // or you can simply use $y = $y + $x; // add the new number x to update the previous number y

//echo “The Total number is: $y \n”; // to check the total

Echo “Total number between 0 and 50 is: $y. \n”;

// end php ?>

Screenshot:

Downloaded by Simeon Dilac Kuyembeh


Downloaded by Simeon Dilac Kuyembeh
2. Write a PHP program to check if a person is eligible to vote using a control structure.
(Criteria: Minimum age required to vote is 18)

Downloaded by Simeon Dilac Kuyembeh


3 : Write a PHP program to check whether a number is prime or not.*/

<?php // start php

Function check_num($numChk) //function to check prime or not using an argument

{ If ($numChk == 1) // check if the number is 1 then not prime

Return 0;

For ($i = 2; $i <= $numChk / 2; $i++) { // for loop to check if number is

divisible If ($numChk % $i == 0) return 0;

Return 1; // means it’s a prime

$numPut = readline(“Enter the number: “); // taking input from user //

Using a flag variable to check if prime or not by calling check_num$flag = check_num($numPut)

;if ($flag == 1)

Echo “Yes, a prime number”;

Else

Echo “No, not a prime number”;

//end php

Downloaded by Simeon Dilac Kuyembeh


Downloaded by Simeon Dilac Kuyembeh
Downloaded by Simeon Dilac Kuyembeh
Downloaded by Simeon Dilac Kuyembeh

You might also like