You are on page 1of 14

class 9th

q-1) underline the error (if any) in the following codes and rewrite
correct code( consider header files and getch() in each codes).
a) void main()
{
int a;
cout<<enter the value”;
cin>>a;
cout<<”the value is “<<a;
}
b) void main()
{
inta=1,b=2,c;
c=a+b+,
cout<<c
}
c) void main()
{
float a=6.0,b=3.0,c;
c=a\b;
cout<<c;
}
d) void main
{
int x=4,y=5z;
z=((y>x)(x<y))&&(x==y));
cout<<z;
}
e) void main()
{
int a,b;
cout<<”enter the value of a &b”;
cin>>a>>b;
c=a/4+b+1,
cout<<c;
}
f) void main()
{
int a=1,b=2;
a+=b;
cout<<a;
}
g) void main()
{
int a=10;
if(a==0)
cout<<”a is zero”;
elseif
cout<<”a is not zero”;
}
h) void main()
{
while(a>=11)
{
cout<<a;
a+
}
}
i) void main()
{
int a= 10;
do while(a<=10)
{
cout<<a;
a--;
}
}
j) void main()
{
inta=0;
for(a=1,a<=5,++a)
{cout<<a;
}
}
q-2) write the output for the following codes(consider header files and
getch() in each code).
a) void main()
{
int a=1,b=2,c;
c=a++ + b++;
cout<<a<<b<<c;
}
b) void main()
{
int a=2,b=4,c;
c=(b*8)/4+(b/4)+4-b+(5/8);
cout<<c;
}
c) void main()
int a=16,b=4;
cout<<a/b<<”\n”;
cout<<a%b;
}
d) void main()
{
int j=11,i=3,k;
k=i%j;
if(k==1)
cout<<”k is 1”;
else if(k==2)
cout<<”k is 2”;
else
cout<<”k is not 2”;
}
e) void main()
{
int a=4,b=0;
b=a++*3;
cout<<b;
}
f) void main ()
{
int a=1,b=2;
a=a*4;
b+=1;
cout<<a/b;
}
g) void main()
{
for(int i=0;i<=11;i++)
{
cout<<i<<”\t”;
}
}
h) void main()
{
int i=2, sum=0;
while(i<=5) 2 3 4 5 6
{
i=i+1; 3 4 5 6
sum=sum+i; 3 7 12 18
}
cout<<sum; 18
}
}
i) void main()
{
int x=4,y=5,z;
z=(y>=x)&&(!(x==y)||(z>y)); 1&&(!(0)||(0)) 1&&(1||0) 1&&1 1

cout<<z;
}
j) void main()
{
for(int i=0;i<=5;i+=2)
{
cout<<i<<”\t”;
}
}
k) void main()
{
int x=10;int y=3; int z;
z=x%y;
cout<<z;
}
l) void main()
{
int a=5,b=6,c;
c=(a==b)?a:b;
cout<<c;
}
m) void main()
{
int a=9,b=3;
int c=++a + b--;
cout<<”the value of c is”<<c;
}
n) void main()
{
int a=16,b=4,c=1,d;
d=a/b*c+15;
cout<<d;
}
o) void main()
int a,b;
cout<<”enter the value of a & b”;
cin>>a>>b;
if(a>b)
cout<<”a is greater than b”;
else
cout<<”b is greater than a”;
}

p) void main()
{
int a=7;
while(a>=0)
{
cout<<a;
a--;
}
}

q) void main()
{
int a=7;
do
{
cout<<a;
a++;
}while (a<=11);
}

r) void main()
{
for (int a=1;a<=5;a++)
{
cout<<”a”<<”\n”;
}
}

q3) write the output for the following codes (consider header files and getch() in
each code).

a) void main()
{
int a=7,b=2,c,d;
c=a/b;
d=a%b;
cout<<c<<”\n”<<d;
}

b) void main()
{
int a =11,b;
b=++a;
cout<<b++<<”\n”<<a<<”\t”<<b;
}
c) void main()
{
int a =8,b=13 c;
c=a++ +++b;
cout<<c;
}
d) void main()
{
int a=20,b=5,c=10,d;
d=((a*c/b)-c)%4;
cout<<d;
}
e) void main()
{
int x=5,y=8,z;
(x>y)?cout<<x:cout<<y;
}
f) void main()
{
int a=500,b=100,c;
if( !a>=400)
b=300;
c=200;
cout<<b<<”\t”<<c;
}
g) void main()
{
int i=1;
switch(i)
{
case 1:
cout<<”this is case 1\n”;
case 2:
cout<<”this is case 2\n”;
default:
cout<<”this is default\n”;
}
}
h) void main()
{
int s=0;
for(int i=1; i<4;i++)
{
s=s+i;
}
cout<<s;
}
i) void main()
{
int a=4;
while(a>1)
{
cout<<a<<”\t”;
a--;
if(a==3)
{
continue;
}
}
}
j) void main()
{
int i=1;
do
{
i=i+1;
i++;
cout<<i<<”\t”;
}while(i<=3);
}
k) rewrite the following program using while loop:
#incude<iostream.h>
#include<conio.h>
void main()
{
int x=0,sum=0;
for(x=1;x<=10;x++)
{
sum+=x;)
cout<<sum;
getch();
}
q-4) underline the error (if any) in the following codes and rewrite the
correct code (consider header file(s) and getch() in each code):
a) void main()
{
int a=10,b=12;
int c==a+b;
cout<<c;
}
b) void main()
{
int p=1;
switch(p){
case p=1: cout<<”hello”;break;
case p=2: cout<<”world”;break;
default: cout<<”none;
}
c) void main()
{
int i=1,j=10;
for(i=1;i<=j;i==1;)
cout<<i*i;
}
d) void main()
{
int k=10,m=2;
p=k/m;
cout<<p;
}
e) void main()
{
int $w=110;
int u=w*10;
cout<<u;
}
f) void main()
{
int t=2;
int y=t++ + t**;
cout<<y;
}
g) void main()
{
int x=10, y=20;
if(x==10 and y==20)
cout<<x<<y;
}
h) void main()
{
int a=10,b=100;
int x=(a>b) : a ? b;
cout<<x;
}
i) void main()
{
int i=1;
for(i=1;<=10;i++)
if(i==4)
break;
}

q-5) write the output of the following programs (consider all required header
files and getch():

a) void main()
{
int a=1;
for(a=1;a<=10;a+=2)
cout<<a<<end 1;
}
b) void main()
{
int a=3,b=4,c=5,d=6,e=0;
e=(a*b + b*c +b)/d;
cout<<e;
}
c) void main()
{
int m=10,n=20;
n= ++m;
cout<<n;
}
d) void main()
{
int a=13;
if(a%2==0)
cout<<”the number is even”;
else
cout<<”the number is odd”;
}
e) void main()
{
int i=1;
clrscr();
for (i=1;i<=10;i+=2)
{
if (i%2==)
{
break;
}
cout <<i<<end1;
}
}
f) void main()
{
int z=5,x=10,v=7,b=0;
b=(z*v + z)/x;
switch(b)
{
case: 2
cout<<”excellent!\n”; break;
case: 4
cout<<”well done!\n”; break;
case:6

cout<<”good\n”; break;

default:
cout<<”not a valid entry\n”;

g) void main()
{
int p=100,q=10,r=1;
r=(p>q)?(p+q):(p-q);
cout<<r;
}
h) void main()
{
int k=3;
if (k==1)
cout<<”one”;
else if (k==2)
cout<<”two”;
else
court<<”check the number”;
}
i) void main()
{
int i=10;
do
{
cout<<i<<end1;
i==;
} while(i>=5);
}

You might also like