Showing Page:
1/15
PF lab manual 10
Mahnoor Tariq
21-cs-37
Tasks:-
Write a program to check whether a entered no.
is prime no, even no. or odd no. using function.
(it must cover all possibilities) Hint : “2 is
a prime even no.”
PROGRAM:
#include<iostream>
using namespace std;
void numcheck(int);
int main()
{
int a;
cout<<"Enter Number :";
cin>>a;
Showing Page:
2/15
numcheck(a);
return 0;
}
void numcheck(int x)
{
if(x%2==0)
{
cout<<"You Entered even Number"<<endl;
}
else if(x%x==0 && x%1==0)
{
cout<<"You Entered prime Number"<<endl;
if(x%2==!0)
{
cout<<"This number is also odd number"<<endl;
}
}
}
Showing Page:
3/15
Task 2:
Write a program that inputs a no. in main function and passes the
no. to a function. Print the table of the number coming after that
entered no.
PROGRAM:
#include<iostream>
using namespace std;
void table(int);
int main()
{
int a;
Showing Page:
4/15
cout<<"Enter Number :";
cin>>a;
table(a);
return 0;
}
void table(int x)
{
for(int i=1;i<=10;i++)
{
cout<<x+1<<'*'<<i<<'='<<(x+1)*i<<endl;
}
}
Task 3:
Showing Page:
5/15
Write a program inputs a no. and a character in main function and
passes them to a function. It then displays a square of that entered
character against the no. of rows user entered. Hint: if user enter 5 and
&, it displays output as follows:
&&&&&
&&&&&
&&&&&
&&&&&
&&&&&
PROGRAM:
#include<iostream>
#include<cstdlib>
#include <math.h>
using namespace std;
void square(int,char);
int main()
{
int a;
char ch;
cout<<"Enter Number :";
cin>>a;
cout<<"Enter Character :";
Showing Page:
6/15
cin>>ch;
square(a,ch);
return 0;
}
void square(int x,char y)
{
for(int i=1;i<=x;i++)
{
for(int j=1;j<=x;j++)
{
cout<<y;
}
cout<<endl;
}
}
Showing Page:
7/15
Task 4:
Write a program to sort an array in ascending order using
function.
PROGRAM:
#include<iostream>
#include<cstdlib>
#include <math.h>
using namespace std;
void sort(int[]);
int main()
Showing Page:
8/15
{
int num[10];
for( int k=0;k<10;k++)
{
cout<<"Enter "<<k+1<<"th Number :";
cin>>num[k];
}
sort(num);
return 0;
}
void sort(int number[])
{ int a;
for (int i = 0; i < 10; ++i)
{
for (int j = i + 1; j < 10; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
Showing Page:
9/15