0% found this document useful (0 votes)
6 views

COMPUTER PROGRAMMING

The document contains a series of C++ programming questions and their corresponding code snippets and outputs. Each question involves basic programming concepts such as conditional statements, user input, and mathematical calculations. The outputs vary based on user inputs, demonstrating different programming scenarios and logic.
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)
6 views

COMPUTER PROGRAMMING

The document contains a series of C++ programming questions and their corresponding code snippets and outputs. Each question involves basic programming concepts such as conditional statements, user input, and mathematical calculations. The outputs vary based on user inputs, demonstrating different programming scenarios and logic.
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/ 11

COMPUTER PROGRAMMING 1

Question 1 CODE OUTPUT:


#include <iostream>
Using namespace std; If number = 4: Output → Even Number
If number = 7: Output → Odd Number
Int main() { If number = -2: Output → Even Number
Int number; If number = 0: Output → Even Number

// Prompt the user for input


Cout << “Enter a number: “;
Cin >> number;

// Check if the number is even or odd


If (number % 2 == 0) {
Cout << “Even Number” << endl;
} else {
Cout << “Odd Number” << endl;
}

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;

cout << "Enter a string: ";


cin >> input; // Takes a single word as
input

int length = input.length();

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.

Cin >> balance; 15% if the balance is 10000 or more.


Finally, it outputs the new balance.

// Calculate the new balance based


on the interest rate
If (balance < 5000) {
newBalance = balance + balance *
0.09; // 9% interest
} else if (balance < 10000) {
newBalance = balance + balance *
0.12; // 12% interest
} else {
newBalance = balance + balance *
0.15; // 15% interest
}

// Output the new balance


Cout << “Your new balance after one
year is: “ << newBalance << endl;

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:

weight = length * 3.5 - 108; // Weight = length * 3.5 – 108,


Female formula
Where length is the height in inches.
} The formula multiplies the height by 3.5
and subtracts 108 to estimate the
perfect weight for a female.
// Output the weight
Output:
cout << "Perfect weight: " << weight
<< " pounds" << endl;
The program then outputs the calculated
perfect weight in pounds, based on the
return 0; input height and gender.

}
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

// Input the person’s age


Cout << “Enter your age: “;
Cin >> age;

// Check and display the appropriate


message
If (age <= 0)
Cout << “Wrong age” << endl;
Else if (age < 8)
Cout << “Child” << endl;
Else if (age < 18)
Cout << “Boy” << endl;
Else if (age < 35)
Cout << “Young” << endl;
Else if (age < 65)
Cout << “Old” << endl;
Else
Cout << “Very Old” << endl;

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;

// Compute z based on the given


conditions
if (k == 1) {
z = 2 * k; }
else if (k == 2) {
z = 2 * k * k * k - 3; // 2k³ - 3 }
else if (k == 3) {
z = k - 2; }
else if (k < 1 || k > 3) {
z = k; }

// Print the result


cout << "The value of z is: " << z <<
endl;

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).

If (a < b && a < 10) {


T = (a * a) + (a * b);
} else if (a == b || b > 10) {
T = (a * a * b) – (2 * a) + (5 * b);
}

Cout << “The value of T is: “ << T <<


endl;

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;

Conditions for calculating f(x):


// Input the value of x
If x < 2, the program calculates f(x) = 3 –
Cout << “Enter the value of x: “;
x.
Cin >> x;
If x == 2, the program outputs f(x) = 2.
If x > 2, the program calculates f(x) = x /
// Calculate f(x) based on the 2 (integer division).
conditions
File:
If (x < 2) {
Cout << “f(x) = “ << 3 – x << endl;
} else if (x == 2) {
Cout << “f(x) = 2” << endl;
} else {
Cout << “f(x) = “ << x / 2 << endl;
}

Return 0;
}

You might also like