Table of Contents
In this tutorial, we will discuss the Divide two numbers in C++ language
In this post, we are going to learn how to find division of two numbers using different 6 ways in C++ language
Program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int num1=100,num2=20; int div=num1/num2; cout << "Division of "<<num1<<" and "<<num2<<" is: " << div<<endl; getch(); return 0; }
When the above program is executed, it produces the following result
Division of 100 and 20 is: 5
In this program,
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { int num1,num2; cout<<"Enter two numbers: "; cin>>num1>>num2; int div=num1/num2; cout << "Division of "<<num1<<" and "<<num2<<" is: " << div<<endl; getch(); return 0; }
When the above program is executed, it produces the following result
Enter two numbers: 666 6 Division of 666 and 6 is: 111
in tis program,
Program 3
#include <iostream> #include <conio.h> using namespace std; int division(int,int); //function prototype / declaration int main() { int num1,num2,result; cout<<"Enter two number to find division\n"; cin>>num1>>num2; //numbers receive from the user result=division(num1,num2);//assign the output to variable result //function call cout << "Division of "<<num1<<" and "<<num2<<" is: " << result<<endl; getch(); return 0; } int division(int a, int b) { return a/b; }
When the above program is executed, it produces the following result
Enter two numbers: 560 70 Division of 560 and 70 is: 8
In this program
Program 4
#include <iostream> #include <conio.h> using namespace std; int division(int,int); //function prototype / declaration int main() { int num1,num2,result; cout<<"Enter two number to find division\n"; cin>>num1>>num2; //numbers receive from the user result=division(num1,num2);//assign the output to variable result //function call cout << "Division of "<<num1<<" and "<<num2<<" is: " << result<<endl; 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 program is executed, it produces the following result
Enter two numbers to find division: 448 16 Division of 448 and 16 is: 28
Program 5
#include <iostream> #include <conio.h> using namespace std; int division(int,int); //function prototype / declaration int main() { int num1,num2,a,b,counter=0; cout<<"Enter two number to find division\n"; cin>>a>>b; //numbers receive from the user num1=a; num2=b; while(num1>=num2) { num1=num1-num2; counter++; } cout << "Division of "<<a<<" and "<<num2<<" is: " << counter<<endl; getch(); return 0; }
When the above program is executed, it produces the following result
Enter two numbers to find division: 1250 25 Division of 560 and 70 is:50
Program 6
#include <iostream> #include <conio.h> using namespace std; int main() { int num1,num2,*ptr1,*ptr2; //variable declaration cout << "Enter the first number: " << endl; cin>>num1;//read the value for num1 cout << "Enter the second number: " << endl; cin>>num2;//read the value for num2 ptr1=&num1; ptr2=&num2; int div = *ptr1/ *ptr2;//division calculation cout <<num1 <<" is devided by "<< num2<<" is: "<<div<<endl; getch(); return 0; }
When the above program is executed, it produces the following result
Enter the first number: 1250 Enter the second number 25 1250 is divided by 25 is: 50
Suggested for you
Data type in C++ language
Variable in C++ language
Similar post
C program to calculate division of two numbers | 6 different Methods
Java program to calculate division of two numbers | 5 different Methods
Python program to calculate division of two numbers | 4 different Methods
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.