Sie sind auf Seite 1von 25

Lab 9

 Practical Exam includes two questions:


 Office question (word OR Excel OR PowerPoint)
 C++ program

 Exam Time: 45 minutes


 You must attend in your section (no excuses)
 Bring your laptop if you can (to avoid time delay)
 Write a program in C++ to print a square pattern with #
character.
Sample Output:
Print a pattern like square with # character:
--------------------------------------------------
Input the number of characters for a side: 4
Output:
####
####
####
####
#include <iostream>
using namespace std;
int main()
{
int size; cout << "\n\n Print a pattern like square with # character:\n";
cout << "--------------------------------------------------\n";
cout << " Input the number of characters for a side: "; cin >> size;
for (int row = 1; row <= size; ++row)
{ for (int col = 1; col <= size; ++col)
{ cout << "# "; }
cout << endl;
}
return 0; }
Write a program in C++ to display the multiplication table vertically from 1 to n.
Sample Output:
Input the number upto: 5
Output:
Multiplication table from 1 to 5
1x1=1 2x1=2 3x1=3 4x1=4 5x1=5
1x2=2 2x2=4 3x2=6 4x2=8 5x2=10
1x3=3 2x3=6 3x3=9 4x3=12 5x3=15
1x4=4 2x4=8 3x4=12 4x4=16 5x4=20
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45
1x10=10 2x10=20 3x10=30 4x10=40 5x10=50
#include <iostream> using namespace std;
int main()
{
int j, i, n; cout << "\n\n Display the multipliaction table vertically from 1 to
n:\n";
cout << "-------------------------------------------------------------\n";
cout << "Input the number upto 5: ";
cin >> n; cout << "Multiplication table from 1 to " << n << endl;
for (i = 1; i <= 10; i++)
{ for (j = 1; j <= n; j++)
{ cout << j << "x" << i << "= " << i * j<< " ";
}
cout << endl;
}
}
Write a program in C++ to make such a pattern like right angle triangle using
number which will repeat the number for that row.
Sample Output:
Input number of rows: 5
1
22
333
4444
55555
#include <iostream> using namespace std;
int main()
{ int i,j,rows;
cout << "\n\n Display the pattern using number repeating for a
row:\n"; cout << "----------------------------------------------------
------\n";
cout << " Input number of rows: "; cin >> rows;
for(i=1;i<=rows;i++)
{ for(j=1;j<=i;j++) cout<<i;
cout<<endl;
}
}
 Write a C++ Program to Reverse an Integer entered by
the user.
Solution
void main ()
{
int n, reversedNumber = 0, remainder;
cout << "Enter an integer: ";
cin >> n;
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
cout << "Reversed Number = " << reversedNumber;
}
 Write a c++ program that asks the user to enter 2
sentences then the program should display which sentence
is longer or the two sentences lengths are equal
Solution
int main (){ while(ch!='\n')
int len_1=0,len_2=0; {
char ch; len_2++;
cout<<"Enter the first ch=getchar();
sentence"<<endl; }
ch=getchar(); if(len_1>len_2)
while(ch!='\n') cout<<"first sentence
{ is longer"<<endl;
len_1++; else if(len_2>len_1)
ch=getchar(); cout<<"second
} sentence is longer"<<endl;
cout<<"Enter the second else
sentence"<<endl; cout<<"Equal"<<endl;
ch=getchar(); }
return 0;
 Write a c++ program that asks the user to enter a list of
characters and when the user hits ‘Enter’ the program
should display the number of capital letters entered.
Solution
int main (){
int num_of_letters= 0;char ch=' ';
while(ch!='\n')
{ Can we use
ch=getchar(); cin>>ch?!!!!
Why???
if(ch>='A'&&ch<='Z')
num_of_letters ++;
}
cout<< num_of_letters;
return 0;
}
 Write a C++ program that takes from the user 3 numbers
and displays them sorted in ascending order.

Hint: use built-in function swap(int a, int b).


int main(){int a,b,c;
Solution
cin>>a>>b>>c;
if(a>b){
swap(a,b);
}
if(a>c){
swap(a,c);
}
if(b>c){
swap(b,c);
}
cout<<a<<b<<c;
return 0;
}
 Write a c++ program that takes a list of N positive numbers
from the user and then display the min and max.
int main( ) Solution
{
int n,counter=0,num,min=9999,max=-1;
cout<<"enter the length of the list"<<endl;
cin>>n;
while(counter<n)
{
cin>>num;
if(num>max)
max=num;
if(num <min)
min=num;
cin>>num;
counter++;
} cout<<"Max:"<<max<<" Min: "<<min<<endl;
return 0;
}
 When Robin’s new baby was born, she opened a savings
account with $1000. On each birthday, starting with the first,
the bank adds 4.5% of the balance and Robin added
another $500 to the account. Write a C++ code that will
determine how much money will be in the account on the
child’s 18th birthday?
Solution
int main (){
int balance=1000;
for (int i=1;i<=18;i++)
{
balance+=balance*0.045+500;
}
cout<<balance;
return 0;}
 Write a c++ program to generate the following output.
ABCDE
ABCD
ABC
AB
A
Solution 1 Solution 2
int main(){ int main(){
for(char i=5;i>=0;i--) int limit = 5;
{ for(char i=0;i<limit;i++)
for(char j='A';j<='A'+i-1;j++) { char ch = 'A';
cout<<j;
for(char j=0;j<limit-i;j++)
cout<<endl;
{
}
cout<<ch;ch++;
return 0;
}
}
cout<<endl;
}
return 0;
}
 Write a C++ Program to find the sum of the following
series.
1/2 + 3/4 + 5/6 + 7/8 + 9/10 + 11/12 + … up to n terms.
 Sample run:
 Enter n : 5
 The series till 5 is : 3.85833
Solution
void main()
{
int n,i;
float sum=0.0,num=1.0,den=2.0;
cout<<"Enter n : ";
cin>>n;
for(i=1 ; i<=n ; i++,num+=2.0,den+=2.0)
{
sum+=num/den;
}
cout<<"The series till "<<n<<"is : "<<sum;
}

Das könnte Ihnen auch gefallen