Subscribe For Free Updates!

We'll not spam mate! We promise.

June 8, 2012

Adding Two Integers

|

Proceeding on this topic means that you already know how to print "Hello World", the very basic program in C.

In this article you will learn how to add two integers using the C program.



#include<stdio.h>

int main()
{

int number1, number2, sum;      /* declaration */


printf("Enter first number : ");             /* prompt */
scanf("%d", &number1);                    /* read an integer */
printf("Enter second number: ");
scanf("%d", &number2);

sum = number1 + number2;         /* sum the numbers entered */

printf("Sum is %d \n", sum);                 /* print sum */

return 0;                         /* ended the program */
}






scanf - obtain integers typed by user
         - computes the sum f the values and print the result using printf

int number1, number2, sum - is a declaration
                                        - int which means that these variables will hold integer values i.e. whole numbers such as 9, 3, 36, 1369, and more.

NOTE: All variables must be declared with a name and datatype immediately after the left brace.

0 comments:

 
Copyright © . ZaKyri NewsTrick - Posts · Comments
Theme Template by BTDesigner · Powered by Blogger