0% found this document useful (0 votes)
6 views8 pages

C++ Assighnment 4

The document explains the differences between 'while' and 'do-while' loops, providing examples and outputs for each. It includes programming tasks to replace loops, count students, find averages, and validate character inputs. Additionally, it discusses control statements like break, continue, and return, along with their effects in loops.

Uploaded by

Sahil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

C++ Assighnment 4

The document explains the differences between 'while' and 'do-while' loops, providing examples and outputs for each. It includes programming tasks to replace loops, count students, find averages, and validate character inputs. Additionally, it discusses control statements like break, continue, and return, along with their effects in loops.

Uploaded by

Sahil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

3.1.1 Differentiate between a “while” and a “do-while” loop.

Ans] while is a pretest loop. It is an entry controlled loop. The variable in test condition must be
initialized prior to entering the loop structure.

Whereas do while loop is a post test loop. It is an exist


control loop. It is not necessary to intilize the variable in the test condition prior to entering loop
structure.

3.1.2 Predict the output for each of the following:


i) int number = 1;
do
{
cout << 2*number;
number = number + 1;
} while (number <= 3);

ii) int number = 1;


while (number <= 3)
{
cout << 2*number;
number = number + 1;
}
Ans] 1. 246
2. 246

3.1.3 The following program fragment is supposed to input numbers from the user and
display them until a 0 is entered, but a statement is missing. What is the missing statement
and where should it be inserted?

cin >> number;

while (number != 0)

cout << number;

Ans] cin >> number;

while (number != 0)

cout << number;

cout<<”enter a number”<<endl;
cin >> number;

3.1.4 Replace the while loop in the Counting program and the Summation program with
the do-while loop.
Ans]1. Counting program

#include <iostream>

using namespace std;

int main()

int marks, pass;

int c=0;

cout<<"enter the passing marks"<<endl;

cin>>pass;

cout << "enter a student's marks" << endl;

cin>>marks;

do

if(marks>=pass)

c++;

cout<<"enter a student's marks"<<endl;

cin>>marks;

while(marks!=-1);

cout<<"number of students that have passed are"<<c<<endl;

return 0;
}

2. accumulating program

#include <iostream>

using namespace std;

int main()

int marks,sum=0;

cout<<"enter student marks"<<endl;

cin>>marks;

do

sum= sum +marks;

cout<<"enter student marks"<<endl;

cin>>marks;

}while(marks!=-1);

cout<<"sum of marks"<<sum<<endl;

return 0;

3.1.5 Write a program to count the number of students when a set of student marks are
entered as input.
Ans]

#include <iostream>

using namespace std;

int main()

int marks,c=0;

cout<<"enter student marks"<<endl;


cin>>marks;

while(marks!=-1)

if(marks>=0)

c++;

cout<<"enter student marks"<<endl;

cin>>marks;

cout<<"total number of students is"<<c<<endl;

return 0;

3.1.6 Write a program to find the average marks scored by a set of students.
Ans]

#include <iostream>

using namespace std;

int main()

double marks,avg,sum=0;

int c=0;

cout<<"enter student marks"<<endl;

cin>>marks;

while(marks!=-1)

{
sum=sum + marks;

c++;

cout<<"enter students marks"<<endl;

cin>>marks;

avg=sum/c;

cout<<"average is"<<avg<<endl;

return 0;

3.2.1 Define a counter controlled loop with an example.


Ans] a controlled loop,a pre test loop is executed a known fixed number of times.
Eg.
int i;
for(i=1;i<=3;i++)
{
Cout<<”hello world”<<endl;
}

3.2.2 Predict the output:


1. for ( int count = 10; count < 6; count = count -2)
cout << count << endl;

2. for ( int count = 0; count > 6; count--)


cout << count << endl

ans] in both cases there will be no output

3.2.3 Write a program that prints multiples of 10 upto 100.

Ans]

#include <iostream>

using namespace std;

int main()

int num=10;

cout<<”multiple of 10 are”<<endl;
for(int i=1; i<=10;i++)

cout<<i*num<<endl;

return 0;

3.2.3 Write a program that prints the following pattern:


55555
4444
333
22
1
Ans]

#include <iostream>

using namespace std;

int main()

for(int i=5;i>=1;i--)

for(int j=1;j<=i;j++)

cout<<i;

cout<<endl;

return 0;

}
3.3.1 What are the differences between break, continue
and return?

Ans] break: In a loop, the break statement is used to exit the loop before
the exit conditions are met. A break causes the program to jump over all the
statements in the loop’s block and continue with the statement immediately
after the loop.

Continue: The continue statement forces return to the next test condition of
the loop before all the statements in the body of the loop are executed. It
serves to bypass certain sections of a loop.

Return: The return statement in the body of the loop will cause the function
(in which the loop is placed) to return immediately to the calling function.

3.3.2 Predict the output


1. for (i = 10; i > 0; i--)
{
cout << i << endl;

if (i == 4)
break;
}
cout << “done”<< endl;

2. for (int n=10; n>0; n--)


{
if (n==5) continue;
cout << n << ", ";
}
cout << "done" << endl;

ans]
10
9
8
7
6
5
4
Done

10
9
8
7
6
5
4
3
2
1
Done

3.3.3 Write a program that accepts a character input and validates that the character is
only between a to z.

Ans]

#include <iostream>

using namespace std;

int main()

char ch;

cout<<"enter a character"<<endl;

cin>>ch;

int uv= (int)ch;

if(uv>=97 && uv<=122)

cout<<" character is valid"<<endl;

else

cout<<"character is not valid"<<endl;

return 0;

You might also like