If else Statement

  1. While purchasing certain items, a discount of 10% is offered if the quantity purchased is more than 1000. If quantity and price per item are input through the keyboard, write a program to calculate the total expenses.
    #include<stdio.h>
    int main(){
    int qty,dis=0;
    float rate, total;

    printf("Enter quantity and rate:");
    scanf("%d%f",&qty,&rate);

    if(qty>1000){
        dis=10;
        }
        total=(qty*rate)-(qty*rate*dis/100);
        printf("Total Expenses=Rs.%f\n",total);


    return 0;
    }









  2. Write a program, take 1 integer from user and check it is greater than 100 or not in C language.
    #include<stdio.h>
    int main()
    {
        int x;
        printf("Enter an integer: ");
        scanf("%d", &x);
        if(x > 100)
        {
            printf("%d is greater than 100", x);
        }
        else
        {
            printf("%d is smallest than 100", x);
        }
        return 0;
    }





  3. Write a program, take 1 integer from user and check it is even or odd number in C language.
    #include<stdio.h>
    int main()
    {
        int x;
        printf("Enter an integer: ");
        scanf("%d", &x);
        if(x % 2 ==0)
        {
            printf("%d is even number", x);
        }
        else
        {
            printf("%d is odd number", x);
        }
        return 0;
    }






  4. Write a program, take 1 integer from user and check it is between 20 to 50 or not in C language.
    #include<stdio.h>
    int main()
    {
        int a;
        printf("Enter an integer: ");
        scanf("%D", &a);
        if(20<a && a<50)
        {
            printf("The number is between 20 to 50");
        }else
        {
            printf("The number isnot between 20 to 50");
        }
        return 0;
    }






  5. Write a program, take 1 integer from user and check it is 2 digit or not in C language.
    #include<stdio.h>
    int main(){

    int x;
    printf("Enter an integer:");
    scanf("%d",&x);
    if(9<x && x<100){
        printf("%d is 2 digit number.",x);

    }else{
            printf("%d is not 2 digit number.",x);

    }

        return 0;

    }






  6. Write a program take 1 Character from user and check it is @ or not in C language.

    #include<stdio.h>
    int main()
    {
        char a;
        printf("Enter one Character: ");
        scanf("%c", &a);
        if(a=='@')
        {
            printf("It is @ character.");
        }else
        {
            printf("It isnot @ character.");
        }
        return 0;
    }






  7. Write a program take 2 integers from user and find greatest in C language.
    #include<stdio.h>
    int main()
    {
        int a, b;
        printf(" Enter first integer data: ");
        scanf("%d", &a);
        printf(" Enter second integer data: ");
        scanf("%d", &b);
        if(a<b)
        {
            printf("\n %d is the greatest than %d", b, a);
        }
        else
        {
            printf("\n %d is the greatest than %d", a, b);
        }
        return 0;
    }





    Write a program, take 3 integer from user and find second greatest in C language.
  8. #include <stdio.h> int main() { int x, y, z; printf("Enter first integer: "); scanf("%d", &x); printf("Enter second integer: "); scanf("%d", &y); printf("Enter third integer: "); scanf("%d", &z); // Determine the second greatest number if ((x >= y && x <= z) || (x <= y && x >= z)) { printf("The second greatest value is: %d\n", x); } else if ((y >= x && y <= z) || (y <= x && y >= z)) { printf("The second greatest value is: %d\n", y); } else { printf("The second greatest value is: %d\n", z); } return 0; }






  9. The current year and the year in which the employee joined the organization are entered through the keyboard. If the number of years for which the employee has served the organization is greater than 3, then bonus of Rs. 2500/- is given to the employee. If the years of service are not greater than 3, then the program should do nothing.
    #include <stdio.h>

    int main() {
        int currentYear, joiningYear, yearsOfService;

        // Input the current year
        printf("Enter the current year: ");
        scanf("%d", &currentYear);

        // Input the year the employee joined
        printf("Enter the year the employee joined the organization: ");
        scanf("%d", &joiningYear);

        // Calculate the years of service
        yearsOfService = currentYear - joiningYear;

        // Check if the years of service are greater than 3
        if (yearsOfService > 3) {
            // Print the bonus amount
            printf("The employee receives a bonus of Rs. 2500/-\n");
        }
       
        // If the service years are not greater than 3, the program does nothing

        return 0;
    }






  10. ) In a company an employee is paid as under: if his basic salary is less than Rs. 1500 then HRA=10% of basic salary and DA=90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA=Rs.500 and DA=98% of basic Salary. If the employee's salary is input through the keyboard write a program to find his gross salary.
    #include <stdio.h>

    int main() {
        float basicSalary, HRA, DA, grossSalary;

        // Input the basic salary
        printf("Enter the basic salary: ");
        scanf("%f", &basicSalary);

        // Calculate HRA and DA based on the basic salary
        if (basicSalary < 1500) {
            HRA = 0.10 * basicSalary;
            DA = 0.90 * basicSalary;
        } else {
            HRA = 500;
            DA = 0.98 * basicSalary;
        }

        // Calculate the gross salary
        grossSalary = basicSalary + HRA + DA;

        // Print the gross salary
        printf("The gross salary of the employee is: %.2f\n", grossSalary);

        return 0;
    }






  11. If cost price and selling price of an item are input through the keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred.
    #include <stdio.h>

    int main() {
        float costPrice, sellingPrice, profitOrLoss;

        // Input the cost price
        printf("Enter the cost price of the item: ");
        scanf("%f", &costPrice);

        // Input the selling price
        printf("Enter the selling price of the item: ");
        scanf("%f", &sellingPrice);

        // Calculate profit or loss
        if (sellingPrice > costPrice) {
            profitOrLoss = sellingPrice - costPrice;
            printf("The seller made a profit of Rs. %.2f\n", profitOrLoss);
        } else if (sellingPrice < costPrice) {
            profitOrLoss = costPrice - sellingPrice;
            printf("The seller incurred a loss of Rs. %.2f\n", profitOrLoss);
        } else {
            printf("There is no profit or loss.\n");
        }

        return 0;
    }







  12. Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number.
    #include <stdio.h>

    int main() {
        int number;

        // Input the integer
        printf("Enter an integer: ");
        scanf("%d", &number);

        // Check if the number is even or odd
        if (number % 2 == 0) {
            printf("%d is an even number.\n", number);
        } else {
            printf("%d is an odd number.\n", number);
        }

        return 0;
    }






  13. Any year is input through the keyboard. Write a program to determine whether the year is a leap year or not.(Hint: Use the %(modulus) operator)
    #include <stdio.h>

    int main() {
        int year;

        // Input the year
        printf("Enter a year: ");
        scanf("%d", &year);

        // Check if the year is a leap year
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            printf("%d is a leap year.\n", year);
        } else {
            printf("%d is not a leap year.\n", year);
        }

        return 0;
    }






  14. According to the Gregorian calendar, it was Monday on the date 01/01/01. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.
    #include <stdio.h>

    int calculateDayOfWeek(int day, int month, int year) {
        if (month < 3) {
            month += 12;
            year -= 1;
        }
       
        int K = year % 100;
        int J = year / 100;
       
        int f = day + ((13 * (month + 1)) / 5) + K + (K / 4) + (J / 4) - 2 * J;
        int dayOfWeek = (f % 7 + 7) % 7;  // to handle negative values
       
        return dayOfWeek;
    }

    int main() {
        int year;

        // Input the year
        printf("Enter a year: ");
        scanf("%d", &year);

        // Calculate the day of the week for January 1st of the given year
        int dayOfWeek = calculateDayOfWeek(1, 1, year);

        // Print the result
        const char* days[] = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
        printf("The day on 1st January %d is: %s\n", year, days[dayOfWeek]);

        return 0;
    }





  15. A five-digit number is entered through the keyboard. Write a program to obtain the reverse number and to determine whether the original and  reversed number are equal or not.
    #include <stdio.h>

    int main() {
        int number, originalNumber, reversedNumber = 0, remainder;

        // Input the five-digit number
        printf("Enter a five-digit number: ");
        scanf("%d", &number);

        // Store the original number
        originalNumber = number;

        // Reverse the number
        while (number != 0) {
            remainder = number % 10;
            reversedNumber = reversedNumber * 10 + remainder;
            number /= 10;
        }

        // Check if the original number and reversed number are equal
        if (originalNumber == reversedNumber) {
            printf("The original number and reversed number are equal.\n");
        } else {
            printf("The original number and reversed number are not equal.\n");
        }

        return 0;
    }






  16. If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three.
    #include <stdio.h>

    int main() {
        int ageRam, ageShyam, ageAjay;

        // Input the ages of Ram, Shyam, and Ajay
        printf("Enter the age of Ram: ");
        scanf("%d", &ageRam);
        printf("Enter the age of Shyam: ");
        scanf("%d", &ageShyam);
        printf("Enter the age of Ajay: ");
        scanf("%d", &ageAjay);

        // Determine the youngest age
        if (ageRam < ageShyam && ageRam < ageAjay) {
            printf("Ram is the youngest.\n");
        } else if (ageShyam < ageRam && ageShyam < ageAjay) {
            printf("Shyam is the youngest.\n");
        } else if (ageAjay < ageRam && ageAjay < ageShyam) {
            printf("Ajay is the youngest.\n");
        } else {
            printf("There is a tie for the youngest age.\n");
        }

        return 0;
    }






  17. Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered through the keyboard. A triangle is valid if the sum of all the three angles is equal to 180 degree.
    #include <stdio.h>

    int main() {
        int angle1, angle2, angle3, sumOfAngles;

        // Input the three angles of the triangle
        printf("Enter the first angle of the triangle: ");
        scanf("%d", &angle1);
        printf("Enter the second angle of the triangle: ");
        scanf("%d", &angle2);
        printf("Enter the third angle of the triangle: ");
        scanf("%d", &angle3);

        // Calculate the sum of the angles
        sumOfAngles = angle1 + angle2 + angle3;

        // Check if the sum of the angles is equal to 180 degrees
        if (sumOfAngles == 180) {
            printf("The triangle is valid.\n");
        } else {
            printf("The triangle is not valid.\n");
        }

        return 0;
    }





  18. Write a program to find the absolute value of a number entered through the keyboard.
    #include <stdio.h>
    #include <stdlib.h>  // Include this for the abs() function for integers

    int main() {
        float number, absoluteValue;

        // Input the number
        printf("Enter a number: ");
        scanf("%f", &number);

        // Calculate the absolute value
        if (number < 0) {
            absoluteValue = -number;
        } else {
            absoluteValue = number;
        }

        // Print the absolute value
        printf("The absolute value of %.2f is %.2f\n", number, absoluteValue);

        return 0;
    }






  19. Given the length and breadth of a rectangle, write a program to find whether the area of the rectangle is greater than its perimeter. For example, the area of the rectangle with length =5 and breadth =4 is greater than its perimeter.
    #include <stdio.h>

    int main() {
        float length, breadth, area, perimeter;

        // Input the length and breadth of the rectangle
        printf("Enter the length of the rectangle: ");
        scanf("%f", &length);
        printf("Enter the breadth of the rectangle: ");
        scanf("%f", &breadth);

        // Calculate the area and perimeter of the rectangle
        area = length * breadth;
        perimeter = 2 * (length + breadth);

        // Compare the area and perimeter
        if (area > perimeter) {
            printf("The area of the rectangle is greater than its perimeter.\n");
        } else {
            printf("The area of the rectangle is not greater than its perimeter.\n");
        }

        return 0;
    }




  20. Given three point (x1,y1), (x2,y2),(x3,y3), write a program to check if all the three points fall on one straight line.
    #include <stdio.h>

    int main() {
        float x1, y1, x2, y2, x3, y3;
        float slope1, slope2;

        // Input the coordinates of the three points
        printf("Enter the coordinates of the first point (x1, y1): ");
        scanf("%f %f", &x1, &y1);
        printf("Enter the coordinates of the second point (x2, y2): ");
        scanf("%f %f", &x2, &y2);
        printf("Enter the coordinates of the third point (x3, y3): ");
        scanf("%f %f", &x3, &y3);

        // Calculate slopes between pairs of points
        slope1 = (y2 - y1) / (x2 - x1);
        slope2 = (y3 - y2) / (x3 - x2);

        // Check if the slopes are equal (i.e., points lie on the same straight line)
        if (slope1 == slope2) {
            printf("The three points (%.2f, %.2f), (%.2f, %.2f), and (%.2f, %.2f) lie on the same straight line.\n",
                   x1, y1, x2, y2, x3, y3);
        } else {
            printf("The three points (%.2f, %.2f), (%.2f, %.2f), and (%.2f, %.2f) do not lie on the same straight line.\n",
                   x1, y1, x2, y2, x3, y3);
        }

        return 0;
    }





  21. Given the coordinates (x,y) of center of a circle and its radius, write a program that will determine whether a point lies inside the circle on the circle or outside the circle. (Hint; Use sqrt() and pow() functions )
    #include <stdio.h>
    #include <math.h>  // Include this for sqrt() and pow() functions

    int main() {
        float x, y, x1, y1, radius, distance;

        // Input the coordinates of the center of the circle and its radius
        printf("Enter the coordinates of the center of the circle (x, y): ");
        scanf("%f %f", &x, &y);
        printf("Enter the radius of the circle: ");
        scanf("%f", &radius);

        // Input the coordinates of the point
        printf("Enter the coordinates of the point (x1, y1): ");
        scanf("%f %f", &x1, &y1);

        // Calculate the distance between the center of the circle and the given point
        distance = sqrt(pow(x1 - x, 2) + pow(y1 - y, 2));

        // Compare the distance with the radius to determine the position of the point
        if (distance < radius) {
            printf("The point (%.2f, %.2f) lies inside the circle.\n", x1, y1);
        } else if (distance == radius) {
            printf("The point (%.2f, %.2f) lies on the circle.\n", x1, y1);
        } else {
            printf("The point (%.2f, %.2f) lies outside the circle.\n", x1, y1);
        }

        return 0;
    }





  22. Given a point(x,y) write a program to find out if it lies on the X-axis, Y-axis or on the origin.
    #include <stdio.h>

    int main() {
        float x, y;

        // Input the coordinates of the point
        printf("Enter the coordinates of the point (x, y): ");
        scanf("%f %f", &x, &y);

        // Check if the point lies on the X-axis, Y-axis, or origin
        if (x == 0 && y == 0) {
            printf("The point (%.2f, %.2f) lies on the origin.\n", x, y);
        } else if (x == 0) {
            printf("The point (%.2f, %.2f) lies on the Y-axis.\n", x, y);
        } else if (y == 0) {
            printf("The point (%.2f, %.2f) lies on the X-axis.\n", x, y);
        } else {
            printf("The point (%.2f, %.2f) does not lie on the X-axis, Y-axis, or origin.\n", x, y);
        }

        return 0;
    }





  23. "Write a program to accept any number and print the sum of that single digit through recursive function."
    #include <stdio.h>

    int sumOfDigits(int num) {
        // Base case: if the number is less than 10, return the number itself
        if (num < 10) {
            return num;
        }
        // Recursive case: sum the last digit with the sum of the remaining digits
        return num % 10 + sumOfDigits(num / 10);
    }

    int main() {
        int number, sum;

        // Input the number
        printf("Enter a number: ");
        scanf("%d", &number);

        // Calculate the sum of the digits using the recursive function
        sum = sumOfDigits(number);

        // Print the sum of the digits
        printf("The sum of the digits of %d is %d.\n", number, sum);

        return 0;
    }


0 Comments had been done yet:

Post a Comment