M0been 7
M0been 7
LAB Report 07
By
Task 1: (a) Write a C++ program to calculate the factorial of a number using a for loop.
Code:
#include<iostream>
using namespace std;
int main()
{
int i,a,result=1;
cout<<"enter your number ";
cin>>a;
for(i=1;i<=a;i++)
{
result=result*i;
}
cout<<"factorial of number is ="<<result;
return 0;
}
Output:
Code:
#include <iostream>
using namespace std;
int main()
{
int i = 1, a, result = 1;
cout << "Enter your number: ";
cin >> a;
while (i <= a)
{
result *= i;
i++;
}
2 | Page
MOBEEN AYYAZ FA23-BCE-068
cout << "Factorial of " << a << " is " << result << endl;
return 0;
}
Output:
Task 2: Write a C++ program to find the sum of all even numbers between 1 and 100 using a
for loop.
Code:
#include<iostream>
using namespace std;
int main()
{
int i,a,sum=0;
for(i=1;i<=100;i++)
{
if(i%2==0)
{
sum=sum+i;
}
}
cout<<sum;
return 0;
}
Output:
for(i=1;i<=10;i++)
{
result=a*i;
cout<<a<<"*"<<i<<"="<<result<<"\n";
}
return 0;
}
Output:
Code:
#include <iostream>
using namespace std;
int main()
{
int i = 1, a, result;
cout << "Enter a number: ";
4 | Page
MOBEEN AYYAZ FA23-BCE-068
cin >> a;
return 0;
}
Output:
Task 4: (a) Create a C++ program to calculate the power of a number. Prompt the user for
the base and exponent. Use a loop to calculate the result by multiplying the base by itself
according to the provided exponent. Then, display the result.
Code:
#include <iostream>
using namespace std;
int main()
{
int a, b, result = 1;
cout << "Enter a base: ";
cin >> a;
cout << "Enter an exponent: ";
cin >> b;
5 | Page
MOBEEN AYYAZ FA23-BCE-068
result *= a;
}
return 0;
}
Output:
Code:
#include<iostream>
using namespace std;
int main()
{
int i=1,a,b,result=1;
cout<<"enter a base " ;
cin>>a;
cout<<"enter a exponent ";
cin>>b;
for(i=1;i<=b;i++)
{
result=a*result;
}
cout<<result;
return 0;
}
Output:
6 | Page
Programming Fundamentals
Laboratory Assessment (Typical Labs)
Learning Level
Marks
Allocated
Criteria Obtained
Marks Poor Good Very Good Excellent (A)
0 - 49% 50% - 70% 71% - 80% 81%-100%
Affective 10%
(CLO-1)
Learning Level
Marks
Allocated
Criteria Obtained
Marks Poor Good Very Good Excellent
(P)
0 - 49% 50% - 70% 71% - 80% 81%-100%
Psychomotor
(CLO-2) 70%
Learning Level
Marks
Allocated
Criteria Obtained
Marks Poor Good Very Good Excellent
(C)
0 - 49% 50% - 70% 71% - 80% 81%-100%
Cognitive 20%
(CLO-3)
Course Instructor:
MOBEEN AYYAZ FA23-BCE-068
7 | Page