C- Control Statement: if-else ladder Codes

 1.Write a program to find the given number is positive or negative

#include <stdio.h>

int main() {
    int a;
    printf("Enter the number, a: ");
    scanf("%d", &a);
    
    if (a > 0) {
        printf("Positive Number");
    } else {
        printf("Negative Number");
    }
    
    return 0;
}

2. Write a program to find the given number is odd or even

#include <stdio.h>

int main() {
    int a, R;
    printf("Enter the number, a: ");
    scanf("%d", &a);
    
    R = a % 2;
    
    if (R == 0) {
        printf("Even Number");
    } else {
        printf("Odd Number");
    }
    
    return 0;
}

3. Write a program to check whether a first number is divisible by second or not

#include <stdio.h>

int main() {
    int a, b, R;
    printf("Enter the two numbers: ");
    scanf("%d%d", &a, &b);
    
    R = a % b;
    
    if (R == 0) {
        printf("The first number %d is divisible by the second %d", a, b);
    } else {
        printf("The first number %d is not divisible by the second %d", a, b);
    }
    
    return 0;
}

4. Write a program to find the given number is exactly divisible by 3 or not

#include <stdio.h>

int main() {
    int a, R;
    printf("Enter the number, a: ");
    scanf("%d", &a);
    
    R = a % 3;
    
    if (R == 0) {
        printf("Divisible by 3");
    } else {
        printf("Not divisible by 3");
    }
    
    return 0;
}


5. WAP a program to read a number from keyboard and test whether the number is exactly divisible by 3 but not by 11 or not.

#include <stdio.h>

int main() {
    int a;
    printf("Enter the number, a: ");
    scanf("%d", &a);
    
    if (a % 3 == 0 && a % 11 != 0) {
        printf("Divisible by 3 but not divisible by 11");
    } else {
        printf("Not divisible by 3 or divisible by 11");
    }
    
    return 0;
}


6. Write a program to find the greatest among three numbers using nested if… else statement

#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter the numbers: ");
    scanf("%d%d%d", &a, &b, &c);
    
    if (a > c && a > b) {
        printf("Greatest is %d", a);
    } else if (b > a && b > c) {
        printf("Greatest is %d", b);
    } else {
        printf("Greatest is %d", c);
    }
    
    return 0;
}

7. Write a program to find the greatest among three numbers using nested if... else if statement

#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter the numbers: ");
    scanf("%d%d%d", &a, &b, &c);
    
    if (a > b) {
        if (a > c) {
            printf("Greatest number is %d", a);
        } else {
            printf("Greatest number is %d", c);
        }
    } else {
        if (b > c) {
            printf("Greatest number is %d", b);
        } else {
            printf("Greatest number is %d", c);
        }
    }
    
    return 0;
}

8. Write a program to find the greatest among four numbers

#include <stdio.h>

int main() {
    int a, b, c, d;
    printf("Enter the numbers: ");
    scanf("%d%d%d%d", &a, &b, &c, &d);
    
    if (a > b && a > c && a > d) {
        printf("Greatest is %d", a);
    } else if (b > a && b > c && b > d) {
        printf("Greatest is %d", b);
    } else if (c > a && c > b && c > d) {
        printf("Greatest is %d", c);
    } else {
        printf("Greatest is %d", d);
    }
    
    return 0;
}

9. Write a program to find the second largest number among three numbers

#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter the numbers: ");
    scanf("%d%d%d", &a, &b, &c);
    
    if ((a > b && a < c) || (a > c && a < b)) {
        printf("Second largest is %d", a);
    } else if ((b > a && b < c) || (b > c && b < a)) {
        printf("Second largest is %d", b);
    } else {
        printf("Second largest is %d", c);
    }
    
    return 0;
}

10. Write a program to convert uppercase to lowercase and lowercase to uppercase

#include <stdio.h>

int main() {
    char ch;
    printf("Enter the desired character, ch: ");
    scanf("%c", &ch);
    
    if (ch >= 65 && ch <= 90) {
        printf("The character is uppercase\n");
        ch = ch + 32;
        printf("Lowercase character is %c", ch);
    } else if (ch >= 97 && ch <= 122) {
        printf("Character is lowercase\n");
        ch = ch - 32;
        printf("Uppercase character is %c", ch);
    } else {
        printf("Invalid character");
    }
    
    return 0;
}

11. WAP to read a number from the user and whether a given number is prime or composite.

#include <stdio.h>

int main() {
    int n, i = 2;
    printf("Enter the number: ");
    scanf("%d", &n);

    while (i < n) {
        if (n % i == 0) {
            printf("Composite Number");
            break;
        }
        i++;
    }

    if (i == n)
        printf("Prime Number");

    return 0;
}


12. Write a program to find whether the given number is loop year or not.

#include <stdio.h>

int main() {
    int year;
    printf("Enter the year: ");
    scanf("%d", &year);
    
    if (year % 4 == 0 && year % 100 != 0) {
        printf("Leap year");
    } else if (year % 400 == 0) {
        printf("Leap year");
    } else {
        printf("Not a leap year");
    }
    
    return 0;
}

13. Write a program to find all the roots of quadratic equation

#include <stdio.h>
#include <math.h>

int main() {
    float a, b, c, D, R1, R2, R, I;
    printf("Enter the value of a, b, c: ");
    scanf("%f%f%f", &a, &b, &c);
    
    D = b * b - 4 * a * c;
    
    if (D == 0) {
        R1 = -b / (2 * a);
        printf("The roots are real and equal = %f, %f", R1, R1);
    } else if (D > 0) {
        R1 = (-b + sqrt(D)) / (2 * a);
        R2 = (-b - sqrt(D)) / (2 * a);
        printf("The roots are real and unequal = %f, %f", R1, R2);
    } else {
        R = -b / (2 * a);
        I = sqrt(-D) / (2 * a);
        printf("The roots are imaginary = %.2f+%.2fi, %.2f-%.2fi", R, I, R, I);
    }
    
    return 0;
}

14. Write a program to check three sides of a triangle and check the validity of the triangle as well as display if it is scalene, isosceles, equivalent or right-angled triangle.

#include <stdio.h>

int main() {
    int a, b, c;
    printf("Enter the three sides of the triangle: ");
    scanf("%d%d%d", &a, &b, &c);
    
    if (a <= 0 || b <= 0 || c <= 0 || (a + b <= c) || (a + c <= b) || (b + c <= a)) {
        printf("Invalid triangle");
    } else {
        if (a == b && b == c) {
            printf("It is an equilateral triangle.");
        } else if (a == b || b == c || a == c) {
            printf("It is an isosceles triangle.");
        } else {
            printf("It is a scalene triangle.");
        }
        
        if ((a * a + b * b == c * c) || (a * a + c * c == b * b) || (b * b + c * c == a * a)) {
            printf(" The triangle is a right-angled triangle.");
        }
    }
    
    return 0;
}

15. The monthly electricity bill is to calculated as: a) Minimum Rs. 80 for up to 20 units. b) Rs. 7.30 per unit for next 100 units. c) Rs. 9.00 per unit for any units beyond 120 units. Write a program to calculate the monthly bill for a given number of units consumed by a customer.

#include <stdio.h>

int main() {
    float unit, Price;
    printf("Enter the consumed unit: ");
    scanf("%f", &unit);
    
    if (unit <= 20) {
        printf("The price is Rs.80");
    } else if (unit <= 100 && unit > 20) {
        Price = 80 + (unit - 20) * 7.3;
        printf("The price is Rs.%.2f", Price);
    } else {
        Price = 80 + (unit - 20) * 9.0;
        printf("The price is Rs.%.2f", Price);
    }
    
    return 0;
}

16. Write a program to check whether a input character is vowel or consonant

#include <stdio.h>

int main() {
    char x;
    printf("Enter the character: ");
    scanf("%c", &x);
    
    if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u') {
        printf("The entered character is a vowel");
    } else {
        printf("The entered character is a consonant");
    }
    
    return 0;
}

17. Write a program to count the number of digits in a given integer number.

#include <stdio.h>

int main() {
    long long n;
    int count = 0;
    
    printf("Enter the number: ");
    scanf("%lld", &n);
    
    if (n == 0) {
        count = 1;
    } else {
        while (n != 0) {
            n = n / 10;
            count++;
        }
    }
    
    printf("Number of digits are %d\n", count);
    
    return 0;
}

Comments

Popular posts from this blog

C-Codes using Recursion

C-User Defined Function Codes ( 24 Questions Solved)

C-Control Statement Codes (52 Questions Solved)