Arithmetic Caculation

C program to subtraction of two numbers | 6 different Methods

C program to subtraction of two numbers | 6 different Methods

In this tutorial, we will discuss the C program to subtraction of two numbers

In this post, we are going to learn how to find subtraction of two numbers via different 6 ways

subtraction of two numbers

Subtraction of two numbers- standard method

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1=78,num2=54;//variable declaration
int sub=num1-num2;//caculation
    printf("subtraction of %d - %d : %d\n",num1,num2,sub);
//display the output
    getch();
    return 0;
}

When the above code is executed, it produces the following result

subtraction of 78 - 54: 24

 

Approach

  • Integer variable num1 and num2 both are declared and initialized
  • the num1 and num2 both are subtracted together and the value assigned to the integer variable sub
  • finally the program is displayed the output using printf() function

 

Subtraction of two numbers- Using user input

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2,sub;//variable declaration
    printf("Enter the first number: ");
    scanf("%d",&num1);//read value for num1
     printf("Enter the second number: ");
    scanf("%d",&num2);//read value for num2

sub=num1-num2;//calculate the subtraction
    printf("subtraction of %d - %d : %d\n",num1,num2,sub);
    //display the result of subtraction
    getch();
    return 0;
}

When the above code is executed, it produces the following result

case 1

Enter the first number: 345
Enter the second number: 125
subtraction of 345-125: 220

 

case 2

Enter the first number: 150
Enter the second number: 245
subtraction of 345-125: -95

 

Approach

  • Integer variable num1 and num2 both are declared,
  • The program takes input from the user
  • Then the user enters the input value for num1 and num2
  • The program will read the input using scanf() function and store the variables in num1, num2.
  • The num1 and num2 both are subtracted together and the value assigned to the integer variable sub
  • Finally the program is displayed the output using printf() function

 

Subtraction of two numbers- Using function

Program 3

#include <stdio.h>
#include <stdlib.h>
int sub(int a,int b);//function declaration
int main()
{
    int num1,num2,result;//variable declaration

    printf("Enter two number to subtract\n");
    scanf("%d%d",&num1,&num2);
    //takes input from the user

    result=sub(num1,num2);//calling the function
    printf("Subtraction of two numbers= %d",result);
    getch();
    return 0;
}
int sub(int a, int b){//function definition
int c=a-b;
return (c);

}

When the above code is executed, it produces the following result

Enter two number to subtract
116
44
Subtraction of two numbers=72

 

Approach

  • Declare a function named as sub() with two int parameters
  • Declare num1,num2 and result.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • the program will read the input using scanf() function and store the variables in num1, num2 respectively
  • define the function (sub())for find sum
  • Call the function to produce output
  • finally, the program displays the result of subtraction using printf() function

Subtraction of two numbers- Using recursion

Program 4

#include <stdio.h>
#include <stdlib.h>
int sub(int,int);//function declaration
int main()
{
    int num1,num2,result;//variable declaration

    printf("Enter two number to subtract\n");
    scanf("%d%d",&num1,&num2);
    //takes input from the user

    result=sub(num1,num2);//calling the function
    printf("Subtraction of two numbers= %d",result);
    getch();
    return 0;
}
int sub(int num1, int num2){//function definition
if(num2==0)
  return num1;
else
    return sub((num1-1),(num2-1));

}

When the above code is executed, it produces the following result

Enter two number to subtract
345
123
Subtraction of two numbers=222

 

Approach

  • Declare a function named as sub() with two int parameters
  • Declare num1,num2 and result.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • the program will read the input using scanf() function and store the variables in num1, num2 respectively
  • define the function (sub())for find subtraction recursively
  • Call the function to produce output
  • finally, the program displays the result of subtraction using printf() function

 

Subtraction of two numbers- Using pointer

Program 5

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *p1,*p2;//pointer variable declaration
    int num1,num2,result;//variable declaration

    printf("Enter two number to subtract\n");
    scanf("%d%d",&num1,&num2);
    //takes input from the user

    p1=&num1;
    p2=&num2;
    result=*p1-*p2;

    printf("Subtraction of two numbers= %d",result);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter two number to subtract
589
478
Subtraction of two numbers=111

 

Approach

  • Declare variables num1,num2 and result.
  • Declare pointer variables *p2,*p2 and result.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • the program will read the input using scanf() function and store the variables in num1, num2 respectively
  • Find subtraction using pointer variabe
  • finally, the program displays the result of subtraction using printf() function

 

Subtraction of two numbers- without – operator

Program 6

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2,sub;//variale declaration

    //input first number
    printf("Enter the first number\n");
    scanf("%d",&num1);//read first number

    //input second number
    printf("Enter the second number\n");
    scanf("%d",&num2);//read second number

    sub=num1+~num2+1;//calculate the subtraction
     printf("subtraction of %d - %d = %d\n",num1,num2,sub);
     //display the result
getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the first number
87
Enter the second number
23
subtraction of 87 - 23 =  64

 

Approach

  • Declare variables num1,num2 and sub.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • the program will read the input using scanf() function and store the variables in num1, num2 respectively
  • calculate the subtraction of num1,num2
  • finally, the program displays the result of subtraction using printf() function

 

Function in C language

Operator in C language

 

Similar post

 

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.