Programs - Govt. HSS Chettiyankinar

Chapter 5
Introduction to C++
Let us Do (Page : 143)
1.
#include <iostream.h>
void main()
{
cout << “SMOKING IS INJURIOS TO HEALTH”;
}
2.
#include <iostream.h>
void main()
{
cout << “TOBACCO CAUSES CANCER”;
}
Chapter 6
Basic Data Types and Operators
Check yourself (Page : 156)
1.
2.
3.
4.
char, int, float, double
variable
?: or conditional operator
a. 5
b. -3
c. 2
d. -8
e. -5
f. -2
g. -2
h. -1
i. 15
j. 0
4.
** Note that 3 is missing in book **
#include <iostream.h>
void main()
{
float ht, cms, inchs; int feet;
cout << “Enter height eg: 1.54 “;
cin >> ht;
cms = ht * 100;
inchs = cms/2.5;
feet = inchs/12;
inchs = inchs – feet*12;
cout << feet << “ feet “;
cout << inchs << “ inches”;
}
5.
Simple Interest I = PNR (R=R/100)
Compound Interest I = P x (1+R/100)N-P
#include <iostream.h>
void main()
{
float p, n, r, si, ci;
cout << “Enter principal amount “;
cin >> p;
cout << “Enter rate of interest “;
cin >> r;
cout << “Enter number of years “;
cin >> n;
si = p * n * r /100;
ci = p * pow(1+r/100, n) – p;
cout << “Simple Interest “ << si <<”\n”;
cout << “Compound Interest “ << ci;
}
Check yourself (Page : 164)
1. a.
c.
2. a.
c.
false
true
3
3
b.
d.
b.
d.
true
true
17
22
Lab Activity (Page : 180)
1.
#include <iostream.h>
void main()
{
int grams; float kg;
cout << “Enter grams “;
cin>>grams;
kg = (float) grams/1000;
cout << kg << “ KG”;
}
2.
#include <iostream.h>
void main()
{
cout << 2013 << “\t”
<< 2012 << ”\t”
<< 2011 << “\t”
<< 2010 << “\t”
<< 2009 << “\t”
}
<<
<<
<<
<<
<<
“100%” << ”\n”
“99.9%” << “\n”
“95.5%” << “\n”
“90.81%” <<“\n”
“85%” << “\n”;
6.
#include <iostream.h>
void main()
{
char ch; int i;
cout << “Enter a digit “;
cin >> ch;
i = ch;
cout << “ASCII of “ << ch << “ is “<<i;
ch=’\b’;
//Backspace
i = ch;
cout << “ASCII Of Backspace is “ << i;
}
7.
#include <iostream.h>
void main()
{
int s, m, h;
cout << “Enter seconds “;
cin >>s;
m = s/60;
s = s%60;
h = m/60;
m = m%60;
cout << h << “ hrs : “;
cout << m << “ min : “;
cout << s << “ sec.”;
}
if (a<c)
cout << “Smallest=” << a;
else
cout << “Smallest=” << c;
else
if (b<c)
cout << “Smallest=” << b;
else
cout << “Smallest=” << c;
Chapter 7
Control Statements
Program to check whether a number is a
positive, negative or zero.
#include <iostream.h>
void main()
{
int num;
cout << “Enter a number “;
cin >> num;
if (num==0)
cout << “It is zero”;
else if (num>0)
cout << num << “ is positive”;
else
cout << num << “ is negative”;
}
Program to input a character for sex and
display Male/Female.
#include <iostream.h>
void main()
{
char sex;
cout << “Enter sex (M/F) “;
cin >> sex;
if (sex==’M’)
cout << “Male”;
else if (sex==’F’)
cout << “Female”;
else
cout << “Error in the code”;
}
}
Program to read a character (a, b, c or d).
Display a – abacus, b – boolean, c –
computer, d – debugging
#include <iostream.h>
void main()
{
char ch;
cout << “Enter your choice “;
cin >> ch;
switch (ch)
{
case ‘a’:
cout << “abacus”;
break;
case ‘b’:
cout << “boolean”;
break;
case ‘c’:
cout << “Computer”;
break;
default:
cout << “Wrong input”;
}
}
Program to read a character and determine
whether it is alphabet, digit or others.
Program to input your age and
whether eligible to vote or not.
display
#include <iostream.h>
void main()
{
int age;
cout << “Enter your age “;
cin >> age;
if (age>=18)
cout << “You are eligible”;
else
cout << “You are not eligible”;
}
#include <iostream.h>
void main()
{
char c;
cout << “Enter a character “;
cin >> c;
if (isalpha(c))
cout << c << “ is alphabet”;
else if (isdigit(c))
cout << c << “ is digit”;
else
cout << c << “ is special char”;
}
Program to read three numbers and display
smallest one.
Program to read a number from 1-12 and
display name of the monthe.
#include <iostream.h>
void main()
{
int a, b, c;
cout << “Enter three numbers “;
cin >> a >> b >> c;
if (a<b)
#include <iostream.h>
void main()
{
int month;
cout << “Enter the month number “;
cin >> month;
switch (month)
{
break;
default:
cout << “Invalid Operator”;
case 1:
cout << “January”;
break;
case 2:
cout << “February”;
break;
case 3:
cout << “March”;
break;
case 4:
cout << “April”;
break;
case 5:
cout << “May”;
break;
case 6:
cout << “June”;
break;
case 7:
cout << “July”;
break;
case 8:
cout << “August”;
break;
case 9:
cout << “September”;
break;
case 10:
cout << “October”;
break;
case 11:
cout << “November”;
break;
case 12:
cout << “December”;
break;
default:
cout << “Wrong Input”;
}
}
Program to read a digit (0-9) and display
it in words.
}
}
Program to read two operands
operator and perform accordingly.
and
#include <iostream.h>
void main()
{
int a, b; char op;
cout << “Enter two numbers “;
cin >> a >> b;
cout << “Enter an operator “;
cin >> op;
switch(op)
{
case ‘+’:
cout <<a<<“+”<<b<<“=”<<a+b;
break;
case ‘-‘:
cout <<a<<”-“<<b<<”=”<<a-b;
break;
case ‘x’:
cout <<a<<”x”<<b<<”=”<<a*b;
break;
case ‘/’:
cout <<a<<”/”<<b<<”=”<<a/b;
an
#include <iostream.h>
void main()
{
int digit;
cout << “Enter a digit “;
cin >> digit;
switch (digit)
{
case 0:
cout << “Zero”;
break;
case 1:
cout << “One”;
break;
case 2:
cout << “Two”;
break;
case 3:
cout << “Three”;
break;
case 4:
cout << “Four”;
break;
case 5:
cout << “Five”;
break;
case 6:
cout << “Six”;
break;
case 7:
cout << “Seven”;
break;
case 8:
cout << “Eight”;
break;
case 9:
cout << “Nine”;
break;
default:
cout << “Not a digit”;
}
}
Program to input a number and display
whether it is a multiple of 5 or not.
#include <iostream.h>
void main()
{
int num;
cout << “Enter a number “;
cin >> num;
if (num%5==0)
cout <<num<<“ is a multiple of 5”;
else
cout<<num<<” is not a multiple”;
}
or
#include <iostream.h>
void main()
{
int num;
cout << “Enter a number “;
cin >> num;
cout << (num%5==0)?”Multiple of 5”:
cout << “Not a multiple of 5”;
}
Rewrite the following using if .. else
result = (mark>=30) ? ‘P’:’F’;
if
#include <iostream.h>
void main()
{
int cnt=0, a, sum=0;
for (a=2; a<49; a=a+2)
{
sum = sum + a;
cnt++;
}
cout << “Sum=”<<sum<<”\n”;
cout << “Average=” <<(float) sum/cnt;
}
Program to print numbers between 10 and 50
which are divisible by 3 and 5.
(mark>=30
result = ‘P’;
else
result = ‘F’;
Program display odd numbers from 100-200
#include <iostream.h>
void main()
{
int a;
a=101;
while (a<=200)
{
cout << a << “\t”;
a = a + 2;
}
}
#include <iostream.h>
void main()
{
int a;
for (a=10; a<=50; a++)
{
if (a%3==0 && a%5==0)
cout << a << “\t”;
}
}
Predict output of the following.
for (int i=1; i<=10; ++i);
cout << i + 2;
Ans:- This is an empty loop. The loop
terminates when i reaches 11. Hence output
will be 13 (i+2 means 11+2)
Program to find average of first N numbers.
#include <iostream.h>
void main()
{
int a, n, sum=0;
cout << “Enter the limit “;
cin >> n;
for (a=1; a<=n; a++)
sum = sum + a;
cout << “Average = “ << (float) sum/n;
}
Program to display multiplication table.
#include <iostream.h>
void main()
{
int a, n;
cout << “Enter a number “;
cin >> n;
for (a=1; a<=10; a++)
cout <<a<<”x”<<n<<”=”<<a*n<<”\n”;
}
Program to find sum and average of numbers
between 1 and 49.
Predict output of the following.
sum=0;
for (i=1; i<=3; ++i)
{
for (j=1; j<=3; ++j)
sum = sum + i * j;
}
cout << sum;
Ans: The outer loop works 3 times with
value of i from 1 to 3. The inner loop
works 3 times with value of j from 1 to 3
for each execution of outer loop. So the
sum will be
1 x 1 + 1 x 2 + 1 x 3 + 2 x 1 + 2 x 2 +
2 x 3 + 3 x 1 + 3 x 2 + 3 x 3 = 36
Write a program to print following.
1
2
3
4
5
2
3 3
4 4 4
5 5 5 5
First pattern
1
1
1
1
1
2
2 3
2 3 4
2 3 4 5
#include <iostream.h>
void main()
{
int x, y;
for (x=1; x<=5; x++)
{
cout << “\n”;
for (y=1; y<=x; y++)
cout << x << “ “;
}
}
Second pattern
#include <iostream.h>
void main()
{
int x, y;
for (x=1; x<=5; x++)
{
cout << “\n”;
for (y=1; y<=x; y++)
cout << y << “ “;
}
}
Answer to sample Questions- Refer
book for questions. (Page 230-232)
Text
Very Short Answer Type
1. The break statement causes the control
to jump outside the switch structure.
Otherwise, it will execute all the
statement below it.
2. 16
3. i=1;
i=1;
while (i<=10)
do
{
{
cout << i;
cout << i;
i++;
i++;
}
} while (i<=10);
4. 5 times
5. process.h
6. goto
7. It will execute a set of statement when
none of the values given in the case
statements are matching.
Short Answer Types.
1. Version 2 tests the first condition and
executes the statement below it when
the condition is True. It will test the
second condition only when the first
condition is false and so on. Version
1
tests
the
first
condition
and
executes
statement
below
it
when
condition is True. It then tests the
second condition and executes statement
below it if condition is true and so on.
Version
1
tests
all
conditions
unnecessarily, but version 2 tests 2nd
condition onwards only if necessary.
2. The for loop brings all the statements
needed to construct a loop in a single
line. Its syntax follows.
for (initialisation; test; updation)
eg;
for (a=1; a<=10; a++)
cout << a << “\t”;
3. The for loop will be usable when a
finite initialisation, increment and
condition is true. It makes the program
compact. The while loop is an entry
controlled loop and condition is tested
before execution of body of loop. If
the condition can be tested before the
first execution, then while loop may be
used. The do while loop is a exit
controlled loop. If we can’t test the
condition before first execution of the
loop, then this may be used.
4. switch (a)
{
case 1:
cout << “One”; break;
case 0:
cout << “Zero”; break;
default:
cout << “Not a binary digit”;
}
5. The loop will be infinite, since it
does not update value of z. The
statement z-- is necessary in the loop.
6.
10
1
10
2
10
3
9
1
9
2
9
3
8
1
8
2
8
3
7
1
7
2
7
3
6
1
6
2
6
3
7. It displays the number 5 10 15 20 25 30
35 40 45 50 each in a line. The inner
loop assigns the value n*m to num. It
multiplies n with m, in the first case
1 x 5, in the second case 2 x 5 and so.
8. A loop defined in a program must be
finite. In order to make it finite, we
need a variable to control execution of
a loop. It is called controlling
variable. It must be first initialized,
and
the
process
is
called
initialisation. A condition is then
designed using the controlling variable.
The loop will be executed again only
when the condition is true. This
condition is called Test condition.
The initial value of the controlling
variable must be changed from its
initial value to reach its final value.
An increment/decrement is needed and it
is
called
updation.
necessary for any loop.
These
are
Long Answer Types
1. a) res will be 400
b) res will be 200
2.
Entry controlled loop
#include <iostream.h>
void main()
{
int num, sum=0, digit;
cout << “Enter a number “;
cin >> num;
while (num>0)
{
digit – num % 10;
sum = sum + digit;
num = num / 10;
}
cout << “Sum=” << sum;
}
Exit controlled loop
#include <iostream.h>
void main()
{
int num, sum=0, digit;
cout << “Enter a number “;
cin >> num;
do
{
digit – num % 10;
sum = sum + digit;
num = num / 10;
} while (num>0);
cout << “Sum=” << sum;
}
3.
#include <iostream.h>
void main()
{
int num, n, sum, d;
for (num=1; num<1000; num++)
{
n = num;
sum = 0;
while (n>0)
{
digit = num % 10;
sum = sum + d*d*d;
n = n / 10;
}
if (num==sum)
cout << num << “\t”;
}
}
4. The different jump statements are break,
continue, goto and exit(0). The break
can be used to jump outside a loop or
switch structure. The continue goes
back to the beginning of a loop and the
condition is tested again. The goto
moves the control of execution of a
program to a named label in a program.
The exit(0) defined in the header file
process.h will terminate execution of a
program.
5.
#include <iostream.h>
void main()
{
char x, y;
for (x=’A’; x<=’E’; x++)
{
cout << “\n”;
for (y=’A’; y<=x; y++)
cout << y << “ “;
}
}
6. when else clause is forgot to use in a
if else statement, the if else became a
simple if. The statement just after if
will
be
executed
only
when
the
condition is true. The statement after
the forgotten else will be executed
always.
Chapter 8
Arrays
Let us Do (Page 238)
1.
Scores of 100 students : int score[100];
English letters
: char letters[26];
A list of 10 years
: int years[10];
A list of 30 real nos
: float num[30];
2. a. int
s[10]={89,75,82,93,78,95,81,88,77,82};
b. float
amt[5]={10.63,13.98,18.45,12.68,14.76};
c. float i[100]={6.95,7.25,7.35,7.40,7.42};
d. int marks[10]={0};
e. char
alpha[]={‘V’,’I’,’B’,’G’,’Y’,’O’,’R’};
f. int days[12]={31,28,31,30,31,30,31,31,
30,31,30,31};
3. for (i=0; i<50; i++)
{
cout << “Enter a number “;
cin >> ar[i];
}
4. for (i=0; i<100; i=i+2)
cout << ar[i] << “\t”;
Lab Activity Questions Page : 258
1.
#include <iostream.h>
void main()
{
float salesamt[12], total, avg;
int i;
for (i=0; i<12; i++)
{
cout << “Enter Amount “;
cin >> salesamt[i];
}
total=0;
for (i=0; i<12; i++)
total = total + salesamt[i];
avg = total / 12;
cout << “Total “ << total << “\n”;
cout << “Average “ << avg << “\n”;
else if (a[i]<min)
min = a[i];
}
cout << “Largest=”<<max<<”\n”;
cout << “Smalles=”<<min<<”\n”;
}
}
2.
#include <iostream.h>
void main()
{
int a[100],n, i, tot=0, avg;
cout << “Enter number of numbers”;
cin >> n;
for (i=0; i<n; i++)
{
cout << “Enter a number “;
cin >> a[i];
tot = tot + a[i];
}
avg = tot / n;
for (i=0; i<n; i++)
{
if (a[i]>avg)
cout << a[i] << “\t”;
}
}
5.
#include <iostream.h>
void main()
{
int a[10][10], n, i, j;
cout << “Enter no. of rows “;
cin>>n;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
{
cout <<”Enter a number”;
cin >> a[i][j];
}
cout << “ZThe matrix will be\n”;
for (i=0; i<n; i++)
{
cout<<”\n”;
for (j=0; j<n; j++)
{
if (i<j)
cout <<” \t”;
else
cout<<a[i][j]<<”\t”;
}
}
}
3.
#include <iostream.h>
void main()
{
int qty[10], i;
float rate[10], amt[10];
for (i=0; i<10; i++)
{
cout << “Enter qty “;
cin >> qty[i];
cout << “Enter rate “;
cin >> rate[i];
amt[i] = qty[i]*rate[i];
}
cout<< “Quantity\tRate\tAmount\n”;
for (i=0; i<10; i++)
{
cout << qty[i] << “\t”;
cout << rate[i] << “\t”;
cout << amt[i] << “\n”;
}
}
4.
#include <iostream.h>
void main()
{
int a[10], i, min, max;
for (i=0; i<10; i++)
{
cout << “Enter a number “;
cin >> a[i];
}
min=max=a[0];
for (i=1; i<10; i++)
{
if (a[i]>max)
max = a[i];
6.
#include <iostream.h>
void main()
{
int a[10][10], n, i, j;
cout << “Enter no. of rows “;
cin>>n;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
{
cout <<”Enter a number”;
cin >> a[i][j];
}
cout << “ZThe matrix will be\n”;
for (i=0; i<n; i++)
{
cout<<”\n”;
for (j=0; j<=i; j++)
{
cout<<a[i][j]<<”\t”;
}
}
}
7. Leading diagonal elements are a[0][0],
a[1][1], a[2][2],........
#include <iostream.h>
void main()
{
int a[10][10], n, i, j,sum=0;
cout << “Enter no. of rows “;
cin>>n;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
{
cout <<”Enter a number”;
cin >> a[i][j];
}
for (i=0; i<n; i++)
sum = sum + a[i][i];
cout << “Sum=” << sum;
7. 0
8. int scores[6]={89,75,82,93,78,95};
9. Array Traversal
10. No. of elements 2x3=6, 6x2=12 bytes
11. n+1, when no of items doubled, no. of
searches will be increased only by 1 in
the case of binary search.
12. int marks[10]={0};
13. False
Short Answer Type Questions
}
4.
#include <iostream.h>
void main()
{
int a[10], evn=odd=0, i;
for (i=0; i<n; i++)
{
cout << “Enter number “;
cin >> a[i];
}
for (i=0; i<n; i++)
if (a[i]%2==0)
evn++;
else
odd++;
cout << “No. of even “<<evn;
cout <<”\nNo. of odd “<<odd;
}
8. Off diagonal elements of a 3x3 square
matrix are a[0][2],a[1][1],a[2][0]
#include <iostream.h>
void main()
{
int a[10][10], n, i, j,sum=0;
cout << “Enter no. of rows “;
cin>>n;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
{
cout <<”Enter a number”;
cin >> a[i][j];
}
for (i=0,j=n-1;i<n;i++,j--)
sum = sum + a[i][j];
cout << “Sum=”<<sum;
}
5. int num[]={2,3,4,5};
int num[4]={2,3,4,5};
9. In Text book, second line of the
triangle is missing. It is 1 1
#include <iostream.h>
void main()
{
int a[10][10]={0}, n, i, j;
cout << "Enter no. of rows ";
cin>>n;
for (i=0; i<n; i++)
{
a[i][0]=1;
for (j=1; j<i; j++)
a[i][j]=a[i-1][j-1]+a[i-1][j];
or
Long Answer Type Questions
2.
#include <iostream.h>
void main()
{
int a[10],b[10],i;
for (i=0; i<10; i++)
{
cout << “Enter number “;
cin >> a[i];
}
for (i=0; i<10; i++)
{
cout << “Enter a number “;
cin >> b[i];
}
cout << “Differences \n”;
for (i=0; i<10; i++)
cout << a[i]-b[i] <<”\t”;
}
a[i][j]=1;
}
// Printing pascals triangle
for (i=0; i<n; i++)
{
cout << "\n";
for (j=0; j<=i; j++)
cout << a[i][j]<<"\t";
}
}
5.
Very Short Answer Type : Page 259
1.
2.
3.
4.
5.
6.
same
0 to 9
index or subscript number.
Eighth
3
2
#include <iostream.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int i, j, m, n;
cout <<”Enter order of matrix “;
cin>>m>>n;
for (i=0; i<m; i++)
for (j=0; j<n; j++)
{
cout <<”Enter a number “;
cin >> a[i][j];
}
for (i=0; i<m; i++)
for (j=0; j<n; j++)
{
cout <<”Enter a number “;
cin >> b[i][j];
}
for (i=0; i<m; i++)
for (j=0; j<n; j++)
{
c[i][j]=a[i][j]-b[i][j];
}
for (i=0; i<m; i++)
cout << “Largest=”<<largest;
}
Chapter 9
String Handling and I/O Operations
Check Yourself (Page : 269)
1.
2.
3.
4.
5.
6.
‘\0’ or NULL character.
char str[]=”Save Earth”;
stdio.h
getch()
getch() / getche()
11 letters in the string hence 12 bytes
needed because an extra byte for ‘\0’
Lab Activity (Page : 273)
{
cout << “\n”;
for (j=0; j<n; j++)
cout << c[i][j]<<”\t”;
}
}
6.
#include <iostream.h>
void main()
{
int a[10][10];
int i, j, m, n;
cout <<”Enter order of matrix “;
cin>>m>>n;
for (i=0; i<m; i++)
for (j=0; j<n; j++)
{
cout <<”Enter a number “;
cin >> a[i][j];
}
int sum=0;
for (i=0; i<m; i++)
for (j=0; j<n; j++)
sum = sum + a[i][j];
cout << “Sum=”<<sum<<”\n”;
cout << “Average=”<<sum/(m*n);
}
7.
#include <iostream.h>
void main()
{
int a[10][10];
int i, j, m, n;
cout <<”Enter order of matrix “;
cin>>m>>n;
for (i=0; i<m; i++)
for (j=0; j<n; j++)
{
cout <<”Enter a number “;
cin >> a[i][j];
}
int largest=a[0][0];
for (i=0; i<m; i++)
for (j=0; j<n; j++)
if (a[i][j]>largest)
largest = a[i][j];
1.
#include <iostream.h>
void main()
{
char str[80];
int i, u=l=d=s=w=0;
cout <<”Enter a string “;
cin.getline(str, 80);
for (i=0; str[i]!=’\0’)
{
if (isupper(str[i]))
u++;
else if (islower(str[i]))
l++;
else if (isdigit(str[i]))
d++;
else if (str[i]==’ ‘)
w++;
else
s++;
}
cout << “Upper case “ << u<<”\n”;
cout << “Lower case “ << l<<”\n”;
cout << “Digits “ << d << “\n”;
cout <<“Special characters “<<s<<”\n”;
cout << “White spaces “ << w;
}
2.
#include <iostream.h>
void main()
{
char str[80];
int i, w=0;
cout <<”Enter a string “;
cin.getline(str, 80);
for (i=0; str[i]!=’\0’; i++)
if (str[i]==’ ‘)
w++;
cout << “No. of words “ << w+1;
}
3.
#include <iostream.h>
#include <ctype.h>
void main()
{
char str[80];
int i, w=0;
cout <<”Enter a string “;
cin.getline(str, 80);
for (i=0; str[i]!=’\0’; i++)
str[i] = tolower(str[i]);
cout << str;
cin.getline(str, 80);
for (i=0, j=strlen(str)-1;i<j;i++,j--)
if (str[i]!=str[j])
{
p=1;
break;
}
if (p==0)
cout << str << “ is palindrome”;
else
cout <<str<<” is not palindrome”;
}
4.
#include <iostream.h>
#include <string.h>
#include <stdio.h>
void main()
{
char str[80], rev[80];
int i, j;
cout <<”Enter a string “;
cin.getline(str, 80);
j=strlen(str)-1;
for (i=0; strlen(str); i++, j--)
rev[i]=str[j];
rev[i]=’\0’;
puts(rev);
}
5.
#include <iostream.h>
#include <ctype.h>
void main()
{
char str[80];
int i, w=0;
cout <<”Enter a string “;
cin.getline(str, 80);
for (i=0; str[i]!=’\0’; i++)
for (j=0; j<=i; j++)
cout.put(str[i]).”\t”;
}
6.
#include <iostream.h>
#include <stdio.h>
void main()
{
char str[80];
int i, w=0;
cout <<”Enter a string “;
cin.getline(str, 80);
putchar(str[0]);
for (i=1; str[i]!=’\0’; i++)
if (str[i]==’ ‘)
putchar(str[i+1]);
}
7.
#include <iostream.h>
#include <string.h>
void main()
{
char str[80];
int i, j, p=0;
cout <<”Enter a string “;
}
Very Short Answer Type Questions(Page: 273)
1. The character input from the keyboard
will be displayed.
3. cin.get(str, 80)l
Short Answer Type Questions (Page : 274)
1. cout.write(“Enter a character”);
cin.getline(str, 10);
puts(str);
2. P-r-o-g-r-a-m3. (a) ch=getchar();
(b) gets(str);
(c) puts(“hello”);
(d) correct
(e) correct
4. Sachin, because the cin command
terminates an input when a white space
is encountered. The solution is to use
either of the following statements.
cin.getline(name,20);
gets(name);
Chapter 10
Functions
Lab Activity (Page : 311)
1.
#include <iostream.h>
int prime(int);
void main()
{
int a;
for (a=100; a<=200; a++)
if (prime(a)==1)
cout << a << “\t”;
}
int prime (int n)
{
int p=1, i;
for (i=2; i<=n/2; i++)
if (n%i==0)
p=0;
return p;
}
2.
#include <iostream.h>
int small(int a, int b, int c=0)
{
int s;
s=(a<b)?(a<c)?a:c:(b<c)?b:c;
return s;
}
void main()
{
cout <<”Smallest of 5, 3, 7 “;
cout << small(5, 3, 7) << “\n”;
cout << “Smallest of 15, 28 “;
cout << small(15, 28);
}
3.
#include <iostream.h>
int sumdigit(int);
void main()
{
int n;
cout << “Enter a number “;
cin >> n;
cout << “Sum of digits is “;
cout << sumdigit(n);
}
int sumdigit(int n)
{
int sum=0, digit;
while (n>0)
{
digit = n % 10;
sum = sum + digit;
n=n/10;
}
return sum;
}
4.
#include <iostream.h>
int lcm(int, int);
void main()
{
int a, b;
cout << “Enter two numbers “;
cin >> a >> b;
cout << “LCM is “<<lcm(a, b);
}
int lcm(int x, int y)
{
int i, min, f;
min = (x<y)?x:y;
for (i=min; i>=1; i--)
{
if (x%i==0 && y%i==0)
{ f=i; break;
}
}
return x*y/f;
}
5.
#include <iostream.h>
int ispalindrome(int);
void main()
{
int a, b;
cout << “Enter range from “;
cin >> a;
cout << “Enter range to “;
cin >> b;
for (;a<=b; a++)
if (ispalindrome(a))
cout << a << “\t”;
}
int ispalindrome(int n)
{
int sum=0, digit, temp;
temp=n;
while (n>0)
{
digit = n % 10;
sum = sum*10 + digit;
n=n/10;
}
if (sum==temp)
return 1;
else
return 0;
}
Short Answer Questions (Page : 312)
2. (a) double Total(double, double);
(b) void Math();
Long Answer Questions (Page : 312)
1. (a) Default arguments.
(b) (i) 6
(ii) 7
(iii) wrong
(iv) 0
3. (a)
(b)
(c)
(d)
(e)
Return value not used in program.
Argument values not given.
Too Many Arguments, Only 2 allowed.
Input to a function not allowed.
Not enough argument values.
*** Recursive function to get nth term of
a fibonacii series
int fibo(int n)
{
if (n<=2)
return 1;
else
return fibo(n-1)+fibo(n-1);
}
** Recursive function to display hexa
decimal of a decimal integer.
void hexadec(int n)
{
if (n>=16)
hexadec(n/16);
switch(n)
{
case 10: cout
case 11: cout
case 12: cout
case 13: cout
case 14: cout
case 15: cout
default: cout
}
}
<<
<<
<<
<<
<<
<<
<<
“A”;
“B”;
“C”;
“D”;
“E”;
“F”;
n;
break;
break;
break;
break;
break;
break;