امثلة على استخدام IF-ELSE في ++C


برنامج يطلب من المستخدم ادخال علامته ومن ثم يقوم بطباعة النتيجة اما ناجح او راسب

#include 

using namespace std;

int main()
{
  //write a program asks student to enter his mark, then checks if student passed of failed and print result
  int mark;
  cout<<"enter your mark:";
  cin>>mark;
  if(mark>=60)
  cout<<"pass";
  else cout<<"fail";
  
return 0;
}

 

نفس البرنامج السابق لكن مع التحقق من ان العلامة المدخلة بين 0 و 100 حصرا

#include 

using namespace std;

int main()
{
//same program but more specfic conditions:
  int mark;
  cout<<"enter your mark:";
  cin>>mark;
  if(mark>100 || mark<0)//2 conditions in one if
  cout<<"invalid entry";
  else if(mark>=60)
  cout<<"pass";
  else cout<<"fail";
return 0;
}

 

برنامج يطلب من الطالب ادخال علامته ومن ثم يطبع الدرجة الموافقة للعلامة:

#include 

using namespace std;

int main()
{
  int mark;
  cout<<"enter your mark:";
  cin>>mark;
  if(mark>100 || mark<0)//2 conditions in one if
  cout<<"invalid entry";
  
  else if(mark>=90)
  cout<<"A";
  else if(mark>=80)
  cout<<"B";
  else if(mark>=70)
  cout<<"C";
  else if(mark>=60)
  cout<<"D";
  else cout<<"F";
  return 0;
}

 

 write java program that ask user to enter the number of week day and prints the equiavalent week day name

 برنامج يطلب من المستخدم ادخال رقم اليوم ومن ثم يقوم بطباعة اسم اليوم الموافق بالاسبوع

#include 

using namespace std;

int main()
{
 c+int daynumber; 
 cout<<"enter the day number:";
 cin>>daynumber;
 if(daynumber==1)
 cout<<"saturday";
 else if(daynumber==2)
 cout<<"sunday";
 else if(daynumber==3)
 cout<<"monday";
 else if(daynumber==4)
 cout<<"tuesday";
 else if(daynumber==5)
 cout<<"wednsday";
 else if(daynumber==6)
 cout<<"tuesday";
 else if(daynumber==7)
 cout<<"thursday";
 if(daynumber>7 || daynumber<=0)
  cout<<"ERROR";
return 0;
}

 

كود يطبع العدد الاكبر بين عددين :

int x = 20;
int y = 18;
if (x > y) {
  cout << "x is greater than y";
}

 

كود مقارنة عددين مع استخدام Else :

int time = 20;
if (time < 18) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}
// Outputs "Good evening."

 

نفس المثال السابق لكن بطريقة اخرى :

int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
cout << result;

 

نفس المثال السابق لكن باستخدام Else If :

int time = 22;
if (time < 10) {
  cout << "Good morning.";
} else if (time < 20) {
  cout << "Good day.";
} else {
  cout << "Good evening.";
}
// Outputs "Good evening."

  

برنامج معرفة ان كان العدد زوجي ام فردي :

int n;

    cout << "Enter an integer: ";
    cin >> n;

    if ( n % 2 == 0)
        cout << n << " is even.";
    else
        cout << n << " is odd.";

المرفقات:
هل كان المحتوى مفيد؟

التعليقات

لاضافة سؤال أو تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!

امثلة عن المتغيرات VARIABLES في ++C
امثلة عن WHILE LOOP في ++C
محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...