Find Difference of two numbers


This program finds the difference of two numbers entered by user.

This is as simple as the previous program, but it would be good practice for beginners to get familiar with C programming syntax. Variables :

integers : number1 and number2 to hold values of number A and number B. difference is used to calculate the total of A - B

Functions:

printf(): is used to display something on the console/screen. %d in printf function is used to display an int variable on screen. \n is used to subtract a newline

scanf() : is used to fetch data inputted by the user on the console/screen using the keyboard. %d in scanf is indicated that inputted text is of type int. &num1 refers to the address of memory location which will hold number1

Description: Minus - is the subtraction operator used to get the difference between two numbers. difference variable holds the difference of number1 and number2.

/*
 * 1000+ C programs + tutorials
 *
 * This is a C program to Subtract two
 * numbers.
 *
 *
 *
 * difference_of_two_numbers.c
 *
 *  Created on: Oct 20, 2014
 *      Author: Code2care.org
 */

#include 
//#include 

void main() {

    int number1;
	int number2;
	int difference;

	//clrscr();
	printf("Program to calculate Difference of two numbers :  \n\n");

	printf("Enter value of 1st number : ");
	scanf("%d", &number1);

	printf("\n\nEnter value of 2nd number : ");
	scanf("%d", &number2);

	difference = number1 - number2;

	printf("\n\n Difference of %d - %d = %d", number1, number2, difference);

	//getch();

}
Output:
Program to calculate the Difference of two numbers :
Enter the value of 1st number :
Enter the value of 2nd number :
Difference of 10 - 5 = 5
If you do not want a 3rd variable sum then we can simply output the sum as,

printf("\n\n Sum of %d + %d = %d", numberA, numberB, numberA-numberB);

/*
 *
 * 1000+ C programs + tutorials
 *
 * Difference of two numbers without using third variable
 *
 *
 *
 * 2_difference_of_two_numbers.c
 *
 *  Created on: Oct 20, 2014
 *  Author: Code2care.org
 */

#include 
//#include 

void main() {

	int numberA;
	int numberB;

	//clrscr();

	printf("C Program to calculate Difference of two numbers :  \n\n");

	printf("Enter value of number A : ");
	scanf("%d", &numberA);

	printf("\n\nEnter value of number B : ");
	scanf("%d", &numberB);


	printf("\n\n Difference of %d - %d = %d", numberA, numberB, numberA - numberB);

	//getch();

}
Output:
C Program to Difference of two numbers :
Enter value of number A : 50
Enter value of number B : 80
Difference of 10 - 40 = -30

The best way to learn C programming is to practice more and more of programs. Code2care C Programming tutorials provide 1000+ programs in C that you can study and become an expert in the language. Programs are divided into categories depending upon type and complexity.

BSc. IT, BSc. Computer Science, BSc. IT, MSc. IT, MSc. Computer Science, MCA, BTech IT & BTech Engineers may find these programs very useful.

Advertisements
Try Out Code2care Dev Tools:

Advertisements



Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency