Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

‎⁨المستند (8) ⁩

Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

Q1//Sum of Two User-Entered Numbers#include <iostream>

using namespace std;

int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
int sum = a + b;
cout << "Sum: " << sum << endl;
return 0;
}

Out put
Enter two numbers: 5 3
Sum: 8

Q2//Conditional Statement Based on User Input

#include <iostream>
using namespace std;

int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number > 0) {
cout << "Positive number" << endl;
} else {
cout << "Non-positive number" << endl;
}
return 0;
}
Out put
Enter a number: 10
Positive number
Q3// programming to Counter to any number
#include <iostream>
using namespace std;

int main() {
int n;
cout << "Enter a number: ";
cin >> n;
for (int i = 0; i <= n; i++) {
cout << i << " ";
}
cout << endl;
return 0;
}
Out put
Enter a number: 5
012345
Q4//calculator can user input any number

#include <iostream>
using namespace std;

int main() {
char op;
double num1, num2, result;

// Ask user for operation and numbers


cout << "Enter an operation (+, -, *, /): ";
cin >> op;

// Ask for two numbers


cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;

// Perform calculation based on the operation


switch (op) {
case '+':
result = num1 + num2;
cout << "Sum: " << result << endl;
break;
case '-':
result = num1 - num2;
cout << "Difference: " << result << endl;
break;
case '*':
result = num1 * num2;
cout << "Product: " << result << endl;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
cout << "Quotient: " << result << endl;
} else {
cout << "Error: Division by zero is not allowed." << endl;
}
break;
default:
cout << "Invalid operation. Please use only +, -, *, or /." <<
endl;
}

return 0;
}
Out put
Enter an operation (+, -, *, /): *
Enter first number: 5
Enter second number: 4
Product: 20
Q5//programing to Addition and Subtraction
#include <iostream>
using namespace std;
int main() {
char op;
double num1, num2, result;
cout << "Enter an operation (+ or -): ";
cin >> op;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
if (op == '+') {
result = num1 + num2;
cout << "Sum: " << result << endl;
} else if (op == '-') {
result = num1 - num2;
cout << "Difference: " << result << endl;
} else {
cout << "Invalid operation. Please use only + or -." << endl;
}

return 0;
Out put
Enter an operation (+ or -): +
Enter two numbers: 10 5
Sum: 15

Q6//Pointer
#include <iostream>
using namespace std;

int main() {
int number;
cout << "Enter a number: ";
cin >> number;
int *ptr = &number;
cout << "Value at pointer: " << *ptr << endl;
return 0;
}
Out put
Enter a number: 10
Value at pointer: 10
Q7//password
#include <iostream>
#include <string>
using namespace std;

int main() {
string password = "12345”;
string inputPassword;
cout << "Enter password: ";
cin >> inputPassword;
if (inputPassword == password) {
cout << "Access granted! Welcome." << endl;
} else {
cout << "Access denied! Incorrect password." << endl;
}

return 0;
}
Out put
Enter password: 12345
Access granted! Welcome.
Q8// positive and negative

#include <iostream>
using namespace std;

int main() {
int number;

cout << "Enter a number: ";


cin >> number;

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

return 0;}
Out put
Enter a number: 10
The number is positive.
Or
Enter a number: -5
The number is negative.
Q9//even and odd

#include <iostream>
#include <vector>
using namespace std;

int main() {
vector<int> evenNumbers;
vector<int> oddNumbers;
int number;
cout << "Enter numbers (-1 to exit):" << endl;
while (true) {
cin >> number;
if (number == -1) {
break;
}
if (number % 2 == 0) {
evenNumbers.push_back(number);
} else {
oddNumbers.push_back(number);
}
}

cout << "Even numbers: ";


for (int num : evenNumbers) {
cout << num << " ";
}
cout << endl;

cout << "Odd numbers: ";


for (int num : oddNumbers) {
cout << num << " ";
}
cout << endl;

return 0;
}
Out put
Enter numbers (-1 to exit):
4
7
2
9
6
-1
Even numbers: 4 2 6
Odd numbers: 7
Q10// ‫حساب مساحة االشكال الهندسية‬
#include <iostream>
#include <cmath>
using namespace std;

‫ حساب مساحة المستطيل‬//


double calculateRectangleArea(double length, double width) {
return length * width;
}

‫ حساب مساحة المثلث‬//


double calculateTriangleArea(double base, double height) {
return 0.5 * base * height;
}

‫ حساب مساحة الدائرة‬//


double calculateCircleArea(double radius) {
return M_PI * radius * radius;
}

int main() {
int choice;
double area, length, width, base, height, radius;

‫ عرض قائمة األشكال لالختيار‬//


cout << "Choose a shape to calculate its area:" << endl;
cout << "1. Rectangle" << endl;
cout << "2. Triangle" << endl;
cout << "3. Circle" << endl;
cout << "Enter your choice (1, 2, or 3): ";
cin >> choice;

‫ استخدام االختيار لحساب المساحة المناسبة‬//


switch (choice) {
case 1:
‫ طول وعرض‬:‫ المستطيل‬//
cout << "Enter length of rectangle: ";
cin >> length;
cout << "Enter width of rectangle: ";
cin >> width;
area = calculateRectangleArea(length, width);
cout << "Area of rectangle: " << area << endl;
break;
case 2:
‫ قاعدة وارتفاع‬:‫ المثلث‬//
cout << "Enter base of triangle: ";
cin >> base;
cout << "Enter height of triangle: ";
cin >> height;
area = calculateTriangleArea(base, height);
cout << "Area of triangle: " << area << endl;
break;
case 3:
‫ نصف القطر‬:‫ الدائرة‬//
cout << "Enter radius of circle: ";
cin >> radius;
area = calculateCircleArea(radius);
cout << "Area of circle: " << area << endl;
break;
default:
cout << "Invalid choice. Please choose 1, 2, or 3." << endl;
}

return 0;
}
Choose a shape to calculate its area:
1. Rectangle
2. Triangle
3. Circle
Enter your choice (1, 2, or 3): 2
Enter base of triangle: 5
Enter height of triangle: 3
Area of triangle: 7.5
Q11//program to find number )‫(العدد المتكرر‬
#include <iostream>
using namespace std;

int countOccurrences(int arr[], int size, int target) {


int count = 0;
for (int i = 0; i < size; ++i) {
if (arr[i] == target) {
count++;
}
}
return count;
}

int main() {
int numbers[] = {2, 5, 2, 8, 2, 4, 2};
int size = sizeof(numbers) / sizeof(numbers[0]);
int targetNumber = 2;
int occurrences = countOccurrences(numbers, size,
targetNumber);
cout << "Occurrences of " << targetNumber << ": " <<
occurrences << endl;
return 0;
}
Out put
Occurrences of 2: 4
Q12// ‫برنامج تبديل القيم‬
#include <iostream>
using namespace std;

void swapValues(int &a, int &b) {


int temp = a;
a = b;
b = temp;
}

int main() {
int x = 5, y = 10;
cout << "Before swapping: x = " << x << ", y = " << y << endl;
swapValues(x, y);
cout << "After swapping: x = " << x << ", y = " << y << endl;
return 0;
}
Out put
Before swapping: x = 5, y = 10
After swapping: x = 10, y = 5
Q13//Square any number
#include <iostream>
using namespace std;

‫ تعريف الدالة لحساب مربع العدد‬//


int square(int num) {
return num * num;
}

int main() {
int number;
cout << "Enter a number: ";
cin >> number;
int result = square(number);
cout << "Square: " << result << endl;
return 0;
}
Out put
Enter a number: 5
Square: 25
Q14//program to calaulate average arr
#include <iostream>
using namespace std;

double calculateAverage(int arr[], int size) {


double sum = 0;
for (int i = 0; i < size; ++i) {
sum += arr[i];
}
return sum / size;
}

int main() {
int numbers[] = {10, 20, 30, 40, 50};
int size = sizeof(numbers) / sizeof(numbers[0]);
double avg = calculateAverage(numbers, size);
cout << "Average: " << avg << endl;
return 0;
}
Out put
Average: 30
Q15//add two numbers
#include <iostream>
using namespace std;
int sum(int a, int b) {
return a + b;
}
int main() {
int x = 5, y = 3;
int result = sum(x, y);
cout << "Sum: " << result << endl;
return 0;}
Out put

Sum: 8
Q16//factorial
#include <iostream>
using namespace std;

int calculateFactorial(int n) {

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

int main() {
int number;

cout << "Enter a number to calculate its factorial: ";


cin >> number;

int result = calculateFactorial(number);

cout << "Factorial of " << number << " is " << result << endl;

return 0;
}
Enter a number to calculate its factorial: 5
Factorial of 5 is 120
Q17//‫تحويل من سليزي الى فرهن هايت‬
#include <iostream>
using namespace std;

double celsiusToFahrenheit(double celsius) {


return (celsius * 9.0/5.0) + 32;{
int main() {
double celsius, fahrenheit;
cout << "Enter temperature in Celsius: ";
cin >> celsius;

fahrenheit = celsiusToFahrenheit(celsius);

cout << "Temperature in Fahrenheit: " << fahrenheit << endl;

return 0;
}
Out put
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77

Q18//‫برنامج الدخال ارقام عشوائيه الكن المستخدم هوا الذي يحدد فترة هذه االرقام‬
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void printRandomNumbers(int count, int lowerBound, int


upperBound) {
srand(time(0));

for (int i = 0; i < count; ++i) {


int randomNumber = rand() % (upperBound - lowerBound + 1)
+ lowerBound;
cout << randomNumber << " ";
}
cout << endl;
}

int main() {
int count, lower, upper;
cout << "Enter number of random numbers to generate: ";
cin >> count;
cout << "Enter lower bound: ";
cin >> lower;
cout << "Enter upper bound: ";
cin >> upper;

cout << "Random numbers: ";


printRandomNumbers(count, lower, upper);

return 0;
}
Out put
Enter number of random numbers to generate: 5
Enter lower bound: 1
Enter upper bound: 100
Random numbers: 17 45 89 3 62
Q19//‫حساب مساحة المثلث‬
#include <iostream>
#include <cmath>
using namespace std;
double calculateTriangleArea(double base, double height) {
return 0.5 * base * height;{
int main() {
double base, height;
cout << "Enter base length of triangle: ";
cin >> base;
cout << "Enter height of triangle: ";
cin >> height;
double area = calculateTriangleArea(base, height);
cout << "Area of triangle: " << area << endl;

return 0;{
Out put
Enter base length of triangle: 5
Enter height of triangle: 8
Area of triangle: 20

Q20//‫حساب المجموع الكلي للمصفوفه‬


#include <iostream>
using namespace std;
int sumArrayElements(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; ++i) {
sum += arr[i];
}
return sum;
}

int main() {
int numbers[] = {10, 20, 30, 40, 50};
int size = sizeof(numbers) / sizeof(numbers[0]);

int totalSum = sumArrayElements(numbers, size);

cout << "Sum of array elements: " << totalSum << endl;

return 0;
}
Out put
Sum of array elements: 150
Q21//‫طباعة عناصر المصفوفه بالعكس‬
#include <iostream>
using namespace std;

int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int arr[size];
cout << "Enter " << size << " integers:\n";
for (int i = 0; i < size; ++i) {
cin >> arr[i];
}

cout << "Array elements in reverse order:\n";


for (int i = size - 1; i >= 0; --i) {
cout << arr[i] << " ";
}
cout << endl;

return 0;
}
Out put
Enter the size of the array: 4
Enter 4 integers:
1234
Array elements in reverse order:
4321
Q22// ‫إيجاد عنصر داخل المصفوفة‬
#include <iostream>
using namespace std;

int main() {
int size, target;
cout << "Enter the size of the array: ";
cin >> size;

int arr[size];
cout << "Enter " << size << " integers:\n";
for (int i = 0; i < size; ++i) {
cin >> arr[i];
}

cout << "Enter the number to search for: ";


cin >> target;
bool found = false;
for (int i = 0; i < size; ++i) {
if (arr[i] == target) {
found = true;
break;
}
}

if (found) {
cout << "Number " << target << " is found in the array.\n";
} else {
cout << "Number " << target << " is not found in the array.\n";
}

return 0;
}
Out put
Enter the size of the array: 5
Enter 5 integers:
10 20 30 40 50
Enter the number to search for: 30
Number 30 is found in the array.
Q23//‫إيجاد اكبر عنصر في المصفوفه‬
#include <iostream>
using namespace std;

int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;

int arr[size];
cout << "Enter " << size << " integers:\n";
for (int i = 0; i < size; ++i) {
cin >> arr[i];
}
int maxElement = arr[0];

for (int i = 1; i < size; ++i) {


if (arr[i] > maxElement) {
maxElement = arr[i];
}
}

cout << "Largest element in the array: " << maxElement << endl;

return 0;
}
Out put
Enter the size of the array: 4
Enter 4 integers:
15 7 22 10
Largest element in the array: 22
Q24//‫جمع عناصر الصفوف في المصفوفه‬
#include <iostream>
using namespace std;

int main() {
const int rows = 3;
const int cols = 3;
int matrix[rows][cols];

cout << "Enter elements of a " << rows << "x" << cols << "
matrix:\n";
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> matrix[i][j];
}
}
cout << "Sum of elements in each row:\n";
for (int i = 0; i < rows; ++i) {
int rowSum = 0;
for (int j = 0; j < cols; ++j) {
rowSum += matrix[i][j];
}
cout << "Row " << i + 1 << ": " << rowSum << endl;
}

return 0;
}
Out put
Enter elements of a 3x3 matrix:
123
456
789
Sum of elements in each row:
Row 1: 6
Row 2: 15
Row 3: 24
Q25//‫إيجاد القطر الرئيسي واستخراج االعداد الزوجيه والفرديه منه‬
#include <iostream>
using namespace std;

int main() {
int size;

cout << "Enter the size of the matrix (should be square): ";
cin >> size;

int matrix[size][size];
cout << "Enter " << size * size << " integers for the matrix:\n";
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
cin >> matrix[i][j];
}
}

cout << "Main diagonal elements: ";


for (int i = 0; i < size; ++i) {
cout << matrix[i][i] << " ";
}
cout << endl;

int evenCount = 0, oddCount = 0;


for (int i = 0; i < size; ++i) {
if (matrix[i][i] % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}

cout << "Number of even elements in main diagonal: " <<


evenCount << endl;
cout << "Number of odd elements in main diagonal: " <<
oddCount << endl;

return 0;
}
Out put
Enter the size of the matrix (should be square): 3
Enter 9 integers for the matrix:
123
456
789
Main diagonal elements: 1 5 9
Number of even elements in main diagonal: 2
Number of odd elements in main diagonal: 1
Q26// ‫جمع عناصر كل صف‬
#include <iostream>
using namespace std;

int main() {
int rows, cols;

cout << "Enter the number of rows: ";


cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;

int matrix[rows][cols];

cout << "Enter " << (rows * cols) << " integers for the matrix:\n";
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cin >> matrix[i][j];
}
}

cout << "Sum of elements in each row:\n";


for (int i = 0; i < rows; ++i) {
int rowSum = 0;
for (int j = 0; j < cols; ++j) {
rowSum += matrix[i][j];
}
cout << "Row " << i + 1 << ": " << rowSum << endl;
}

return 0;
}
Out put
Enter the number of rows: 3
Enter the number of columns: 3
Enter 9 integers for the matrix:
123
456
789
Sum of elements in each row:
Row 1: 6
Row 2: 15
Row 3: 24
Q27// ‫قلب المصفوفة‬
#include <iostream>
using namespace std;
int main() {
const int size = 3;

int matrix[size][size] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

cout << "Original Matrix:\n";


for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
cout << matrix[i][j] << " ";
}
cout << endl;
}

int invertedMatrix[size][size];
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
invertedMatrix[j][i] = matrix[i][j];
}
}

cout << "Inverted Matrix:\n";


for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
cout << invertedMatrix[i][j] << " ";
}
cout << endl;
}

return 0;
}
Out put
Original Matrix:
123
456
789
Inverted Matrix:
147
258
369
Q28//‫جمع مصفوفتين‬
#include <iostream>
using namespace std;

int main() {
int rows1, cols1, rows2, cols2;

cout << "Enter dimensions of Matrix 1 (rows cols): ";


cin >> rows1 >> cols1;

cout << "Enter dimensions of Matrix 2 (rows cols): ";


cin >> rows2 >> cols2;
if (rows1 != rows2 || cols1 != cols2) {
cout << "Matrices dimensions mismatch. Cannot perform
addition." << endl;
return 1;
}
int matrix1[rows1][cols1];
int matrix2[rows2][cols2];
int sumMatrix[rows1][cols1];

cout << "Enter elements of Matrix 1:\n";


for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols1; ++j) {
cin >> matrix1[i][j];
}
}

cout << "Enter elements of Matrix 2:\n";


for (int i = 0; i < rows2; ++i) {
for (int j = 0; j < cols2; ++j) {
cin >> matrix2[i][j];
}
}

sumMatrix
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols1; ++j) {
sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

cout << "Resultant Matrix (Matrix1 + Matrix2):\n";


for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols1; ++j) {
cout << sumMatrix[i][j] << " ";
}
cout << endl;
}

return 0;
}
Out put
Enter dimensions of Matrix 1 (rows cols): 2 2
Enter dimensions of Matrix 2 (rows cols): 2 2
Enter elements of Matrix 1:
12
34
Enter elements of Matrix 2:
56
78
Resultant Matrix (Matrix1 + Matrix2):
68
10 12
Q29//‫ضرب مصفوفتين‬
#include <iostream>
using namespace std;
const int MAX_SIZE = 10; // ‫تعريف أقصى حجم للمصفوفات‬

void matrixMultiplication(int mat1[][MAX_SIZE], int rows1, int


cols1,
int mat2[][MAX_SIZE], int rows2, int cols2,
int result[][MAX_SIZE]) {
‫ التأكد من أن عدد األعمدة في المصفوفة األولى يساوي عدد الصفوف في‬//
‫المصفوفة الثانية‬
if (cols1 != rows2) {
cout << "Error: Matrices dimensions mismatch. Cannot
perform multiplication." << endl;
return;
}

‫ ضرب المصفوفتين‬//
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
result[i][j] = 0;
for (int k = 0; k < cols1; ++k) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}

int main() {
int rows1, cols1, rows2, cols2;

‫ إدخال أبعاد المصفوفة األولى من المستخدم‬//


cout << "Enter dimensions of Matrix 1 (rows cols): ";
cin >> rows1 >> cols1;

‫ إدخال أبعاد المصفوفة الثانية من المستخدم‬//


cout << "Enter dimensions of Matrix 2 (rows cols): ";
cin >> rows2 >> cols2;

‫ التحقق من صحة األبعاد إلجراء عملية الضرب‬//


if (cols1 != rows2) {
cout << "Error: Matrices dimensions mismatch. Cannot
perform multiplication." << endl;
return 1; // ‫خروج برنامج بسبب فشل في األبعاد‬
}

)‫ إنشاء المصفوفتين ومصفوفة الناتج (المصفوفة المضروبة‬//


int matrix1[MAX_SIZE][MAX_SIZE];
int matrix2[MAX_SIZE][MAX_SIZE];
int result[MAX_SIZE][MAX_SIZE];

‫ إدخال قيم المصفوفة األولى من المستخدم‬//


cout << "Enter elements of Matrix 1:\n";
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols1; ++j) {
cin >> matrix1[i][j];
}
}

‫ إدخال قيم المصفوفة الثانية من المستخدم‬//


cout << "Enter elements of Matrix 2:\n";
for (int i = 0; i < rows2; ++i) {
for (int j = 0; j < cols2; ++j) {
cin >> matrix2[i][j];
}
}

‫ ضرب المصفوفتين وحفظ الناتج في المصفوفة‬// result


matrixMultiplication(matrix1, rows1, cols1, matrix2, rows2,
cols2, result);

)‫ عرض المصفوفة الناتجة (الناتج من ضرب المصفوفتين‬//


cout << "Resultant Matrix (Matrix1 * Matrix2):\n";
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
cout << result[i][j] << " ";
}
cout << endl;
}

return 0;
}
Out put
Enter dimensions of Matrix 1 (rows cols): 2 3
Enter dimensions of Matrix 2 (rows cols): 3 2
Enter elements of Matrix 1:
123
456
Enter elements of Matrix 2:
78
9 10
11 12
Resultant Matrix (Matrix1 * Matrix2):
58 64
139 154
Q30// ‫حل معادلة‬a=3d+c/2
#include <iostream>
using namespace std;

const int MAX_SIZE = 10; // ‫تعريف أقصى حجم للمصفوفات‬

int main() {
int rows_a, cols_a, rows_d, cols_d, rows_c, cols_c;

‫ إدخال أبعاد المصفوفة‬// a ‫من المستخدم‬


cout << "Enter dimensions of Matrix a (rows cols): ";
cin >> rows_a >> cols_a;

‫ إدخال أبعاد المصفوفة‬// d ‫من المستخدم‬


cout << "Enter dimensions of Matrix d (rows cols): ";
cin >> rows_d >> cols_d;

‫ إدخال أبعاد المصفوفة‬// c ‫من المستخدم‬


cout << "Enter dimensions of Matrix c (rows cols): ";
cin >> rows_c >> cols_c;
‫ التحقق من صحة األبعاد للقيام بالعملية‬//
if (rows_a != rows_d || cols_a != cols_d || rows_a != rows_c ||
cols_a != cols_c) {
cout << "Error: Matrices dimensions mismatch. Cannot
perform operation." << endl;
return 1; // ‫خروج برنامج بسبب فشل في األبعاد‬
}

‫ إنشاء المصفوفات‬// a, d, c ‫ومصفوفة الناتج‬result


int a[MAX_SIZE][MAX_SIZE];
int d[MAX_SIZE][MAX_SIZE];
int c[MAX_SIZE][MAX_SIZE];
int result[MAX_SIZE][MAX_SIZE];

‫ إدخال قيم المصفوفة‬// a ‫من المستخدم‬


cout << "Enter elements of Matrix a:\n";
for (int i = 0; i < rows_a; ++i) {
for (int j = 0; j < cols_a; ++j) {
cin >> a[i][j];
}
}

‫ إدخال قيم المصفوفة‬// d ‫من المستخدم‬


cout << "Enter elements of Matrix d:\n";
for (int i = 0; i < rows_d; ++i) {
for (int j = 0; j < cols_d; ++j) {
cin >> d[i][j];
}
}

‫ إدخال قيم المصفوفة‬// c ‫من المستخدم‬


cout << "Enter elements of Matrix c:\n";
for (int i = 0; i < rows_c; ++i) {
for (int j = 0; j < cols_c; ++j) {
cin >> c[i][j];
}
}
‫ حساب المصفوفة‬// a = 3d + c/2
for (int i = 0; i < rows_a; ++i) {
for (int j = 0; j < cols_a; ++j) {
a[i][j] = 3 * d[i][j] + c[i][j] / 2;
}
}

‫ عرض المصفوفة‬// a ‫بعد الحساب‬


cout << "Result Matrix a = 3d + c/2:\n";
for (int i = 0; i < rows_a; ++i) {
for (int j = 0; j < cols_a; ++j) {
cout << a[i][j] << " ";
}
cout << endl;
}

return 0;
}
Out put
Enter dimensions of Matrix a (rows cols): 2 2
Enter dimensions of Matrix d (rows cols): 2 2
Enter dimensions of Matrix c (rows cols): 2 2
Enter elements of Matrix a:
12
34
Enter elements of Matrix d:
56
78
Enter elements of Matrix c:
9 10
11 12
Result Matrix a = 3d + c/2:
17 23
29 35
Q31//‫حساب اعداد‬
#include <iostream>
using namespace std;
int sumUpTo(int n) {
if (n == 0)
return 0;
else
return n + sumUpTo(n - 1);
}

int main() {
int number;
cout << "Enter a positive integer: ";
cin >> number;
cout << "Sum of numbers from 1 to " << number << " is: " <<
sumUpTo(number) << endl;
return 0;
}
Out put
Enter a positive integer: 5
Sum of numbers from 1 to 5 is: 15
Q31// ‫قلب الكلمه‬
#include <iostream>
using namespace std;

void reversePrint(const char* str) {


if (*str == '\0')
return;
reversePrint(str + 1);
cout << *str;
}

int main() {
const char* message = "Hello, World!";
cout << "Original message: " << message << endl;
cout << "Reversed message: ";
reversePrint(message);
cout << endl;
return 0;
}
Out put
Original message: Hello, World!
Reversed message: !dlroW ,olleH
Q32//‫حساب تنازلي‬
#include <iostream>
using namespace std;

void countDown(int n) {
if (n < 0)
return;
cout << n << " ";
countDown(n - 1);
}

int main() {
int number;
cout << "Enter a positive integer: ";
cin >> number;
cout << "Counting down from " << number << ": ";
countDown(number);
cout << endl;
return 0;
}
Out put
Enter a positive integer: 5
Counting down from 5: 5 4 3 2 1 0
Q33//‫تحويل من ثنائي الى العشري‬

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

‫ دالة لتحويل العدد الثنائي إلى عدد عشري‬//


int binaryToDecimal(string binary) {
int decimal = 0;
int power = 0;

)‫ البدء بالتحويل من أقل األرقام (أقل األقدام‬//


for (int i = binary.length() - 1; i >= 0; --i) {
‫ إذا كان الرقم في الموضع‬// i ^2 ‫ فإننا نضيف‬،'1' ‫يساوي‬power ‫إلى القيمة‬
‫العشرية‬
if (binary[i] == '1') {
decimal += pow(2, power);
}
)‫ زيادة القوة (العدد الذي نقوم برفعه إلى األس‬//
power++;
}

return decimal;
}

int main() {
string binaryNumber;

‫ استقبال العدد الثنائي من المستخدم كسلسلة نصية‬//


cout << "Enter a binary number: ";
cin >> binaryNumber;

‫ التحويل من العدد الثنائي إلى العدد العشري باستخدام الدالة‬//


binaryToDecimal
int decimalNumber = binaryToDecimal(binaryNumber);

)‫ عرض الناتج (العدد العشري‬//


cout << "Decimal equivalent: " << decimalNumber << endl;

return 0;
}
Out put
Enter a binary number: 10101
Decimal equivalent: 21
Q34// //#write a program in c++ for calculating the time spent

#include <iostream>
using namespace std;
void time(double time, double distance)
{
double th, tm, AvrageSpeed = 60;
// the Avrage Speed of car is 60km/h
int t;
time = distance / AvrageSpeed;
// time in minute
t = time * 60;
//the hours
th = t / 60;
//the minute
tm = t % 60;
cout << "the time is: " << th << " hour and " << tm << " minute";
}
int main()
{
double t, d;
cout << "enter the distance(km): ";
cin >> d;
time(t, d);
}
Out put
Enter the distance (km): 120
Time taken to cover 120 km at an average speed of 60.0 km/h is: 2
hours and 0 minutes
Q34//‫عكس الرقم‬
#include <iostream>

using namespace std;

#include <stdio.h>

int main(){

int x,i=0;
while(i<3){
cout<<"Enter your number: ";
cin>>x;
if(x>0 && x<=99999)
{
int a1= x % 10
,a2 = (x / 10)%10
,a3=(x / 100)%10
,a4=(x / 1000)%10
,a5=x / 10000;
cout<<a1<<a2<<a3<<a4<<a5<<endl;
}
else
cout<<"Error Try again\n";

}
return 0;
}
Out put
Enter your number: 12345
54321
Enter your number: 9876
6789
Enter your number: 456
654
Q35//
/Write a program in C++ to find the square of any //number using
the function.
#include <iostream>

using namespace std;

#include <stdio.h>

double square(double num)


{
return (num * num);
}
int main()
{
double n,num;
cout<<"\n\n Function : find square of any number :\n";

cout<<"------------------------------------------------\n";

cout<<"Input any number for square : ";


cin>>num;
n = square(num);
cout<<"The square of "<<num<<" is : "<<n<<endl;

return 0;
}
Out put
Function: Find square of any number
------------------------------------
Input any number to find its square: 5
The square of 5 is: 25
Q36//
#include <iostream>
#include <string>
using namespace std;

int main() {
string text;
char target;
int count = 0;

cout << "Enter a string: ";


getline(cin, text);

cout << "Enter a character to count: ";


cin >> target;

for (char c : text) {


if (c == target) {
count++;
}
}
cout << "The character '" << target << "' appears " << count << "
times." << endl;
return 0;
}
Q37//‫إيجاد القيمة المطلقة لعدد‬
#include <iostream>
#include <cmath>
using namespace std;

int main() {
double number;

cout << "Enter a number: ";


cin >> number;

double absoluteValue = abs(number);

cout << "Absolute value: " << absoluteValue << endl;


return 0;}
Output
Enter a number: -5
Absolute value: 5
Q38//‫إيجاد اكبر عددين‬

#include <iostream>
using namespace std;

int main() {
double num1, num2;

cout << "Enter first number: ";


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

double maxNumber = (num1 > num2) ? num1 : num2;


cout << "Maximum number is: " << max
Out put

Enter first number: 15


Enter second number: 22.5
Maximum number is: 22.5
Q39//‫حساب العمر‬
#include <iostream>
#include <ctime>
using namespace std;

int main() {
int birthYear, currentYear;

cout << "Enter your birth year: ";


cin >> birthYear;

time_t now = time(0);


tm *ltm = localtime(&now);
currentYear = 1900 + ltm->tm_year;

int age = currentYear - birthYear;

cout << "Your age is: " << age << " years old." << endl;
return 0;
}
Out put
Enter your birth year: 1990
Your age is: 34 years old.
Q40//‫حساب االحرف في كلمه‬
#include <iostream>
#include <string>
using namespace std;

int main() {
string inputString;
cout << "Enter a string: ";
getline(cin, inputString);

int charCount = inputString.length();


cout << "Number of characters: " << charCount << endl;

return 0;
}
Out put
Enter a string: Hello World!
Number of characters: 12
Q41//‫تحويل الحروف الصغيره الى كبيره‬
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main() {
string inputString;

cout << "Enter a string: ";


getline(cin, inputString);

cout << "Convert to (U)pper or (L)ower case? ";


char choice;
cin >> choice;

if (choice == 'U' || choice == 'u') {


for (char &c : inputString) {
c = toupper(c);
}
} else if (choice == 'L' || choice == 'l') {
for (char &c : inputString) {
c = tolower(c);
}
} else {
cout << "Invalid choice!" << endl;
return 1;
}

cout << "Converted string: " << inputString << endl;

return 0;
}
Out put
Enter a string: Hello World
Convert to (U)pper or (L)ower case? U
Converted string: HELLO WORLD
Q41//‫جدول الضرب‬
#include <iostream>
using namespace std;

int main() {
int number;
cout << "Enter a number to display its multiplication table: ";
cin >> number;

cout << "Multiplication Table of " << number << ":\n";


for (int i = 1; i <= 10; ++i) {
cout << number << " * " << i << " = " << (number * i) << endl;
}

return 0;
}
Out put
Enter a number to display its multiplication table: 7
Multiplication Table of 7:
7*1=7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
Q42//‫برنامج لحساب فاتوره ناقص منها الخصم‬
#include <iostream>
using namespace std;

int main() {
double totalAmount, discountRate, finalAmount;

cout << "Enter total amount: ";


cin >> totalAmount;

cout << "Enter discount rate (%): ";


cin >> discountRate;

finalAmount = totalAmount - (totalAmount * (discountRate /


100));

cout << "Final amount after discount: " << finalAmount << endl;

return 0;
}
Out put
Enter total amount: 100
Enter discount rate (%): 10
Final amount after discount: 90
Q43//‫حساب درجات والمعدل‬
#include <iostream>
using namespace std;

int main() {
int numSubjects;
cout << "Enter the number of subjects: ";
cin >> numSubjects;

double totalMarks = 0;
for (int i = 1; i <= numSubjects; ++i) {
double marks;
cout << "Enter marks for subject " << i << ": ";
cin >> marks;
totalMarks += marks;
}

double average = totalMarks / numSubjects;


cout << "Your average marks are: " << average << endl;

return 0;
}
Out put
Enter the number of subjects: 4
Enter marks for subject 1: 85
Enter marks for subject 2: 78
Enter marks for subject 3: 92
Enter marks for subject 4: 80
Your average marks are: 83.75
Q44//‫تحقق اذا كان عدد اولي‬
#include <iostream>
using namespace std;

bool isPrime(int number) {


if (number <= 1) return false;
for (int i = 2; i * i <= number; ++i) {
if (number % i == 0) return false;
}
return true;
}

int main() {
int num;
cout << "Enter a number: ";
cin >> num;

if (isPrime(num)) {
cout << num << " is a prime number." << endl;
} else {
cout << num << " is not a prime number." << endl;
}

return 0;
}
Out put
Enter a number: 17
17 is a prime number.
Q45// ‫تحقق اذا كانت سنه كبيسه أوال‬
#include <iostream>
using namespace std;

bool isLeapYear(int year) {


return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
}

int main() {
int year;
cout << ";" :‫أدخل سنة‬
cin >> year;

if (isLeapYear(year)) {
cout << year << " << ".‫هي سنة كبيسة‬endl;
} else {
cout << year << " << ".‫ليست سنة كبيسة‬endl;
}

return 0;
}
Out put
2024 :‫أدخل سنة‬
.‫ هي سنة كبيسة‬2024
Q45//‫برنامج لحساب اعداد اوليه ضمن نطاق محدد‬
#include <iostream>
#include <cmath>
using namespace std;
bool isPrime(int num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;
for (int i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) return false;
}
return true;
}

int main() {
int start, end;

cout << ";" :‫أدخل الحد األدنى للنطاق‬


cin >> start;
cout << ";" :‫أدخل الحد األعلى للنطاق‬
cin >> end;

cout << " << "[ ‫األعداد األولية في النطاق‬start << ", " << end << "]
<< ":‫هي‬endl;
for (int i = start; i <= end; ++i) {
if (isPrime(i)) {
cout << i << " ";
}
}
cout << endl;

return 0;
}
Out put
10 :‫أدخل الحد األدنى للنطاق‬
30 :‫أدخل الحد األعلى للنطاق‬
:‫] هي‬30 ,10[ ‫األعداد األولية في النطاق‬
11 13 17 19 23 29
Q46// ‫تحويل العدد الثنائي الى ثماني‬
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

‫ تحويل العدد الثنائي إلى عدد ثماني‬//


int binaryToOctal(string binary) {
int decimal = 0;
int octal = 0;
int base = 1;

)‫ تحويل العدد الثنائي إلى عدد صحيح (عشري‬//


for (int i = binary.length() - 1; i >= 0; --i) {
if (binary[i] == '1') {
decimal += base;
}
base *= 2;
}

base = 1;
‫ تحويل العدد العشري إلى عدد ثماني‬//
while (decimal != 0) {
octal += (decimal % 8) * base;
decimal /= 8;
base *= 10;
}

return octal;
}

int main() {
string binary;

‫ استقبال العدد الثنائي من المستخدم‬//


cout << ";" :‫أدخل عددًا ثنائيًا‬
cin >> binary;

‫ التحويل وطباعة الناتج‬//


int octal = binaryToOctal(binary);
cout << " << " :‫العدد الثماني المعادل هو‬octal << endl;
return 0;
}
Out put
110110 :‫أدخل عددًا ثنائيًا‬
66 :‫العدد الثماني المعادل هو‬
Q47//‫تحويل الثنائي الى سادس عشر‬
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

‫ تحويل العدد الثنائي إلى عدد سداسي عشري‬//


string binaryToHexadecimal(string binary) {
string hexadecimal = "";
int decimal = 0;
int base = 1;

)‫ تحويل العدد الثنائي إلى عدد صحيح (عشري‬//


for (int i = binary.length() - 1; i >= 0; --i) {
if (binary[i] == '1') {
decimal += base;
}
base *= 2;
}

‫ تحويل العدد العشري إلى عدد سداسي عشري‬//


while (decimal != 0) {
int remainder = decimal % 16;
char hexDigit;

‫ تحويل الباقي إلى رمز سداسي عشري‬//


if (remainder < 10) {
hexDigit = remainder + '0'; // ‫تحويل الرقم إلى الشخصية المناسبة‬
)'9' ‫' إلى‬0'(
} else {
hexDigit = remainder - 10 + 'A'; // ‫تحويل الرقم إلى الحرف المناسب‬
'(A' ' ‫إلى‬F')
}
hexadecimal = hexDigit + hexadecimal; // ‫إضافة الرمز إلى بداية‬
‫النص السداسي‬
decimal /= 16;
}

return hexadecimal;
}

int main() {
string binary;

‫ استقبال العدد الثنائي من المستخدم‬//


cout << ";" :‫أدخل عددًا ثنائيًا‬
cin >> binary;

‫ التحويل وطباعة الناتج‬//


string hexadecimal = binaryToHexadecimal(binary);
cout << " << " :‫العدد السداسي عشر المعادل هو‬hexadecimal << endl;

return 0;
}
Out put
110110 :‫أدخل عددًا ثنائيًا‬
36 :‫العدد السداسي عشر المعادل هو‬
Q48// ‫تحويل العدد العشري الى أي نظام يريده المستخدم‬
#include <iostream>
#include <string>
#include <stack>
using namespace std;

‫ تحويل العدد العشري إلى أي نظام معين‬//


string decimalToBase(int decimal, int base) {
string result = "";
stack<char> digits;

‫ تحويل العدد العشري إلى النظام المطلوب‬//


while (decimal > 0) {
‫;‪int remainder = decimal % base‬‬
‫;‪char digit‬‬

‫‪ //‬تحويل الباقي إلى رقم أو حرف معتمد على القاعدة‬


‫{ )‪if (remainder < 10‬‬
‫تحويل الرقم إلى الشخصية المناسبة ('‪digit = remainder + '0'; // '0‬‬
‫إلى '‪)'9‬‬
‫{ ‪} else‬‬
‫'‪A‬تحويل الرقم إلى الحرف المناسب (' ‪digit = remainder - 10 + 'A'; //‬‬
‫)'‪F‬إلى '‬
‫}‬

‫إضافة الرقم أو الحرف إلى الستاك ‪digits.push(digit); //‬‬


‫;‪decimal /= base‬‬
‫}‬

‫‪ //‬بناء النتيجة من الستاك‬


‫{ ))(‪while (!digits.empty‬‬
‫;)(‪result += digits.top‬‬
‫;)(‪digits.pop‬‬
‫}‬

‫;‪return result‬‬
‫}‬

‫{ )(‪int main‬‬
‫;‪int decimalNumber, base‬‬

‫‪ //‬استقبال العدد العشري والقاعدة المطلوبة من المستخدم‬


‫أدخل العدد العشري‪cout << ";" :‬‬
‫;‪cin >> decimalNumber‬‬
‫أدخل القاعدة المطلوبة (‪ 2‬للثنائي‪ 8 ،‬للثماني‪ 16 ،‬للسداسي عشري‪cout << " ،‬‬
‫إلخ)‪;" :‬‬
‫;‪cin >> base‬‬

‫‪ //‬التحويل وطباعة الناتج‬


‫;)‪string result = decimalToBase(decimalNumber, base‬‬
‫;‪result << endl‬هو‪base << " << " :‬العدد في القاعدة " << " << ‪cout‬‬
‫;‪return 0‬‬
‫}‬
‫‪Out put‬‬
‫أدخل العدد العشري‪123 :‬‬
‫أدخل القاعدة المطلوبة (‪ 2‬للثنائي‪ 8 ،‬للثماني‪ 16 ،‬للسداسي عشري‪ ،‬إلخ)‪16 :‬‬
‫‪B‬العدد في القاعدة ‪ 16‬هو‪7 :‬‬
‫برنامج لطباعه نجوم يمكن للمستخدم ادخال االرتفاع الذي يريده ‪Q49//‬‬
‫>‪#include <iostream‬‬
‫;‪using namespace std‬‬

‫{ )‪void printStars(int height‬‬


‫{ )‪for (int i = 1; i <= height; ++i‬‬
‫{ )‪for (int j = 1; j <= i; ++j‬‬
‫;" *" << ‪cout‬‬
‫}‬
‫;‪cout << endl‬‬
‫}‬
‫}‬

‫{ )(‪int main‬‬
‫;‪int height‬‬

‫أدخل ارتفاع السلسلة النجمية‪cout << ";" :‬‬


‫;‪cin >> height‬‬

‫;‪endl‬هي‪height << " << ":‬السلسلة النجمية بارتفاع " << " << ‪cout‬‬
‫;)‪printStars(height‬‬

‫;‪return 0‬‬
‫}‬
‫‪Out put‬‬
‫أدخل ارتفاع السلسلة النجمية‪4 :‬‬
‫السلسلة النجمية بارتفاع ‪ 4‬هي‪:‬‬
‫*‬
‫**‬
‫***‬
‫****‬
Q50//‫متسلسلة بنا ًء على التسلسل الفيبوناتشي‬
#include <iostream>
using namespace std;

int fibonacciSum(int n) {
if (n <= 0) return 0;
if (n == 1) return 1;

int a = 0, b = 1, sum = 1;
for (int i = 2; i <= n; ++i) {
int next = a + b;
sum += next;
a = b;
b = next;
}

return sum;
}

int main() {
int number;

cout << ";" :‫أدخل عدد لحساب مجموع سلسلة فيبوناتشي‬


cin >> number;

int sum = fibonacciSum(number);


cout << " << " ‫مجموع أعداد سلسلة فيبوناتشي حتى العدد‬number << " :‫هو‬
<< "sum << endl;

return 0;
}
Out put
5 :‫أدخل عدد لحساب مجموع سلسلة فيبوناتشي‬
12 :‫ هو‬5 ‫مجموع أعداد سلسلة فيبوناتشي حتى العدد‬

You might also like