Name: Aadil Section: B CMS ID 053-23-0056
Programming Lab 06
Q1. Write a C++ program that should ask a user to input an integer number and then displays/prints its
table as shown below:
CODE: -
#include <iostream>
using namespace std;
int main()
int number;
cout<<"Enter any number to print its table : ";
cin>>number;
for(int i=1; i<=10;i++)
cout<<number<<"*"<<i<<" = "<<number*i<<endl;
return 0;
OUTPUT: -
Name: Aadil Section: B CMS ID 053-23-0056
Q2. Write a program that ask user to input the start and the End of the series. Then it prints whole,
natural, Even and odd numbers between these ranges. Let’s suppose the user enters value 4 for start,
and value 12 for start End. Then the output of the program:
CODE: -
#include <iostream>
using namespace std;
int main()
int e,s,x;
cout<<"Start the series with the number ";
cin>>s;
cout<<"End the series with number ";
cin>>e;
cout<<"Whole numbers ";
for(int x=s;x<=e;x++)
cout<<x<<" ";
cout<<"\nNatural numbers ";
for(int x=s;x<=e;x++)
cout<<x<<" ";
cout<<"\nEven numbers ";
for(int x=s;x<=e;x++)
if(x%2==0)
cout<<x<<" ";
cout<<"\nOdd numbers ";
for(int x=s;x<=e;x++)
if(x%2!=0)
cout<<x<<" ";
Name: Aadil Section: B CMS ID 053-23-0056
return 0;
OUTPUT: -
Q3. Write a program that ask user to input the start and the End of the series. Then it prints whole,
natural, Even and odd numbers between these ranges but in reverse order. Let’s suppose the user
enters value 0 for start, and value 10 for start End. Then the output of the program:
CODE: -
#include <iostream>
using namespace std;
int main()
int end,start;
cout<<"Enter starting number of series: ";
cin>>start;
cout<<"Enter ending number of series: ";
cin>>end;
cout<<"The whole numbers ";
for(int x=end;x>=start;x--)
cout<<x<<" ";
cout<<"\nThe natural numbers ";
Name: Aadil Section: B CMS ID 053-23-0056
for(int x=end;x>=start;x--)
if(end!=0||start!=0)
cout<<x<<" ";
cout<<"\nThe even numbers ";
for(int x=end;x>=start;x--)
if(x%2==0)
cout<<x<<" ";
cout<<"\nThe odd numbers ";
for(int x=end;x>=start;x--)
if(x%2!=0)
cout<<x<<" ";
return 0;
OUTPUT: -
Name: Aadil Section: B CMS ID 053-23-0056
Q4. Write a C++ program that should ask the user to input the final range up to which the loop should
run and then prints ONLY the multiples of 5 (fully divided by 5 only), as
CODE: -
#include <iostream>
using namespace std;
int main()
int r,s;
cout<<"Enter the range upto which the loop should run : ";
cin>>r;
cin>>s;
for(int y=2;y*s<=r;y++)
cout<<y*s<<" "<<endl;
return 0;
OUTPUT: -
Name: Aadil Section: B CMS ID 053-23-0056
Q5. Create two separate programs (one using a for loop and the other using a while loop) to print a
series of 10 numbers as shown below
CODE: -
#include <iostream>
using namespace std;
int main()
int y;
y=0;
for(int x=1;x<=10;x++){
y+=x;
cout<<y<<" ";
return 0;
OUTPUT: -
Name: Aadil Section: B CMS ID 053-23-0056
Q6. Write a C++ program that asks a user to input the range (final value) up to which the loop should run
and then for each number in that range, it should tell whether that (each) number is Odd or Even. For
example, if the user enters 7 as the final value of the range, then it should display like following:
CODE: -
#include <iostream>
using namespace std;
int main()
int number;
cout<<"Enter the range upto which the loop should run : ";
cin>>number;
for(int x=0;x<=number;x++){
if(x%2!=0)
cout<<x<<" is "<<"odd \n";
if(x%2==0)
cout<<x<<" is "<<"even\n";
return 0;
OUTPUT: -
Name: Aadil Section: B CMS ID 053-23-0056
And in another case, if the user inputs 10 as the final value of range, then
Q7. Using loop, write a C++ program that displays a series of alphabets in ascending order from ‘A’ to ‘Z’
and then in descending order from ‘Z’ to ‘A’.
CODE: -
Ascending: -
#include <iostream>
using namespace std;
int main()
for(char al='A';al<='Z';al++)
cout<<al<<" ";
return 0;
OUTPUT: -
Name: Aadil Section: B CMS ID 053-23-0056
CODE: -
Descending
#include <iostream>
using namespace std;
int main()
for(char al='Z';al>='A';al--)
cout<<al<<" ";
return 0;
OUTPUT: -
Q8. Modify your program in the previous part so that the program will display consonants only, no
vowels.
CODE: -
#include <iostream>
using namespace std;
int main()
for(char al='A';al<='Z';al++){
Name: Aadil Section: B CMS ID 053-23-0056
if(al!='A'&&al!='E'&&al!='I'&&al!='O'&&al!='U')
cout<<al<<" ";
return 0;
OUTPUT: -
Q9. The Fibonacci sequence is a series where the next term is the sum of the previous two terms. The
first two terms of the Fibonacci sequence are 0 and 1.
The series is as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … Write a C++ program (using loop) to print the 10
numbers of the Fibonacci series as shown above.
CODE: -
#include <iostream>
using namespace std;
int main() {
int m = 10;
int f = 0, s= 1, next;
cout << "Fibonacci Series (first " << m << " terms):" << endl;
for(int x = 0; x < m; ++x) {
if (x <= 1)
next = x;
else {
next = f + s;
f = s;
Name: Aadil Section: B CMS ID 053-23-0056
s = next;
cout<< next << " ";
return 0;
OUTPUT: -
Q10. Write a program to ask the user to input n_courses (Total Courses), then iterate loop n_courses
time and ask user marksin each course and perform the following tasks:
a) Make sure n_courses is between (3 to 8)
b) Calculate the averageof all course marks.
c) Calculate the total percentageof all courses.
d) Tell the user whether she/he is PASS/FAIL, FAIL → less than 60, and otherwise PASS.
CODE: -
#include<iostream>
using namespace std;
int main()
int marks;
int n_courses, sum = 0, average = 0, total_marks;
double percentage;
cout<<"N_courses between (3 to 8) : ";
cin>>n_courses;
for(int i=1; i<=n_courses; i++){
Name: Aadil Section: B CMS ID 053-23-0056
cout<<"Enter marks for course "<<i<<": ";
cin>>marks;
sum+=marks;
average = (sum/n_courses);
total_marks = n_courses*100;
percentage = (double)sum/total_marks*100;
cout<<"The average is: "<<average<<endl;
cout<<"Your percentage is : "<<percentage<<endl;
if(percentage>60)
cout<<"Pass."<<endl;
else
cout<<"Fail."<<endl;
return 0;
OUTPUT: -
SAMPLE: 1
SAMPLE: 2
Name: Aadil Section: B CMS ID 053-23-0056
Q11. Write a program to output the N terms of HARMONIC series and their SUM.
CODE: -
#include <iostream>
using namespace std;
int main() {
int n;
double sum = 0.0;
cout << "Enter the number of terms for the Harmonic series: ";
cin >> n;
if (n <= 0) {
cout<<"Please Enter Positive Integar : ";
} else {
cout<<"Harmonic Series "<< n << " terms"<<endl;
for (int i = 1; i <= n; ++i) {
double term = 1.0 / i;
sum += term;
cout << "Harmonic Series " << i << ": " << term <<endl;
cout << "Total Sum is : " << sum <<endl;
return 0;
}
Name: Aadil Section: B CMS ID 053-23-0056
OUTPUT: -