0% found this document useful (0 votes)
30 views

Lecture 2

The document provides information on various C++ programming concepts like input/output operators, if/else statements, functions, loops, and function overloading. It includes code examples to demonstrate concepts like taking user input, conditional execution, calculating greatest common divisor, and passing arguments by value vs reference. Functions are defined to calculate factorial, swap variables, and find GCD recursively. Overloaded functions are defined to handle different data types.

Uploaded by

ashodhiya14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Lecture 2

The document provides information on various C++ programming concepts like input/output operators, if/else statements, functions, loops, and function overloading. It includes code examples to demonstrate concepts like taking user input, conditional execution, calculating greatest common divisor, and passing arguments by value vs reference. Functions are defined to calculate factorial, swap variables, and find GCD recursively. Overloaded functions are defined to handle different data types.

Uploaded by

ashodhiya14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

C++ Programming for

Mathematics
Input operator:
cin >> variable_name;

● cin is the input operator.


● >> is the stream extraction operator.
using namespace std;
int main(){
int x, y;
cout<<"Enter the value of x: "<<endl;
cin>>x;
cout<<"Enter the value of y: "<<endl;
cin>>y;
cout<<"x = "<<x<<" , y = "<<y<<endl;
return 0;
}

Enter the value of x:


5
Enter the value of y:
6
x=,5,y =6
if statement
if (condition){
statement(s)
}

● Condition is a logical statement that return true or false.


● If condition is true than statement(s) are executed.
using namespace std;
int main(){
int x, y;
x =4;
y = 5;

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)
}

● If condition is true then statement(s) is executed else statement1(s) is executed.

using namespace std;


int main(){
int x, y;
x =4;
y = 5;
if(x>=y){
cout<<"x is greater than equal to y"<<endl;
}else{
cout<<"x is less than y"<<endl;
}
}
else if
If (condition){
statement(s)
} else if (condition1) {
statement1(s)
}else {
statement2(s)
}

● if condition is true then statement(s) is executed else if condition1 is true then


statement1(s) is executed else statement2(s) is executed.

#include<iostream>
using namespace std;
int main(){
int x, y;
cout<<"Enter x: " ;
cin>>x;
cout<<"Enter y: " ;
cin>>y;
if(x>y){
cout<< "x is greater than y" <<endl;
}else if(x==y){
cout<< "x is equal to y" <<endl;
}else{
cout<< "x is less than y" <<endl;
}
}
Functions in C++:
Function declaration:

return_type function_name(input1_type, input2_type, … ){ }

E.g. -

int gcd(int, int)

Function Definition:

return_type function_name(input1_type var1, input2_type var2, … ){ body}

E.g.

long gcd(long a, long b){body}


#include<iostream>
int getMax(int, int);
using namespace std;
int main(){
int x, y, max;
cout<<"Enter x: ";
cin>>x;
cout<<"Enter y: ";
cin>>y;
max = getMax(x, y);
cout<<"Maximum value is "<<max<<endl;
}
int getMax(int a, int b){
if(a>=b)
return a;
else
return b;
}
GCD finding: (gcd.h)
#ifndef GCD_H
#define GCD_H
/**
* Calculate the greatest common divisor of two integers.
* Note: gcd(0,0) will return 0 and print an error message.
* @param a the first integer
* @param b the second integer
* @return the greatest common divisor of a and b
*/

long gcd(long a, long b);


#endif
#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){
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){

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>
using namespace std;
long getFact(long);

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

return_type function_name(dataType var1, dataType var2, …. )

● This is how we are passing arguments to the functions till now.


● The above way of passing inputs to functions is called Call by value.
● In this, the value of the variables passed as inputs do not change.
● A new copy of the variables are made and all computations in the function call
happened on these new variables.
#include<iostream>
using namespace std;
void func(int);
int main(){
int a = 10;
func(a);
cout<<"After function call value of a = " <<a<<endl;
}
void func(int a){
a = a + 5;
cout<<"Value of a inside function = " <<a<<endl;
}
Output:
Value of a inside function = 15
After function call value of a = 10
Call by reference:

● 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.

return_type function_name(dataType &var1, dataType &var2, …. )


#include<iostream>
using namespace std;
void func(int &);
int main(){
int a = 10;
func(a);
cout<<"After function call value of a = "<<a<<endl;
}
void func(int &a){
a = a + 5;
cout<<"Value of a inside function = "<<a<<endl;
}

Output:
Value of a inside function = 15

After function call value of a = 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 = 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:

If d = gcd(a, b), there exists x and y such that:

d = a.x + b.y

How to find x and y.

Read the recursion idea from book.


Function overloading:

Two function in C++ can have same names if they differ by their inputs types or
number of inputs.

E.g.

int fun(int, int)

int fun(float, float)

int fun(int, float)

int fun(int, int, int)

are different functions.

Depending on the inputs tuppes/numbers compiler decides which function to call.


#include <iostream>
using namespace std;
int fun( int, int);
float fun( float , float );
int fun( char, char);
int main(){
int x, y;
float x1, y1;
char c1, c2;
x = 5;
y = 6;
x1 = 1.2;
y1 = 5.3;
c1 = 'A';
c2 = 'B';
cout<< "Call with integer inputs " <<fun(x, y)<<endl;
cout<< "Call with float inputs " <<fun(x1, y1)<<endl;
cout<< "Call with charater inputs " <<fun(c1, c2)<<endl;
}
int fun( int a, int b){
return (a+b)% 5;
}
float fun( float a, float b){
return (a+b);
}
int fun( char a, char b){
return a+b;
}

You might also like