Previous Index Next

In previous lesson, you learned some simple C programs. You also learned about the basic structure of a C program. In this lesson you'll learn more essentials within a C program, such as the concept of variable so that our program can perform calculation on data.

Now we'll create a program that adds two numbers. We'll solve this problem in C with the following steps:
Step 1 : Allocate memory for storing three numbers
Step 2 : Store first number in computer memory
Step 3 : Store second number in computer memory
Step 4 : Add these two numbers together and store the result of the addition in a third memory location
Step 5 : Print the result

Step 1

Now first we'll allocate memory for storing numbers.

Location of the computer memory which is used to store data and is given a symbolic name for reference is known as variable. We need three variables, two for storing input and third for storing result. Before a variable is used in a program, we must declare it. This activity enables the compiler to make available the appropriate type of location in the memory.

Following statements declare three variables of type integer to store whole numbers.

int x;
int y;
int z;

You can declare more than one variable of same type in a single statement like :

int x, y, z;

Step 2

Following statement stores value in first variable

x = 25;

Step 3

Following statement stores value in second variable

y = 10;

Step 4

Now, add these two numbers together and store the result of the addition in third variable

z = x + y;

Step 5

Print the result

printf("The sum is %d", z);
You can output integer variables in a similar way as you output strings by tying %d instead of any text. The %d a placeholder that tells the printf() command to print an integer value.

Here, is the complete program

#include <stdio.h>

int main()
{
    /* declare variables of integer type */
    int x;
    int y;
    int z;

    /* storing value in variables */
    x = 25;
    y = 10;

    /* adding numbers and store the result in sum */
    z = x + y;

    /* print the result */
    printf("The sum is %d", z);

    return 0;
}

Output:

The sum is 35


Input and Output Statement in C

printf() - We can use printf() function to output strings and numbers on screen as we seen in previous examples.
scanf() - The scanf() function is used to read data from keyboard and store it in variable.

Now, we will create a program that reads two numbers from keyboard and print the result on screen.

#include <stdio.h>

int main()
{
    /* declare variables of integer type */
    int x, y, z;

    /* Reading data from keyboard */
    printf("Enter first number: ");
    scanf("%d", &x);

    printf("Enter second number: ");
    scanf("%d", &y);

    /* adding numbers and store the result in sum */
    z = x + y;

    /* print the result */
    printf("The sum is %d", z);

    return 0;
}

Output:

Enter first number: 35
Enter second number: 15
The sum is 50

Previous Index Next

privacy policy