0% found this document useful (0 votes)
39 views15 pages

Programming C++ Assignment

The document contains code snippets that demonstrate using functions to encapsulate code and pass parameters. It includes examples of: 1) Defining functions to calculate velocity, conversions, and prime numbers and calling them from main. 2) Passing parameters to functions to get user input and return values. 3) Creating multiple functions to generate different patterns and selecting which one to call.

Uploaded by

Awais Arshad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
39 views15 pages

Programming C++ Assignment

The document contains code snippets that demonstrate using functions to encapsulate code and pass parameters. It includes examples of: 1) Defining functions to calculate velocity, conversions, and prime numbers and calling them from main. 2) Passing parameters to functions to get user input and return values. 3) Creating multiple functions to generate different patterns and selecting which one to call.

Uploaded by

Awais Arshad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 15

BS-(Geology& Geo Phy-3A)

Programming Fundamentals
Assignment No. 3

Q:1 Shift your 1st and 2nd assignment code in functions?

#include <iostream>
using namespace std;
int velo();

int main()
{
float v;
v = velo();
cout <<"Velocity is "<<v<<endl;
return 0;
}

int velo()
{
int d,t;
cout<<"Enter distance and time: ";
cin>>d>>t;
return (d/t);
}
Output:

Code 2:
#include <iostream>
using namespace std;
int deg();
int sec();
int lit();

int main()
{
float d,s,l;
cout<<"Press 1 for degree, 2 for second, 3 for liter";
int op;
cin >>op;
if (op==1)
{
d = deg();
cout <<"Degree is "<<d<<endl;
}
else if(op==2)
{
s = sec();
cout <<"Second is "<<s<<endl;
}
else if(op==3)
{
l = lit();
cout <<"Liter is "<<l<<endl;
}
return 0;
}

int deg()
{
int r;
cout<<"Enter Radian: ";
cin>>r;
return (57.2*r);
}
int sec()
{
int h;
cout<<"Enter hours: ";
cin>>h;
return (h*3600);
}
int lit()
{
int cm;
cout<<"Enter centi meter: ";
cin>>cm;
return (1000*cm);
}
Output:

Assignment # 2:
Code 1:
#include <iostream>
#include <cmath>
using namespace std;
//protype in Function
int adde();
int subr();
float mulp();
float divi();
float powe();
float table();
float fact();
float perc();

int main()
{
float ans;
char op;
do{
cout <<"Press '+' for Add, - for Sub, * for Mul, / for Div"<<endl;
cout <<"Press '#' for Table, % for Perc, ! for Factorial, ^ for power"<<endl;
cout <<"Press x for exist"<<endl;
cin >>op;

if (op=='+')
{
ans= adde(); //do calling
cout <<"Sum: "<<ans<<endl;
}

else if(op=='-')
{
ans= subr();
cout <<"Sub: "<<ans<<endl;
}
else if(op=='*')
{
ans= mulp();
cout <<"Mul: "<<ans<<endl;
}
else if(op=='/')
{
ans= divi();
cout <<"Div: "<<ans<<endl;
}
else if(op=='!')
{
int i,j,f=1;
ans= fact();
for(j=1;j<=ans;j++)
{
cout <<j<<"!=";
for(i=1;i<=j;i++)
{
f=f*i;
}
cout <<"Factorial "<<f<<endl;
f=1;
}}

else if (op=='^')
{
ans= powe();
cout <<"Power is : "<<ans<<endl;
}

else if (op=='%')
{
ans= perc();
cout <<"Percentage: "<<ans<<endl;
}

else if(op=='#')
{
ans = table();
for(int i=1;i<=10;i++)
{
cout<<ans<<"x"<<i<<"="<<ans*i<<endl;
}}

else
{
cout<< "Not Found"<<endl;
}}while(op!='x');
return 0;
}

//Function type without parameter


int adde()
{
int a,b;
cout<<"Enter 2 value";
cin>>a>>b;
return (a+b);
}

int subr()
{
int a,b;
cout<<"Enter 2 value";
cin>>a>>b;
return (a-b);
}

float mulp()
{
int a,b;
cout<<"Enter 2 value";
cin>>a>>b;
return (a*b);
}

float divi()
{
int a,b;
cout<<"Enter 2 value";
cin>>a>>b;
return (a/b);
}

float fact()
{
int r;
cout<<"Enter Factorial Range";
cin>>r;
return r;
}

float table()
{
int a;
cout<<"Enter Table Digit:"<<endl;
cin>>a;
return a;
}

float perc()
{
int a,b;
cout<<"Enter obtained then total Marks"<<endl;
cin>>a>>b;
return ((a/b)*100);
}

float powe()
{
int a,b;
cout<<"Enter base and exponent";
cin>>a>>b;
return (pow(a,b));
}
Output:

Code 2:

#include <iostream>
#include <cmath>
using namespace std;
//protype in Function
int prime();
int main()
{
int n,np=1;
n= prime();
for(int i=2;i<n-1;i++)
{
if(n%i!=0)
{continue;}
else
{np=0;}
}
if(np==0)
{
cout<<"Number is not Prime"<<endl;
}
else
{
cout <<"Number is prime"<<endl;
}
return 0;
}

//Function type without parameter


int prime()
{
int p;
cout<<"Enter Number";
cin>>p;
return p;
}
Output:

Code 3:
#include <iostream>
using namespace std;
void star();
void revister();
int main()
{
star();
revister();
return 0;
}

void star()
{
int i,j,k;
for (i=1;i<=5;i++)
{
for(j=4;j>=i;j--)
{cout<<" ";}
for(k=1;k<=(2*i-1);k++)
{cout<<"*";}
cout<<endl;
}
}

void revister()
{
int i,j,k;
for (i=4;i>=0;i--)
{
for(j=4;j>=i;j--)
{cout<<" ";}
for(k=1;k<=(2*i-1);k++)
{cout<<"*";}
cout<<endl;
}
}
Output:

Assignment 2b:

Code 1:
#include <iostream>
using namespace std;
void revidigit();
int main()
{
revidigit();
return 0;
}

void revidigit()
{
int a,i,j;
cout<<"Enter range of digits"<<endl;
cin>>a;
for (i=1;i<=a;i++)
{cout<<i;}
cout<<endl;
for(j=a;j>=1;--j)
{cout<<j;}
}
Output:

Code 2: (Passing by Parameter)


#include <iostream>
using namespace std;
int valofa(int);
int valofb(int);
int main()
{
int a,b,i,gcd=1, xy;
a=valofa(a);
b=valofb(b);
xy=min(a,b);
for(i=1;i<=xy;i++)
{
if(a%i==0 && b%i==0)
{
gcd=max(gcd,i);
}
}
cout<<"Greatest Common Divisor: "<<gcd<<endl;
return 0;
}

int valofa(int a)
{
cout<<"Enter 1st digit"<<endl;
cin>>a;
return a;
}

int valofb(int b)
{
cout<<"Enter 2nd digit"<<endl;
cin>>b;
return b;
}
Output:

Code 3a:
#include <iostream>
using namespace std;
int valofn(int);
int main()
{
int n,i,j,sum=0,t;
n=valofn(n);

for(i=1;i<=n;i++)
{
t=0;
for(j=1;j<=i;j++)
{
sum=sum+j;
t=t+j;
cout<<j;
if(j<i)
{
cout<<"+";
}
}
cout<<"="<<t<<endl;
}
cout<<"The sum of the series is: "<<sum<<endl;
return 0;
}

int valofn(int n)
{
cout<<"Enter the value of n"<<endl;
cin>>n;
return n;
}

Output:
Code 3b:
#include <iostream>
using namespace std;
void pattern1();
void pattern2();
void pattern3();
void pattern4();
int main()
{
int x;
cout <<"Selected your Pattern"<<endl;
cin >>x;
if(x==1)
{
pattern1();
}
else if(x==2)
{
pattern2();
}
else if(x==3)
{
pattern3();
}
else if(x==4)
{
pattern4();
}
else
cout <<"Not found"<<endl;
return 0;
}

void pattern1()
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
cout<<"*";
}
cout<<endl;
}
}

void pattern2()
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<10-i;j++)
{
cout<<"*";
}
cout<<endl;
}
}

void pattern3()
{
int i,j,k;
for(i=1;i<=10;i++)
{
for(j=1;j<=10-i;j++)
cout<<" ";
for(k=1;k<=i;k++)
cout<<"*";
cout<<endl;
}
}

void pattern4()
{
int i,j,k;
for(i=10;i>=1;i--)
{
for(j=1;j<=10-i;j++)
cout<<" ";
for(k=1;k<=i;k++)
cout<<"*";
cout<<endl;
}
}
Output:

Q:2 Write a program that calculate the sum of first 10 natural number using functions?

#include <iostream>
using namespace std;
int nat();
int main()
{
int ans;
ans = nat();
cout <<"Sum is "<<ans<<endl;
return 0;
}

int nat()
{
int n,sum=0;
cout<<"Enter a value: ";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<i<<" ";
sum=sum+i;
}
return sum;
}

Output:
Q:3 Create a c++program use built-in functionsfor following operations:
 log(), exp(), fmax(),fmin() Math Library

#include <iostream>
#include <cmath>
using namespace std;
int max();
int min();
float logx();
double expx();

int main()
{
float ans;
int op;
cout<<"Press '1' for max,'2' for min, '3' for exp, '4' for log";
cin >>op;
if(op==1)
{
ans=max();
cout<<"Max no: "<<ans<<endl;
}
else if(op==2)
{
ans=min();
cout<<"Min no: "<<ans<<endl;
}
else if(op==3)
{
ans=expx();
cout<<"Exp is "<<ans<<endl;
}
else if(op==4)
{
ans=logx();
cout<<"Log is "<<ans<<endl;
}
return 0;
}
int max()
{
float max,x,y;
cout<<"Enter two Value"<<endl;
cin >>x>>y;
max = fmax(x,y);
return max;
}

int min()
{
float min,x,y;
cout<<"Enter two Value"<<endl;
cin >>x>>y;
min= fmin(x,y);
return min;
}

double expx()
{
double x,ans;
cout<<"Enter a value Exp";
cin>>x;
ans = exp(x);
return ans;
}

float logx()
{
float x,ans;
cout<<"Enter Log Value";
cin>>x;
ans = log (x);
return ans;
}
Output:

You might also like