Monday, 26 October 2015

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.


No comments:

Post a Comment