C Program to Add Two Integers | ZenTek ZenTek: C Program to Add Two Integers
Contact Us:

If You Have Any Problem, Wanna Help, Wanna Write Guest Post, Find Any Error Or Want To Give Us Feedback, Just Feel Free To Contact Us. We Will Reply You Soon.

Name: *


Email: *

Message: *


ZenTek

LiveZilla Live Help

C Program to Add Two Integers

In this program, user is asked to enter two integers and this program will add these two integers and display it.

Source Code


/*C programming source code to add and display the sum of two integers entered by user */

#include <stdio.h>
int main( )
{
    int num1, num2, sum;
    printf("Enter two integers: ");
    scanf("%d %d",&num1,&num2); /* Stores the two integer entered by user in variable num1 and num2 */

    sum=num1+num2;      /* Performs addition and stores it in variable sum */
    printf("Sum: %d",sum);  /* Displays sum */
    return 0;
}
Output
Enter two integers: 12
11
Sum: 23
Explanation

In this program, user is asked to enter two integers. The two integers entered by user will be stored in variables num1 and num2 respectively. This is done using scanf( ) function. Then, + operator is used for adding variables num1 and num2 and this value is assigned to variable sum.
Adding two numbers in C programming
Finally, the sum is displayed and program is terminated.
There are three variables used for performing this task but, you can perform this same task using 2 variables only. It can done by assiging the sum of num1 and num2 to one of these variables as shown in figure below.
Displaying sum of two integers using two variables only in C programming


/* C programming source code to add and display the sum of two integers entered by user using two variables only. */

#include <stdio.h>
int main( )
{
    int num1, num2, sum;
    printf("Enter a two integers: ");
    scanf("%d %d",&num1,&num2);     
    num1=num1+num2; /* Adds variables num1 and num2 and stores it in num1 */
    printf("Sum: %d",num1); /* Displays value of num1 */
    return 0;
}
This source code calculates the sum of two integers and displays it but, this program uses only two variables.


Like the Post? Share with your Friends:-

Team SB
Admin: Akash

0 comments:

POST A COMMENT

Contact Us

Name

Email *

Message *

 

About Me

Hello Guys

I am an IT(Information Technology) Student design this blog for your Preference I always enjoyed technical things as a kid

now here i am sharing my knowledge about Tips and Tricks In Internet PC Windows Free Recharges and Other Cool Stuffs Too

So enjoy it

Followers

Widget by : ZenTek
Widget by : ZenTek
Copyright © . ZenTek. All Rights Reserved.
Designed by :-Way2themes