C Program To Find Whether Area of Rectangle Is Greater Than Its Perimeter

Given the length and breadth(or width) of a rectangle, write a C 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.

Related Read:
Area of Rectangle: C Program
Perimeter of Rectangle: C Program

Note:
Rectangle is a quadrilateral(i.e., it has 4 sides). And all the 4 angles of the rectangle are of 90 degree. To calculate area of a rectangle we need its length and width/breadth.

Formula To Calculate Area of a Rectangle

Area = Length * Width;
area of rectangle

Formula To Calculate Perimeter of a Rectangle

Perimeter = 2 * (Length + Width);
perimeter of rectangle

Length: is the length of the longest side of the rectangle.
width: is the length of the smallest side of the rectangle.

Expected Output for the Input

User Input:
Enter length of the Rectangle
5
Enter width of the Rectangle
4

Output:
yes, area(20.00) is greater than its perimeter(18.00)

Video Tutorial: C Program To Find Whether Area of Rectangle Is Greater Than Its Perimeter


[youtube https://www.youtube.com/watch?v=TmnErbGucfk]

YouTube Link: https://www.youtube.com/watch?v=TmnErbGucfk [Watch the Video In Full Screen.]

Source Code: C Program To Find Whether Area of Rectangle Is Greater Than Its Perimeter

#include < stdio.h >

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

    printf("Enter length of the Rectangle\n");
    scanf("%f", &length);

    printf("Enter width of the Rectangle\n");
    scanf("%f", &width);

    area      =     (length * width);
    perimeter = 2 * (length + width);

    if(area > perimeter)
    {
        printf("yes, area(%0.2f) is greater than its perimeter(%0.2f)\n", 
               area, perimeter);
    }
    else
    {
        printf("Area(%0.2f) is not greater than its perimeter(%0.2f)\n", 
               area, perimeter);
    }

    return 0;
}

Output:
Enter length of the Rectangle
12
Enter width of the Rectangle
6
yes, area(72.00) is greater than its perimeter(36.00)

Logic To Find Whether Area of Rectangle Is Greater Than Its Perimeter

We ask the user to enter length and breadth/width of the rectangle and store it inside address of variable length and width. Next we calculate area and perimeter of the rectangle using the user entered value for length and width of the rectangle.

Area = Length * Width;
Perimeter = 2 * (Length + Width);

Next we check if the value present in variable area is greater than the value of perimeter or not, and output the result accordingly to the console window.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Determine Youngest of Three

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

Related Read:
Decision Control Instruction In C: IF
Relational Operators In C

Expected Output for the Input

User Input:
Enter the age of Ram, Shyam and Ajay
20
20
30

Output:
Ram is the youngest
Shyam is the youngest

Video Tutorial: C Program To Determine Youngest of Three


[youtube https://www.youtube.com/watch?v=EPq2lvHXs-k]

YouTube Link: https://www.youtube.com/watch?v=EPq2lvHXs-k [Watch the Video In Full Screen.]

Source Code: C Program To Determine Youngest of Three

#include<stdio.h>

int main()
{
    int ram, shyam, ajay;

    printf("Enter the age of Ram, Shyam and Ajay\n");
    scanf("%d%d%d", &ram, ­­­­&shyam, &ajay);

    if(ram <= shyam && ram <= ajay)
    {
        printf("Ram is the youngest\n");
    }

    if(shyam <= ram && shyam <= ajay)
    {
        printf("Shyam is the youngest\n");
    }

    if(ajay <= ram && ajay <= shyam)
    {
        printf("Ajay is the youngest\n");
    }

    return 0;
}

Output 1:
Enter the age of Ram, Shyam and Ajay
20
30
20
Ram is the youngest
Ajay is the youngest

Output 2:
Enter the age of Ram, Shyam and Ajay
20
30
19
Ajay is the youngest

Logic To Determine Youngest of Three

In this C program we ask the user to enter age for 3 people – Ram, Shyam and Ajay. We store it inside the address of variables ram, shyam and ajay.

We check if Ram’s age is less than or equal to Shyam and Ajay’s age. If true Ram is the youngest person.

We check if Shyam’s age is less than or equal to Ram and Ajay’s age. If true Shyam is the youngest person.

We check if Ajay’s age is less than or equal to Ram and Shyam’s age. If true Ajay is the youngest person.

We write separate if condition for each person. If we take else if clause it gives wrong result when 2 or more people have same age.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Calculate Profit or Loss

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

Related Read:
else if statement in C
Relational Operators In C

Note:
Cost Price: is the price paid by the seller for the product or the manufacturing cost of the product.

Selling Price: is the price seller sold the product for.

Formula To Calculate Profit

profit = (selling_price – cost_price);

Formula To Calculate Loss

loss = (cost_price – selling_price);

Expected Output for the Input

User Input:
Enter the cost price of the product
1500
Enter the selling price of the product
2000

Output:
Your profit is 500

Video Tutorial: C Program To Calculate Profit or Loss


[youtube https://www.youtube.com/watch?v=ZjMNJNxKXuE]

YouTube Link: https://www.youtube.com/watch?v=ZjMNJNxKXuE [Watch the Video In Full Screen.]

Source Code: C Program To Calculate Profit or Loss

#include < stdio.h >

int main()
{
    int cp, sp;

    printf("Enter the cost price of the product\n");
    scanf("%d", &cp);

    printf("Enter the selling price of the product\n");
    scanf("%d", &sp);

    if(sp > cp)
    {
        printf("Your profit is %d\n", (sp-cp));
    }
    else if(cp > sp)
    {
        printf("Loss Incurred is %d\n", (cp-sp));
    }
    else
    {
        printf("Neither profit, nor loss\n");
    }

    return 0;
}

Output 1:
Enter the cost price of the product
2000
Enter the selling price of the product
2500
Your profit is 500

Output 2:
Enter the cost price of the product
2000
Enter the selling price of the product
1500
Loss Incurred is 500

Output 3:
Enter the cost price of the product
1500
Enter the selling price of the product
1500
Neither profit, nor loss

Logic To Calculate Profit or Loss

We ask the user to enter cost price as well as selling price. If selling price is more than cost price, then the seller is in profit. If the cost price is more than selling price, then the seller incurred loss.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Count Positive, Negative and Zero using Ternary Operator and without using Array

Lets write a C program to enter number till the user wants. At the end it should display the count of positive number, negative number and zeros entered, using Ternary Operator or Conditional Operator and without using arrays.

Related Read:
while loop in C programming
Positive or Negative or Zero Using Ternary Operator: C Program

Note:
Any number greater than 0 is positive.
Any number less than 0 is negative.

scale

Expected Output for the Input

User Input:
Enter the limit
6
Enter 6 numbers
-1
0
2
-3
5
6

Output:
Positive Numbers: 3
Negative Numbers: 2
Number of zero: 1

Logic To Count Positive, Negative and Zero using Ternary Operator and without using Array

Complete logic to this program is present at C Program To Count Positive, Negative and Zero without using Array

Video Tutorial: C Program To Count Positive, Negative and Zero using Ternary Operator and without using Array


[youtube https://www.youtube.com/watch?v=OMGSxVCav24]

YouTube Link: https://www.youtube.com/watch?v=OMGSxVCav24 [Watch the Video In Full Screen.]

Source Code: C Program to Count Positive, Negative and Zero using Ternary Operator and without using Array

#include < stdio.h >

int main()
{
    int limit, num, positive = 0, negative = 0, zero = 0;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d numbers\n", limit);

    while(limit)
    {
        scanf("%d", &num);
        (num > 0) ? positive++ : ((num < 0) ? negative++ : zero ++);
        limit--;
    }

    printf("\nPositive Numbers: %d\n", positive);
    printf("Negative Numbers: %d\n", negative);
    printf("Number of zero: %d\n", zero);

    return 0;
}

Output:
Enter the limit
5
Enter 5 numbers
2
-1
5
6
0

Positive Numbers: 3
Negative Numbers: 1
Number of zero: 1

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Count Positive, Negative and Zero without using Array

Lets write a C program to enter number till the user wants. At the end it should display the count of positive number, negative number and zeros entered, without using arrays.

Related Read:
while loop in C programming
Number is Positive or Negative or Zero: C Program

Note:
Any number greater than 0 is positive.
Any number less than 0 is negative.

scale

Expected Output for the Input

User Input:
Enter the limit
5
Enter 5 numbers
0
5
3
2
-1

Output:
Positive Numbers: 3
Negative Numbers: 1
Number of zero: 1

Video Tutorial: C Program to Count Positive, Negative and Zero without using Array


[youtube https://www.youtube.com/watch?v=SuvrPe4Bg2c]

YouTube Link: https://www.youtube.com/watch?v=SuvrPe4Bg2c [Watch the Video In Full Screen.]

Source Code: C Program to Count Positive, Negative and Zero without using Array

#include < stdio.h >

int main()
{
    int limit, num, positive = 0, negative = 0, zero = 0;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d numbers\n", limit);

    while(limit)
    {
        scanf("%d", &num);

        if(num > 0)
        {
            positive++;
        }
        else if(num < 0)
        {
            negative++;
        }
        else
        {
            zero++;
        }

        limit--;
    }

    printf("\nPositive Numbers: %d\n", positive);
    printf("Negative Numbers: %d\n", negative);
    printf("Number of zero: %d\n", zero);

    return 0;
}

Output 1:
Enter the limit
8
Enter 8 numbers
1
-5
0
6
-2
0
5
4

Positive Numbers: 4
Negative Numbers: 2
Number of zero: 2

Output 2:
Enter the limit
10
Enter 10 numbers
2
6
9
3
-5
-7
0
9
-10
-50

Positive Numbers: 5
Negative Numbers: 4
Number of zero: 1

Logic To Count Positive, Negative and Zero without using Array

We ask the user to enter the maximum numbers he or she wants to enter, and store it inside variable limit. Now we iterate the while loop limit number of times. Inside the while loop, for each iteration, until limit is equal to zero, we keep asking the user to enter a number. Once the user enters the number we check if its greater than 0, if its true we’ll increment the value of variable positive by one. If the user entered number is less than 0, then we increment the value of variable negative by one. If the user entered number is neither greater than 0 nor less than 0, then its a zero – so we increment the value of variable zero by one.

For each iteration of the while loop, we decrement the value of variable limit by one. Once the value of variable limit is 0, control exits the while loop and the result will be printed. That is, the number of positives, negatives and the zeros entered by the user.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert