Sunday

Chapter 2: The Decision Control Structure

[C] Attempt the following

a) if cost price and selling price of an item is input through the keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit or loss he made or loss he incurred.

Soluion
IDE: CodeBlock
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float cost,price;
    printf("Enter Price:");
    scanf("%f",&price);
    printf("Enter Cost:");
    scanf("%f",&cost);
    if (cost>price)
    {
        printf("He has incurred loss of Rs.%f",cost-price);
    }
    else
    {
        printf("He has made profit of Rs.%f",price-cost);
    }
    getchar();
    return(0);
}

b) Any integer is input through keyboard. Write a program to find out whether it is an odd number or even number.

Solution
IDE: CodeBlock

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int odd_even;
    printf("Enter the number:");
    scanf("%d",&odd_even);
    if (odd_even%2==0) //if the number is divided by 2 and give remainder 0 then it is an even number.
    {
        printf("%d is an even number.",odd_even);
    }
    else
    {
        printf("%d is an odd number.",odd_even);
    }
    getchar();
    return 0;
}

f) If the ages of Ram,Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three.

Solution

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int ram_age,shyam_age,ajay_age;

    printf("Enter the age of Ram:");

    scanf("%d",&ram_age);

    printf("Enter the age of Shyam:");

    scanf("%d",&shyam_age);

    printf("Enter the age of Ajay:");

    scanf("%d",&ajay_age);

    if (ram_age<shyam_age && ram_age<ajay_age)
    {
        printf("Ram is the youngest.");

    }
    else if(shyam_age<ram_age && shyam_age<ajay_age)
    {
        printf("Shyam is the youngest.");
    }
    else if(ajay_age<ram_age && ajay_age<shyam_age)
    {
        printf("Ajay is the youngest.");
    }
    getchar();

    return 0;
}

g) 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 angle is equal to 180 degrees.

Solution

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,c;
    printf("Enter the 1st angle:");
 
    scanf("%d",&a);
 
    printf("Enter the 2nd angle:");
 
    scanf("%d",&b);
 
    printf("Enter the 3rd angle:");
 
    scanf("%d",&c);
 
    if (a+b+c ==180)
    {
        printf("It is a valid triangle.");
    }
    else
    {
        printf("It is not a valid triangle.");
    }
    getchar();
 
    return 0;
}

i) Given the length and bbreadth 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 = 5 is greater than its perimeter.

Solution
FORMULA
Area = Length* Height
Perimeter = 2(length+height)

IDE : CodeBlock

#include <stdio.h>
#include <stdlib.h>

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

    printf("Enter the length:");

    scanf("%d",&length);

    printf("Enter the breadth:");

    scanf("%d",&breadth);

    area = length*breadth;

    perimeter = 2*(length+breadth);

    if (area>perimeter)
    {
        printf("Area is greater than perimeter.");
    }
    else if (area<perimeter)
    {
        printf("Area is less than perimeter.");
    }
    else if (area==perimeter)
    {
        printf("area is equal to perimeter.");
    }

    getchar();

    return 0;
}