PF Grand Assingnment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 60

1

PF EXERCISE
SALEHA TAIMOOR

DR SHARIQ HUSSAIN
2

LAB 2
EXERCISE 2.1

#include <iostream>

using namespace std;

int main()

int num1, num2, num3;

int max, min;

int sum, product;

float average;

cout<<"Input three different integers:"<<endl;

cin>>num1>>num2>>num3;

sum=num1+num2+num3;

product=num1*num2*num3;

average=sum/3.0;

cout<<"Sum is "<<sum<<endl;

cout<<"Average is "<<average<<endl;

cout<<"Product is "<<product<<endl;

min=num1;
3

if(num2<min)

min=num2;

if(num3<min)

min=num3;

cout<<"Smallest is "<<min<<endl;

max=num1;

if(num2>max)

max=num2;

if(num3>max)

max=num3;

cout<<"Largest is "<<max<<endl;

return 0;

}
4

EXERCISE 2.2

#include <iostream>

using namespace std;

int main()

cout<< " ************* ********* " <<endl;

cout<< " * * * * " <<endl;

cout<< " * * * * " <<endl;

cout<< " * * * * " <<endl;


5

cout<< " * * * * " <<endl;

cout<< " * * * * " <<endl;

cout<< " * * * * " <<endl;

cout<< " * * * * " <<endl;

cout<< " * * * * " <<endl;

cout<< " ************* ********* " <<endl;

Excercise 2.3
#include <iostream>

using namespace std;


6

int main()

cout<<" integer square cube " <<endl;

cout<<" 0 0 0 " <<endl;

cout<<" 1 1 1 " <<endl;

cout<<" 2 4 8 " <<endl;

cout<<" 3 9 27 " <<endl;

cout<<" 4 16 64 " <<endl;

cout<<" 5 25 125 " <<endl;

cout<<" 6 36 216 " <<endl;

cout<<" 7 49 343 " <<endl;

cout<<" 8 64 512 " <<endl;

cout<<" 9 81 729 " <<endl;

cout<<" 10 100 1000" <<endl;

return 0;

}
7

LAB 3
Exercise 3.1
#include<iostream>

using namespace std;

int main()

int n1,n2;

in a,b;

cout<<"Enter two numbers: ";

cin>>n1>>n2;

a=(n1*n1)+2*(n1)*(n2)+(n2*n2);

b=(n1*n1)-2*(n1)*(n2)+(n2*n2);
8

cout<<"\n1.(a+b)^2 = a^2 + 2ab + b^2";

cout<<"\n ("<<n1<<"+"<<n2<<")^2 = "<<n1<<"^2 + 2("<<n1<<" * "<<n2<<") +


"<<n2<<"^2";

cout<<"\n ("<<n1<<"+"<<n2<<")^2 ="<<a;

cout<<"\n\n2.(a-b)^2=a^2-2ab+b^2";

cout<<"\n ("<<n1<<"-"<<n2<<")^2="<<n1<<"^2-2("<<n1<<"*"<<n2<<")+"<<n2<<"^2";

cout<<"\n ("<<n1<<"+"<<n2<<")^2 ="<<b;

return 0;

LAB 4

EXERCISE 4.1.
9

Write a C++ program which takes two numbers from the user and tells which number is greater.
Your output should be as below.
Enter the first number : 25
Enter the second number : 32
First number is 25 and second number is 32.
Second number is greater.

#include<iostream>

using namespace std;

in main()

float first_number=0;

float second_number=0;

cout<<"Enter the first number : ";

cin>>first_number;

cout<<endl;

cout<<"Enter the second number : ";

cin>>second_number;

cout<<endl;

cout<<"First number is "<<first_number<<" and second number is "<<second_number;

cout<<endl;

cout<<endl;

if(first_number>second_number)

cout<<"First number is greater";

else

cout<<"Second number is greater";

return 0;

}
10

Exercise 4.2
Write a C++ program which takes five numbers from the user and finds the largest of these
numbers. Your output should be as below.

Enter the first number : 25


Enter the second number : 32
Enter the third number : 12
Enter the fourth number : 85
Enter the fifth number : 53
The largest of the 5 numbers is 85.

#include<iostream>

using namespace std;

in main()
11

{in first_no=0;

in second_no=0;

in third_no=0;

in fourth_no=0;

in fifth_no=0;

cout<<"Enter the first number : ";

cin>>first_no;

cout<<endl;

cout<<"Enter the second number : ";

cin>>second_no;

cout<<endl;

cout<<"Enter the third number : ";

cin>>third_no;

cout<<endl;

cout<<"Enter the fourth number : ";

cin>>fourth_no;

cout<<endl;

cout<<"Enter the fifth number : ";

cin>>fifth_no;

cout<<endl;

if((first_no>second_no)&&(first_no>third_no)&&(first_no>fourth_no)&&(first_no>fifth_no))

cout<<"The largest of the 5 numbers is "<<first_no;

else
if((second_no>first_no)&&(second_no>third_no)&&(second_no>fourth_no)&&(second_no>fift
h_no))
12

cout<<"The largest of the 5 numbers is "<<second_no;

else
if((third_no>first_no)&&(third_no>second_no)&&(third_no>fourth_no)&&(third_no>fifth_no))

cout<<"The largest of the 5 numbers is "<<third_no;

else
if((fourth_no>first_no)&&(fourth_no>second_no)&&(fourth_no>third_no)&&(fourth_no>fifth_
no)

out<<"The largest of the 5 numbers is "<<fourth_no;

else

cout<<"The largest of the 5 numbers is "<<fifth_no;

return 0;

}
13

Exercise 4.3
#include<iostream>

using namespace std;

in main()

char ch='a';

number1=number2=0;

cout<<"Enter the arithmetic expression to be evaluated : \n";

cin>>number1;

cout<<endl;

cin>>ch;

cout<<endl;

cin>>number2;

cout<<endl;

if(ch=='+')

cout<<number1<<"+"<<number2<<"="<<number1+number2

else if(ch=='-')

cout<<number1<<"-"<<number2<<"="<<number1-number2;

else if(ch=='*')

cout<<number1<<"*"<<number2<<"="<<number1*number2;

else if(ch=='/')

cout<<number1<<"/"<<number2<<"="<<number1/number2;

else if(ch=='%')
14

cout<<number1<<"%"<<number2<<"="<<number1%number2;

else

cout<<" ";

return 0;

By using switch
#include<iostream>

using namespace std;

in main()

in number1,number2;

char ch='*';
15

number1=number2=0;

cout<<"enter the arithmetic expression to be evauated : ";

cout<<endl;

cin>>number1;

cin>>ch;

cin>>number2;

switch(ch)

case'+':

cout<<number1<<"+"<<number2<<"="<<number1+number2;

case'-':

cout<<number1<<"-"<<number2<<"="<<number1-number2;

break;

case'*':

cout<<number1<<"*"<<number2<<"="<<number1*number2;

break;

case'/':

cout<<number1<<"/"<<number2<<"="<<number1*number2;

break;

case'%':

cout<<number1<<"%"<<number2<<"="<<number1%number2;

break;

default:

cout<<" ";
16

return 0;

LAB 5

Exercise 5.1

#include<iostream>

using namespace std;

in main()
17

in num,sum

in i;

num=sum=0;

cout<<"Enter any value : ";

cin>>num;

i=1;

while(i<=num)

sum=sum+i;i++;

cout<<"Sum of integers from 1 to "<<num<<" is : "<<sum;

return 0;

}
18

Exercise # 5.2

#include<iostream>

using namespace std;

in main()

in num,sum;

in i;

num=sum=0;

cout<<"Enter any value : ";

cin>>num;

i=1;

do

sum=sum+i

i++;

while(i<=num);

cout<<"Sum of integers from 1 to "<<num<<" is : "<<sum;


19

return 0;

Exercise # 5.3

#include<iostream>

using namespace std;

in main()

in number=0;

in factorial=1;

in i;

cout<<"Enter any value to calculate its factorial : ";

cin>>number;
20

for(i=1;i<=number;i++)//using for loopfactorial=factorial*i;

cout<<"\nThe factorial of "<<number<<" is "<<factorial;

return 0;

LAB 6
Exercice # 6.1

#include<iostream>

#include<iomanip>

using namespace std;

in main()

in num=0;
21

in outer,inner;

in pos=40;

cout<<"Enter a number : ";

cin>>num;

cout<<endl<<endl

for(outer=1;outer<=num;outer=outer+2){

cout<<setw(pos);

for(inner=1;inner<=outer;inner++)

cout<<"*";

pos--;

cout<<endl;

in pos1=pos;

for(in outer1=outer-2;outer>=1;outer=outer-2)

cout<<setw(pos1)

for(in inner=1;inner<=outer;inner++)

cout<<"*";

}
22

pos1++;cout<<endl;

return 0;

Exercice # 6.2

#include<iostream>

#include<iomanip>

using namespace std;

in main()

{
23

nt outer,inner; // variables declare

in result=1; // initialize a variable

for(outer=1;outer<=12;outer++) // for loop

result=outer; // variable

for(inner=1;inner<=12;inner++) // for loop

cout<<setw(3)<<result*inner// output

cout<<" ";

} // end of loop

cout<<endl;

return 0;

} // end of program
24

Exercise # 6.3
Write a C++ program which reads in a number from user, stores it in a variable named ‘N’ and
calculates the sum of powers using the following formula.

Sum = 11 + 22 + 33 +……+NN

Your program should have the following interface.

Enter a number : 4
Sum = 1^1 + 2^2 + 3^3 + 4^4 = 288

#include<iostream>

#include<cmath>

using namespace std;


25

in main()

in num,sum,count_power,count_base;

sum=0;

cout<<"\nEnter a number : ";

cin>>num;

cout<<"\nsum = ";

for(count_power=1;count_power<=num;count_power++)

for(count_base=1;count_base<=num;count_base++)

if(count_power==count_base)

cout<<count_base<<"^"<<count_power<<"+\t";

sum=sum+pow(count_base,count_power);

cout<<" = "<<sum;

return 0;

}
26

LAB 7
Exercise 7.1:

#include<iostream>

using namespace std;

void seprator();

void multiples(in);
27

// Main Function

in main()

in n=0, i=1;

cout<<"Enter the value of N : "

cin>>n;

cout<<"The tables from"<<" "<< i <<" to "<<n<<endl<<endl;

for(in i=1;i<=n;i++)

multiples(i)

cout<<"\n";

seprator();

cout<<"\n";

return 0;

void seprator()

in i;

for(i=1;i<=15;i++)

cout<<"* ";
28

void multiples(in a)

for(in j=1;j<=10;j++

cout<<a*j<<" ";

}
29

Exercise 7.2

#include <iostream>

using namespace std;

void perfect(in number)

in DivTotal = 0, Div;

for (in halfNum = number / 2; halfNum >= 1; halfNum--)

Div = number % halfNum;

if (Div == 0)

DivTotal += halfNum;

if (DivTotal == number)

cout << number << endl;

in main()

in num=0;

cout<<"Enter value for N :"

cin>>num;

cout << "\nPerfect numbers between 1 and "<<num<<" are:" << endl;

for (in count = 1; count <= num; count++)

{
30

perfect(count);

Exercise 7.3

#include<iostream>

using namespace std;

in largest (in number1,int number2,int number3,int number4)


31

in res;

if(number1>number2&&number1>number3&&number1>number4)

res=number1;

else if(number2>number1&&number2>number3&&number2>number4)

res=number2;

else if(number3>number1&&number3>number2&&number3>number4)

res=number3;

else

res=number4;

return(res);

in main()

in int1,int2,int3,int4,res;

int1=int2=int3=int4=0;

cout<<"Enter four integers :\n"

cin>>int1>>int2>>int3>>int4;

res=largest(int1,int2,int3,int4);

cout<<"The Largest integers among "<<int1<<", "<<int2<<", "<<int3<<", "<<int4<<" is "<<res;

return 0;

}
32
33

PF EXERCISE
SALEHA TAIMOOR

DR SHARIQ HUSSAIN
34

LAB 2
TASK 1

#include<iostream>

using namespace std;

int main()

int a;

float b;

char c;

cout<<"Enter integer value:";

cin>>a;

cout<<endl;

cout<<"Enter float value:";

cin>>b;

cout<<endl;

cout<<"Enter character value:";

cin>>c;

cout<<endl;

cout<<"***** You have entered the following values *****";

cout<<endl;

cout<<"Integer value is: "<<a<<endl;

cout<<"Float value is: "<<b<<endl;


35

cout<<"Character value is: "<<c<<endl;

return 0;

TASK 2

#include<iostream>

using namespace std;

int main()

int n1;
36

int n2;

cout<<"Enter value 1 : ";

cin>>n1;

cout<<"Enter value 2: ";

cin>>n2;

cout<<"Sum : ";

cout<<n1<<" + "<<n2<<" = "<<n1+n2<<endl;

cout<<"Difference : ";

cout<<n1<<" - "<<n2<<" = "<<n1-22;

cout<<endl;

cout<<"Product : ";

cout<<n1<<" * "<<n2<<" = "<<n1*n2;

cout<<endl;

cout<<"Quotient : ";

cout<<n1<<" / "<<n2<<" = "<<n1/n2<<endl;

cout<<"Remainder : ";

cout<<n1<<" % "<<n2<<" = "<<n1%n2;

return 0;

}
37

TASK 3

#include<iostream>

using namespace std;

int main(void)

cout<<"BCSE\n\tBCSE\n\t\tBCSE\n\t\t\tBCSE";

return 0;

}
38

LAB 3
TASK 1

#include <iostream>

using namespace std;

int main()

char c;

cout << "Enter a character: ";

cin >> c;

cout << "ASCII Value of " << c << " is " << int(c);
39

return 0;

TASK 2

#include<iostream>

using namespace std;

int main()

float temp_in_c;

float temp_in_f;

cout<<"Enter the temperature in Centigrade: ";

cin>>temp_in_c;
40

cout<<endl;

temp_in_f=((temp_in_c)*1.8)+32;

cout<<temp_in_c<<" Degrees Centigrade is equal to "<<temp_in_f<<" Fahrenheit";

return 0;

TASK 3

#include<iostream>

using namespace std;

int main()

{
41

int num=0;

int hundreds,tens,ones;

int temp=0;

cout<<"Enter a three digit number : ";

cin>>num;

cout<<endl;

hundreds=num/100;

temp=num%100;

tens=temp/10;

ones=temp%10;

cout<<"There are "<<hundreds<<" Hundreds, "<<tens<<" Tens and "<<ones<<" Ones in


"<<num;

return 0;

}
42

LAB 4
NO TASK

LAB 5
43

TASK 5

#include <iostream>

using namespace std;

int main ()

int number=0;

int even=0;

int even_sum=0;

cout << "Enter a number : ";

cin >> number;

cout<<"\nEven numbers less than or equal to "<<number<<" are\n";

for(even=2;even<=number;even=even+2)

cout<<" "<<even;

even_sum=even_sum+even;

cout<<"\nSum of even numbers : "<<even_sum;


44

return 0;

TASK 2

#include <iostream>

using namespace std;

int main ()

int num;

int sum=0;
45

cout << "Enter a number : ";

cin >> num;

cout<<"\nEven numbers less than or equal to "<<num<<" are\n";

int i=1;

while(i<=num)

if(i%2==0)

cout<<i<<"\t";

sum+=i;

i++;

cout<<"\nSum of even numbers : "<<sum;

return 0;

}
46

LAB 6
TASK 1
Part a
#include<iostream>

using namespace std;

int main()

for(int i=0; i<10; i++)

for(int j=0;j<=i;j++)

{
47

cout<<"*";

cout<<"\n";

return 0;

Part b
#include<iostream>

using namespace std;

int main()

for(int i=10; i>=0; i--)


48

for(int j=0;j<=i;j++)

cout<<"*";

cout<<"\n";

return 0;

Part c
#include<iostream>

using namespace std;

int main()
49

for(int i=0; i<10; i++)

for(int j=10;j>i;j--)

cout<<"*";

cout<<"\n";

for(int k=0;k<=i;k++)

cout<<" ";

return 0;

}
50

Part d
#include<iostream>

using namespace std;

int main()

for(int i=0; i<10; i++)

for(int k=10;k>i;k--)

cout<<" ";

for(int j=0;j<i;j++)
51

cout<<"*";

cout<<"\n";

return 0;

TASK 2

#include<iostream>

using namespace std;

int main()
52

int outer,inner;

cout<<"(a)\n";

outer=1;

while(outer<=10)

inner=1;

while(inner<=outer)

cout<<"*";

inner++;

cout<<"\n";

outer++;

cout<<"\n(b)\n";

outer=10;

while(outer>=1)

inner=1;

while(inner<outer)

cout<<"*";

inner++;
53

cout<<"\n";

outer--;

cout<<"\n(c)\n";

outer=1;

while(outer<=10)

inner=1;

while(inner<outer)

cout<<" ";

inner++;

outer=inner;

while(inner<=10)

cout<<"*";

inner++;

cout<<"\n";

outer++;
54

cout<<"(d)\n";

outer=1;

while(outer<=10)

inner=outer;

while(inner<10)

cout<<" ";

inner++;

inner=1;

while(inner<=outer)

cout<<"*";

inner++;

cout<<"\n";

outer++;

return 0;
55

TASK 3

Write a C++ program to generate a 4-digit number using nested for loops and displays each
number from 0000 to 9999. You will have to use 3 nested ‘for’ loops to generate the output.

#include<iostream>

using namespace std;

int main()

int a,b,c,d,e;

for(a=0;a<=9;a++)

{
56

for(b=0;b<=9;b++)

for(c=0;c<=9;c++)

for(d=0;d<=9;d++)

for(e=0;e<=9;e++)

cout<<a<<b<<c<<d<<e<<"\n";

return 0;

}
57

TASK 4
58

#include<iostream>

using namespace std;

int main()

int a,b,c,d,e;

a=0;

while(a<=9)

{ b=0;

while(b<=9)

{ c=0;

while(c<=9)

{ d=0;

while(d<=9)

{e=0;

while(e<=9)

cout<<a<<b<<c<<d<<e<<"\n";

e++;

d++;

c++;

}
59

b++;

a++;

return 0;

}
60

LAB 7

NO TASK

You might also like