شيئية OOP
282 subscribers
53 photos
10 files
12 links
عمي رحمة لوالديك ولعشيرتك وللعزاز وللعراق وللشرق الأوسط، لا تكعد تحفظ الأكواد
الكود فهم فهم فهم مو حفظ
و السلام
Download Telegram
Q14: Write a C++ program to determine if a given number is positive, negative, or zero. Use appropriate conditional statements.

#include <iostream> 
using namespace std;

int main()
{
int num;
cout<<" enter the number ";
cin>>num;
if ( num > 0 )
{
cout<<" the number is positive "<<endl;
}
else if ( num < 0)
{
cout<<" the number is negative "<<endl;
}

else
{
cout<<" the number is zero "<<endl;
}
return 0;
}
👍1
Q15: Develop a simple C++ calculator program with a menu that performs addition, subtraction, multiplication, and division based on the user's choice.

#include <iostream> 
using namespace std;

int main()
{
float num1, num2, result;
char op;
cout<<" enter the number1 : ";
cin>>num1;
cout<<" enter operator (+, -, *, /) : ";
cin>>op;
cout<<" enter the number2 : ";
cin>>num2;
switch (op)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if ( num2 != 0)
{
result = num1 / num2;
}
else
{
cout<<" error "<<endl;
return 1;
}
break;
default:
cout<<" error "<<endl;
return 1;
}

cout<<result<<endl;

return 0;
}
👍1
Q16: Create a C++ program that takes a character as input and determines whether it is a vowel, consonant.

#include <iostream> 
using namespace std;

int main()
{
char character;

cout << "Enter a character: ";
cin >> character;

if ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z'))
{
switch (character)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
cout << character << " is a vowel." << endl;
break;
default:
cout << character << " is a consonant." << endl;
}
}
else
{
cout << "Invalid input. Please enter a valid alphabet character." << endl;
}

return 0;
}
👍1
Q17: Write a C++ program that classifies a person into different age groups: child (0-12), teenager (13-19), adult (20-59), or senior (60 and above).

#include <iostream>
using namespace std;
int main()
{
int age;
cout<<" enter your age : ";
cin>>age;
if( age <= 12 )
{
cout<<"your a child"<<endl;
}
else if ( age <=19 )
{
cout<<"your a teenager"<<endl;
}
else if ( age <= 59 )
{
cout<<"your a adult"<<endl;
}
else if ( age >= 60 )
{
cout<<"your a senior"<<endl;
}
return 0;
}
👍1
Q18: Implement a C++ program that converts temperatures between Celsius and Fahrenheit. Ask the user for the temperature and the unit, then perform the conversion based on the input. Hint:
(Temperature in degrees Fahrenheit (°F) = (Temperature in degrees Celsius (°C) * 9/5) + 32)

#include <iostream>
using namespace std;
int main() {
float c, f;
cout << " value ";
cin >> c;
f = ( c * 9/5 ) + 32;
cout << " in F the temp is :" << f <<endl;
return 0;
}
👍1
Q19: Create a C++ program that takes two integers as input and checks whether the first number is divisible by the second one. Provide appropriate messages based on the result.

#include <iostream> 
using namespace std;

int main()
{
int num1, num2;

cout << "Enter the first number: ";
cin >> num1;

cout << "Enter the second number: ";
cin >> num2;

if (num2 != 0)
{
if (num1 % num2 == 0)
{
cout << num1 << " is divisible by " << num2 << endl;
}
else
{
cout << num1 << " is not divisible by " << num2 << endl;
}
}
else
{
cout << "Error: Division by zero is not allowed." << endl;
}

return 0;
}
👍1
Q20: Write a C++ program that takes three numbers as input and determines the largest among them using conditional statements

#include <iostream> 
using namespace std;

int main() {
double num1, num2, num3;

cout << "Enter num1 : ";
cin >> num1;

cout << "Enter num2 : ";
cin >> num2;

cout << "Enter num3 : ";
cin >> num3;

if (num1 >= num2 && num1 >= num3) {
cout << "The largest number is : " << num1 << endl;
}
else if (num2 >= num1 && num2 >= num3)
{
cout << "The largest number is : " << num2 << endl;
}
else
{
cout << "The largest number is : " << num3 << endl;
}

return 0;
}
👍2
Q21: Create a C++ program that takes three angles as input and classifies the triangle based on the angle values (acute, obtuse, or right-angled). determines the largest among them using conditional statements

#include <iostream> 
using namespace std;

int main()
{
int angle1, angle2, angle3;

cout << "Enter the first angle: ";
cin >> angle1;

cout << "Enter the second angle: ";
cin >> angle2;

cout << "Enter the third angle: ";
cin >> angle3;

if (angle1 + angle2 + angle3 == 180)
{
if (angle1 < 90 && angle2 < 90 && angle3 < 90)
{
cout << "The triangle is acute-angled." << endl;
}
else if (angle1 == 90 || angle2 == 90 || angle3 == 90)
{
cout << "The triangle is right-angled." << endl;
}
else
{
cout << "The triangle is obtuse-angled." << endl;
}
}
else
{
cout << "Invalid input: The angles do not form a triangle." << endl;
}

return 0;
}
👍1
Q22: Write a C++ program that calculates the total cost after applying a discount based on the purchase amount. For example, provide a 10% discount if the purchase amount is above $100.

#include <iostream> 
using namespace std;

int main()
{
int purchaseamount, discount, totalcost;
cout<<" enter the purchaseamount: ";
cin>>purchaseamount;
if ( purchaseamount > 100)
{
discount = 0.10 * purchaseamount;
}
else
{
discount = 0;
}
totalcost = purchaseamount - discount;

cout<< "Discount applied: $"<<discount<<endl;
cout<< "Total cost after discount: $"<<totalcost<<endl;

return 0;
}
👍1
Q23: C++ code to calculate the factorial of any entered number.

#include <iostream> 
using namespace std;

int main()
{
int a , fac;
cout << "enter the number (a) : ";
cin >> a;
fac = 1;
for ( int i =1; i <= a ; i++)
{
fac = fac * i;
}
cout << "the fac of the entered number is : "<<fac<<endl;

return 0;
}
👍1
Q24: C++ code to calculate the power of any entered number (the base and exponent should be entered by user).

#include <iostream> 
using namespace std;

int main()
{
float base, exponent, result;

cout << "Enter the base: ";
cin >> base;

cout << "Enter the exponent: ";
cin >> exponent;

result = 1;

for(int i=0;i<exponent;i++)
{
result*=base;
}

cout << base << " raised to the power of " << exponent << " is: " << result << endl;

return 0;
}
👍1
Q25: Write a program that calculates and prints the sum of the even integers from 2 to user defined limit. Use a do-while loop that asks the user whether the program should be terminated or not.
#include <iostream>
using namespace std;
int main()
{
char input;
do {
int num, sum_even = 0;
cout << "enter the number : ";
cin >> num;

for (int b = 2; b <= num; b += 2)
{
sum_even += b;
}
cout << " sum of even numbers from 2 to "<<num<<" is :"<< sum_even<< endl;
cout << " Do you want to terminate the program or not? ( y/n):" ;
cin >> input;
}
while (input != 'y');

return 0;
}
1👍1
Q26: Write a C++ program to print the sequence of numbers (101, 111, 112 … 130), except number 107 and 125.

#include <iostream> 
using namespace std;
int main()
{
for ( int i = 101; i <= 130; i++ )
{
if ( i!=107 && i!=125)
cout << i << endl;
}
return 0;
}
2👍1
Q27: Write a program that calculates and prints the product of odd integers from 1 to user defined limit. Use a do-while loop that asks the user whether the program should be terminated or not.

#include <iostream>
using namespace std;
int main()
{
char input;
do {
int num, product_odd = 1;
cout << "enter the value of m : ";
cin >> num;
for (int b = 1; b <= num; b += 2)
{
product_odd *= b;
}
cout << " product of odd numbers from 1 to"<<num<<":"<< product_odd<< endl;
cout << " Do you want to terminate the program or not? ( y/n):" ;
cin >> input;
}
while (input != 'y');
return 0;
}
👍1
Q28: Write a program which takes PIN number of the user as input and then verifies his pin. If pin is verified the program shall display “pin verified” and “Welcome”; otherwise, the program shall give user another chance. After 4 wrong attempts, the program shall display “Limit expired” and then exit.
Use for loops to implement the logic.
Note: 5 random PIN numbers can be assumed and fed into the program with which input pin is matched.

#include <iostream> 
using namespace std;

int main()
{
const int MAX_ATTEMPTS = 4;
int pinAttempts = 0;
int enteredPIN;
int validPINs[] = {1234, 5678, 9876, 5432, 1111};

for (int i = 0; i < MAX_ATTEMPTS; ++i)
{
cout << "Enter your PIN: ";
cin >> enteredPIN;

bool pinVerified = false;

for (int j = 0; j < 5; ++j)
{
if (enteredPIN == validPINs[j])
{
pinVerified = true;
break;
}
}

if (pinVerified)
{
cout << "PIN verified. Welcome!" << endl;
break;
} else
{
cout << "Incorrect PIN. Please try again." << endl;
++pinAttempts;

if (pinAttempts == MAX_ATTEMPTS)
{
cout << "Limit expired. Exiting program." << endl;
break;
}
}
}

return 0;
}
Q28: Write a program which takes PIN number of the user as input and then verifies his pin.
If pin is verified the program shall display “pin verified” and “Welcome”; otherwise, the program shall give user another chance. After 4 wrong attempts, the program shall display “Limit expired” and then exit.
Use for loops to implement the logic.
Note: 5 random PIN numbers can be assumed and fed into the program with which input pin is matched.

#include <iostream>
using namespace std;

int main() {
// Assume 5 random PIN numbers
int pinNumbers[5] = {1234, 5678, 9876, 4321, 7890};
int userPIN;
int attempts = 0;

// Loop for maximum 4 attempts
for (int i = 0; i < 4; ++i) {
cout << "Enter your PIN number: ";
cin >> userPIN;

// Check if the entered PIN matches any of the stored PINs
bool pinVerified = false;
for (int j = 0; j < 5; ++j) {
if (userPIN == pinNumbers[j]) {
pinVerified = true;
break;
}
}

if (pinVerified) {
cout << "PIN verified" << endl;
cout << "Welcome" << endl;
break;
} else {
cout << "Invalid PIN. Please try again." << endl;
attempts++;
}
}

// If the user exceeds 4 attempts
if (attempts == 4) {
cout << "Limit expired" << endl;
}

return 0;
}
Q29: Write a C++ program to calculate the summation of the following sequence for nth terms.
𝑆𝑢𝑚 = 1/1 + 2/4 + 3/9 + ....

#include <iostream> 
using namespace std;

int main()
{
int n;
double sum = 0;
cout<<" enter the value of n : ";
cin>>n;
for( int i = 1; i <= n; i++)
{
sum += float (i) / (i*i);
}
cout<<" the summation the following sequence for "<<n<<" is : "<<sum<<endl;
return 0;
}
👍1
Q30: C++ code to find the summation of the following series.
Sum = a + a^2 + a^3 + ... + a^n

#include <iostream>
using namespace std;

int main() {
int a, n;
int sum = 0;

cout << "Enter the value of 'a': ";
cin >> a;

cout << "Enter the value of 'n': ";
cin >> n;

// Calculating the summation of the series
for (int i = 1; i <= n; ++i) {
int term = 1;
for (int j = 1; j <= i; ++j) {
term *= a;
}
sum += term;
}
cout << "The summation of the series is: " << sum << endl;

return 0;
}
1
Q31: C++ code to find the summation of the following series for nth terms.
𝑆𝑢𝑚 = 1/a^1 + 2/a^2 + 3/a^3 + ....

#include <iostream>
using namespace std;

int main() {
int a, n;
float sum = 0.0;
float power_of_a = 1.0;

cout << "Enter the value of 'a': ";
cin >> a;

cout << "Enter the value of 'n': ";
cin >> n;

// Calculating the summation of the series
for (int i = 1; i <= n; ++i) {
power_of_a *= a; // Update power_of_a by multiplying it by a
sum += float(i) / power_of_a;
}

cout << "The summation of the series up to the " << n << "th term is: " << sum << endl;

return 0;
}
1
Q32: C++ code to find the summation of the following series.
Sum = 1 + 2! + 3! + ⋯ + n!

#include <iostream>
using namespace std;

int main() {
int n;
int sum = 0;
int factorial = 1;

cout << "Enter the value of 'n': ";
cin >> n;

for (int i = 1; i <= n; ++i) {
factorial *= i;
sum += factorial;
}

cout << "The summation of the series is: " << sum << endl;

return 0;
}
1👍1