Skip to content

Week2: If-Else (Selection Programming) in C

Q1. ASCII Value to Character

Question:
Write a C program to accept an integer value (ASCII) from the user and print its equivalent character. The ASCII values vary from 0 to 255.

Program:

#include <stdio.h>

int main() {
    int asciiValue;

    printf("Enter an ASCII value (0-255): ");
    scanf("%d", &asciiValue);

    if (asciiValue < 0 || asciiValue > 255) {
        printf("Invalid input! Please enter a value between 0 and 255.\n");
    } else {
        printf("The character for ASCII value %d is: '%c'\n", asciiValue, (char)asciiValue);
    }

    return 0;
}
````

---

## Q2. Swap Two Variables

**Question:**
Write a program to swap the values of two variables.

Example:

Before swapping: a=10, b=20 After swapping: a=20, b=10


**Program:**

```c
#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter 2 numbers: ");
    scanf("%d %d", &a, &b);

    printf("Before swapping a = %d and b = %d\n", a, b);

    c = a;
    a = b;
    b = c;

    printf("After swapping a = %d and b = %d\n", a, b);

    return 0;
}

Q3. Determine Quadrant of a Point

Question: Write a C program that takes a point (x, y) and displays the quadrant in which it falls using if-else.

Program:

#include <stdio.h>

int main() {
    float x, y;
    printf("Enter the coordinates (x y): ");
    scanf("%f %f", &x, &y);

    if (x > 0 && y > 0) {
        printf("Quadrant I\n");
    } else if (x < 0 && y > 0) {
        printf("Quadrant II\n");
    } else if (x < 0 && y < 0) {
        printf("Quadrant III\n");
    } else if (x > 0 && y < 0) {
        printf("Quadrant IV\n");
    } else if (x == 0 && y != 0) {
        printf("On Y-axis\n");
    } else if (y == 0 && x != 0) {
        printf("On X-axis\n");
    } else {
        printf("Origin\n");
    }

    return 0;
}

Q4. Student Division Calculation

Question: Write a C program to calculate the division of a student in 5 subjects. Consider the maximum marks in a subject is 100. The division will be decided as below:

Percentage Division
>80 Distinction
>=60 First
>=45 Second
<45 Fail

Program:

#include <stdio.h>

int main() {
    float marks[5];
    float total = 0.0, percentage;

    printf("Enter the marks for 5 subjects (out of 100):\n");
    for (int i = 0; i < 5; i++) {
        scanf("%f", &marks[i]);
        total += marks[i];
    }

    percentage = (total / 500) * 100;

    printf("Total Marks: %.2f\n", total);
    printf("Percentage: %.2f%%\n", percentage);

    if (percentage > 80) {
        printf("Division: Distinction\n");
    } else if (percentage >= 60) {
        printf("Division: First\n");
    } else if (percentage >= 45) {
        printf("Division: Second\n");
    } else {
        printf("Division: Fail\n");
    }

    return 0;
}

Q5. Check Vowel or Consonant

Question: Write a program to check if a character entered by the user is a vowel or a consonant.

Program:

#include <stdio.h>

int main() {
    char ch;

    printf("Enter a character: ");
    scanf("%c", &ch);

    if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
            ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
            printf("%c is a Vowel\n", ch);
        } else {
            printf("%c is a Consonant\n", ch);
        }
    } else {
        printf("%c is not a valid alphabet character\n", ch);
    }

    return 0;
}

Comments