شيئية OOP
282 subscribers
53 photos
10 files
12 links
عمي رحمة لوالديك ولعشيرتك وللعزاز وللعراق وللشرق الأوسط، لا تكعد تحفظ الأكواد
الكود فهم فهم فهم مو حفظ
و السلام
Download Telegram
Q33: Grades in the programming lab are classified according to letters A case of Excellent, B case of Well done, C case of done, D case of Pass and F case of Try again.
Write C++ code to implement this logic using Switch statement.

#include <iostream>
using namespace std;
int main ()
{
char grade ;
cout << " take one from this ( A , B, C, D, F) ";
cin >> grade;
switch(grade)
{
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
cout << "Well done" << endl;
break;
case 'C' :
cout << "done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
default :
cout << "Invalid grade" << endl;
}
cout << "Your grade is " << grade << endl;
return 0;
}
1
Q34: Create a C++ program that takes five integers as input and determines whether their product is positive or negative.

#include <iostream> 
using namespace std;

int main()
{
int num1, num2, num3, num4, num5;
int product;

cout << "Enter five integers: ";
cin >> num1 >> num2 >> num3 >> num4 >> num5;

product = num1 * num2 * num3 * num4 * num5;

if (product > 0)
{
cout << "The product is positive." << endl;
}
else if (product < 0)
{
cout << "The product is negative." << endl;
}
else
{
cout << "The product is zero." << endl;
}

return 0;
}
2
Q35: Write a C++ program that takes a day number (1-7) as input and prints the corresponding day of the week.

#include <iostream> 
using namespace std;

int main()
{
int daynumber;

cout << " enter the day ( 1 -7 ) : ";
cin >> daynumber;
switch (daynumber)
{
case 1:
cout<< " sunday "<<endl;
break;
case 2:
cout<< " monday "<<endl;
break;
case 3:
cout<< " tuseday "<<endl;
break;
case 4:
cout<< " wednesday "<<endl;
break;
case 5:
cout<< " thursday "<<endl;
break;
case 6:
cout<< " friday "<<endl;
break;
case 7:
cout<< " saturday "<<endl;
break;
default:
cout<<" not defined/true "<<endl;
}

return 0;
}
1
Q36: Write a C++ program that determines if a person is eligible to vote based on their age. Consider the legal voting age as 18.

#include <iostream> 
using namespace std;

int main()
{
int age;
cout<<" enter your age : ";
cin>>age;
if ( age >= 18)
{
cout<< " you are eligible to vote "<<endl;
}
else
{
cout<< " you are not eligible to vote "<<endl;
}
return 0;
}
1
Q37: Write C++ program takes an input of a person first name and uses a switch statement to output the corresponding person's age (consider three persons)

#include <iostream> 
using namespace std;

int main()
{
char firstName;

cout << "Enter a person's first initial (A/B/C): ";
cin >> firstName;

switch (firstName)
{
case 'A':
case 'a':
cout << "The age of person with first initial A is: 25" << endl;
break;
case 'B':
case 'b':
cout << "The age of person with first initial B is: 30" << endl;
break;
case 'C':
case 'c':
cout << "The age of person with first initial C is: 35" << endl;
break;
default:
cout << "Unknown person" << endl;
}

return 0;
}
Q38: Implement a C++ program that converts an amount in one currency to another. Ask the user for the input currency, amount, and desired currency.

#include <iostream>
using namespace std;

int main()
{
float currency, currency1, ratio;
cout<<" enter the your currency : ";
cin>>currency1;
cout<<" enter the different between the currencies : ";
cin>>ratio;
currency = currency1 / ratio;
cout<<currency<<endl;

return 0;
}
👎1
Q39: Write C++ Program which computes area of circle. The program takes radius of the circle as an input, calculates the area of the circle and outputs it on the screen.
(Area =𝜋𝑟²).

#include <iostream>
using namespace std;

int main()
{
float r, area;
cout<<" enter the radius : ";
cin>>r;
area = r * r * 3.14;
cout<<" the area is : "<<area<<endl;

return 0;
}
👍1
Q40: Write C++ code to computes area of a shape using switch case.
The program takes shape as an input and identifies the shape using a switch case. The statements then take required values as input, computes the area and prints it.
(assume two shapes only, square and rectangle)

#include <iostream> 
using namespace std;

int main()
{
char shape;
float side, length, width, area;

cout << "Enter the shape (S for square, R for rectangle): ";
cin >> shape;

switch (shape)
{
case 'S':
case 's':
cout << "Enter the side length of the square: ";
cin >> side;
area = side * side;
break;
case 'R':
case 'r':
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
area = length * width;
break;
default:
cout << "Invalid shape" << endl;
}

cout << "The area of the shape is: " << area << endl;

return 0;
}
1👍1
Q41: Implement a C++ code to decide the car type depending on the number of seats.
Print an appropriate massage to indicate a car type.
(Saloon has four seats, SUV has seven seats and Bus has more than seven seats).

#include <iostream> 
using namespace std;

int main() {
int numSeats;

cout << "Enter the number of seats in the car: ";
cin >> numSeats;

if (numSeats == 4)
{
cout << "This is a Saloon car." << endl;
}
else if (numSeats == 7) {
cout << "This is an SUV." << endl;
}
else if (numSeats > 7 && numSeats < 100 )
{
cout << "This is a Bus." << endl;
}
else
{
cout << "Invalid number of seats." << endl;
}

return 0;
}
1
Q42: Consider two integer numbers A=7 and B=14.
Write C++ program to implement the following assignment operator.
1- Left shift operator. (shift by 2).
2- Modulus operator.

#include <iostream> 
using namespace std;

int main()
{
int A = 7, B = 14;

A = A << 2;
B = B << 2;

B = A % B;

cout << "After left shifting A by 2: " << A << endl;
cout << "After left shifting B by 2: " << B << endl;
cout << "After using modulus operator on B: " << B << endl;

return 0;
}
1
Q43: Consider two integer numbers C=8 and D=4.
Write C++ program to implement the following assignment operator.
1- Right shift operator. (shift by 1)
2- Bitwise AND assignment operator.

#include <iostream> 
using namespace std;

int main()
{
int C = 8, D = 4;

C = C >> 1;
D = D >> 1;

C &= D;

cout << "After right shifting C by 1: " << C << endl;
cout << "After right shifting D by 1: " << D << endl;
cout << "After using bitwise AND operato on C,D : " << C << endl;

return 0;
}
1
Q44: Consider two integer numbers N=11 and M=9.
Write C++ program to implement the following assignment operator.
1- Divide AND assignment operator.
2- Bitwise inclusive OR and assignment operator.

#include <iostream> 
using namespace std;

int main()
{
int N = 11, M = 9;

N /= M;
N |= M;

cout << "After using Divide AND assignment operator : " << N << endl;
cout << "After using Bitwise inclusive OR and assignment operator : " << N << endl;

return 0;
}
Q45: Consider two integer numbers X=1 and Y=0.
C++ program to implement the following logical operator.
1- Logical AND operator.
2- Logical NOT Operator.

#include <iostream> 
using namespace std;

int main()
{
int X = 1, Y = 0;

cout << "After using Logical AND operator X,Y: " << (X&&Y) << endl;
cout << "After using Logical NOT Operator on X : " << !X << endl;
cout << "After using Logical NOT Operator on Y : " << !Y << endl;

return 0;
}

بهذا تكتب متغيرات بغير اسم او تشيل bool وتكتب int
بالاثنين صح
1
Q46: Write C++ code to determine the size of various data type on a computer.
(use character and Boolean only).

#include <iostream> 
using namespace std;

int main()
{
char character;
bool boolean;
cout << "Size of char: " << sizeof(character) << " bytes" << endl;
cout << "Size of bool: " << sizeof(boolean) << " bytes" << endl;

return 0;
}
1
Q47: Write C++ code to determine the size of various data type on a computer.
(use Floating point and integer only).

#include <iostream> 
using namespace std;

int main()
{
int integer;
float floatingPoint;

cout << "Size of int: " << sizeof(integer) << " bytes" << endl;
cout << "Size of float: " << sizeof(floatingPoint) << " bytes" << endl;

return 0;
}
1
Q48: Write C++ code to determine the size of various data type on a computer.
(use character and double only).

#include <iostream> 
using namespace std;

int main()
{
char character;
double doubleonly;

cout << "Size of int: " << sizeof(character) << " bytes" << endl;
cout << "Size of float: " << sizeof(doubleonly) << " bytes" << endl;

return 0;
}
Q49: Consider two integer numbers U=15 and V=8.
C++ program to implement the following operator description.
1- Checks if the values of two operands are equal or not.
2- Checks if the value of left operand is greater than or equal to the value of right operand.

#include <iostream> 
using namespace std;

int main()
{
int U = 15, V = 8;
bool areEqual = (U == V);
bool isGreaterOrEqual = ( U >= V);

cout << "Are the values of U and V equal? " << areEqual << endl;
cout << "Is U greater than or equal to V? " << isGreaterOrEqual << endl;

return 0;
}
1
Q50: Write a C++ program to print the sequence of numbers (95-105), except number 101. The code should has continue statement.

#include <iostream> 
using namespace std;

int main()
{
for ( int i = 95; i <= 105; i++)
{
if ( i == 101)
{
continue;
}
cout<<i<<", ";
}
cout<<endl;

return 0;
}


اذا طلبه :
use do while loop

#include <iostream> 
using namespace std;
int main ()
{
int a = 95;
do
{
if( a == 101)
{
a = a + 1;
continue;
}
cout <<a<<", ";
a = a + 1;
}
while( a <= 105);
return 0;
}
👍1
#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;
// or write ( cout << " the fac of the entered number is \n" ;
return 0;
}
Q6/
Write C++ code to swap two integer number (use appropriate function).

So/

بالتأكيد، إليك كود C++ يستخدم وظيفة لتبديل قيمتين صحيحتين، ويستخدم iostream و using namespace std فقط:

#include <iostream>

using namespace std;

// وظيفة لتبديل قيمتين
void swapNumbers(int &num1, int &num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}

int main() {
int number1, number2;

// الحصول على إدخال المستخدم
cout << "Enter the first number: ";
cin >> number1;

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

// استخدام الوظيفة لتبديل الأرقام
swapNumbers(number1, number2);

// عرض النتيجة
cout << "After swapping, the numbers are: " << number1 << " and " << number2 << endl;

return 0;
}
هذا الكود يستخدم iostream و using namespace std ويحتوي على وظيفة swapNumbers لتبديل قيمتين.
1👍1