Lecture 2
Lecture 2
Mathematics
Input operator:
cin >> variable_name;
if(x>=y){
cout<<"x is greater than equal to y" <<endl;
}
if(x<y){
cout<<"x is less than y" <<endl;
}
}
Output:
x is less than y
If (condition){
statement(s)
} else {
statement1(s)
}
E.g. -
Function Definition:
E.g.
if(gcd!=0)
cout<< "GCD of " <<x<<" and " <<y<< " = "<<gcd<<endl;
}
long gcdFun( long a, long b){
long gcd;
if(a==0 && b== 0){
cerr<< "Both the inputs are zero. GCD is not defined" <<endl;
return 0;
}
if(a==0)
return b;
if(b==0)
return a;
for(long i = 1;i<=a;i++){
if((a%i == 0) && (b%i== 0))
gcd = i;
}
return gcd;
}
#include<iostream>
#include "gcd.h"
using namespace std;
int main(){
int x, y, gcd;
cout<<"Enter the value of x : " ;
cin>>x;
cout<<"Enter the valud of y : " ;
cin>>y;
gcd = gcdFun(x, y);
if(gcd!=0)
cout<<"GCD of "<<x<<" and "<<y<< " = "<<gcd<<endl;
}
long gcdFun(long a, long b){
int main(){
long n,fact;
cout<<"Enter the value of n ";
cin>>n;
fact = getFact(n);
cout<<"Factorial of "<<n<<" is "<<fact<<endl;
}
long getFact(long n){
if(n==1)
return 1;
return n*getFact(n-1);
}
Do while loop.
#include<iostream>
using namespace std;
int main(){
int x;
do{
cout<<"Enter your input ";
cin>>x;
cout<<"User input is "<<x<<endl;
}while(x!=0);
}
#include <iostream>
using namespace std;
long gcdFun( long, long);
int main(){
long n, gcd, count;
cout<< "Enter the value of n " ;
cin>>n;
count = 0;
for(long a=1;a<=n;a++){
for(long b=a+1;b<=n;b++){
gcd = gcdFun(a, b);
if(gcd==1){
cout<< "a and b " <<a<<" "<<b<<endl;
count = count + 1;
}
}
}
cout<< "count = " <<count<<endl;
cout<< "Proportion of coprime pairs = " <<double(2*count)/( double(n)*double(n))<<endl;
return 0;
}
long gcdFun( long a, long b){
if(a==0 && b== 0){
cerr<< "Both the inputs are zero. GCD is not defined" <<endl;
return 0;
}
if(b==0)
return a;
long r;
r = a%b;
return gcdFun(b, r);
}
#include<iostream>
#include "gcd.h"
using namespace std;
int main(){
int a, b, gcd;
cout<<"Enter the value of a : ";
cin>>a;
cout<<"Enter the valud of b : ";
cin>>b;
gcd = gcdFun(a, b);
if(gcd!=0)
cout<<"GCD of "<<a<<" and "<<b<< " = "<<gcd<<endl;
return 0;
}
long gcdFun(long a, long b){
long r;
if(a==0 && b==0){
cerr<<"Both the inputs are zero. GCD is not defined"
<<endl;
return 0;
}
if(a<0)
a = -a;
if(b<0)
b = -b;
r = b;
while(r!=0){
r = a%b;
a = b;
b = r;
}
return a;
}
Functions: Call by value
● In call by reference, the address of the variables are passed. So any update
in variable value inside the function , will change the value of the variable.
Output:
Value of a inside function = 15
Output:
Before function call value of a and b = 10 , 15
After function call value of a and b = 10 , 15
#include<iostream>
using namespace std;
void func(int &, int &);
int main(){
int a, b;
a =10;
b = 15;
cout<<"Before function call value of a and b = "<<a<<" , "<<b<<endl;
func(a, b);
cout<<"After function call value of a and b = "<<a<<" , "<<b<<endl;
}
void func(int &a, int &b){
int temp;
temp = b;
b = a;
a = temp;
}
Output:
Before function call value of a and b = 10 , 15
After function call value of a and b = 15 , 10
Extended Euclidean Algorithm:
d = a.x + b.y
Two function in C++ can have same names if they differ by their inputs types or
number of inputs.
E.g.