Variables & Data Types
Master the fundamentals with 10 hands-on exercises
Age Calculator
Problem:
Write a pseudocode algorithm that asks the user for their birth year and calculates their current age.
Requirements:
- Prompt the user to enter their birth year
- Calculate age using current year (2024)
- Display the calculated age
Example Output:
Solution
Algorithm AgeCalculator
var currentYear = 2024
var birthYear = Input "Enter your birth year:"
var age = currentYear - birthYear
Print "Your age is:", age, "years old."
Endalgorithm
- Variables store data
- Input keyword gets user data
- Arithmetic operators perform calculations
- Print keyword displays output
Rectangle Area
Problem:
Write a pseudocode algorithm to calculate the area of a rectangle given its length and width.
Requirements:
- Prompt the user to enter the length
- Prompt the user to enter the width
- Calculate the area (length × width)
- Display the calculated area
Example Output:
Solution
Algorithm RectangleAreaCalculator
var length = Input "Enter length:"
var width = Input "Enter width:"
var area = length * width
Print "The area of the rectangle is:", area
Endalgorithm
- Variables store numerical values
- Multiplication operator (*) for area calculation
- Input and Output operations are fundamental
String Concatenation
Problem:
Ask the user for their first name and last name, then combine them to display their full name.
Requirements:
- Get first name from user
- Get last name from user
- Combine them with a space in between
- Display the full name
Example Output:
Solution
Algorithm FullNameGenerator
var firstName = Input "Enter first name:"
var lastName = Input "Enter last name:"
var fullName = firstName + " " + lastName
Print "Your full name is:", fullName
Endalgorithm
- String variables store text
- The + operator can concatenate (join) strings
- Literal strings (like " ") are used for fixed text
Temperature Converter
Problem:
Convert a temperature from Celsius to Fahrenheit using the formula: F = C × 9/5 + 32
Example Output:
Solution
Algorithm CelsiusToFahrenheit
var celsius = Input "Enter temperature in Celsius:"
var fahrenheit = (celsius * 9 / 5) + 32
Print celsius, "Celsius is", fahrenheit, "Fahrenheit"
Endalgorithm
Shopping Bill Calculator
Problem:
Calculate the total bill for two items, including a 10% sales tax.
Example Output:
Solution
Algorithm ShoppingBill
var price1 = Input "Enter price of item 1:"
var price2 = Input "Enter price of item 2:"
var subtotal = price1 + price2
var taxAmount = subtotal * 0.10
var totalBill = subtotal + taxAmount
Print "Subtotal:", subtotal
Print "Tax (10%):", taxAmount
Print "Total Bill:", totalBill
Endalgorithm
Time Converter
Problem:
Convert a given number of days into years, weeks, and remaining days (365 days/year, 7 days/week).
Example Output:
Solution
Algorithm TimeBreakdown
var totalDays = Input "Enter total days:"
var years = totalDays / 365
var remainingDays = totalDays mod 365
var weeks = remainingDays / 7
var finalDays = remainingDays mod 7
Print totalDays, "days is:"
Print years, "Year"
Print weeks, "Weeks"
Print finalDays, "Days"
Endalgorithm
- Integer division (/) for whole units
- Modulo operator (mod) for remainders
- Sequential calculations
BMI Calculator
Problem:
Calculate the Body Mass Index (BMI) given weight in kilograms and height in meters. Formula: BMI = weight / (height × height)
Example Output:
Solution
Algorithm BMICalculator
var weightKg = Input "Enter weight in kg:"
var heightMeters = Input "Enter height in meters:"
var bmi = weightKg / (heightMeters * heightMeters)
Print "Your BMI is:", bmi
Endalgorithm
- Exponentiation (squaring) using multiplication
- Float variables for precise calculations
- Order of operations with parentheses
Circle Properties
Problem:
Calculate both area and circumference of a circle given its radius (π = 3.14159).
Example Output:
Solution
Algorithm CircleProperties
var pi = 3.14159
var radius = Input "Enter radius:"
var area = pi * radius * radius
var circumference = 2 * pi * radius
Print "Circle Area:", area
Print "Circle Circumference:", circumference
Endalgorithm
- Constants (like π) stored in variables
- Mathematical formulas in pseudocode
- Squaring by multiplying number by itself
Currency Exchange
Problem:
Convert USD to EUR, GBP, and JPY with proper formatting (1 USD = 0.85 EUR, 0.73 GBP, 110.50 JPY).
Example Output:
Solution
Algorithm CurrencyExchange
var usdToEur = 0.85
var usdToGbp = 0.73
var usdToJpy = 110.50
var usdAmount = Input "Enter amount in USD:"
var eurAmount = usdAmount * usdToEur
var gbpAmount = usdAmount * usdToGbp
var jpyAmount = usdAmount * usdToJpy
Print "$", usdAmount, "USD equals:"
Print "€", eurAmount, "EUR"
Print "£", gbpAmount, "GBP"
Print "¥", jpyAmount, "JPY"
Endalgorithm
- Multiple related constants
- One input for multiple calculations
- Currency symbols for user-friendly output
Student Grade Average
Problem:
Calculate a student's final grade average from 5 test scores and determine pass/fail status (average >= 60).
Example Output:
Solution
Algorithm StudentGradeAverage
var test1 = Input "Enter test 1 score:"
var test2 = Input "Enter test 2 score:"
var test3 = Input "Enter test 3 score:"
var test4 = Input "Enter test 4 score:"
var test5 = Input "Enter test 5 score:"
var total = test1 + test2 + test3 + test4 + test5
var average = total / 5
var status = "Fail"
If average >= 60 Then
status = "Pass"
Endif
Print "---Student Report---"
Print "Test Scores:", test1, ",", test2, ",", test3, ",", test4, ",", test5
Print "Total:", total
Print "Average:", average
Print "Status:", status
Endalgorithm
- Multiple related variables (test1, test2, etc.)
- Accumulating values for totals
- Average calculation: sum ÷ count
- Comparison operators (>=) for conditions