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

Week 3 Assignment

Uploaded by

iirwed79
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Week 3 Assignment

Uploaded by

iirwed79
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1- Write a factorial program using function?

#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;
}
}

4-Write a program draws this shape of stars?

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

#include <iostream>
using namespace std;
int main()
{
for(int raw=1; raw<=5; raw++)
{
for(int col=1; col<=5; col++)
{
cout<<" * ";
}
cout<<endl;
}
}

5-Write a program that draws this shape?


1
22
333
4444
55555

#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;
}
}

6-Write a program that draws this shape?


1
12
123
1234
12345

#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;
}
}

7-Write a program that draws this shape?


1
44
999
16 16 16 16
25 25 25 25 25

#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;
}
}

8-Write a program that applies fibonacci using loop?

#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;
}

You might also like