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

C Program To Check Leap Year Using Ternary Operator

C Program to Find if a given Year is a Leap Year or Not, using Ternary Operator.

Related Read:
Ternary Operator / Conditional Operator In C
C Program To Check Leap Year

General Form of Ternary Operator

(expression_1) ? (expression_2) : (expression_3);

expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.

We are illustrating the use of multiple nested ternary operator in this program. We have a nested ternary operator in place of expression2 as well as expression3.

Leap Year Logic

Leap Year
1. If a year is a century year(years ending with 00) and if it’s perfectly divisible by 400, then it’s a leap year.
2. If the given year is not a century year and it’s perfectly divisible by 4, then it’s a leap year.

Not Leap Year
1. If the year entered is a century year(perfectly divisible by 100), but not perfectly divisible by 400. Then it’s not a leap year.
2. A year which is not a century year and is not perfectly divisible by 4 is not a leap year.

C Program To Check Leap Year Using Nested Ternary Operator


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

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


In this video tutorial we illustrate the use of multiple nested ternary operator / conditional operator inside a ternary operator.

First we check if given year is perfectly divisible by 100. If its true, then the entered year is a century year(year ending with 00). If the year is also perfectly divisible by 400, then its leap year, if not, its not a leap year.

If the user entered year is not a century year, then we check if the year is perfectly divisible by 4. If yes, then its a leap year, if not, then its not a leap year.

Check Leap Year Using Ternary Operator: C Program

 
#include < stdio.h >

int main()
{
    int year;

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

    (year % 100 == 0) ?
    ( (year % 400 == 0)?
      (printf("%d is leap year\n", year)):
      (printf("%d is not leap year\n", year))
    ) :
    ( (year % 4 == 0)?
      (printf("%d is leap year\n", year)):
      (printf("%d is not leap year\n", year))
    );

    return 0;
}

Output 1:
Enter the year
2019
2019 is not leap year!

Output 2:
Enter the year
2004
2004 is leap year!

Output 3:
Enter the year
2024
2024 is leap year!

List of some leap years

2000
2004
2008
2012
2016
2020
2024
2028
2032
2036
2040
2044
2048

You can run the program and input any of the above leap year and check for the output.

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

Number is Positive or Negative or Zero: C Program

C Program to check whether the user entered integer number is positive, negative or zero using else if construct.

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


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

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


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);

    if(a > 0)
    {
        printf("%d is positive\n", a);
    }
    else if(a < 0)
    {
        printf("%d is negative\n", a);
    }
    else
    {
        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

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