C Programming

Division in c program ( A step-by-step easy guide )

Recently I’ve written an explanatory post on 🙄 Addition of two numbers in c. In this post, I will try to clear all of your doubt regarding the division of two numbers using c programming. Well, a division of two numbers is pretty simple. You just need two numbers stored in any two formats and with the help of assignment operator we have to divide them. If you are interested in c programming then I will surely refer you ⌨️ Let Us C book to enhance your knowledge.

Algorithm (Division of two numbers in C)

  1. Start
  2. Accept two numbers.
  3. Divide these numbers using operator.
  4. Print the result.
  5. End

program to divide two numbers in c

Overview

This program is pretty simple. We just need 3 steps to implement this program.

  • INPUT – Firstly we have to take two inputs from the user ( maybe in float, integer or in any other format ). Also, we have to store these two inputs in two variables. We may call these variable num1 & num2.
  • Operation – Moving to the next step, after taking two inputs we have to perform division operation using “/” operator & store its value in any third variable (maybe num3). For example – num3=num1/num2 or num3=num2/num1.
  • Result – Finally we have to print the final result which may be stored in the third variable. For this, we have to use printf() function.

Program of division in C

#include<stdio.h>
int main()
{
int num1,num2,num3;
printf("Enter the first number :\n");
scanf("%d",&num1);
printf("Enter the second number :\n");
scanf("%d",&num2);
num3=num1/num2;
printf("Result :\n %d",num3);

return 0;
}

Output

Enter the first number :

4

Enter the second number :

2

Result :

2

Line-By-Line Explanation

  • #include<stdio.h> – Before we start writing any program in C, firstly we have to declare all the header files which we have to use in our program. I will recommend you to go through this article ( 😍 Use of header file in C ) for clear understanding.
  • int main() – Declaration of main function. By default, we have to write all of our programs under the main() function.
  • int num1,num2,num3 – Declaration of data types and its associated variables. Here num1 and num2 are two variables which we will use to take the input of the two numbers and num3 is the variable which we can use to store our result. I would recommend to use float data type in place of integer data type for division.
  • printf(“……….”) – printf() is a predefined function which is used to display output or any message to the user. In this program, we have used 3 printf() function, two of them is used to display the message to the user to ask them for input and the third one is for displaying the final result.
  • num3=num1/num2 – Here this statement is used perform the division operation between these numbers.
  • Result – Finally we need to print the result stored in num3 variable using printf function.

Program of division in C

#include<stdio.h>
int main()
{
int num1,num2;
printf("Enter the first number :\n");
scanf("%d",&num1);
printf("Enter the second number :\n");
scanf("%d",&num2);
num1=num1/num2;
printf("Result :\n %d",num1);

return 0;
}

Output

Enter the first number :

4

Enter the second number :

2

Result :

2

ALSO READ

Leave a Reply