top of page

SUBSTRACTION OF TWO NUMBERS

Updated: Jan 22, 2023



#include <stdio.h>

void main() {

int num1, num2, sub;

printf("Enter the first number: ");

scanf("%d", &num1); // Read the first number from the user

printf("Enter the second number: ");

scanf("%d", &num2); // Read the second number from the user

sub = num1 - num2; // Calculate the sub

printf("The subtraction of %d and %d is %d\n", num1, num2, sub); //Result

} Here's a brief explanation of the code:

  1. The first line #include <stdio.h> is a preprocessor directive that tells the compiler to include the standard input/output library. This library contains functions like printf and scanf that are used to read input and print output.

  2. int main() is the main function where the program starts executing.

  3. int num1, num2, sub; declares three variables: num1 and num2 to store the two numbers and sub to store the result.

  4. printf("Enter the first number: "); prints the message "Enter the first number: " on the screen, asking the user to enter the first number.

  5. scanf("%d", &num1); reads an integer from the user and stores it in the variable num1. The & operator is used to pass the address of the variable to the function, so that it can store the input in that location.

  6. The same steps are repeated for the second number.

  7. sub = num1 - num2; calculates the difference of num1 and num2 and stores it in the variable sub.

  8. printf("The sub of %d and %d is %d\n", num1, num2, sub); prints the result on the screen. The %d is a placeholder for an integer.

You can run this program on a C compiler.


12 views0 comments

Recent Posts

See All

Stack Using Queue

A stack can be implemented using two queues in C. The basic idea is to use one queue for enqueue operations and another for dequeue operations. When an element is pushed onto the stack, it is enqueued

Queue Using Stack

A queue can be implemented using two stacks in C. The basic idea is to use one stack for enqueue operations and another for dequeue operations. When an element is enqueued, it is pushed onto the enque

Queue Using Array

#include <stdio.h> #include<stdlib.h> #include<math.h> #define size 5 int queue[size]; int front = -1; int rear = -1; void enque(void); void deque(void); void peek(void); void isEmpty(void); void isFu

bottom of page