B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP

C if...else with Examples

The if...else statement is used in C to allow decision making in programs. It enables the program to execute different blocks of code based on whether a condition is true or false. This is useful for tasks like checking user input, validating data, and controlling the flow of a program.

C भाषा में if...else स्टेटमेंट का उपयोग निर्णय लेने के लिए किया जाता है। यह प्रोग्राम को true या false स्थिति के आधार पर अलग-अलग कोड ब्लॉक निष्पादित करने देता है। यह यूजर इनपुट चेक करने, डेटा वैलिडेट करने और प्रोग्राम के प्रवाह को नियंत्रित करने में मदद करता है।

1. Check Even or Odd
#include <stdio.h>

int main() {
  int num = 4;
  if(num % 2 == 0)
    printf("Even");
  else
    printf("Odd");
  return 0;
}

Output: Even

Explanation: Checks if the number is even or odd.

व्याख्या: यह जांचता है कि संख्या सम है या विषम।


2. Positive or Negative
#include <stdio.h>

int main() {
  int num = -5;
  if(num >= 0)
    printf("Positive");
  else
    printf("Negative");
  return 0;
}

Output: Negative

Explanation: Determines if the number is positive or negative.

व्याख्या: यह जांचता है कि संख्या धनात्मक है या ऋणात्मक।


3. Check Voting Eligibility
#include <stdio.h>

int main() {
  int age = 20;
  if(age >= 18)
    printf("Eligible to vote");
  else
    printf("Not eligible to vote");
  return 0;
}

Output: Eligible to vote

Explanation: Checks if age is 18 or more for voting eligibility.

व्याख्या: यह जांचता है कि व्यक्ति मतदान के योग्य है या नहीं।


4. Greater Number
#include <stdio.h>

int main() {
  int a = 10, b = 15;
  if(a > b)
    printf("a is greater");
  else
    printf("b is greater");
  return 0;
}

Output: b is greater

Explanation: Compares two numbers and prints the greater one.

व्याख्या: दो संख्याओं की तुलना करता है और बड़ी संख्या को प्रिंट करता है।


5. Check Leap Year
#include <stdio.h>

int main() {
  int year = 2024;
  if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    printf("Leap year");
  else
    printf("Not a leap year");
  return 0;
}

Output: Leap year

Explanation: Determines if the year is a leap year using leap year rules.

व्याख्या: यह जांचता है कि वर्ष लीप ईयर है या नहीं।


6. Check Divisibility by 5
#include <stdio.h>

int main() {
  int num = 25;
  if(num % 5 == 0)
    printf("Divisible by 5");
  else
    printf("Not divisible by 5");
  return 0;
}

Output: Divisible by 5

Explanation: Checks if the number is divisible by 5.

व्याख्या: यह जांचता है कि संख्या 5 से विभाज्य है या नहीं।


7. Check Character is Vowel
#include <stdio.h>

int main() {
  char ch = 'e';
  if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
    printf("Vowel");
  else
    printf("Consonant");
  return 0;
}

Output: Vowel

Explanation: Checks if the character is a vowel.

व्याख्या: यह जांचता है कि वर्ण स्वर है या व्यंजन।


8. Check for Zero
#include <stdio.h>

int main() {
  int num = 0;
  if(num == 0)
    printf("Zero");
  else
    printf("Non-zero");
  return 0;
}

Output: Zero

Explanation: Checks if the number is zero or non-zero.

व्याख्या: यह जांचता है कि संख्या शून्य है या नहीं।


9. Check Marks Pass or Fail
#include <stdio.h>

int main() {
  int marks = 65;
  if(marks >= 40)
    printf("Pass");
  else
    printf("Fail");
  return 0;
}

Output: Pass

Explanation: Checks if the marks are enough to pass.

व्याख्या: यह जांचता है कि छात्र पास है या फेल।


10. Check Even and Positive
#include <stdio.h>

int main() {
  int num = 6;
  if(num > 0 && num % 2 == 0)
    printf("Positive Even Number");
  else
    printf("Not a Positive Even Number");
  return 0;
}

Output: Positive Even Number

Explanation: Checks if a number is both positive and even.

व्याख्या: यह जाँचता है कि संख्या धनात्मक और सम है या नहीं।


11. Find Max of 3 Numbers
#include <stdio.h>

int main() {
  int a = 5, b = 8, c = 3;
  if(a > b && a > c)
    printf("a is greatest");
  else if(b > c)
    printf("b is greatest");
  else
    printf("c is greatest");
  return 0;
}

Output: b is greatest

Explanation: Compares three numbers to find the greatest one.

व्याख्या: यह तीन संख्याओं में से सबसे बड़ी संख्या को दर्शाता है।


12. Check Uppercase or Lowercase
#include <stdio.h>

int main() {
  char ch = 'G';
  if(ch >= 'A' && ch <= 'Z')
    printf("Uppercase Letter");
  else
    printf("Lowercase Letter");
  return 0;
}

Output: Uppercase Letter

Explanation: Checks if the character is uppercase or lowercase.

व्याख्या: यह अक्षर कैपिटल है या स्मॉल यह जाँचता है।


13. Check Alphabet or Not
#include <stdio.h>

int main() {
  char ch = '@';
  if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
    printf("Alphabet");
  else
    printf("Not an alphabet");
  return 0;
}

Output: Not an alphabet

Explanation: Checks if the character is an alphabet letter.

व्याख्या: यह चेक करता है कि दिया गया कैरेक्टर अक्षर है या नहीं।


14. Check Equal Numbers
#include <stdio.h>

int main() {
  int a = 10, b = 10;
  if(a == b)
    printf("Both numbers are equal");
  else
    printf("Numbers are not equal");
  return 0;
}

Output: Both numbers are equal

Explanation: Compares two values to check if they are equal.

व्याख्या: यह दो संख्याओं की समानता को जाँचता है।


15. Positive, Negative or Zero
#include <stdio.h>

int main() {
  int num = 0;
  if(num > 0)
    printf("Positive");
  else if(num < 0)
    printf("Negative");
  else
    printf("Zero");
  return 0;
}

Output: Zero

Explanation: Checks the sign of the number (positive, negative, or zero).

व्याख्या: यह जाँचता है कि संख्या धनात्मक, ऋणात्मक या शून्य है।