Fundamentals of Programming Lab
Lab Report 08
1st SEMESTER
Submitted to: Engr. Ali Hassan
Session: ME-14 Section: C Group:
SUBMITTED BY
Sr. Lab
No. Names CMSID Report Lab work Lab Tasks Total
(Theory)
1 Muhammad Abdullah 405642
Zafar Ghauri
School of Mechanical and Manufacturing Engineering
Lab Work
Q.1 Write a program in C++ using loop to print a series of astericks(*).
Input Program:
#include<iostream>
using namespace std;
int main()
int i,j;
cout<<"\n My name is Abdullah Zafar";
cout<<"\n My CMS ID is 405642 and i belong to Section ME-14(C)";
for(i=1;i<=5;i++) //for rows
cout<<"\n";
for(j=1;j<=20;j++) //for columns
cout<<"*";
return 0;
Output:
Q.2 Write a Program using while loops to take in few numbers and produce the result on the
basis of meeting of condition criterion.
Input Program:
#include<iostream>
using namespace std;
int main()
int a,b;
a=5;
b=9;
while(b>a)
cout<<b;
a=a+1;
return 0;
Output:
Otherway around of this program
#include<iostream>
using namespace std;
int main()
int a=5, b=9;
cout<<"\n My name is ABdullah Zafar and I study in ME-14(C) with CMS ID=405642"<<endl;
while(b>a)
cout<<(b>a)<<endl;
a++;
Output:
Q.4 Write a program using while loop to print out multiplication table (Say of 5)
Input Program:
#include<iostream>
using namespace std;
int main()
{
int a, b;
cout << "\n My name is ABdullah Zafar and I study in ME-14 with CMS=405642"
<< endl;
a = 5;
b = 0;
while(b <= 10)
{
cout << a << "X" << b << "=" << a * b << endl;
b++;
}
return 0;
}
Output Program:
Q.5 Write a C++ Program to calculate the final velocity of a body from a given value of
acceleration and time.
Input Program:
#include<iostream>
using namespace std;
int main()
const double v(0.0);
const double a(2.537);
double time, velocity;
cout<<"\n My name is Abdullah Zafar and I study in ME_14(C) with CMS ID=405642"<<endl;
cout<<"Time(s)\t\t Velocity (m/s) \n"<<endl;
time=0.0;
while(time<=20.0)
velocity=a*time+v;
cout<<time<<"\t\t"<<velocity<<endl;
++time;
}
return 0;
Output:
Q.6 Write a program using for loop to print a series of numbers.
Input:
#include<iostream>
using namespace std;
int main()
{
int a;
for(a=20;a>=0;--a)
{
cout<<a<<endl;
}
return 0;
}
Output:
Q.7 Write a program in C++ using nested looping to take in basic requirements of a student and
related to submission of lab report.
Input:
#include<iostream>
using namespace std;
int main()
char section;
string name, CMS_ID,title;
char ch;
cout<<"\n My NAme is ABdullah Zafar and I study in mE_14(C) with CMS 405642"<<endl;
do
cout<<"\n \t\t\t Please enter your Section Name=";
cin>>section;
cout<<"\n \t\t\t Please enter your name=";
cin>>name;
cout<<"\n \t\t\t Please enter your CMS ID=";
cin>>CMS_ID;
cout<<"\n \t\t\t Title=";
cin>>title;
cout<<"\n \t\t\t Press Y to submit again and press N to exit! ";
cin>>ch;
while(ch=='y'||ch=='Y');
return 0;
}
Output:
Lab Assignment
Q.1 Write a program using while looping to calculate the exponent of a number.
Input:
#include<iostream>
using namespace std;
int main()
{
X: //For continual movement
int a;
int b;
int c = 0;
int ans = 1;
cout << "\n Please Enter the Number(Base) (Whose exponennt is to be
calculated=";
cin >> a;
cout << "\n Please enter the Exponenet of the number=";
cin >> b;
while (c < b)
{
ans = ans*a;
c++;
}
cout << "\n The answer is=" << ans;
goto X;
return 0;
}
OR
#include<iostream>
using namespace std;
int main()
{
X: //For continual movement
int a;
int b;
int c = 1;
int ans = 1;
cout << "\n Please Enter the Number(Base) (Whose exponennt is to be
calculated=";
cin >> a;
cout << "\n Please enter the Exponenet of the number=";
cin >> b;
while (c <= b)
{
ans = ans*a;
c++;
}
cout << "\n The answer is=" << ans;
goto X;
return 0;
}
Output:
Q.2 Write a program to display a series of Fibonacci Numbers (Say upto 100 order).
#include <iostream>
using namespace std;
int main() {
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "\n My name is Abdullah Zafar and I study in ME-14 with CMS Id of
405642" << endl;
cout << "Enter the number of terms required for Fibonacci Series: ";
cin >> n;
cout << "The Fibonacci Series for given Number of terms shall be= " << endl;
for (int i = 1; i <= n; ++i) {
if (i == 1)
{
cout << "," << t1;
continue;
}
if (i == 2)
{
cout << "," << t2;
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << ",";
}
return 0;
}
Output:
Q. Using nested looping (preferable for loop with if statements) print out the series of first 100
prime numbers between 1 and 100.
Program Input:
#include<iostream>
using namespace std;
int main()
{
int i; //For the Series of Prime Numbers within a prescribed range
int j; //2 is the first prime number so it is the initial starting
poiunt for the loop
int n = 0;
cout << "\n The Prime numbers between the range of 1 and 100 are=";
for (i = 1; i <= 100; i++) //Progresses the loop from 1 to 100
{
for (j = 2; j < i; j++) //Progresses from 2 as it is the first prime
number
{
if (i % j == 0)
{
n++;
break;
}
}
if (n == 0 && i != 0)
cout << i << endl;
n = 0;
}
cout << endl;
return 0;
}
Output:
Q. Write a program in C++ that takes in an input of even numbers from 1 to 12 and prints the
sum of them. (The range is between 1 and 12).
Input Program:
#include<iostream>
using namespace std;
int main()
{
cout << "\n My name is Abduullah Zafar and I study in ME-14 with CMS
ID=405642" << endl;
int i = 1;
int sum = 0;
while (i <= 12)
{
if (i % 2 == 0)
{
sum = sum + i; //Alternatively sum+=1
}
i++;
}
cout << "The Sum of Even Numbers between 1 and 12 ="<< sum << endl;
return 0;
}
Output:
Q. Write a program to calculate the factorial of a number/numbers
Input Program:
#include<iostream>
using namespace std;
int main()
{
cout << "\n My name is Abdullah Zafar and I study in ME-14(C) with CMS
ID=405642" << endl;
X: //In order for the program to run again and again
performing multiple calculations
int n, m;
double f; //Initialized f with double type as it can hold
exponents of 308
cout << "\n Please Enter the Number whose Factorial is to be calculated=";
cin >> n;
m = n;
f = 1; //Initializing the value for f with 1
do
{
f = f * n;
n--; //Making decrement in the value of n in order to
calculate factorial
} while (n >= 1);
{
cout << "\n The Factorial of Number=" << f << endl;
}
goto X;
return 0;
}
Output: