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

  • 2020-10-10

توصيف

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

#include

main()
{
  int a = 15, b = 20;

  if (b & gt; a) {  
    printf("b is greater");
  }
}

 

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

main()
{
  int a, b;

  printf("Please enter the value for a:");
  scanf("%d", & amp; a);

  printf("\nPlease enter the value for b:");
  scanf("%d", & amp; b);

  if (a & gt; b) {  
    printf("\n a is greater");
  } else {  
    printf("\n b is greater");
  }
}

 

كود ايجاد القيمة المطلقة لعدد مدخل من الكيبورد :

#include

main()
{
  int number;
  printf( & quot; Type a number: & quot;);
  scanf( & quot; % d & quot;, & amp; number);

  /* check whether the number is negative number */  if (number & lt; 0) {
    /* If it is a negative then convert it into positive. */   
    number = -number;  
    printf( & quot; The absolute value is % d\ n & quot;, number);
  }
  grtch();
} 

 

كود عن Nested if-else :

#include

main()
{
int x=20,y=30;

    if(x==20)
    {
        if(y==30)
        {
            printf("value of x is 20, and value of y is 30.");
        }
    }
}

 

اختبار هل عدد معين زوجي ام فردي :

#include 
#include 
 
int main(int argc, char *argv[])
{
  
  int number;
 
    printf("Enter a nuber to check even or odd");
    scanf("%d", &number);
 
    // returns true if number is divisible by 2: even
    if(number % 2 == 0)
        printf("%d is even.", number);
    else
        printf("%d is odd.", number);
 
getch();
 
}

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

التعليقات

لاضافة سؤال أو تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول
محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...