Code Assignments
Question 1: Even or Odd Checker
Pseudo Code:
1. Start
2. Declare an integer variable `num`.
3. Ask the user to input an integer.
4. Check if the number is divisible by 2:
- If true, the number is even.
- Else, the number is odd.
5. Display whether the number is even or odd.
6. End
Program:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
if (num % 2 == 0)
cout << num << " is even." << endl;
else
cout << num << " is odd." << endl;
return 0;
}
Output:
Enter an integer: 5
5 is odd.
Question 2: Circle Properties
Pseudo Code:
1. Start
2. Declare constant `PI = 3.14159` and an integer `radius`.
3. Ask the user to input the radius of the circle.
4. Calculate:
- Diameter = 2 * radius
- Circumference = 2 * PI * radius
- Area = PI * radius * radius
5. Display the diameter, circumference, and area.
6. End
Program:
#include <iostream>
using namespace std;
int main() {
const float PI = 3.14159;
int radius;
cout << "Enter the radius: ";
cin >> radius;
cout << "Diameter: " << 2 * radius << endl;
cout << "Circumference: " << 2 * PI * radius << endl;
cout << "Area: " << PI * radius * radius << endl;
return 0;
}
Output:
Enter the radius: 7
Diameter: 14
Circumference: 43.9823
Area: 153.93791
Question 3: Celsius to Fahrenheit Conversion
Pseudo Code:
1. Start
2. Declare variables for Celsius and Fahrenheit temperatures.
3. Ask the user to input the temperature in Celsius.
4. Convert the Celsius temperature to Fahrenheit using the formula:
Fahrenheit = (9 / 5) * Celsius + 32
5. Display the temperature in Fahrenheit.
6. End
Program:
#include <iostream>
using namespace std;
int main() {
float celsius, fahrenheit;
cout << "Enter temperature in Celsius: ";
cin >> celsius;
fahrenheit = (9.0 / 5.0) * celsius + 32;
cout << "Temperature in Fahrenheit: " << fahrenheit << endl;
return 0;
}
Output:
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77