PF Grand Assingnment
PF Grand Assingnment
PF Grand Assingnment
PF EXERCISE
SALEHA TAIMOOR
DR SHARIQ HUSSAIN
2
LAB 2
EXERCISE 2.1
#include <iostream>
int main()
float average;
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>
int main()
Excercise 2.3
#include <iostream>
int main()
return 0;
}
7
LAB 3
Exercise 3.1
#include<iostream>
int main()
int n1,n2;
in a,b;
cin>>n1>>n2;
a=(n1*n1)+2*(n1)*(n2)+(n2*n2);
b=(n1*n1)-2*(n1)*(n2)+(n2*n2);
8
cout<<"\n\n2.(a-b)^2=a^2-2ab+b^2";
cout<<"\n ("<<n1<<"-"<<n2<<")^2="<<n1<<"^2-2("<<n1<<"*"<<n2<<")+"<<n2<<"^2";
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>
in main()
float first_number=0;
float second_number=0;
cin>>first_number;
cout<<endl;
cin>>second_number;
cout<<endl;
cout<<endl;
cout<<endl;
if(first_number>second_number)
else
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.
#include<iostream>
in main()
11
{in first_no=0;
in second_no=0;
in third_no=0;
in fourth_no=0;
in fifth_no=0;
cin>>first_no;
cout<<endl;
cin>>second_no;
cout<<endl;
cin>>third_no;
cout<<endl;
cin>>fourth_no;
cout<<endl;
cin>>fifth_no;
cout<<endl;
if((first_no>second_no)&&(first_no>third_no)&&(first_no>fourth_no)&&(first_no>fifth_no))
else
if((second_no>first_no)&&(second_no>third_no)&&(second_no>fourth_no)&&(second_no>fift
h_no))
12
else
if((third_no>first_no)&&(third_no>second_no)&&(third_no>fourth_no)&&(third_no>fifth_no))
else
if((fourth_no>first_no)&&(fourth_no>second_no)&&(fourth_no>third_no)&&(fourth_no>fifth_
no)
else
return 0;
}
13
Exercise 4.3
#include<iostream>
in main()
char ch='a';
number1=number2=0;
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>
in main()
in number1,number2;
char ch='*';
15
number1=number2=0;
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>
in main()
17
in num,sum
in i;
num=sum=0;
cin>>num;
i=1;
while(i<=num)
sum=sum+i;i++;
return 0;
}
18
Exercise # 5.2
#include<iostream>
in main()
in num,sum;
in i;
num=sum=0;
cin>>num;
i=1;
do
sum=sum+i
i++;
while(i<=num);
return 0;
Exercise # 5.3
#include<iostream>
in main()
in number=0;
in factorial=1;
in i;
cin>>number;
20
return 0;
LAB 6
Exercice # 6.1
#include<iostream>
#include<iomanip>
in main()
in num=0;
21
in outer,inner;
in pos=40;
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>
in main()
{
23
result=outer; // variable
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
Enter a number : 4
Sum = 1^1 + 2^2 + 3^3 + 4^4 = 288
#include<iostream>
#include<cmath>
in main()
in num,sum,count_power,count_base;
sum=0;
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>
void seprator();
void multiples(in);
27
// Main Function
in main()
in n=0, i=1;
cin>>n;
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>
in DivTotal = 0, Div;
if (Div == 0)
DivTotal += halfNum;
if (DivTotal == number)
in main()
in num=0;
cin>>num;
cout << "\nPerfect numbers between 1 and "<<num<<" are:" << endl;
{
30
perfect(count);
Exercise 7.3
#include<iostream>
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;
cin>>int1>>int2>>int3>>int4;
res=largest(int1,int2,int3,int4);
return 0;
}
32
33
PF EXERCISE
SALEHA TAIMOOR
DR SHARIQ HUSSAIN
34
LAB 2
TASK 1
#include<iostream>
int main()
int a;
float b;
char c;
cin>>a;
cout<<endl;
cin>>b;
cout<<endl;
cin>>c;
cout<<endl;
cout<<endl;
return 0;
TASK 2
#include<iostream>
int main()
int n1;
36
int n2;
cin>>n1;
cin>>n2;
cout<<"Sum : ";
cout<<"Difference : ";
cout<<endl;
cout<<"Product : ";
cout<<endl;
cout<<"Quotient : ";
cout<<"Remainder : ";
return 0;
}
37
TASK 3
#include<iostream>
int main(void)
cout<<"BCSE\n\tBCSE\n\t\tBCSE\n\t\t\tBCSE";
return 0;
}
38
LAB 3
TASK 1
#include <iostream>
int main()
char c;
cin >> c;
cout << "ASCII Value of " << c << " is " << int(c);
39
return 0;
TASK 2
#include<iostream>
int main()
float temp_in_c;
float temp_in_f;
cin>>temp_in_c;
40
cout<<endl;
temp_in_f=((temp_in_c)*1.8)+32;
return 0;
TASK 3
#include<iostream>
int main()
{
41
int num=0;
int hundreds,tens,ones;
int temp=0;
cin>>num;
cout<<endl;
hundreds=num/100;
temp=num%100;
tens=temp/10;
ones=temp%10;
return 0;
}
42
LAB 4
NO TASK
LAB 5
43
TASK 5
#include <iostream>
int main ()
int number=0;
int even=0;
int even_sum=0;
for(even=2;even<=number;even=even+2)
cout<<" "<<even;
even_sum=even_sum+even;
return 0;
TASK 2
#include <iostream>
int main ()
int num;
int sum=0;
45
int i=1;
while(i<=num)
if(i%2==0)
cout<<i<<"\t";
sum+=i;
i++;
return 0;
}
46
LAB 6
TASK 1
Part a
#include<iostream>
int main()
for(int j=0;j<=i;j++)
{
47
cout<<"*";
cout<<"\n";
return 0;
Part b
#include<iostream>
int main()
for(int j=0;j<=i;j++)
cout<<"*";
cout<<"\n";
return 0;
Part c
#include<iostream>
int main()
49
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>
int main()
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>
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>
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>
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