Programming fundamentals
LAB Report 07
By
NAME Regestration Number
Mobeen Ayyaz FA23-BCE 2B-068
For the course
Programming Fundamentals
Submitted to:
Usman Khalid
Departmentof Electrical & Computer Engineering
COMSATS University Islamabad -Abbottabad Campus
MOBEEN AYYAZ FA23-BCE-068
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:
(fig 1: program output)
(b) Repeat the task 1 by using while loop.
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:
(fig 2: program 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:
(fig 3: program output)
Task 3: (a) Write a C++ program that prompts the user to enter a number and displays its
3 | Page
MOBEEN AYYAZ FA23-BCE-068
multiplication table up to 10 by using for loop.
Code:
#include<iostream>
using namespace std;
int main()
{
int i,a,result;
cout<<"enter a number" ;
cin>>a;
for(i=1;i<=10;i++)
{
result=a*i;
cout<<a<<"*"<<i<<"="<<result<<"\n";
}
return 0;
}
Output:
(fig 4: program output)
(b) Repeat the task 3 by using while loop.
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;
while (i <= 10)
{
result = a * i;
cout << a << " * " << i << " = " << result << "\n";
i++;
}
return 0;
}
Output:
(fig 5: program 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;
for (int i = 1; i <= b; ++i)
{
5 | Page
MOBEEN AYYAZ FA23-BCE-068
result *= a;
}
cout << "Result: " << result << endl;
return 0;
}
Output:
(fig 6: program output)
(b) Repeat the task 4 by using while loop.
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)
Student Name: Reg. No.: .
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)
Obtained Marks = __________ ___________________________ Total Marks: 10
Course Instructor:
MOBEEN AYYAZ FA23-BCE-068
(fig 7: program output)
7 | Page