Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

www.Padasalai.Net www.TrbTnpsc.

com

VOLUME – II
CHAPTER – 9
INTRODUCTION TO C++
HANDS ON PRACTICE PROGRAMS
1. Write C++ programs to interchange the values of two variables.
a. Using with third variable
Coding:
#include <iostream>
using namespace std;
int main()
{
int n1, n2, temp;
cout<<"Enter 1st Number: ";
cin>>n1;
cout<<"Enter 2nd Number: ";
cin>>n2;
//displaying numbers before swapping
cout<<"Before Swap"<<n1<<n2;
//swapping
temp=n1;
n1=n2;
n2=temp;
//displaying numbers after swapping

}
www.Padasalai.Net
cout<<"\nAfter Swap"<<n1<<n2;
system("pause");
return 0;

b. Without using third variable


Coding:
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"\nEnter two numbers : ";
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"\nAfter swapping numbers are : ";
cout<<a<<"\t"<<b;
system("pause");
return 0;
}

2. Write C++ programs to do the following:


a. To find the perimeter and area of a quadrant.
Page 1
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

Coding:
#include<iostream>
using namespace std;
int main( )
{
int r;
float area,peri;
cout<<"enter radius";
cin>>r;
area = 3.14*r*r*;
peri = 2*3.14*r;
cout<<"area= "<<area;
cout<<"\n perimeter = "<<peri;
system("pause");
return 0;
}

b. To find the area of triangle.


Coding:
#include <iostream>
using namespace std;
int main()
{
int height, base;

www.Padasalai.Net
float area;
cout<<"Enter height and base : ";
cin>>height>>base;
area= (0.5)*height*base;
cout<<"Area of triangle is : "<<area;
system("pause");
return 0;
}

c. To convert the temperature from Celsius to Fahrenheit.


Coding:
#include<iostream>
using namespace std;
int main()
{
float fahrenheit, celsius;
cout << "Enter the temperature in Celsius : ";
cin >> celsius;
fahrenheit = (celsius * 9.0) / 5.0 + 32;
cout << "The temperature in Celsius : " << celsius << endl;
cout << "The temperature in Fahrenheit : " << fahrenheit << endl;
system("pause");
return 0;
}

Page 2
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

3. Write a C++ to find the total and percentage of marks you secured from
10th Standard Public Exam. Display all the marks one-by-one along with
total and percentage. Apply formatting functions.
Coding:
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int rollno,tam,eng,mat,sci,soc,total;
float percentage;
char name[20], div[10];
cout << "Input the Roll Number of the student :";
cin >> rollno;
cout << "Input the Name of the Student :";
cin >> name;
cout << "Input the marks of Tamil, English, Maths, Science, & Social";
cin >>tam>>eng>>mat>>sci>>soc;
total=tam+eng+mat+sci+soc;
percentage = total/5.0;
if (percentage >= 60)
strcpy(div,"First");
else if (percentage < 60 && percentage >= 48)
strcpy(div,"Second");

www.Padasalai.Net
else if (percentage <48 && percentage >= 36)
strcpy(div,"Pass");
else
strcpy(div, "Fail");
cout << "Roll No : "<< rollno <<endl << "Name of Student : " << name <<endl;
cout << "Marks in Tamil : "<<tam<< endl;
cout << "Marks in English : "<<eng<< endl;
cout << "Marks in Maths : "<<mat<< endl;
cout << "Marks in Science : "<<sci<< endl;
cout << "Marks in Social Science : "<<soc<< endl;
cout << "Total Marks = " << total <<endl;
cout << "Percentage = " << percentage <<endl;
cout << "Division = " << div <<endl;
system("pause");
return 0;
}

CHAPTER – 10
FLOW OF CONTROL
HANDS ON PRACTICE PROGRAMS
1. Temperature – conversion program that gives the user the option of
converting Fahrenheit to Celsius or Celsius to Fahrenheit and depending
upon user's choice.

Page 3
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

Coding:
#include<iostream>
using namespace std;
int main()
{
int a;
cout<<"1. For Celsius To Fahrenheit. \n";
cout<<"2. For Fahrenheit To Celsius. \n";
cout<<"3. For Exit\n\n";
cout<<"Enter Your Choice \n ";
cin>>a;
switch(a)
{
double cel,feh;
case 1:
cout<<"Enter The Temperature In Celsius\n";
cin>>cel;
feh=(cel*9/5)+32;
cout<<"\nTemperature In Fahrenheit Is = "<<feh ;
break;
case 2:
cout<<"Enter The Temperature In Fahrenheit\n";
cin>>feh;
cel=(feh-32)*5/9;

www.Padasalai.Net
cout<<"\nTemperature In Celsius Is = "<<cel ;
break;
case 3:
exit(0);
default:cout<<"\nEnter The Right Choice \n";
break;
}
system("pause");
return 0;
}

2. The program requires the user to enter two numbers and an operator.
It then carries out the specified arithmetical operation: addition,
subtraction, multiplication or division of the two numbers. Finally, it
displays the result.
Coding:
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
char ch;
cout<<”Enter the Two Numbers”;
cin>>a>>b;
cout<<”\nEnter the Choice”;

Page 4
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

cout<<”\nAddition(+)”;
cout<<”\nSubtraction(-)”;
cout<<”\nMultiplication(*)”;
cout<<”\nDivision(/)”;
cin>>ch;
switch(ch)
{
case „+‟:
c=a+b;
cout<<”\nThe Addition of two numbers are=”<<c;
break;
case „-„:
c=a-b;
cout<<”\nThe Subtraction of two numbers are=”<<c;
break;
case „*‟:
c=a*b;
cout<<”\nThe Multiplication of two numbers are=”<<c;
break;
case „/‟:
c=a/b;
cout<<”\nThe Division of two numbers are=”<<c;
break;
default:
cout<<”\nYou chose the Wrong Character”;

} www.Padasalai.Net
break;

system(“pause”);
return 0;
}

3. Program to input a character and to print whether a given character is


an alphabet, digit or any other character.
Coding:
#include<iostream>
#include <stdio.h>
using namespace std;
int main()
{
char ch;
cout<<”Enter any Character:”; /* Input character from user */
cin>>ch;
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) /* Alphabet check */
{
cout<<ch<<”is alphabet”;
}
else if(ch >= '0' && ch <= '9')
{
cout<<ch<<”is digit”;

Page 5
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

}
else
{
cout<<ch<<”is special character”;
}
system("pause");
return 0;
}

4. Program to print whether a given character is an uppercase or a


lowercase character or a digit or any other character. Use ASCII codes for
it. The ASCII codes are as given below:
Characters ASCII Range
'0' - '9' 48 - 57
'A' - 'Z' 65 - 90
'a' - 'z' 97 - 122
other characters 0- 255 excluding the above mentioned codes.
Coding:
#include<iostream>
using namespace std;
int main()
{

www.Padasalai.Net
char ch;
cout<<"Enter any character: ";
cin>>ch;
if(ch>=65&&ch<=90)
cout<<endl<<"You entered an uppercase character";
else if(ch>=48&&ch<=57)
cout<<endl<<"You entered a digit";
else if(ch>=97&&ch<=122)
cout<<endl<<"You entered a lowercase character";
else
cout<<endl<<"You entered a special character";
system("pause");
return 0;
}

5. Write a C++ Program to calculate the factorial of an integer.


Coding:
#include <iostream>
using namespace std;
int main()
{
unsigned int n;
unsigned long long factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;

Page 6
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

for(int i = 1; i <=n; ++i)


{
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
system("pause");
return 0;
}

6. Write a C++ Program that print 1 2 4 8 16 32 64 128.


Coding:
#include<iostream>
using namespace std;
int main()
{
int i;
for(i=1;i<=128;i*=2)
cout<<i<<" ";
system("pause");
return 0;
}

7. Program to generate divisors of an integer.


Coding:

www.Padasalai.Net
#include<iostream>
using namespace std;
int main()
{
int i,n;
cout<<"enter the value of the integer";
cin>>n;
for(i=1;i<=n;i++)
{
if(n%i==0)
cout<<i<<endl;
}
system("pause");
return 0;
}

8. Program to print fibonacci series i.e., 0 1 1 2 3 5 8......


Coding:
#include <iostream>
using namespace std;
int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "Enter the number of terms: ";
cin >> n;

Page 7
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

cout << "Fibonacci Series: ";


for (int i = 1; i <= n; ++i)
{
// Prints the first two terms.
if(i == 1)
{
cout << " " << t1;
continue;
}
if(i == 2)
{
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << " ";
}
system("pause");
return 0;
}

9. Programs to produces the following design using nested loops


a)
A
A
A
A
www.Padasalai.Net
B
B
B
C
C D
b)
5
5
5
5
4
4
4
4
3
3
3
2
2
1
c)
# #
#
#
#
#
#
#
#
#
#
#
#
#
# #

A B C D E 5
A B C D E F
a) Coding:
#include<iostream>
using namespace std;
int main()
{
char ch='A';
int n;
for(int i=1;i<=6;i++)
{
ch = 'A';
for(int j=1;j<=i;j++)
{
cout<< ch<< " ";
ch++;
}
cout<< "\n";
}
system("pause");
return 0;
Page 8
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

b) Coding:
#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<=5;i++)
{
for(int j=5;j>=i;j--)
{
cout<<j<<"\t";
}
cout<<"\n";
}
system("pause");
return 0;
}

c) Coding:
#include<iostream>
using namespace std;
int main()
{

www.Padasalai.Net
int n;
for(int i=4,sp=0;i>0;i--,sp++)
{
for(int k=0;k<=sp;k++)
cout<<" "<<" ";
for(int j = 1;j<2*i ;j++)
{
cout<<"#"<<" ";
}
cout<<"\n";
}
system("pause");
return 0;
}

10. Program to check whether square root of a number is prime or not.


Coding:
#include <iostream>
using namespace std;
int main()
{
int n, i;
bool isPrime = true;
cout << "Enter a positive integer: ";
cin >> n;

Page 9
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

for(i = 2; i <= n / 2; ++i)


{
if(n % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
cout << "This is a prime number";
else
cout << "This is not a prime number";
system("pause");
return 0;
}

BOOK BACK PROGRAMS


PART – II
6. Write a C++ program which contains for loop that displays the number
from 21 to 30.
Coding:
#include<iostream>
#include<stdio.h>

{ www.Padasalai.Net
using namespace std;
int main()

for(int i=21;i<=30;i++)
{
cout<<i<<”\n”;
}
system(“pause”);
return 0;
}

7. Write a while loop that displays numbers 2, 4, 6, 8.......20


Coding:
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
for(int i=2;i<=20;i+=2)
{
cout<<i<<”\n”;
}
system(“pause”);
return 0;
}

Page 10
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

PART – III
3. Write a C++ program to print multiplication table of a given number.
Coding:
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the Number To Find Multiplication Table ";
cin>>n;
for(int i=1;i<=10;i++)
{
cout<<n<<" * "<<i<<" = "<<n*i<<endl;
}
return 0;
}

5. Write a short program to print following series:


(a) 1 4 7 10...... 40
Coding:
#include<iostream>
using namespace std;
int main()
{

www.Padasalai.Net
clrscr();
int i,a=-1,b;
for(i=1;i<=40;i+=3)
{
a*=-1;
b=i;
b*=a;
cout<<b<<" ";
}
system(“pause”);
return 0;
}

PART – IV
3. Write a program to find the LCM and GDC of two numbers.
Coding:
//Program to find the LCM
#include <iostream>
using namespace std;
int main()
{
int n1, n2, max;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
max = (n1 > n2) ? n1 : n2;

Page 11
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

do
{
if (max % n1 == 0 && max % n2 == 0)
{
cout << "LCM = " << max;
break;
}
else
++max;
} while (true);
return 0;
}

Coding:
//Program to find the GCD
#include <iostream>
using namespace std;
int main()
{
int n1, n2;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
while(n1 != n2)
{

else www.Padasalai.Net
if(n1 > n2)
n1 -= n2;

n2 -= n1;
}
cout << "GDC = " << n1;
return 0;
}

4. Write programs to find the sum of the following series:


(a) x- x2/2! + x3/ 3! - x4/4! + x5/5! - x6/6!
Coding:
#include <iostream>
using namespace std;
int main()
{
int x,p,i,j;
double fact=1.0,ans=0;
cout<<"Enter the value of x:";
cin>>x;
cout<<"\n Enter till what power you want:";
cin>>p;
ans=x;
for(i=2,j=1;i<=p;i++,j++){
fact=fact*i;

Page 12
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

if(i%2==0)
ans+=(pow(-1,j))*((pow(x,i))/(fact));
}
cout<<"\n The sum of the series is:"<<ans;
return 0;
}

(b) x+x2/2 + x3/2 +....+ xn/n


Coding:
#include <iostream>
using namespace std;
int main()
{
clrscr();
int i,n;
float x,sum=0;
cout<<“x+x^2/2+x^3/3+…..+x^n/n”;
cout<<“\n Enter value of x and n:”;
cin>>x>>n;
for(i=1;i<=n;++i)
sum+=pow(x,i)/i;
cout<<“\n sum=”<<sum;
system(“pause”);
return 0;
}

www.Padasalai.Net
5. Write a program to find sum of the series
S = 1 + x + x2 +..... + xn
Coding:
#include <iostream>
using namespace std;
int main()
{
clrscr();
long i,n,x,sum=1;
cout<<“1+x+x^2+……+x^n”;
cout<<“\n Enter the value of x and n:”;
cin>>x>>n;
for(i=1;i<=n;++i)
sum+=pow(x,i);
cout<<“\n Sum=”<<sum;
system(“pause”);
return 0;
}

CHAPTER – 11
FUNCTIONS
HANDS ON PRACTICE PROGRAMS

Page 13
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

1. Program that reads two strings and appends the first string to the
second. For example, if the first string is entered as Tamil and second
string as nadu, the program should print Tamilnadu. Use string library
header.
Coding:
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char s1[ ]=”Tamil”,s2[ ]=”nadu”;
strcat(s1,s2);
puts(s1);
system("pause");
return 0;
}

2. Program that reads a string and converts it to uppercase. Include


required header files.
Coding:
#include <iostream>
#include <ctype.h>
using namespace std;

{
www.Padasalai.Net
int main()

char ch;
cout<<"Enter the Character";
ch=getchar();
cout<<"The Character is changed to Upper Case="<<toupper(ch);
system("pause");
return 0;
}

3. Program that checks whether a given character is an alphabet or not. If


it is an alphabet, whether it is lowercase character or uppercase
character? Include required header files.
Coding:
#include<iostream>
#include<stdio.h>
#include<ctype.h>
using namespace std;
int main()
{
char ch;
int n;
cout << "\n Enter a character: ";
ch = getchar();

Page 14
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

cout<<"\n The Return Value of isalpha(ch) is :" << isalpha(ch) ;


n=islower(ch);
if(n==0)
cout<<”\n The Given Character is in Upper Case”;
else
cout<<”\n The Given Character is in Lower Case”;
system(“pause”);
return 0;
}

4. Program that checks whether the given character is alphanumeric or a


digit. Add appropriate header file.
Coding:
#include<iostream>
#include<stdio.h>
#include<ctype.h>
using namespace std;
int main()
{
char ch;
int r;
cout<<"\n Type a Character :";
ch = getchar();
r = isalnum(ch);

}
www.Padasalai.Net
cout<<"\nThe Return Value of isalnum(ch) is :"<<r;

5. Write a function called zero_small ( ) that has two integer arguments


being passed by reference and sets smaller of the two numbers to 0. Write
the main program to access this funtion.
Coding:
#include<iostream>
using namespace std;
void zero_small(int &,int &);
int main()
{
int x,y;
cout<<"Enter first number : ";
cin>>x;
cout<<"Enter second number : ";
cin>>y;
zero_small(x,y);
cout<<"First number is : "<<x;
cout<<"\nSecond number is : "<<y;
system("pause");
return 0;
}
void zero_small(int &a, int &b)

Page 15
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html
www.Padasalai.Net www.TrbTnpsc.com

{
if(a<b)
a=0;
else
b=0;
}

7. Program that invokes a function calc ( ) which intakes two integers and
an arithmetic operator and prints the corresponding result.
Coding:
#include<iostream>
using namespace std;
void calc(int x, int y)
{
int z;
z=x+y;
cout<<”The Sum of the Two Integers are=”<<z;
}
int main()
{
int a,b;
cout<<”Enter the Two Integers”;
cin>>a>>b;

www.Padasalai.Net
calc(a,b);
system(“pause”);
return 0;
}

BOOK BACK PROGRAMS


PART – IV
5. Write a program to accept any integer number and reverse it.
#include <iostream>
using namespace std;
int main()
{
int n, reversedNumber = 0, remainder;
cout << "Enter an integer: ";
cin >> n;
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
cout << "Reversed Number = " << reversedNumber;
system("pause");
return 0;
}

Page 16
http://www.trbtnpsc.com/2018/06/latest-plus-one-11th-study-materials-tamil-medium-english-medium-new-syllabus-based.html

You might also like