Report File: Maitreyi Singh Class - Xi/A Roll No. - 26
Report File: Maitreyi Singh Class - Xi/A Roll No. - 26
MAITREYI SINGH
CLASS – XI/A
ROLL NO. – 26
CERTIFICATE
Signature:
2
CONTENTS
(b) Array:-
(i) Binary search 15
(ii) Selection sort 16
(iii) Bubble sort 16-17
(iv) Insert sort 17-18
(v) Arranging text values 18-19
(vi) Character count 19-20
(vii) Lucky number 20-21
(ix) Displaying diagonal elements 21-23
(x) Difference of diagonals 23-25
3
(xi) Prime number count in diagonals 25-27
(c) Functions:-
(i) Creation, traversal, insertion,
deletion and sorting 29-33
(ii) Sum of digits of number 33-34
(iii) Swap values 34-35
(iv) Convert into feets/inches 35-37
(v) arithmetic operator 37-38
(d) Structures:-
(i) Modifying time 40
(ii) Print student result 41-42
4
5
Q1. Write a C++ program to display the following :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Sol: #include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int i,j;
for(i=1; i<=5; i++)
{
for(j=1; j<=i; j++)
cout<<j<<’\t’;
cout<<endl;
}
getch( );
}
o/p – 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
6
Sol: #include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int n,i,t;
cout<<”enter number” <<endl;
cin>>n;
t=1;
for(i=1; i<=n; i++)
{
cout<<t<<’\t’;
t=t+2;
}
getch( );
}
Sol: #include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int num,mid,i;
7
cout<<”enter a number”;
cin>>num;
mid=num/2;
for(i=2; i<=mid; i++)
if(num%i==0)
break;
if(i>mid)
cout<<”prime”;
else
cout<<”not prime”;
getch( );
}
Sol:#include<iostream.h>
#include<conio.h>
void main( );
{
clrscr( );
char ch;
cout<<”enter a symbol from keyboard : ”;
cin>>ch;
if((ch>=65 && ch<=90) || (ch>=97 && ch<=122))
cout<<ch<<”is a alphabet”;
8
else
if(ch>=48 && ch<=57)
cout<<ch<<”is a digit”;
else
cout<<ch<<”is neither a digit nor an alphabet”;
getch( );
}
Sol:#include<iostream.h>
#include<conio.h>
void main ( )
{
clrscr( );
int n,s,i,num;
cout <<”the number of entries to be done ”;
cin<<n;
s = 0;
for( i = 1; i< = n; i++)
{
cin>>num;
s = s+num;
}
cout <<”average of numbers entered is”
9
<<(float)s/n;
getch( );
}
Sol:#include <iostream.h>
#include <conio.h>
void main ( )
{
clrscr ( );
int X,Y,S;digit;
cout <<”enter any number”;
cin>>X;
S = 0;
Y = 0;
10
While (X>0)
{
digit = X%10;
S = S+digit;
Y = Y *10+digit;
X = X/10;
}
cout<<Y<<’/n’; cout<<S;
getch ( );
}
o/p – enter any number 432
234
9
Q7. Write a C++ program to print the truth table for XY+Z.
Sol:#include<iostream.h>
#include<conio.h>
void main ( )
{
clrscr ( );
int x,y,z,f;
cout <<”x\ty\tz\tf\n\n”;
for (x = 0; x< = 1; x++)
for (y = 0; y< = 1; y++)
for (z = 0; z< = 1; z++)
{
f = x&&y||z;
11
cout<<x<<’\t\<<y<<’\t’<<x
<<’\t’<<f<<endl;
}
getch ( );
}
o/p - x y z f
0 0 0 0
0 0 1 1
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 1
1 1 0 1
1 1 1 1
Sol:#include<iostream.h>
#include<conio.h>
void main ( )
{
clscr ( );
int num, rev, digit, original;
cout<< “enter a number ”;
cin>>num;
original = num;
rev = 0;
while (num>0)
12
{
digit = num%10;
rev = rev*10+digit;
num = num/10;
}
cout <<rev;
if (rev = = original)
cout<< “the given number is palindrome”;
else
cout<< “the given number is not a palindrome’;
getch ( );
}
13
14
Q1. Write a C++ program to search a number in an array using
binary search.
Sol:#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int num, beg, end, mid;
beg=0;
end=6;
int A[ ]={2,4,14,400,800,850,9320};
cout<< “enter number to be searched: ”;
cin>>num;
mid=(beg+end)/2;
while(beg<=end && num!=A[mid])
{
if(num>A[mid])
beg=mid+1;
else
end=mid-1;
mid=(beg+mid)/2;
}
15
if(beg>mid)
cout<< “not present”;
else
cout<< “present”;
getch( );
}
o/p – num 800
present
Sol:#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main( )
{
clrscr( );
int A[ ]={5,10,-2,0,12,200,40}
int i,j,t;
for(i=0; i<(7-1); i++)
for(j=i+1; j<7; j++)
if(A[i]>A[j])
{
t=A[i];
A[i]=A[j];
A[j]=t;
}
for(i=0; i<7; i++)
cout<<setw(5)<<A[i];
16
cout<<endl;
getch( );
}
o/p - -2 0 5 10 12 40 200
Sol: #include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int i, j;
int A[ ]={4,5,17,-2,0,12};
for(i=0; i<(6-1); i++)
for(j=0; j<((6-1)-i); j++)
if(A[j]>A[j+1])
{
t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
for(i=0; i<6; i++)
cout<<A[i]<<’\t’;
getch( );
}
17
o/p - -2 0 4 5 12 17
Sol:#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
int i, x, temp;
int A[ ]={12,5,0,7,200,28,30};
for(i=1; i<7; i++)
{
temp=A[i];
x=i-1;
while(x>=0 && temp<A[x])
{
A[x+1]=A[x];
x=x-1;
}
A[x+1]=temp;
}
for(i=0; i<7; i++)
cout<<A[i]<<’\t’;
getch( );
}
o/p – 0 5 7 12 28 30 200
18
Q5.Write a C++ program to arrange text values in A – Z order in an
array.
Sol:#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main( )
{
clrscr ( );
char names [10] [25];
char t [25];
int i,j;
for (i = 0; i<3; i++)
gets (names[ i ] );
for (i = 0; i< 2; i++)
for (j = i + 1; j<3; j++)
if (strcmp (names [ i ], name [ j ] )> 0 )
{
strcpy ( t, name [ i ] );
strcpy (name [ i ], name [ j ] );
strcpy (name [ j ], t );
}
for (i = 0, i<10; i++)
puts (names [ i ] );
getch ( );
}
o/p – apple qwerty lion
apple
19
lion
qwerty
Sol:#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main ( )
{
clrscr ( );
int i = 0, len, now, c, noc ;
char st[ 50 ];
gets (st);
while (st [ i ] ! = ‘\0’ )
{
if (st [ i ] = = ‘ ‘)
c = c + 1;
i = i + 1;
}
len = i;
now = c + 1;
noc = i – c;
cout << “number of words = ”<<now<< endl;
cout << “number of characters”<<noc;
getch ( );
}
20
3
12
Q7. Write a C++ program to display the lucky number for the one
entering his name.
Sol:#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
void main ( )
{
clrscr ( );
char name [25];
int S, d, C, num;
cout << “enter your name”;
gets (name);
C = 0; S = 0;
While (name [ c ] ! = ‘\0’ )
{
S = S + name[ C ];
C++;
}
while (S>9)
{
num = S;
S = 0;
While (num>0)
{
d = num%10;
21
S = S + d;
Num = num/10;
}
}
cout <<name<< “your lucky number is”<<s<< ‘\n’;
getch ( );
}
o/p – Deepali
4
Sol: #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[4][4];
int r,c;
for(r=0;r<4;r++)
for(c=0;c<4;c++)
cin>>a[r][c];
for(r=0;r<=3;r++)
{ for(c=0;c<=3;c++)
{
cout<<a[r][c]<<'\t';
}
cout<<endl;
}
22
cout<<endl;
for(r=0;r<4;r++)
cout<<a[r][r]<<'\t';
r=0;
cout<<endl;
for(c=3;c>=0;c--)
cout<<a[r++][c]<<'\t';
getch();
}
o/p – 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
23
1 6 11 16
4 7 10 13
Sol: #include<iostream.h>
#include<conio.h>
void main()
{
int a[4][4];
int r,c,sum1=0,sum2=0;
for(r=0;r<4;r++)
for(c=0;c<4;c++)
cin>>a[r][c];
for(r=0;r<=3;r++)
{ for(c=0;c<=3;c++)
{ cout<<a[r][c]<<'\t';
}
cout<<endl;
}
cout<<endl;
for(r=0;r<4;r++)
sum1=sum1+a[r][r];
r=0;
cout<<endl;
for(c=3;c>=0;c--)
sum2=sum2+a[r++][c];
24
if(sum1>sum2)
cout<<sum1-sum2;
else
cout<<sum2-sum1;
getch();
}
o/p – 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
25
Q10. Write a C++ program to find the count of prime numbers on
both the diagonals of a 2-D array.
Sol: #include<iostream.h>
#include<conio.h>
int is_prime(int num)
{
int mid=0;
mid=num/2;
for(int i=2;i<=mid;i++)
if(num%i==0)
break;
if(i>mid)
return(1);
else
return(0);
}
void main()
{
clrscr();
int a[4][4];
int r,c,f=0;
for(r=0;r<4;r++)
for(c=0;c<4;c++)
cin>>a[r][c];
for(r=0;r<=3;r++)
{ for(c=0;c<=3;c++)
{ cout<<a[r][c]<<'\t';
}
cout<<endl;
}
26
cout<<endl;
for(r=0;r<4;r++)
{
if(is_prime(a[r][r])==0)
f++;
}
r=0;f--;
cout<<endl;
for(c=3;c>=0;c--)
{
if(is_prime(a[r++][c])==0)
f++;
}
cout<<endl<<endl<<"frequency->"<<f<<endl<<endl;
getch();
}
o/p – 1
2
3
4
5
6
7
8
9
10
11
12
13
27
14
15
16
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
frequency->3
28
Q1. Write a C++ program which includes creation, traversal,
insertion, deletion and sorting of an array (using functions).
Sol: #include<iostream.h>
#include<conio.h>
29
#include<iomanip.h>
void menu( );
void creation(int[ ],int n);
void traversal(int [ ],int n);
void insertion(int [ ],int &,int, int);
void deletion(int [ ],int &,int);
void sorting(int [ ],int);
void menu( )
{
clrscr( );
cout<<"OPERATION\n";
cout<<"1. CREATION\n";
cout<<"2. TRAVERSAL\n";
cout<<"3. INSERTION\n";
cout<<"4' DELETION\n";
cout<<"5. SORTING\n";
cout<<"6. EXIT\n";
cout<<"enter your option->";
}
o/p –
enter your option->1 creation
enter value for n->6
1 2 3 4 5
33
Q2. Write a C++ program that uses following functions:
(a) sqlarge( ) where two arguments are passed by reference and
then it sets the larger of the two numbers to its square.
(b) sum( ) where an int argument is passed by value and then it
returns the sum of the individual digits of the passed number.
(c) main( ) that exercises above two functions by getting two
integers from the user and by printing the sum of the
individual digits of the square of the large number.
Sol:#include<iostream.h>
#include<conio.h>
int sqlarge(int x, int y)
{
if(x > y)
return(x*x)
else
return(y*y)
}
int sum(int num)
{
int t=0;
while(num>0)
{
t=t+(num%10);
num=num/10;
}
return(t);
}
void main( )
{
34
clrscr( );
int n1,n2;
cout<< “enter any two numbers: ”<<endl;
cin>>n1>>n2;
cout<<sum(sqlarge(n1,n2));
getch( );
}
o/p –
enter any two numbers: 25 14
13
Sol:#include<iostream.h>
#include<conio.h>
void main( )
{
clrscr( );
void swap(int &,int &);
int a,b;
a=7;
b=4;
cout<< “\nthe original values are : \n”;
cout<< “a=”<<a<< “,b=”<<b<<’\n’;
swap(a,b);
cout<< “\nthe values after swap( ) are :\n”;
cout<< “a=”<<a<< “,b=”<<b<<’\n’;
getch( );
35
}
void swap(int &x,int &y)
{
int temp;
temp = x;
x = y;
y = temp;
cout<< “\nthe swapped values are : \n”;
cout<< “a=”<<x<< “,b=”<<y<<’\n’;
}
Sol:#include<iostream.h>
#include<conio.h>
#include<process.h>
void main( );
{
clrscr( );
void convert(float &,char &,char);
float dist;
36
char opt, type = ‘F’;
cout<<”\nenter dist. in feet : ”;
cin>>dist;
cout<< “\nyou want dist. in feet or inches? (F/I) : \n”;
cin>>opt;
switch(opt)
{
case ‘F’ : convert(dist, type, ‘F’);
break;
case ‘I’ : conert(dist, type, ‘I’)
break;
default : cout<< “\nyou have entered a wrong choice!!!”;
exit(0);
}
cout<< “\ndist. = ”<<dist<< “ ”<<type<<’\n’;
getch( );
}
void convert(float &d, char &t, char ch)
{
switch(ch)
{
case ‘F’ : if (t==’I’)
{
d=d/2;
t=’F’;
}
break;
case ‘I’ : if(t==’F’)
{
d=d*12;
t=’I’;
}
37
break;
}
return;
}
o/p –
enter dist. in feet : 15
you want dist. in feets or inches? (F/I) :
I
dist. = 180 I
Sol:#include<iostream.h>
#include<conio.h>
#include<process.h>
void main( )
{
clrscr( );
void calc(int, int, char)
int a,b;
char ch;
cout<< “\nenter arithmetic operator (+, -, *, /, %) : \n”;
cin>>ch;
calc(a, b, ch);
getch( );
}
38
void calc(int x, int y, char ch)
{
switch(ch)
{
case ‘+’ :cout<< “\nsum of”<<x<< “ and ”<<y<<
“is”<<(x+y);
break;
case ‘-’ :cout<< “\ndifference of” <<x<< “and”<<y<<
“is”<<(x-y);
break;
case ‘*’ :cout<< “\nproduct of”<<x<< “and”<<y<<
“is”<<(x*y);
break;
case ‘/’ :cout<< “\nquotient of”<<x<< “and”<<y<<
“is”<<(x/y);
break;
case ‘%’ : cout<< “\nremainder of”<<x<< “and”<<y<<
“is”<<(x%y);
break;
}
return;
}
39
Q1. Write a C++ program that declares a structure time having two
int variables hrs and mins and declares a variable of data type time.
40
Also write statements to change time in AM or PM according to the
trivial system of 24 hours.
Sol:#include<iostream.h>
#include<conio.h>
struct time
{
int hrs;
char mins;
}
void main( )
{
clrscr( );
time time1;
cin>>time1.hrs>>time1.mins;
if(time1.hrs<12)
cout<<time1.hrs<<":"<<time1.mins<<"A.M.";
else
if(time1.hrs>12)
cout<<time1.hrs<<":"<<time1.mins<<"P.M.";
else
if(time1.hrs==12 && time1.mins==0)
cout<<time1.hrs<<":"<<time1.mins<<"noon";
else
if(time1.hrs==12 && time1.mins>0)
cout<<time1.hrs<<":"<<time1.mins<<"P.M.";
getch();
}
o/p – 12 34
12:34 P.M.
41
Q2. Write a C++ program to accept and print a student’s result using
a structure having array inside it.
Sol:#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct student
{
int rollno;
char name[21];
float marks[5];
char grade;
};
student learner;
void main( );
{
cout<< “\n”<< “enter roll no. :”;
cin>>learner.roll.no;
cout<< “\n”<< “enter name : ”;
gets(learner.name);
cout<< “\n”<< “enter marks in 5 suvjects : ”<< “\n”;
for(int i=0; i<5;i++)
{
cout<<’\n’<< “subject”<<i+1<< “:”;
cin>>learner.marks[i];
}
float avg,total;
total=(learner.marks[0]+learner.marks[1]+learner.marks[2]+lear
ner.marks[3]+learner.marks[4]);
avg=total/5;
if(avg<50)
learner.grade = ‘F’;
else
42
if(avg<60)
learner.grade = ‘C’;
else
if (avg<80)
learner.grade = ‘B’;
else
learner.grade = ‘A’;
cout<< “\n”<<”student result :”<< “\n”;
cout<< “rollno :”<<learner.roll<< “\t”;
cout<<”name :”;
cout.write(learner.name,21);
cout<< “\n”<< “total marks :”<<total;
cout<< “\t”<< “grade :”<<learner.grade<<endl;
getch( );
}
o/p –
enter roll no : 21
enter name : Navya
enter marks in five subjects :
subject 1: 98
subject 2: 89
subject 3: 99
subject 4: 82
subject 5: 79
student result
roll no : 21 name : Navya
total marks : 457 grade : A
43
Teacher’s Signature:
44