This is a simple C program that calculates the sum of two numbers entered by the user.
Variables :integers : num1 and num2 to hold values of number A and number B.
sum is used to store the value of A + B
Functions:printf(): is used to disply text and values on the console/screen. %d in printf function is used to display a int variable on screen. \n is used to add a new line
scanf(): is used to fetch data inputted by the user on the console/screen using the keyboard. %d in scanf is indicated that inputted text is of type int. &num1 refers to the address of memory location which will hold number1
Description: Plus + is the addition operator used to add two numbers. sum variable holds the sum of num1 and num2.
/*
*
* 1000+ C programs + tutorials
* This is a C program to calculate
* sum of two numbers.
* 1_calculate_sum_of_two_numbers.c
* Created on: Oct 20, 2019
* Author: Code2care.org
*/
#include <stdio.h>
//#include <conio.h>
void main() {
int num1;
int num2;
int sum;
//clrscr();
printf("Program to calculate Sum of two numbers : \n\n");
printf("Enter value of 1st number : ");
scanf("%d", &num1);
printf("\n\nEnter value of 2st number : ");
scanf("%d", &num2);
sum = num1 + num2;
printf("\n\n Sum of %d + %d = %d", num1, num2, sum);
//getch();
}
Output:
Program to calculate the sum of two numbers :
Enter the value of 1st number: 2
Enter the value of 2nd number: 4
Sum of 2 + 4 = 6
If you do not want a 3rd variable sum then we can simply output the sum as,
printf("\n\n Sum of %d + %d = %d", numberA, numberB, numberA+numberB);
/*
*
* 1000+ C programs + tutorials
*
* Sum of two numbers without using third variable
*
*
*
* 1_calculate_sum_of_two_numbers.c
*
* Created on: Oct 20, 2014
* Author: Code2care.org
*/
#include <stdio.h>
//#include <conio.h>
void main() {
int numberA;
int numberB;
//clrscr();
printf("C Program to calculate Sum of two numbers : \n\n");
printf("Enter value of number A : ");
scanf("%d", &numberA);
printf("\n\nEnter value of number B : ");
scanf("%d", &numberB);
printf("\n\n Sum of %d + %d = %d", numberA, numberB, numberA+numberB);
//getch();
}
Output:
C Program to calculate Sum of two numbers :
Enter value of number A : 10
Enter value of number B : 40
Sum of 10 + 40 = 50
Adding 3 numbers entered by user.
printf("\n\n Sum of %d + %d = %d", numberA, numberB, numberA+numberB+numberC);
/*
*
* 1000+ C programs + tutorials
*
* Sum of two numbers without using third variable
*
*
*
* 1_calculate_sum_of_two_numbers.c
*
* Created on: Oct 20, 2014
* Author: Code2care.org
*/
#include <stdio.h>
//#include <conio.h>
void main() {
int numberA;
int numberB;
int numberC;
//clrscr();
printf("C Program to calculate Sum of two numbers : \n\n");
printf("Enter value of number A : ");
scanf("%d", &numberA);
printf("\n\nEnter value of number B : ");
scanf("%d", &numberB);
printf("\n\nEnter value of number C : ");
scanf("%d", &numberC);
printf("\n\n Sum of %d + %d + %d = %d", numberA, numberB,numberC numberA+numberB+numberC);
//getch();
}
Output:
C Program to calculate Sum of two numbers :
Enter value of number A : 10
Enter value of number B : 40
Enter value of number B : 50
Sum of 10 + 40 + 50 = 100
Similar Question :
1. C Program to calculate sum of two hardcoded numbers
2. C Program to Add two floating values
Hashtags: #cProgramming #SumOfNumbers
The best way to learn C programming is to practice more and more programs. Code2care C Programming tutorials provide 1000+ programs in C that you can study and become an expert in the language. Programs are divided into categories depending upon type and complexity.
BSc. IT, BSc. Computer Science, BSc. IT, MSc. IT, MSc. Computer Science, MCA, BTech IT & BTech Engineers may find these programs very useful.
- Calculate Volume of Cylinder
- Calculate Volume of Sphere
- Calculate Volume of Cube
- Calculate Volume of Ellipsoid
- Find Sum of two numbers
- Division between two numbers
- Calculate Area of Trapezoid
- Calculate Area of a Rectangle
- Calculate Volume of Pyramid
- Calculate Area of ellipse
- Find Difference of two numbers
- Calculate Volume of Cone
- Calculate Area of Square
- Calculate Area and Circumference of Circle
- Calculate Area of Parallelogram
- How to make TextView Text Transparent [Android] - Android
- JSON Nest Objects Example: JSON Tutorial - Json-Tutorial
- Google translate in spreadsheet - Google
- SharePoint update append Required Field to display name of mandatory columns - SharePoint
- How to make a div tag clickable - Html
- Restore deleted Office 365 SharePoint group site - SharePoint
- Android : Neither user 10085 nor current process has android.permission.ACCESS_NETWORK_STATE - Android
- Android-Failed to install apk on device EOF Timeout Error - Android
- HTML5 HELLO WORLD Example - Html
- Calculate Area of Square - C-Program
- How to get the Android OS installed version programmatically - Android
- List of Eclipse IDE Versions and future releases : Mars and Neon - Eclipse
- [Eclipse] Syntax error, annotations are only available if source level is 1.5 or greater - Eclipse
- How to Generate Self-Signed OpenSSL certificate in three easy steps - HowTos
- Change Title text for Android Activity using java code - Android