Programming Assignment Unit 7
Bachelor of Computer Science
University of the People
CS 2205-01: Web Programming 1
Dr. Crystal Cummings
December 31, 2024
In this exercise, I used online PHP editor found in w3school.
1. Program to add all the integers between 0 and 50.
<?php
$total = 0; // Initialize total variable
// Use a for loop to add integers from 0 to 50
for ($i = 0; $i <= 50; $i++) {
$total += $i; // Add current integer to total
// Display the total
echo "The total of integers between 0 and 50 is: " . $total;
?>
Output
1. Program to check if a person is eligible to vote
<?php
$age = 23; // Example age, you can change this value for testing
// Check if the person is eligible to vote
if ($age >= 18) {
echo "You are eligible to vote.";
} else {
echo "You are not eligible to vote.";
?>
Output
2. Program to check a number is prime or not.
<?php
$number = 39; // Example number, you can change this value for testing
$isPrime = true; // Assume the number is prime
// Check if the number is less than 2
if ($number < 2) {
$isPrime = false;
} else {
// Check for factors from 2 to the square root of the number
for ($i = 2; $i <= sqrt($number); $i++) {
if ($number % $i == 0) {
$isPrime = false; // Found a factor, so it's not prime
break;
// Display whether the number is prime or not
if ($isPrime) {
echo "$number is a prime number.";
} else {
echo "$number is not a prime number.";
?>
Output
Reference:
[Link]