Table of Contents
Perform division of two numbers in C language|using 6 ways
In this tutorial, we will discuss the Perform division of two numbersĀ in C language
In this post, we are going to learn how to perform division of two numbers using different 6 ways
Division of two numbers
Division of two numbers – standard method
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int num1,num2,division;//variable declaration num1=35,num2=5;//variable initialization division=num1/num2;//calculate division printf("Division of %d and %d is: %d",num1,num2,division); //display result on the screen getch(); return 0; }
When the above code is executed, it produces the following result
Division of 35 and 7 is:5
Approach
- Integer variable num1 and num2 both are declared and initialized
- the num1 and num2 both are divided together and the value is assigned to the integer variable division
- finally the program is displayed the output using printf() function
Division of two numbers – Using user input
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int num1,num2,division;//variable declaration printf("Enter two numbers: ");//Ask the input from the user scanf("%d %d",&num1,&num2);//Store the given input division=num1/num2;//Calculate the division printf("division of %d and %d : %d",num1,num2,division); getch(); return 0; }
When the above code is executed, it produces the following result
Enter two numbers: 1000 40 division of 1000 and 40: 25
Approach
- Integer variable num1 and num2 both are declared,
- The program takes input from the user
- Then the user enters the value as input 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 divided together and the value assigned to the integer variable division
- Finally the program is displayed the output using printf() function
Division of two numbers – Using function
Program 3
#include <stdio.h> #include <stdlib.h> int division(int,int); //function prototype / declaration int main() { int num1,num2,result; printf("Enter two number\n"); scanf("%d %d",&num1,&num2); //numbers receive from the user result=division(num1,num2);//assign the output to variable result //function call printf("division of %d and %d is %d\n",num1,num2,result); getch(); return 0; } int division(int a, int b)//function definition { return a/b; }
When the above code is executed, it produces the following result
Enter two number 450 15 Division of 450 and 15 is: 30
Approach
- Declare a function named as division() 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 (division())to find division
- Call the function to produce output
- finally, the program displays the output using printf() function
Division of two numbers – Using recursion
Program 4
#include <stdio.h> #include <stdlib.h> int division(int,int); //function prototype / declaration int main() { int num1,num2,result; printf("Enter two number to find division\n"); scanf("%d %d",&num1,&num2); //numbers receive from the user result=division(num1,num2);//assign the output to variable result //function call printf("Division of %d and %d is %d\n",num1,num2,result); getch(); return 0; } int division(int x, int y) { if (y==0) { return 0; } else if (x-y==0){ return 1; } else if (x<y){ return 0; } else{ return (1+division(x-y,y)); } }
When the above code is executed, it produces the following result
Enter two number to find division
2400
40
Division of 2400 and 40 is: 60
Approach
- Declare a function named as division() 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 and num2
- the program will read the input using scanf() function and store the variables in num1, num2 respectively
- define the recursive function (division())to find division recursively
- Call the function to produce output
- finally, the program displays the output using printf() function
Division of two numbers – Using pointer
Program 5
#include <stdio.h> #include <stdlib.h> int main() { int num1,num2,*ptr1,*ptr2,division; printf("Enter two number to find division\n"); scanf("%d %d",&num1,&num2); //numbers receive from the user //function call ptr1=&num1; ptr2=&num2; division=*ptr1 / *ptr2; printf("division of %d and %d is %d\n",num1,num2,division); getch(); return 0; }
When the above code is executed, it produces the following result
Enter two number to find division 12345 15 Division of 12345 and 15 is: 823
Approach
- Integer variable num1 and num2 both are declared,
- Integer pointer variable *ptr1 and *ptr2 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.
- Addresses of num1, num2 are assigned to variable ptr1 and ptr2 respectively
- The pointer variables ptr1 and ptr2 both are used to divide given numbers together and the value is assigned to the integer variable of division
- Finally the program is displayed the output using printf() function
Division of two numbers – without divisional operator
Program 6
#include <stdio.h> #include <stdlib.h> int division(int,int); //function prototype / declaration int main() { int num1,num2,a,b,count=0; printf("Enter two number to find division\n"); scanf("%d %d",&a,&b); //numbers receive from the user num1=a; num2=b; while(num1>=num2) { num1=num1-num2; count++; } //function call printf("division of %d and %d is %d\n",a,b,count); getch(); return 0; }
When the above code is executed, it produces the following result
Enter two number to find division
250
50
Division of 12345 and 15 is: 5
Suggested for you
Data type in C language
Variable in C language
Similar post
Perform division of two numbers in C++ language|using 6ways
Perform division of two numbers in Java language|using 5ways
Perform division of two numbers in Python language|using 4ways