Example programs
Program to add two numbers
The basic Arithmetic Operation performed by C language.The Arithmetic Operations are:
- Addition,
- Subtraction,
- Multiplication,
- Division e.t.c.
Here, we will discuss a program to add two numbers in the C programming language.
Addition of two numbers
There are three ways :-
1.Adding Two Numbers with user Input
This way is where a user can enter the number he wants to add, this requires an addition statement that will tell the computer that it need to ask the user for the number, which is a scanf() statement. Let’s see
#include<stdio.h> void main() //here the user is asked to enter the numbers { int a,b,c; printf(“enter first number”); //here the user is asked to enter the numbers a and scanf(“%d”,&a); printf(“enter second number”); scanf(“%d”,&b); c=a+b; printf("The sum of numbers a and b is %d",c); }
Output :
enter first number 4 enter second number 5 The sum of numbers a and b is 9
2. When numbers are provided in the code.
where we provide the number to be added in the code itself.
#include<stdio.h> int main() { int a,b,c; a=4; b=6; c=a+b; printf("The sum of numbers a and b is %d",c); }
Output:
The sum of numbers a and b is 10.
3.Add two numbers without using third variable.
#include<stdio.h> int main() { int a,b; a=4; b=5; printf(“sum is %d”,a+b); return 0; }
Output:
sum is 9
Login/Signup to comment