pTutorial

New way to learn C programming

C program to add two numbers

Simple C program to perform addition and substraction of two numbers. Number will be the constant and will be entered by the user.

C program to add two number

// Addition and substraction c program
#include <stdio.h>
#include <conio.h>

void main()
{
   int a = 38;    
   
   int b =  7;

   int sum, sub;
    
   sum = a + b;
 
   printf("sum of a and b =%d ",sum);

   sub = a - b;
 
   printf("Difference of a and b =%d ",sub);
}
 

Download C Addition Source Code

In above example there are two integer variable that hold constant value and sum, sub are also integer type variable. Sum hold the result of addition and sub hold the result of subtraction.

Output of above c program

c program to add two numbers

User define addition c program

// User Define addition program
#include <stdio.h>
#include <conio.h>

void main()
{
      int number1, number2;
      
      int sum;
  
      printf("Enter numbers\n");
 
      scanf("%d%d",&number1,&number2);
 
      sum = number1 + number2;
 
      printf("number1 + number2 = %d\n", sum);
      
      getch();
  
 }

Download C program

In above example there are two integer variable that hold the value at run time and sum is a integer type variable that store the result of addition.

c program to add two number using function

// User Define addition program addition program by using function
#include <stdio.h>
#include <conio.h>

add_num(int x ,int y);

void main()
{
	 int first_no second_no;

	 int result;

	 clrscr();

	 printf("Enter First number\n");

     scanf("%d",&first_int);
	 
     printf("Enter second number\n");

     scanf("%d",&second_int);

     result=add_num(first_int,second_int); 

	 printf("Sum =")     
 }

 int add_num(int x, int y)
 {
  return x+y;  
 }

Download C Program to Add Numbers

In this example, first declare the function prototype. Create three integer type variable and store the numbers at run time .Pass both number as argument in add function and add function return the result.

See Also

Xmlhttprequest method in ajax   |   Special symbol form validation java script   |   C program reverse string

QUOTES OF THE DAY

Eat to live, and not live for eat.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER