Perimeter of Rectangle: C Program

The length and breadth / width of a rectangle are entered through the keyboard. Write a program to calculate the perimeter of the rectangle.

To get perimeter of a rectangle, we add the length and width of rectangle and then multiply it with 2. In other words, perimeter is the addition of length of all the sides of a rectangle.

Related Read:
Basic Arithmetic Operations In C
Area of Rectangle: C Program

Note: Perimeter of a Rectangle is calculated using the formula
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.

Find Perimeter of a Rectangle: C Program


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

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


Source Code: Find Perimeter of a Rectangle: C Program

#include < stdio.h >

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

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

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

    perimeter = 2 * (length + width);

    printf("Perimeter of Rectangle is %f\n", perimeter);

    return 0;
}

Output:
Enter length of Rectangle
12
Enter width of Rectangle
6
Perimeter of Rectangle is 36.000000

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

Convert Fahrenheit To Degree Celsius: C Program

Temperature of a city in Fahrenheit degree is input through the keyboard. Write a program to convert this temperature into Degree Celsius / Centigrade.

Related Read:
Basic Arithmetic Operations In C
Convert Degree Celsius To Fahrenheit: C Program

Formula to Convert Fahrenheit To Celsius.

From our previous video tutorial Convert Degree Celsius To Fahrenheit: C Program we already know the formula to convert temperature from Celsius to Fahrenheit. We will use the same formula and convert it to calculate Degree celsius from Fahrenheit.

Fahrenheit = (Centigrade * 1.8) + 32;
(Fahrenheit – 31) = (Centigrade * 1.8);
(Fahrenheit – 31) / 1.8 = (Centigrade);

So we’ll use this formula Centigrade = (Fahrenheit – 31) / 1.8; to calculate Degree Centigrade by getting Fahrenheit value from the user.

Convert Fahrenheit To Degree Celsius: C Program


[youtube https://www.youtube.com/watch?v=jBR9hco2O-w]

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


Source Code: Convert Fahrenheit To Degree Celsius: C Program

#include < stdio.h >

int main()
{
    float c, fh;

    printf("Enter temperature in Fahrenheit\n");
    scanf("%f", &fh);

    c = (fh - 32) / 1.8;

    printf("Temperature in Degree Celius is %f\n", c);

    return 0;
}

Output 1:
Enter temperature in Fahrenheit
212
Temperature in Degree Celius is 100.000000

Output 2:
Enter temperature in Fahrenheit
113
Temperature in Degree Celius is 45.000000

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

Convert Degree Celsius To Fahrenheit: C Program

Temperature of a city in Degree Celsius / Centigrade degree is input through the keyboard. Write a program to convert this temperature into Fahrenheit.

Related Read:
Basic Arithmetic Operations In C
Division of 2 Numbers: C
Convert Fahrenheit To Degree Celsius: C Program

Formula to Convert Celsius To Fahrenheit

Fahrenheit = (Centigrade * (9/5)) + 32;
But in C programming, any number divided by an integer number will return integer value. So 9/5 will give 1 and not 1.8

So we need to be careful while doing division operation in C. To solve this issue we can write following formula in C program to convert degree celsius to Fahrenheit.

Fahrenheit = (Centigrade * (9/5.0)) + 32;
OR
Fahrenheit = (Centigrade * 1.8) + 32;

Convert Degree Celsius To Fahrenheit: C Program


[youtube https://www.youtube.com/watch?v=UFtA-OVypHs]

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


Source Code: Convert Degree Celsius To Fahrenheit: C Program

#include < stdio.h >

int main()
{
    float c, fh;

    printf("Enter temperature in Centigrade\n");
    scanf("%f", &c);

    fh = (c * 1.8) + 32;

    printf("Temperature in Fahrenheit is %f\n", fh);

    return 0;
}

Output 1:
Enter temperature in Centigrade
100
Temperature in Fahrenheit is 212.000000

Output 2:
Enter temperature in Centigrade
45
Temperature in Fahrenheit is 113.000000

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

DA, HRA, Gross Salary: C Program

Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.

Related Read:
C Program to Calculate Gross Salary of an Employee

Basic Salary, DA, HRA, Gross Salary: C Program


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

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


Source Code: Basic Salary, DA, HRA, Gross Salary: C Program

#include < stdio.h >

int main()
{
    float bs, hra, da, gs;

    printf("Enter basic salary\n");
    scanf("%f", &bs);

    hra = bs * (20/100.00);
    da  = bs * (40/100.00);

    gs  = bs + hra + da;

    printf("Gross Salary = %f\n", gs);

    return 0;
}

Output 1:
Enter basic salary
10000
Gross Salary = 16000.000000

Output 2:
Enter basic salary
20000
Gross Salary = 32000.000000

#include < stdio.h >

int main()
{
    float bs, hra, da, gs;

    printf("Enter basic salary\n");
    scanf("%f", &bs);

    hra = bs * (0.2);
    da  = bs * (0.4);

    gs  = bs + hra + da;

    printf("Gross Salary = %f\n", gs);

    return 0;
}

Output 1:
Enter basic salary
10000
Gross Salary = 16000.000000

Output 2:
Enter basic salary
20000
Gross Salary = 32000.000000

Wrong division, while calculating HRA and DA

#include < stdio.h >

int main()
{
    float bs, hra, da, gs;

    printf("Enter basic salary\n");
    scanf("%f", &bs);

    hra = bs * (20/100);
    da  = bs * (40/100);

    gs  = bs + hra + da;

    printf("Gross Salary = %f\n", gs);

    return 0;
}

Output 1:
Enter basic salary
10000
Gross Salary = 10000.000000

Output 2:
Enter basic salary
20000
Gross Salary = 20000.000000

You can see that the answer is wrong. This is because we are dividing 20 and 40 by an integer number i.e., 100, so it returns only the integer part of the result which is 0. So 0 multiplied by anything will be 0. So HRA and DA will be 0. So basic salary and gross salary will be shown equal in that case, which is wrong result.

Formula to calculate Gross Salary is:
Gross Salary = Basic Salary + Dearness allowance + House Rent Allowance.

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

Positive or Negative or Zero Using Ternary Operator: C Program

C Program to check whether the user entered integer number is positive, negative or zero using ternary operator or Conditional operator.

Related Read:
Number is Positive or Negative or Zero: C Program

Check Whether Number Is Positive or Negative or Zero Using Ternary Operator: C Program


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

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


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

Check Whether Number is Positive or Negative or Zero: C Program

#include < stdio.h >

int main()
{
    int a;

    printf("Enter an integer number\n");
    scanf("%d", &a);

    (a > 0) ?
    (printf("%d is positive\n", a)) :
    ( (a < 0) ?
      (printf("%d is Negative\n", a)) :
      (printf("%d is Zero\n", a))  
    );

    return 0;
}

Output 1:
Enter an integer number
15
15 is positive

Output 2:
Enter an integer number
-2
-2 is negative

Output 3:
Enter an integer number
0
0 is zero

In above C source code, we are using nested ternary / conditional operator. First we check if a is greater than 0, if its true then the user entered number is positive. If its false, then we check if a is less than 0 using nested ternary / conditional operator. If that is true, then a is negative, else the user entered number is 0.

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