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.

Monday, 26 October 2015

C LANGUAGE PROGRAM TO DECIDE EITHER NUMBER IS LESSER OR GREATER

WRITE A PROGRAM THAT TAKES TWO DIFFERENT NUMBERS AS INPUT FROM USER, AND DECIDE WHICH NUMBER IS LESSER, GREATER OR EQUAL TO OTHER

#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 number1, number2;                                    // Variable declaration
            printf("Enter first number = ");                       // Display prompt for first number
            scanf("%d",&number1);                                  // Take first input from user
            printf("Enter second number = ");                  // Display prompt for first number
            scanf("%d",&number2);                                  // Take second input from user
            if (number1 > number2)                                  // Check number 1 is greater than number 2
                        printf("%d is greater than %d",number1,number2);
            else if (number1 < number2)                           // Check number 1 is less than number 2
                        printf("%d is less than %d",number1,number2);
            else                                                                  // Otherwise the numbers will be equal
                        printf("%d is equal to %d",number1, number2);

getch();
}

OUTPUT

Enter first number = 4
Enter second number = 10
4 is less than 10

Enter first number = 35
Enter second number = 23
35 is greater than 23

Enter first number = 20
Enter second number = 20
20 is equal to 20

EXPLANATION

Multiple If-Else is used and three different conditions are applied in this program. First condition is checking for the greater number, second condition is checking for the lesser number and third condition is used to check the equality of numbers.


Program will test the condition one by one and when the condition is tested successfully the corresponding output will be displayed and program jumps out of the if-else structure, and the program will be end.

Read More

1. What is File Path?
2. How to open hidden files directly?
3. How to rename multiple files in one second?

C LANGUAGE PROGRAM TO DECIDE EITHER NUMBER IS ODD OR EVEN

WRITE A PROGRAM THAT TAKES INPUT FROM USER, AND DECIDE EITHER THIS NUMBER IS EVEN OR ODD

#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 number, remainder;                        // Variable declaration
            printf(“Enter any number = “);           // Display prompt for number
            scanf(“%d”,&number);                       // Take input from user
           
            remainder = number % 2;                    // Divide the number with 2, and calculate the remainder
            if (remainder == 0)                              // Decide if remainder is zero its mean it is an even number
                        printf(“This number is even”);           
            else                                                      // otherwise if the remainder is 1, it is an odd number
                        printf(“This number is odd”);

getch();
}

OUTPUT

Enter any number = 24
The number is even

Enter any number = 57
The number is odd

EXPLANATION

The logic I have used in this program is that I did not divide the number by 2, but I have taken the remainder of the input number. You know that even numbers are fully divided by 2 and the remainder becomes zero (0), and if the number is odd the remainder becomes always 1.


This remainder is then used to take decision that when the remainder will be zero, it is even number and if the remainder is 1 the number will be odd. 

C LANGUAGE PROGRAM TO MULTIPLY TWO NUMBERS

WRITE A PROGRAM THAT TAKES TWO DIFFERENT INPUTS FROM USER, MULTIPLY THEM AND FINALLY DISPLAY THE RESULT IN OUTPUT SCREEN


#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, product;                                  // Variable declaration
            printf(“Enter first number = “);           // Display prompt for first number
            scanf(“%d”,&a);                                 // Take first input from user
            printf(“Enter second number = “);      // Display prompt for first number
            scanf(“%d”,&b);                                 // Take second input from user
           
            product = a * b;                                               // Formula for multiplying two numbers
            printf(“The product = %d“, product);                        // Display output on screen
           
getch();                                                //Get character
}

OUTPUT

Enter first number = 15
Enter second number = 6
The product = 90

EXPLANATION

1. I have used clrscr() function to clear the output screen, then I have declared three variables “a” and “b” to take input and “product” to store the multiplication result.

2. printf() function is used to display text on output screen, and scanf() function is used to take input into the variables.

3. Then I have used formula product = a * b; to multiply both numbers. After multiplying both numbers I have displayed the result using printf() function.

4. In the end of the program I have used getch() function, this function have no use in the program, I just use it to stop the output screen for a while so that user can see the output.