شيئية OOP
281 subscribers
53 photos
10 files
12 links
عمي رحمة لوالديك ولعشيرتك وللعزاز وللعراق وللشرق الأوسط، لا تكعد تحفظ الأكواد
الكود فهم فهم فهم مو حفظ
و السلام
Download Telegram
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
Q2/
Write C++ code to find the Factorial for any integer number (use appropriate function).

So/
#include <iostream>
using namespace std;

// Function to calculate factorial recursively
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int main() {
int number;

// Input the number from the user
cout << "Enter a non-negative integer number: ";
cin >> number;

// Calculate and display the factorial of the number
cout << "Factorial of " << number << " is: " << factorial(number) << endl;

return 0;
}
Q51: Write a function which, given an array of integers, returns the integer that appears most frequently in the array.
E.g. for the array [ 1 2 3 2 3 4 2 5 ] your function should return 2.


#include <iostream> 
using namespace std;

int mostFrequent(const int arr[], int size)
{
int mostFrequentNum = arr[0];
int maxFrequency = 1;

for (int i = 0; i < size; ++i)
{
int currentNum = arr[i];
int currentFrequency = 1;

for (int j = i + 1; j < size; ++j)
{
if (arr[j] == currentNum)
{
currentFrequency++;
}
}

if (currentFrequency > maxFrequency)
{
mostFrequentNum = currentNum;
maxFrequency = currentFrequency;
}
}

return mostFrequentNum;
}

int main()
{
int numbers[] = {1, 2, 3, 2, 3, 4, 2, 5};
int size = sizeof(numbers) / sizeof(numbers[0]);

int result = mostFrequent(numbers, size);

cout << "The most frequent number is: " << result << endl;

return 0;
}
2👍1
Q52: Write the function sum() with four parameters that calculates the arguments provided and returns their sum. Parameters: Four variables of type long.
Returns: The sum of type long.
Use the default argument 0 to declare the last two parameter of the function sum().
Test the function sum() by calling it by all three possible methods.
Use random integers as arguments.


#include <iostream>
using namespace std;

// دالة لحساب مجموع أربعة متغيرات من نوع long
long sum(long a, long b, long c = 0, long d = 0) {
return a + b + c + d;
}

int main() {
// تعيين أرقام ثابتة للاختبار
long num1 = 10;
long num2 = 20;
long num3 = 30;
long num4 = 40;

// طريقة 1: استدعاء دالة sum() مع أربع متغيرات
long result1 = sum(num1, num2, num3, num4);
cout << "Method 1: Sum of " << num1 << ", " << num2 << ", " << num3 << ", " << num4 << " is: " << result1 << endl;

// طريقة 2: استدعاء دالة sum() مع ثلاث متغيرات
long result2 = sum(num1, num2, num3);
cout << "Method 2: Sum of " << num1 << ", " << num2 << ", " << num3 << " is: " << result2 << endl;

// طريقة 3: استدعاء دالة sum() مع متغيرين
long result3 = sum(num1, num2);
cout << "Method 3: Sum of " << num1 << ", " << num2 << " is: " << result3 << endl;

return 0;
}
👍2
تغيير قيمة المعاملات باستخدام البوينتر :
#include <iostream>
using namespace std;

int p ( int *ptr){
*ptr+= 1;
return *ptr;
}
int main() {
int m = 5;

cout << p(&m) << endl;
cout << m << endl;
cout << p(&m) << endl;
}
اظهار عنصر معين من المصفوفة باستخدام البوينتر :
#include <iostream>
using namespace std;

int main() {
int arr[] = {5, 7, 9};

cout << *(arr+2) << endl;
}
طباعة مصفوفة باستخدام البوينتر :
#include <iostream>
using namespace std;

int main() {
int arr[5] = {3, 5 , 7, 9, 11};

for (int i = 0; i < 5; i++){
cout << *(arr+i) << ", ";
}
cout<<endl;
}