Week 3 Assignment
Week 3 Assignment
#include <iostream>
int Fact(int);
using namespace std;
int main()
{
int n;
cout<<"Enter any number : "<<endl;
cin>>n;
cout<<"Result is : "<<Fact(n);
}
int Fact(int n)
{
int Fact=1;
for(int i=2; i<=n; i++)
{
Fact*=i;
}
return Fact;
}
2- Write a program that prints even numbers between any two numbers using
functions?
#include <iostream>
void e(int, int);
using namespace std;
int main()
{
int x,y;
cout<<"Enter any two number : "<<endl;
cin>>x>>y;
e(x,y);
}
void e(int x, int y)
{
int e=1;
for(int i=x; i<=y; i++)
{
if(i%2==0)
{
cout<<"Even "<<i<<endl;
}
}
}
3-Write a program draws this shape of stars?
*
**
***
****
*****
#include <iostream>
using namespace std;
int main()
{
int r;
cout<<"Enter the raws : "<<endl;
cin>>r;
for(int raw=1; raw<=r; raw++)
{
for(int col=1; col<=raw; col++)
{
cout<<"*";
}
cout<<endl;
}
}
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
#include <iostream>
using namespace std;
int main()
{
for(int raw=1; raw<=5; raw++)
{
for(int col=1; col<=5; col++)
{
cout<<" * ";
}
cout<<endl;
}
}
#include <iostream>
using namespace std;
int main()
{
int N;
cout<<"Enter number of raws : "<<endl;
cin>>N;
for(int raw=1; raw<=N; raw++)
{
for(int col=1; col<=raw; col++)
{
cout<<raw<<" ";
}
cout<<endl;
}
}
#include <iostream>
using namespace std;
int main()
{
int N;
cout<<"Enter number of raws : "<<endl;
cin>>N;
for(int raw=1; raw<=N; raw++)
{
for(int col=1; col<=raw; col++)
{
cout<<col<<" ";
}
cout<<endl;
}
}
#include <iostream>
using namespace std;
int main()
{
int N;
cout<<"Enter number of : "<<endl;
cin>>N;
for(int i=1; i<=N; i++)
{
for(int j=1; j<=i; j++)
{
cout<<i*i<<" ";
}
cout<<endl;
}
}
#include <iostream>
using namespace std;
int main()
{
int N, F0=0, F1=1, F=0;
cout<<"Enter number "<<endl;
cin>>N;
for(int i=1; i<=N; i++)
{
F=F0+F1;
F0=F1;
F1=F;
cout<<F<<endl;
}
}
9-Write a program that takes the base and the exponent from the user and
prints the number?
#include <iostream>
using namespace std;
int main()
{
int N,X,PO=1;
cout<<"Enter the base : "<<endl;
cin>>X;
cout<<"Enter the exponent : "<<endl;
cin>>N;
for(int i=1; i<=N; i++)
{
PO*=X;
}
cout<<"The result is : "<<PO;
}