Q50: Write a C++ program to print the sequence of numbers (95-105), except number 101. The code should has continue statement.
اذا طلبه :
use do while loop
#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++ يستخدم وظيفة لتبديل قيمتين صحيحتين، ويستخدم
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/
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.
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.
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
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
تغيير قيمة المعاملات باستخدام البوينتر :
#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;
}
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
اظهار عنصر معين من المصفوفة باستخدام البوينتر :
#include <iostream>
using namespace std;
int main() {
int arr[] = {5, 7, 9};
cout << *(arr+2) << endl;
}
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
طباعة مصفوفة باستخدام البوينتر :
#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;
}
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
سواب بالبوينتر :
#include <iostream>
using namespace std;
void first(int *a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int m = 5, n = 3;
first(&m, &n);
cout << m << ", "<< n <<endl;
return 0;
}
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
سواب بالرفرنس :
#include <iostream>
using namespace std;
void first(int &a, int &b){
int temp;
temp = a;
a = b;
b = temp;
}
int main()
{
int m = 5, n = 3;
first(m,n);
cout << m << ", "<< n <<endl;
return 0;
}
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
سواب بالستركت :
#include <iostream>
using namespace std;
struct numb {
int a, b;
};
void swapp(numb &num) {
int temp;
temp = num.a;
num.a = num.b;
num.b = temp;
}
int main() {
numb num ={9 ,5};
swapp(num);
cout << num.a << ", " << num.b << endl;
}
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
ارسال قيم للفنكشن باستخدام Array و بوينتر :
#include <iostream>
using namespace std;
int* Arr(int rar[3]) {
for (int i = 0; i < 3; ++i) {
cout << rar[i] <<", ";
}
cout<<endl;
return rar;
}
int main() {
int A[3] = {5, 3, 8};
int* rar = Arr(A);
return 0;
}
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
declare a struct called personweight with a member variables name and weight. the struct should have an implementation to print a message " person-name you are overweight " if the person's weight is over 75kg, otherwise print a message " person-name you are fit ".
#include <iostream>
using namespace std;
struct personweight {
string name;
float weight;
void printStatus() {
if (weight > 75) {
cout << name << " you are overweight" << endl;
} else {
cout << name << " you are fit" << endl;
}
}
};
int main() {
personweight person1;
person1.name = "John";
person1.weight = 80;
personweight person2;
person2.name = "Alice";
person2.weight = 70;
person1.printStatus();
person2.printStatus();
return 0;
}
❤3👍1
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
تكدر تستبدل التعريف الي بالmain بهذا
personweight person1 = {"Alice", 70};
personweight person2 = {"John", 80};❤2
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
سؤال مختبر الهياكل-OOP-
الفكرة مهمة جداً حل وفهم قبل المحاضرة القادمة - الإثنين -
تحياتي 🌹
الفكرة مهمة جداً حل وفهم قبل المحاضرة القادمة - الإثنين -
تحياتي 🌹
❤1
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
تجمع بؤساء الهندسة²⁴⁻²³
سؤال مختبر الهياكل-OOP- الفكرة مهمة جداً حل وفهم قبل المحاضرة القادمة - الإثنين - تحياتي 🌹
الكود مع شرح بسيط👇
❤1
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
#include <iostream>
using namespace std;
class bank {
public :
string AccountHolder;
int AccountNumber;
double balance;
void deposit (double amount) {
balance += amount;
}
void withDrawal(double amount) {
if (balance > amount ){
balance -= amount;
}
else {
cout << "balance < amount "<<endl;
}
}
void display (){
cout << "AccountHolder : " << AccountHolder << ", AccountNumber : " << AccountNumber << ", balance : " << balance << endl;
}
};
int main()
{
bank s1 = {"am0m", 3456, 360.50};
s1.deposit(50.9);
s1.withDrawal(33.6);
s1.display();
return 0;
}
❤3
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
هنا اول شيء فتحنا كلاس بأسم bank
و سويناه public
public :
لان اي شيء بداخل الكلاس يعتبر شيء خاص ممنوع الوصول له ف نكتب public حتى نكدر نستخرج هاي المتغيرات بالmain براحتنا و نستدعيها
و كتبنه المتغيرات الي رادهن ثلاث متغيرات
اسم الحساب AccountHolder ( نوع سترنغ )
رقم الحساب AccountNumber ( نوع انتجر )
الفلوس الي بالحساب balance ( نوع دوبل )
و سويناه public
public :
لان اي شيء بداخل الكلاس يعتبر شيء خاص ممنوع الوصول له ف نكتب public حتى نكدر نستخرج هاي المتغيرات بالmain براحتنا و نستدعيها
و كتبنه المتغيرات الي رادهن ثلاث متغيرات
اسم الحساب AccountHolder ( نوع سترنغ )
رقم الحساب AccountNumber ( نوع انتجر )
الفلوس الي بالحساب balance ( نوع دوبل )
❤1
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
بعدين سوينا فنكشن
طلب من عدنا ثلاث فكشنات
الأولى deposit وهاي شغلها ايداع الشخص فلوسه بالبنك
الدكتور كال اريد الكود بسيط ما نحتاج فكرة cin
ف نحط بداخل اقواس الvoid متغير من نفس نوع متغير balance وهو متغير double لان قيمة بيها بوينتات
هنا تصير عملية جمع و نعوفها ما نسوي cout بداخلها بحسب مطلوب السؤال
طلب من عدنا ثلاث فكشنات
الأولى deposit وهاي شغلها ايداع الشخص فلوسه بالبنك
الدكتور كال اريد الكود بسيط ما نحتاج فكرة cin
ف نحط بداخل اقواس الvoid متغير من نفس نوع متغير balance وهو متغير double لان قيمة بيها بوينتات
هنا تصير عملية جمع و نعوفها ما نسوي cout بداخلها بحسب مطلوب السؤال
❤1
Forwarded from تجمع بؤساء الهندسة²⁴⁻²³
الثانية withDrawal
هاي شغلها سحب الشخص فلوسه
نفس الأولى همين نعرف متغير اسمه amount و نوعه double بداخل الvoid و نسوي عمليه طرح ( تنقيص )
بس هنا حط if condition يعني اذا جانت الفلوس الي تريد تسحبها اكثر من فلوسك ما راح تكدر تسحب
لازم اقل من فلوسك و بحسب ما موضح بصياغة الكود
و ايضا بدون cout و نقفلها
هاي شغلها سحب الشخص فلوسه
نفس الأولى همين نعرف متغير اسمه amount و نوعه double بداخل الvoid و نسوي عمليه طرح ( تنقيص )
بس هنا حط if condition يعني اذا جانت الفلوس الي تريد تسحبها اكثر من فلوسك ما راح تكدر تسحب
لازم اقل من فلوسك و بحسب ما موضح بصياغة الكود
و ايضا بدون cout و نقفلها
❤1