Xi Cs First Setb2 Sol
Xi Cs First Setb2 Sol
Xi Cs First Setb2 Sol
Max Marks : 70
General Instructions :
Q1a) Name the header files are required for the following :
i)
cin
ii)
setw( )
iii)
sqrt( )
iv)
exit( )
Ans . i)
ii)
iii)
iv)
cin
setw( )
sqrt( )
exit( )
(2)
iostream.h
iomanip.h
math.h
process.h
b) Explain the difference between break and continue with the help of an
example.
(3)
Ans.
Break statement is used to terminate the smallest enclosing while, dowhile, for or switch statement while continue statement forces the next
iteration of the loop to take place, skipping any code in between.
for(int i=1;i<=10;i++)
{
If (i%3 ==0)
break;
else
cout<<i<<endl;
}
It will give the output as :
1
2
for(int i=1;i<=10;i++)
{
If (i%3 ==0)
continue;
else
cout<<i<<endl;
}
It will give the output as :
1
1
2
4
5
7
8
10
c)
Ans.
The implicit conversion of one data type to another one by the compiler is
called type promotion. E.g if one operand is int and other is float, int
operand is converted to float by the compiler.
Example :
int a;
float b, c;
c= a / b
The explicit type conversion is done by the programmer. It is also called
type casting.
Example :
int a,b;
(float) a+b/2;
d)
Differentiate between a while loop and a do- while loop with example. (2)
Ans.
e)
Ans:
How many bytes are reserved for the following data types:
i)
float
ii)
char
(2)
f)
Ans.
g)
Ans:
(2)
(2)
Q2 Find the syntax errors in the following code and write the corrected code
underlining the corrections made.
a) #include(iostream.h)
(3)
void main( )
int x; y;
cin>>x;
for (y= =0; y<10, y++)
if x= =y
cout<<y+x;
else
cout<<y;
Ans.
#include<iostream.h>
3
void main( )
{
int x, y;
cin>>x;
for (y =0; y<10 ; y++)
if (x= =y)
cout<<y+x;
else
cout<<y;
}
b) #include<<iostream.h>>
void main( );
{
const int max=0;
int a,b;
cin>>a>>b;
if(a>b) max=a;
for(x; x<max; x++)cout>>x;
}
Ans.
#include<iostream.h>
void main( ) ;
{
const int max=0;
int a,b;
cin>>a>>b;
if(a>b) max=a;
for(int x = 1; x<max; x++)cout<<x;
}
c) #include<iostream.h>
int main
{
int j=99,a;
float u=10.0;
cin(a);
while a<=j
{
a+=10
u=/a;
}
}
Ans.
(3)
1)
2)
(3)
3)
4)
5)
6)
Q3.
Find the output of the following code (assuming all the necessary files are
included.)
a) #include<iostream.h>
void main( )
{
int i,j,k,x=0;
for(i=0;i<3;++i)
for(j=0;j<i;j++)
{ switch(i+j-1)
{
case -1:
case 0:
x+=1;
break;
case 1:
case 2:
case 3:
x+=3;
default:
x+=3;
}
cout<<x<< ;
}
cout<<"\n "<<x;
}
Ans.
b)
Ans.
(4)
1 7 13
13
int x=25, y=10;
cout<<++x<<@<<y++<<@<<--y<<endl;
cout<<x++<<@<<--y<<@<<x++<<endl;
(3)
26@9@9
27@9@26
c) #include<iostream.h>
(4)
void main( )
{ for ( int a = 3 ; a< 10 ; a++ )
{ if ( a==4)
a+=5;
5
12@9@6@ 3@
36@27@18@ 9
Q4.
Ans :
int x=1, y;
do
{ y=1;
do
{ sum+ = (x+y);
cout<<sum<<endl;
y++;
} while (y<=x);
x++;
} while (x<=n);
b)
Ans.
(2)
(2)
{
case 5:
case 10:
val=num*25-20;
cout<<num+val;
break;
val=num*20-15;
cout<<val-num;
break;
}
c)
Ans.
(2)
int x=0;
for ( int n=0 ; n < 10 ; n++)
{
if ( (n%2 ==0)
{
x=x+n;
cout<<x;
}
}
b)
Ans
(2)
Write a program that reads the value of an integer n and then display the
following patterns till n number of lines. For e.g. If n=4 then the output
should be:
(4)
4
Ans.
d)
Ans.
3
3
2
2
2
1
1
1
1
2
2
2
3
3
#include <iostream.h>
void main( )
{
int i,j,n;
cout <<Enter the no of lines to be printed \n;
cin>>n;
for (i = 1; i <= n; i ++)
{
cout<<\n;
for (j = 1; j <= n-i ; j ++)
cout<< ;
for (k = i; k >= 1 ; k --)
cout<<j;
for (l = 1; k >= i ; l++)
cout<<j;
}
}
Write a program that reads two integer numbers n & m and then prints
every integer between 1 and n divisible by m. Also report whether the
number that is divisible by m is even or odd.
(4)
#include <iostream.h>
void main( )
{
int n, m;
cout <<Enter the value of n & m;
cin>>n>>m;
for (int i=1;i<=n; i++)
{
if (i%m= = 0)
{
cout<<i<<\t;
if (i%2)
cout<<It is a odd number;
else
cout<<It is an even number;
}
}
8
}
e)
Write a program that reads a given number and prints whether it is prime
or not. The program should continue as long as user wants.
(4)
Ans.
#include <iostream.h>
void main( )
{
char choice;
int n, prime=1;
do
{
cout <<Enter the value of n;
cin>>n;
for (int i=2;i<=n/2; i++)
{
if (n%j= = 0)
{
prime=0;
break;
}
}
if(prime= =1)
cout<<It is not a prime number;
else
cout<<It is a prime number;
cout<<Do u want to continue;
cin>>choice;
}while (choice = = Y || choice = =y);
}
f)
Ans.
Write a program that reads a number, n and prints all armstrong numbers
from 1 to n.
(4)
#include <iostream.h>
void main( )
{
int n, num, digit, sum=0;
cout<<Enter the number;
cin>>n;
for(int i=1;i<=n;i++)
{
num=n;
sum=0;
do
{
digit = n%10;
sum = sum + pow(digit,3);
n = n /10;
9
} while (n >0);
if (num = = sum)
cout<< i;
}
}
g)
Write a program that reads a number n and find the sum of Fibonacci
series upto n terms.
(4)
Ans.
#include <iostream.h>
void main( )
{
int n, first=0, second=1, third, sum=0;
cout<<Enter the number;
cin>>n;
sum = first + second;
for ( int i =3; i<=n ; ++i)
{
third = first + second;
sum = sum + third;
first = second;
second = third;
}
cout<< Sum of Fibonacci series up to n is <<sum;
}
10