C program to subtract two number using Function
- Home
- Calculations
- C program to subtract two number using Function
- On
- By
- 0 Comment
- Categories: Calculations
C program to subtract two number using Function
C program to subtract two number using Function
In this tutorial, we will discuss the C program to subtract two numbers using the function
In this topic, we are going to learn how to subtract two numbers (integer, floating point) using the function in C language
already we are learned the same concept using the operator
if you want to remember click here, C program to subtract two numbers
Approach
- Function declaration or prototype
- Variable declaration
- Read value from user
- function definition
- call te function
- display result on the screen
Code to subtract two integer number using Function
The program allows to enter two integer values then, it calculates subtraction of the given numbers
Program 1
#include <stdio.h> #include <stdlib.h> int subNum(int a, int b);//Function prototype int main() { int result,num1,num2; printf("Enter two numbers to subtract\n"); scanf("%d%d",&num1,&num2); //read the input given by user result=subNum(num1,num2);//call the function printf("subtraction of given two numbers:%d",result); getch(); return 0; } int subNum(int x, int y)//function definition { int c=x-y; return (c); }
When the above code is executed, it produces the following result
case 1
Enter two numbers to subtract: 456 345 subtraction of given two numbers: 111
case 2
Enter two numbers to subtract: 345 567 subtraction of given two numbers: -222
Code to subtract two floatnumbers using Function
The program allows to enter two floating point values then, it calculates subtraction of the given numbers
Program 2
#include <stdio.h> #include <stdlib.h> int subNum(float a, float b);//Function prototype int main() { float result,num1,num2; printf("Enter two numbers to subtract\n"); scanf("%f%f",&num1,&num2);//read input value from user result=subNum(num1,num2);//call the function printf("subtraction of given two numbers:%.3f",result); getch(); return 0; } int subNum(float x, float y)//function definition { float c=x-y; return (c); }
When the above code is executed, it produces the following result
case 1
Enter two numbers to subtract 455.15 230.40 Subtraction of given two numbers:224.000
case 2
Enter two numbers to subtract 345.67 456.78 Subtraction of given two numbers:-111.000
Suggetsed post
Function in C programming language
Similar post
Subtract two numbers in Java language
Subtract two numbers in C language
Subtract two numbers in C++ language
Subtract two numbers in Python language