COMPUTER PROGRAMMING
COMPUTER PROGRAMMING
Return 0;
}
Question 2 CODE OUTPUT:
If input = “hello”: Output → Less than 6
characters
#include <iostream>
If input = “goodbye”: Output → Greater
using namespace std;
than 6 characters
If input = “school”: Output → Equal to 6
int main() { characters
string input;
if (length > 6) {
cout << "Greater than 6 characters"
<< endl;
} else if (length < 6) {
cout << "Less than 6 characters"
<< endl;
} else {
cout << "Equal to 6 characters" <<
endl;
}
return 0;
}
Question 3 CODE OUTPUT:
#include <iostream>
Using namespace std; X > 0 and Y < 0:
W=X+Y
Int main() { X == 0 and Y > 0:
Double X, Y, W; W = (X * X) * (Y * Y * Y) + 5 * X
(Simplified, since X == 0, this results in
W = 0)
// Input values for X and Y
X < 0 and Y == 0:
Cout << “Enter values for X and Y: “;
W = (2 * X) / 3 + 4 * X
Cin >> X >> Y;
If none of the above conditions match:
If (X > 0 && Y < 0) {
Output → “No condition matched
W = X + Y;
}
Else if (X == 0 && Y > 0) {
W = (X * X) * (Y * Y * Y) + 5 * X;
}
Else if (X < 0 && Y == 0) {
W = (2 * X) / 3 + 4 * X;
}
Else {
Cout << “No condition matched.”
<< endl;
Return 0;
}
Cout << “The value of W is: “ << W
<< endl;
Return 0; }
Question 4 CODE }
#include <iostream>
Using namespace std; Return 0;
Int main() { }
Double a, b, c, discriminant;
Cout << “Enter values for a, b, and c:
“;
Cin >> a >> b >> c;
OUTPUT:
If (a == 0) {
Cout << “Divided by zero” << endl;
Input: a = 1, b = -3, c = 2
} else {
Output: Real roots, Root 1: 2.0, Root 2:
Discriminant = b * b – 4 * a * c; 1.0
If (discriminant < 0) {
Cout << “No real roots” << endl; Input: a = 1, b = 2, c = 1
} else if (discriminant == 0) { Output: Equal roots, Root: -1.0
Cout << “Equal roots” << endl;
Double root = -b / (2 * a); Input: a = 1, b = 1, c = 1
Cout << “Root: “ << root << endl; Output: No real roots
} else {
Cout << “Real roots” << endl; Input: a = 0, b = 2, c = 3
Double root1 = (-b + Output: Divided by zero
discriminant) / (2 * a);
Double root2 = (-b –
discriminant) / (2 * a);
Cout << “Root 1: “ << root1 <<
endl;
Cout << “Root 2: “ << root2 <<
endl;
}
Question 5 CODE OUTPUT:
#include <iostream>
Using namespace std; It prompts the user to enter their current
balance.
Int main() {
Based on the balance, it calculates a
Double balance, newBalance;
new balance by adding interest:
9% if the balance is less than 5000.
// Input the balance
12% if the balance is between 5000
Cout << “Enter your balance: “; (inclusive) and less than 10000.
Return 0;
}
Question 6 CODE OUTPUT:
#include <iostream>
using namespace std; Inputs:
int main() {
char gender; The program first asks for the user’s
gender (either ‘M’ for male or ‘F’ for
double length, weight;
female). It accepts both uppercase and
// Input gender and height lowercase inputs.
cout << "Enter your gender (M/F): "; Then, it prompts the user to input their
height in inches.
cin >> gender;
Weight Calculation:
cout << "Enter your height in inches:
";
cin >> length; If the gender is male (‘M’ or ‘m’), the
program uses the formula:
Weight = length * 4 – 125,
// Calculate weight based on gender
Where length is the height in inches.
if (gender == 'M' || gender == 'm') {
The formula multiplies the height by 4
weight = length * 4 - 125; // Male and subtracts 125 to estimate the
formula perfect weight for a male.
} else if (gender == 'F' || gender == 'f') If the gender is female (‘F’ or ‘f’), the
{ program uses the formula:
}
Question 7 CODE OUTPUT:
#include <iostream>
Using namespace std; The output of this program will depend
on the value entered for age. The
Int main() {
program categorizes the entered age
Int age; into different groups and prints a
corresponding message
Return 0;
}
Question 8 CODE OUTPUT:
#include <iostream> If k = 1: z = 2k (approximated as 2 * 1 =
2).
using namespace std;
If k = 2: z = 2k³ - 3 (e.g., 2 * 2 * 2 * 2 – 3
int main() {
= 13).
int k;
If k = 3: z = k – 2 (e.g., 3 – 2 = 1).
int z;
If k < 1 or k > 3: z = k.
// Input value of k
cout << "Enter the value of k: ";
cin >> k;
return 0;
}
Question 9 CODE OUTPUT:
#include <iostream>
Using namespace std; The output of the program depends on
the value of t entered by the user and
Int main() {
the choice of the equation
Int choice;
Double t, x;
Cout << “Enter the value of t: “;
Cin >> t;
Cout << “Choose the equation (1-4):“;
Cin >> choice;
If (choice == 1) {
X = t + t;
} else if (choice == 2) {
X = t * t * t + 2 * t;
} else if (choice == 3) {
X = t * t + t * t;
} else if (choice == 4) {
X = 0.25 * t * t * t * t + t * t;
} else {
Cout << “Invalid choice!” << endl;
Return 0;
}
Cout << “The value of x is: “ << x <<
endl;
Return 0; }
Question 10 CODE OUTPUT:
#include <iostream>
Using namespace std; The output of this program depends on
the value of x that the user inputs. The
program calculates and prints the value
Int main() { of f(x) based on the following conditions:
Double a, b, T;
If x < 2, the program calculates f(x) = 3 –
x.
If x == 2, the program outputs f(x) = 2.
Cout << “Enter values for a and b: “;
If x > 2, the program calculates f(x) = x /
Cin >> a >> b;
2 (integer division).
Return 0;
}
Question 11 CODE OUTPUT:
#include <iostream>
Using namespace std; The output of this program depends on
the value of x that the user inputs. The
program calculates and displays the
Int main() { value of f(x) based on different
conditions:
Int x;
Return 0;
}