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.


C LANGUAGE PROGRAM TO ADD TWO NUMBERS

WRITE A PROGRAM THAT TAKES TWO DIFFERENT INPUTS FROM USER, ADD THEM AND FINALLY DISPLAY THE RESULT ON 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, sum;                                        // 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
           
            sum = a + b;                                        // Formula for adding two numbers
            printf(“The sum = %d“, sum);            // Display output on screen
           
getch();                                                //Get character
}

OUTPUT

Enter first number = 10
Enter second number = 20
The sum = 30

EXPLANATION

1. I have used clrscr() function to clear the output screen, then I have declared two variables “a” and “b” to take input and another variable “sum” to store the 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 sum = a + b; to add both numbers. After adding 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.


Sunday 25 October 2015

WRITING PROGRAMS IN GW BASIC

Open GW BASIC editor by double clicking on the file.



By default GW basic is in direct mode. Direct mode mean when you write the command it will be executed at once.

Program 1: Write a command for adding two numbers 10 and 20.

Answer: PRINT 10 + 20



Explanation: PRINT command is used to print the result of any action. And to add the numbers we use addition operator ( + ).

Program 2: Write a command for multiplying two numbers 40 and 5.

Answer: PRINT 40 * 5



Explanation: PRINT command is used to print the result of any action. And to multiply the numbers we use multiplication operator ( * ) which is known as asterisk.

NOTE: Similarly for subtraction we will use the negative operator ( - ) and for division we will use division operator ( / ) which is known as forward slash.

Program # 3: Write a command to evaluate the following expression.
40 + 12 * 3 – 2
Answer: PRINT 40 + 2 * 3 -2

Program # 4: Subtract the numbers 100 and 45 by using variables.

Answer:
x = 100
y = 45
PRINT x – y

Explanation: In this program we used two variables x and y and initialize them with values 100 and 45 simultaneously. And then we use the PRINT command to display our result.




What is variable?

A variable is a named memory location which is used to store program’s input and its computational results during program execution.

Program # 5: Write a command that display “HELLO PAKISTAN” on output screen.

Answer: PRINT “HELLO PAKISTAN



Explanation: In this program we are trying to display a text string “HELLO PAKISTAN”. And whenever we work with strings we use double quotation marks.

What is concatenation?

Answer: Concatenation is the process of combining or joining two or more text strings. And in GW Basic we use plus sign ( + ) to combine.

Program 6: Write a command which concatenate two strings, “PAKISTAN” and “ZINDABAD”.

Answer: PRINT “PAKISTAN” + “ZINDABAD”



Program 7: Write commands to solve the following expressions. (i) 23 (ii) 37 (iii) square of 10

Answer:
                        PRINT 2^3
                        PRINT 3^7
PRINT 10^2

Explanation: Carrot sign ( ^ ) is used for taking power or exponent.







Program 7: Write a command to calculate the modulus or remainder by dividing 13 with 2.

Answer: PRINT 13 MOD 2



Explanation: MOD command is used to take modulus or remainder of a division.