Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Q01: Fill in The Blanks

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

CHAPTER 03

Q01: Fill in the blanks:

i. loop
ii. while
iii. do-while
iv. break
v. nested loop

Q02: Mark true or false:

i. true
ii. false
iii. true
iv. false
v. true
vi. false
vii. false
viii. false
ix. true
x. true

Q03: Write down the outputs of the following programs (point out error if any into the following
programs).

1. errors in this program so do not execute.

I. Terminator after for statement.


II. Missing terminator before cout and before }.
III. Newline in constant (no “ “ with lines which are going to be execute, is written.)

2.
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i,j;
for(i=1;i<=10;i++)
{for(j=1;j<=5;j++)
cout<<"x";
cout<<endl;
}
cin.get();
getch();
}
3. Nothing is displayed. It is a infinite loop which goes on repeating without displaying anything on
console.
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int x=20;
while(1)
{if (x>=1)
if(x%2==1)
cout<<x<<endl;
else
break;
x++;
}
cin.get();
getch();
}

4. Errors in this program so do not execute.

I. I is not initialized.
II. There is space between relational operator (= =)

Q04: Differentiate between the do-while and while loops. Explain with program examples.

 In while loop , test condition comes before the body of the loop.
First the condition is tested and then the body of the loop is
executed.
//Program using while loop
void main()
{
int i;
i=1;
while(i<=5)
{
cout<<"hello"<<endl;
i++;
}

}
 In do while loop the body of the loop comes before the test
condition .The body of the loop is executed and then the condition
is tested.

void main()
{
int i=1;
do
{
cout<<"hello"<<endl;
i++;
}
while(i<=5);

Q05:

//Program using for loop

#include<iostream>

#include<conio.h>

using namespace std;

void main()
{ int sum=0;
for(int no=1;no<=10;no=no+2)
sum=sum + no;
cout<<"sum of odd numbers of first ten natural numbers="<<sum<<endl;
cin.get();
getch();

Q06:
//Program using while loop
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{ int sum=0;
int no=1;
while(no<=10)
{ sum=sum + no;
no=no+2;
}
cout<<"sum of odd numbers of first ten natural numbers="<<sum<<endl;

cin.get();
getch();
}

Q07:

//Program using do-while loop


#include<iostream>
#include<conio.h>
using namespace std;
void main()
{ int sum=0;
int no=1;
do
{ sum=sum + no;
no=no+2;
}
while(no<=10);
cout<<"sum of odd numbers of first ten natural numbers="<<sum<<endl;

cin.get();
getch();
}

Q08:
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{ int product=1;
int no=2;
while(no<=12)
{
product=product*no;
no=no+2;
}
cout<<"product of even numbers from 1 to 12= "<<product<<endl;

cin.get();
getch();
}

Q09:
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int a,b,result;
result=1;
cout<<"Enter any number ";
cin>>a;
cout<<"Enter Power to be calculated ";
cin>>b;
for(int i=1;i<=b;i++)
result=result*a;
cout<<a<<" raised to power "<<b<<" is "<<result;
cin.get();
getch();
}

Q10:
//program doesn't executes in the required shape, when decrement is c-=1
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
void main()
{int s,c;
for (s=20,c=7;c>=0;c-=2,s++)
{ cout<<setw(s);
for(int i=1;i<=c;i++)
{ cout<<"*";}
cout<<endl;
}

getch();
cin.get();
}
Q11:
#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
void main()
{ int s=10;
int c=6;
int i=1;
while(c>=1)
{cout<<setw(s);
i=1;
while(i<=c)
{cout<<"*";
i++;}
c--;
s++;
cout<<endl;}

cin.get();
getch();
}
EXAMPLES

3.
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{ int tab,res,c;
cout<<"enter the value of table ";
cin>>tab;
c=1;
while(c<=10)
{res=tab*c;
cout<<tab<<"*"<<c<<"="<<res<<endl;
c++;
}

cin.get();
getch();
}

4.
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int n;
int a[100];
int b=1;
cout<<"Enter the integer=";
cin>>n;
while(n>=1)
{
a[b]=n%2;
n=n/2;
b++;
}
for(n=b-1;n>=1;n--)

{
cout<<a[n];
}

getch();
cin.get();
}

6.
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
float sum,b;
sum=0.0;
for(b=1;b<=45;b++)
{
sum=sum+(1/b);
}
cout<<"sum of the series="<<sum;
cin.get();
getch();
}

9.
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{

char name[15];
int age;
float GPA;
char choice;
do
{
cout<<"Enter name of student=";
cin>>name;
cout<<"Enter age of student=";
cin>>age;
cout<<"Enter GPA of student=";
cin>>GPA;
cout<<"student record is\n";
cout<<"name="<<name<<"\n";
cout<<"age="<<age<<"\n";
cout<<"GPA="<<GPA<<"\n";
cout<<"do you want to enter more records?"<<endl;
cin>>choice;
}
while(choice=='y'||choice=='Y');
cout<<"ok";
cin.get();
getch();
}

10.
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int n;
int a[100];
int b=1;
cout<<"Enter the integer=";
cin>>n;
do
{
a[b]=n%8;
n=n/8;
b++;
}
while(n>=1);
cout<<"octal number is=";
for(n=b-1;n>=1;n--)
{
cout<<a[n];
}

getch();
cin.get();
}

12.
#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
void main()
{

char name[25] ,choice;


int age;
while(1)
{
cout<<"enter name=";
cin>>name;
cout<<"enter age=";
cin>>age;
cout<<"name is "<<name<<"\nage is "<<age<<endl;
cout<<"More[Y/N]";
cin>>choice;
if(choice=='y'||choice=='Y')
continue;
else
break;
}
getch();
cin.get();
}
14.

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int a,b,result;
cout<<"enter any number=";
cin>>a;
for(b=1;b<=10;b++)
{
result=a*b;
cout<<a<<"*"<<b<<"="<<result<<endl;
}
cin.get();
getch();
}

17.
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int n;
long int fact;
cout<<"Enter any no ";
cin>>n;
for (fact=1; n>=1; n--)
{
fact=fact*n;
}
cout<<"the factorial of number is "<<fact<<endl;
cin.get();
getch();
}

18.
//Program using for loop
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i;
for(i=1;i<=5;i++)
{
cout<<"I LOVE PAKISTAN"<<endl;
}
cin.get();
getch();
}

//Program using while loop


#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i;
i=1;
while(i<=5)
{
cout<<"I LOVE PAKISTAN"<<endl;
i++;
}
cin.get();
getch();
}

//Program using do-while loop


#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int i;
i=1;
do
{
cout<<"I LOVE PAKISTAN"<<endl;
i++;
}
while(i<=5);
cin.get();
getch();
}

19.
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int a=1,b=1,c=1;
while(a<=5)
{
cout<<a<<" ";
a++;
}
cout<<endl;
while(b<=9)
{
cout<<b<<" ";
b+=2;
}
cout<<endl;

while(c<=13)
{
cout<<c<<" ";
c+=3;
}

cin.get();
getch();
}

20.
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
for (int i=1;i<=5;i++)
{
for (int j=1;j<=i;j++)
cout<<j<<" ";
cout<<endl;
}
cin.get();
getch;
}

21.
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
for (int i=1;i<=4;i++)
{
for (int j=1;j<=i;j++)
cout<<"*"<<" ";
cout<<endl;
}
cin.get();
getch;q
}

22.
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
void main()
{int s,c;
for (s=20,c=1;c<=7;c+=2,s--)
{ cout<<setw(s);
for(int i=1;i<=c;i++)
cout<<"*";
cout<<endl;
}
cin.get();
getch();

You might also like