Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Python program to subtraction of two numbers | 4 different Methods

C program to subtraction of two numbers | 6 different Methods

Posted on August 9, 2020August 9, 2020

Table of Contents

  • C program to subtraction of two numbers | 6 different Methods
    • subtraction of two numbers
      • Subtraction of two numbers- standard method
      • Subtraction of two numbers- Using user input
      • Subtraction of two numbers- Using function
      • Subtraction of two numbers- Using recursion
      • Subtraction of two numbers- Using pointer
      • Subtraction of two numbers- without – operator

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

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes