BACK TO 10+2 STEM LAB
10+2 MATHEMATICS & DRAWINGS•2016-05-31•3 MIN READ

Print sum of two or more number

**Write a c program that takes two or more numbers from the user then print the sum of these number.**

Key Architecture Takeaways

  • ›
    Point 1: Core computer science and engineering computing concept: Print sum of two or more number.
  • ›
    Point 2: Detailed trace of memory representation, algorithmic complexity, and control structures.
  • ›
    Point 3: Includes portable ISO C (C17/C23) code examples verified with modern GCC and Clang.
Notice

Compiler & Toolchain Modernization

While legacy educational environments used 16-bit DOSBox Turbo C++, all code presented here adheres to standard ANSI/ISO C standards. You can compile these programs cleanly on modern Linux, Windows (MSYS2/WSL), or macOS using:

bash
> gcc -Wall -Wextra -O2 program.c -o program
> ./program
> ```


---

#

# **Write a c program that takes two or more numbers from the user then print the sum of these number.**

This c language program performs the basic arithmetic operation of addition on two numbers and then prints the sum on the screen. For example, if the user entered two numbers as 2, 4 then 6 (2 + 4) will be printed on the screen.

## C programming code

C

#include<stdio.h> int main() { int a, b, c; printf("Enter two numbers to add\n"); scanf("%d%d",&a,&b); c = a + b; printf("Sum of entered numbers = %d\n",c); getch(); return 0; }

SHELL
Output of program:

[![add output](http://technhit.in/wp-content/uploads/2016/05/add-output.jpg)](http://technhit.in/wp-content/uploads/2016/05/add-output.jpg)

In the expression (c = a + b) overflow may occur if the sum of a and b is larger than maximum value which can be stored in variable c.

## Addition without using the third variable

c

#include<stdio.h> main() { int a = 1, b = 2; / Storing result of addition in variable a / a = a + b; /* Not recommended because original value of a is lost and you may be using it somewhere in code considering it as it was entered by the user. / printf("Sum of a and b = %d\n", a); getch(); return 0; }

SHELL
## C program to add two numbers repeatedly

c

#include <stdio.h> int main() { int a, b, c; char ch; while (1) { printf("Inut two integers\n"); scanf("%d%d", &a, &b); getchar(); c = a + b; printf("(%d) + (%d) = (%d)\n", a, b, c); printf("Do you wish to add more numbers (y/n)\n"); scanf("%c", &ch); if (ch == 'y' || ch == 'Y') continue; else break; } return 0; }

SHELL
Output of program:

c Inut two integers 2 6 (2) + (6) = (8) Do you wish to add more numbers (y/n) y Inut two integers 2 -6 (2) + (-6) = (-4) Do you wish to add more numbers (y/n) y Inut two integers -5 3 (-5) + (3) = (-2) Do you wish to add more numbers (y/n) y Inut two integers -5 -6 (-5) + (-6) = (-11) Do you wish to add more numbers (y/n) n

SHELL
## Adding numbers in c using function

c

#include<stdio.h> long addition(long, long); main() { long first, second, sum; scanf("%ld%ld", &first, &second); sum = addition(first, second); printf("%ld\n", sum); getch(); return 0; } long addition(long a, long b) { long result; result = a + b; return result; } ```

We have used long data type as it can handle large numbers if you want to add still larger numbers which don't fit in long range then use an array, string or other data structure.

π
TechNHIT STEM Faculty
Department of Applied Mathematics & Physics
Print sum of two or more number | TechNHIT 10+2 STEM Lab | TechNHIT