Saturday, 12 December 2015

C Language Program To Decide The Year is A Leap Year Or Not?

WRITE A PROGRAM TO DECIDE THE YEAR IS A LEAP YEAR OR NOT?


#include<stdio.h>       // Header file  “Standard Input and Output”
#include<conio.h>      //Header file “Console input and output”
void main ()
{
            clrscr();                                                // Clear the output screen
            int year;
printf(“Please enter any year ”);
scanf(“%d”,&year);

if(year%4 == 0)                                   // Divide the year by 4 and decide either it is leap or not
            printf(“\nThis year is a leap year”);
else
            printf(“\nThis year is not a leap year”);

getch();
}

OUTPUT
Please enter any year 2012
This year is a leap year

Please enter any year 1997
This year is not a leap year

EXPLANATION

Simple formula is used in this program. Leap years are always fully divided by 4. So when the user input any year, the program divided that input by 4 and if the remainder is “0” its mean the year is leap year otherwise it is not a leap year.

Program That Swap (Exchange) The Value Of Two Variables.

WRITE A PROGRAM THAT SWAP (EXCHANGE) THE VALUE OF TWO VARIABLES.

#include<stdio.h>       // Header file  “Standard Input and Output”
#include<conio.h>      //Header file “Console input and output”
void main ()
{
            clrscr();                                                // Clear the output screen
            int a, b, c;
            a = 15;
b = 25;

c = a;                           // The value of a copied in c
a = b;                           // the value of a is exchanged by b
b = c;                           // and value of b is exchanged by c
printf(“After swapping the values of a and b is %d, %d“, a, b);

getch();
}

OUTPUT
After swapping the values of a and b is 25 15

EXPLANATION
Initially we have taken two variables “a” and “b” and gave initial values 15 and 25 respectively. In the second step we have swapped (exchange) the values of variable “a” and “b” by using another variable “c”. For swapping values of any two variables we always use an extra variable.

C Language Program That Convert Temperature From Fahrenheit To Centigrade

WRITE A PROGRAM THAT CONVERT TEMPERATURE FROM FAHRENHEIT TO CENTIGRADE.


#include<stdio.h>       // Header file  “Standard Input and Output”
#include<conio.h>      //Header file “Console input and output”
void main ()
{
            clrscr();                                                            // Clear the output screen
            float temperature;                                            // Variable declaration
           
printf("Enter the temperature in Fahrenheit = ");
scanf("%f", &temperature);

temperature = 5*(temperature - 32)/9;            // Formula: 5/9*(temperature – 32)
printf("\nThe temperature in centigrade is = %f",temperature);

getch();
}

OUTPUT

Enter the temperature in Fahrenheit = 100
The temperature in centigrade is = 37.78

Enter the temperature in Fahrenheit = 58
The temperature in centigrade is = 14.44

EXPLANATION


Simple conversion formula is used in this program. The exact formula is written in the comments.

C Language Program That Convert Temperature From Centigrade To Fahrenheit

WRITE A PROGRAM THAT CONVERT TEMPERATURE FROM CENTIGRADE TO FAHRENHEIT.

#include<stdio.h>       // Header file  “Standard Input and Output”
#include<conio.h>      //Header file “Console input and output”
void main ()
{
            clrscr();                                                            // Clear the output screen
            float temperature;                                            // Variable declaration
           
printf("Enter the temperature in centigrade =  ");
scanf("%f", &temperature);

temperature = (9*temperature)/5 + 32;           // Formula: 9/5 * temperature + 32
printf("\nThe temperature in Fahrenheit is =  %f",temperature);

getch();
}

OUTPUT

Enter the temperature in centigrade = 32
The temperature is Fahrenheit is = 89.6

Enter the temperature in centigrade = 100
The temperature is Fahrenheit is = 212

EXPLANATION

Simple conversion formula is used in this program. The exact formula is written in the comments.