0% found this document useful (0 votes)
911 views

Report File: Maitreyi Singh Class - Xi/A Roll No. - 26

The document is a report file submitted by Maitreyi Singh, a student of Class XI/A at Delhi Public School in Greater Noida. It contains programs on various topics like flow control, arrays, functions, and structures in C++ that were completed under the guidance of her computer teacher. The report consists of a certificate, contents listing the topics covered and their page numbers, and the programs written by the student to solve the given problems.

Uploaded by

Mridul Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
911 views

Report File: Maitreyi Singh Class - Xi/A Roll No. - 26

The document is a report file submitted by Maitreyi Singh, a student of Class XI/A at Delhi Public School in Greater Noida. It contains programs on various topics like flow control, arrays, functions, and structures in C++ that were completed under the guidance of her computer teacher. The report consists of a certificate, contents listing the topics covered and their page numbers, and the programs written by the student to solve the given problems.

Uploaded by

Mridul Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 44

REPORT FILE

MAITREYI SINGH
CLASS – XI/A
ROLL NO. – 26
CERTIFICATE

This is to certify that the presentation and documentation of this


report file is successfully completed by Maitreyi Singh of grade XI of
Delhi Public School, Greater Noida in the session 2010-2011 under the
guidance and supervision of undersigned.

Signature:

Mr. Narendra Singh


(Computer Teacher)
Delhi Pubic School, Greater Noida

2
CONTENTS

Topic Page no.

(a) Flow of Control:-


(i) Display pattern 6
(ii) Display odd nos. 6-7
(iii) Prime 7-8
(iv) Identification of character 8-9
(v) Average 9-10
(vi) Reversing a number 9-11
(vii) Truth table 11-12
(viii) Palindrome 12-13

(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

(e) Teacher’s Remarks 43

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

Q2. Write a C++ program to display odd numbers.

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( );
}

o/p – enter number


12
1 3 5 7 9 11 13 15 17 19 21 23

Q3. Write a C++ program to check whether a number is prime or not.

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( );
}

o/p – enter a number 121


not prime

Q4. Write a C++ program to find whether the given character is a


digit or a letter.

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( );
}

o/p – enter a symbol from keyboard : A


A is an alphabet

Q5. Write a C++ program to find average of list of numbers entered


through keyboard.

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( );
}

o/p – the numbers of enteries to be done 5


90
94
100
92
81
average of numbers entered is 91.4

Q6. Write a complete C++ program to do the following :


(1) Read an integer X
(2) Form an integer Y by reversing the digits of X and integer S
having
the sum of digits of X
(3) Output Y and S.

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

Q8. Write a C++ program to find those which are palindromes.

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 ( );
}

o/p – enter a number


1331
1331
the given number is palindrome.

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

Q2. Write a C++ program to sort an array in ascending order using


selection sort.

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

Q3. Write a C++ program to sort an array in ascending order using


bubble sort.

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

Q4. Write a C++ program to sort an array in ascending order using


insert sort.

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

Q6. Write a C++ program to count the number of words in a string as


well as the number of characters other than a space.

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 ( );
}

o/p – my name is abcd

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

Q8. Write a C++ program to display diagonal elements in a 2-D array.

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

Q9. Write a C++ program to calculate difference of diagonal elements


in a 2-D array.

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

0 //difference of elements of two diagonals

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->";
}

void creation(int a[ ],int n)


{
int i;
for(i=0;i<n;i++)
cin>>a[i];
}

void traversal(int a[ ],int n)


{
int i;
for(i=0;i<n;i++)
30
cout<<setw(5)<<a[i];
}

void insertion(int a[ ],int & n,int num,int p)


{
(n>=10)
cout<<"sorry, underflow situation\n";
else
{
for(int i=(n-1);i>=p;i--)
a[i+1]=a[i];
a[p]=num;
n=n+1;
}
}

void deletion(int a[ ], int & n, int p)


{
if(n==0)
cout<<"sorry,underflow situation\n";
else
{
for(int i=p;i<(n-1);i++)
a[i]=a[i+1];
n=n-1;
}
}

void sorting(int a[ ],int n)


{
int i,x,t;
for(i=1;i<n;i++)
31
{ t=a[i];
x=i-1;
while(x>=0 && t<a[x])
{ a[x+1]=a[x];
x=x-1;
}
a[x+1]=t;
}
}//end of function definition
void main( )
{
int opt, n, a[10], num, p;
clrscr( );
ABC:menu( );
cin>>opt;
switch(opt)
{ case 1:cout<<"enter value for n->";
cin>>n;
creation(a,n);
break;
case 2:traversal(a,n);
getch( );
break;
case 3:cout<<"enter value and position->";
cin>>num>>p;
insertion(a,n,num,p);
break;
case 4:cout<<"enter position->";
cin>>p;
deletion(a,n,p);
break;
case 5:sorting(a,n);
32
}
if(opt<6)
goto ABC;
}

o/p –
enter your option->1 creation
enter value for n->6
1 2 3 4 5

enter your option->2 traversal


1 2 3 4 5

enter your option->3 insertion


enter value and position->24 2

enter your option->2


1 2 24 3 4 5

enter your option->4 deletion


enter position->5

enter your option->2


1 2 24 3 4

enter your option->5 sorting

enter your option->2


1 2 3 4 24

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

Q3. Write a program to swap two values using functions.

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’;
}

o/p – the original values are:


a=7,b=4
the swapped values are:
a=4,b=7
the values after swap( ) are:
a=4,b=7

Q4. Write a C++ program to convert distance in feet or inches using a


call by reference method.

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

Q5. Write a C++ program that invokes a function calc( ) which


intakes two integers and an arithmetic operator and prints the
corresponding result.

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;
}

o/p – enter two integers : 12 5


enter arithmetic operator (+, -, *, /, %) : %
remainder of 12 and 5 is 2.

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:

Mr. Narendra Singh

44

You might also like