- RAJESH BHARTI NOTES
c++
visul basic
html
photoshop
tally
corel draw
video editing
tally
c++
c++ is an object oriented programming language. it was developed in
early eighties by bjarne stroustrup at
laboratories,usa.it can be used for writing procedural code like c, but its real strength lies in the writing object oriented programs. an
object oriented program is a collection of discrete objects, which are self
contained collection of both data structures as well as functions that interact
with other objects.
oops – object –oriented programming system
provides many advantages to the programmer and the user. the technology solves
many problems related to software development, provides improved quality, and
low cost software. opps has the following advantages.
[1] object- oriented programs can be comfortably to
upgrade.
[2] the encapsulation features provided by oops language
allows programmer to define the class with many functions and characteristics
and only few functions are exposed to the user.
[3]the technology of data hiding facilitates the
programmer to design and develop safe programs that do not disturb code in
other parts of the problem
data types in c++
data type size
in bytes range
char 1 -128 to 127
unsigned char 1 0 to 255
int 2 -32768 to 32767
long int 4 -2147483648to 2147483647
float 4
double float 8
string depend
on size
main function-
like c and c++ programs always start execution from main () function
.the execution of every program in c++ starts with main () functions
structure of a c++ program
#include <header
file>
void main ()
{
statement;
}
iostream.h- the library file <iostream.h> is used
for keyword cout and <conio.h> for function clrscr () and getch (). these
are examples of using preprosessor
cout- print the message and output the result on the
screen
cin – input the numeric value
cin.getline- input the string type character
getch () – wait for single stock
clrscr () - clear the screen
alt+f9 – compile the program
ctrl+f9- run the program
symbol name
# -
preprocessor directory
(- parenthesis open (function symbol)
) – parenthesis close
: - colon
;
- terminator/semi colon
{- carlybresess open
}
- carlybresess close
/ -
slash
>- less then
<- greater then
$- dollar
&- ampersand
%- percent, modulus
(1) wap enters two no then prints the sum of total no.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a, b, t;
cout<<”enter your two no”;
cin>>a>>b;
t=a+b;
cout<<”total”<<t;
getch ();
}
(2) wap enter three no then print total and average
marks.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a, squ;
cout<<”enter any no”;
cin>>a;
squ=a*a;
cout<<”squre”<<squ;
getch ();
}
(3)wap enter two no then print addition, multiplication,
substurction and division.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a, b;
cout<<”enter two no”;
cin>>a>>b;
cout<<”sum”<<a+b<<endl;
cout<<”sub”<<a-b<<endl;
cout<<”multi”<<a*b<<endl;
cout<<”divi”<<a/b;
getch ();
}
(4) wap enters three no then prints the biggest no.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a, b, c;
cout<<”enter three no”;
cin>>a>>b>>c;
if(a>b && a>c)
{
cout<<” a is the biggest no”;
}
else
if (b>a && b>c)
{
cout<<” b is the biggest no”;
}
else
{
cout<<” c is the biggest no”;
}
getch ();
}
(5) wap to enter three no then prints the smallest no.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a, b, c;
cout<<”enter three no”;
cin>>a>>b>>c;
if (a<b and a<c)
{
cout<<” a is the smallest no”;
}
else
if (b<c and b<a)
{
cout<<” b is the smallest no”;
}
else
{
cout<<” c is the smallest no”;
}
getch ();
}
(6) wap enter name, roll and three subject marks print
name, roll, total and average marks.
#include<iostream.h>
#include<conio.h>
void main ()
{
int r, a, b, c, t, avg;
char nm [22];
cout<<”enter your name”;
cin.getline (nm, 22);
cout<<”enter your roll”;
cin>>r;
cout<<”enter three subject marks”;
cin>>a>>b>>c;
t=a+b+c;
avg=t/3;
cout<<”name”<<nm<<endl;
cout<<”roll”<<r<<endl;
cout<<”total”<<t<<endl;
cout<<”average”<<avg;
getch ();
}
(7)wap enter two no then print the enter change no.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a, b;
cout<<”enter two no”;
cin>>a>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<a<<endl;
cout<<b;
getch ();
}
control statement
(1)
decision making statement
(A)
if statement
(B)
if else statement
(C)
nexted if
(D)
switch case statement
(A)
if statement(single statement)
syntax
if (condition)
{
statement;
}
(B)
if else statement
syntax
if (condition)
{
statement;
}
else
{
statement;
}
(c).
nexted if
syntax
if
(condition)
{
statement;
}
else if (condition)
{
statement;
}
else
{
statement;
}
(8) wap enter a no if no is >50 then print good.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a;
cout<<”enter a no”;
cin>>a;
if (a>50)
{
cout<<” a is the good no”;
}
getch ();
}
(9) wap enter a no if no is>50 then print good
otherwise print bad.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a;
cout<<”enter a no”;
cin>>a;
if (a>50)
{
cout<<”good”;
}
else
{
cout<<”bad”;
}
getch ();
}
(10) wap enter a no then no>80 then print very good if
no>60 then print good otherwise print bad.
(11) wap enters an a then print even or odd no.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a;
cout<<”enter a no”;
cin>>a;
if (a%2==0)
{
cout<<” a is the even no”;
}
else
{
cout<<” a is the odd no”;
}
getch ();
}
(12) wap enters a year then check leap or not leap year.
#include<iostream.h>
#include<conio.h>
void main ()
{
int y;
cout<<”enter your year”;
cin>>y;
if(y%4==0)
{
cout<<”leap year”;
}
else
{
cout<<”not leap year”;
}
getch ();
}
(13) wap enter name, roll and three subject marks
calculate total and average marks if average>80 then result=”excellent” if
average>70 then result=”verygood” if average>60 then result=”good”
otherwise result=”fail”.
#include<iostream.h>
#include<conio.h>
void main ()
{
int r, a, b, c, t, ave;
char nm [22];
cout<<”enter your name”;
cin.getline (nm, 22);
cout<<”enter your roll”;
cin>>r;
cout<<”enter your three subject marks”;
cin>>a>>b>>c;
t=a+b+c;
ave=t/3;
if (ave>80)
{
cout<<”excellent”;
}
else if (ave>70)
{
cout<<”very good”;
}
else if (ave>60)
{
cout<<”good”;
}
else
{
cout<<”fail”;
}
getch ( );
}
(q) wap two
convert a given no of days into year week.
#include<iostream.h>
#include<conio.h>
void main ( )
{
int a, y, w, k;
cout<<”enter a no”;
cin>>a;
y=a/365;
k=a%365;
w=k%7;
cout<<”years”<<y<<endl;
cout<<”weeks”<<w;
getch ( );
}
(14) wap to calculate commission for the sales man the
commission is calculate to following rate.
sales
commission
30000 on forward 15%
22000-30000 10%
12000-22000 7%
12000 on backward 5%
#include<iostream.h>
#include<conio.h>
void main ()
{
long int s, c;
cout<<”enter your sales”;
cin>>s;
if(s>30000)
c=s*15/100;
else if(s>22000)
c=s*10/100;
else if(s>12000)
c=s*7/100;
else
c=s*5/100;
cout<<c;
getch ();
}
(15) wap enter name, dept and basic pay if bp>10000
then ta, da=12% if bp>9000 then ta, da=10% otherwise ta, da=8% of bp
calculate total pay print name, dept, basic and total pay.
#include<iostream.h>
#include<conio.h>
void main ()
{
int bp, ta, da, tp;
char nm [22];
char dept [12];
cout<<”enter your name”;
cin.getline (nm, 22);
cout<<”enter your dept”;
cin.getline (dept, 12);
cout<<”enter your basic pay”;
cin>>bp;
if (bp>10000)
{
ta=bp*12/100;
da=bp*12/100;
}
else if (bp>9000)
{
ta=bp*10/100;
da=bp*10/100;
}
else
{
ta=bp*8/100;
da=bp*8/100;
}
tp=bp+da+ta;
cout<<”name”<<nm<<endl;
cout<<”dept”<<dept<<endl;
cout<<”basic pay”<<bp<<endl;
cout<<”total pay”<<tp;
getch ();
}
(16) wap enters name and age and display message whether
the user is eligible for voting or not eligible for voting.
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main ()
{
clrscr ();
char nm [22];
int age;
cout<<”enter your name”;
cin.getline (nm, 22);
cout<<”enter your age”;
cin>>age;
if (age>=18)
{
cout<<”you are eligible for voting”;
}
else
{
cout<<”you are not eligible for voting”;
cout<<”wait for”<<18-age;
}
getch ();
}
(q) wap simple terrific charge for reaching different
place by auto.
#include<iostream.h>
#include<conio.h>
void main ()
{
int cost, ch;
clrscr ();
cout<<”\n------------------------barh bus
stand--------------\n”;
cout<<”\n------------------------court area
barh----------\n”;
cout<<”\n----------------------menu option---------------\n”;
cout<<”\n 1 patna”;
cout<<”\n 2 mor”;
cout<<”\n 3 kanpur”;
cout<<”\n 4 nagpur”;
cout<<”\n 5 delhi”;
cout<<”\n\n enter your choice”;
cin>>ch;
if (ch==1)
cost=60;
else if (ch==2)
cost=100;
else if (ch==3)
cost=180;
else if (ch==4)
cost=200;
else if (ch==5)
cost=280;
else
cost=0;
if (cost! =0)
{
cout<<”\n\n the ticket cost is rs”<<cost;
cout<<”\n pay the amount to get booked”;
}
else
{
cout<<”sorry there is no bus rute”;
cin.get ();
getch ();
}
loops in c and
c++
(1)
for loop—the for loop allows of a set of instruction
until a condition is met.
syntax
for (initalization; condition; increment/decrement)
{
statement;
}
(2) do while loop
syntax
do
{
statement;
}
while (condition);
(3)while loop
syntax
while (text condition)
{
statement;
}
(17) wap to print sum of even or odd no between 1 to 10.
#include<iostream.h>
#include<conio.h>
void main ()
{
int i, s=0, e=0, odd=0;
for (i=1; i<10; i++)
{
if (i%2==0)
{
e=e+i;
}
else
{
odd=odd+i;
}
s=s+i;
}
cout<<”sum of even no”<<e<<endl;
cout<<”sum of odd no”<<odd<<endl;
cout<<”sum of no”<<s;
getch ();
}
(18) wap to print even no between 20 to 50.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a;
clrscr ();
for (a=20; a<50; a++)
{
if (a%==0)
{
cout<<”even no”;
}}
getch ();
}
(19)wap to print 100 to 50.
#include<iostream.h>
#include<conio.h>
void main ()
{
int a;
for (a=100; a>=50; a--)
{
cout<<a<<endl;
}
getch ();
}
(22) wap enter a
number then print reverse no.
#include<iostream.h>
#include<conio.h>
void main()
{
int n,r=0,p=0;
cout<<”enter a no”;
cin>>n;
while(n!=0)
{
p=n%10;
r=r*10+p;
n=n/10;
}
cout<<”reverse no”<<r;
getch();
}
(23) wap enter a no then print the factorial no.
#include<iostream.h>
#include<conio.h>
void main()
{
int n,f;
f=1;
cout<<”enter a no”;
cin>>n;
while(n!=0)
{
f=f*n;
n--;
}
cout<<”factorial no”<<f;
getch();
}
(25) wap to print
square no between 1 to 10 and quibe no.
#include<iostream.h>
#include<conio.h>
void main()
{
int a;
for(a=1; a<100; a++)
{
cout<<a*a<<’\t’<<a*a*a<<endl;
}
getch();
}
(26) wap enter a no then print the sum of digit no and
count of digit no.
#include<iostream.h>
#include<conio.h>
void main()
{
int n,k=0,p=0,s=0;
cout<<”enter a no”;
cin>>n;
while(n!=0)
{
p=n%10;
s=s+p;
k++;
n=n/10;
}
cout<<”sum of digit no”<<s<<endl;
cout<<”count of digit no”<<k;
getch();
}
(q) wap enter a name then print the lenth of name.
#include<iostream.h>
#include<conio.h>
void main()
{
int a=0;
char nm[22];
cout<<”enter your name”;
cin.getline(nm,22);
while(nm[a]!=null)
{
a++;
}
cout<<”length of name”<<a;
getch();
}
(27) wap to enter a name then prints the first name.
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0;
char nm[22];
cout<<”enter your name”;
cin.getline(nm,22);
while(nm[i]!=’ ‘)
{
cout<<nm[i];
i++;
}
getch();
}
(28) wap to enter a name then print the last name.
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0;
char nm[22];
cout<<”enter your name”;
cin.getline(nm,22);
while(nm[i]!=null)
{
i++;
}
while(nm[i]!=’ ‘)
{
i--;
}
while(nm[i]!=null)
{
cout<<nm[i];
i++;
}
getch();
}
(29) wap enter a name then print the middle name.
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0,j=0;
char nm[22];
cout<<”enter your name”;
cin.getline(nm,22);
while(nm[i]!=’ ‘)
{
i++;
}
while(nm[j]!=null)
{
j++;
}
while(nm[j]!=’ ‘)
{
j--;
}
while(i<j)
{
cout<<nm[i];
i++;
}
getch();
}
(30) wap to enter a sentence then counts the vobel and
consonant.
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0,v=0,con=0;
char sen[22];
cout<<”enter your sentence”;
cin.getline(sen,22);
while(sen[i]!=NULL)
{
if(sen[i]==’o’||sen[i]==’e’||
sen[i]==’i’||sen[i]==’a’||sen[i]==’u’)
{
v++;
}
else
if(sen[i]!=’ ‘)
{
con++;
}
i++;
}
cout<<”vowel”<<v;
cout<<”consonant”<<con;
getch();
}
(31) wap to enter a number then convert the days,month
and year.
#include<iostream.h>
#include<conio.h>
void main()
{
int today,years,weeks,days,num;
cout<<”enter total no of days”;
cin>>today;
years=today/365;
num =today%365;
weeks=num/7;
days=num%7;
cout<<”years”<<years<<endl;
cout<<”weeks”<<weeks<<endl;
cout<<”days”<<days;
getch();
}
(q) wap to enter a no then print the table of number.
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0,n;
cout<<”enter your no”;
cin>>n;
for(i=1;i<=10; i++)
{
cout<<i*n<<endl;
}
getch();
}
(q) wap enter a no then print the table of no between 1
to 20.
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=20;i++)
{
for(j=1; j<=10;j++)
{
cout<<i*j<<endl;
}}
getch();
}
(q) wap enter a no then count the no of digit no.
#include<iostream.h>
#include<conio.h>
void main()
{
int n,k=0;
cout<<”enter a no”;
cin>>n;
while(n!=0)
{
k++;
n=n/10;
cout<<”cout of digit no”<<k;
getch();
}
/*W A P to parform
arithmetic calculation using switch */
#include<iostream.h>
#include<conio.h>
void
main()
{
float
num1,num2,result;
char
ch;
int
q,r;
cout
<< "enter two numbers:";
cin
>>num1 >>num2;
cout<<"\n"
<<"enter an operator(+,-,*,/,%) :";
cin
>>ch;
cout<<"\n";
switch
(ch)
{
case
'+' : result =num1+num2;
break;
case
'-' : result =num1-num2;
break;
case
'*' : result =num1*num2;
break;
case
'/' : if (num2== 0)
cout << "Divide by zero error
!!!";
else
result =num2 /num1;
break;
case
'%' : if (num2 ==0)
cout<<
"Divide by zero error !!!";
else
{
q = num2/num1;
r = num2 - (q * num1);
result = r;
}
break;
default
: cout << "\n" << "wrong operator !!!";
}
cout <<
"the calculated result is : " << result<< "\n";
getch();
}
(Q)write a program to calculate area of circle,a
rectangle or a triangle depending upon user's choice
#include<iostream.h>
#include<conio.h>
void main()
{
float area,rad,len,bre,base,hight;
int ch;
cout << " Area menu" <<
"\n";
cout << " 1. circle" <<
"\n";
cout << " 2. Rectangle"
<<"\n";
cout << " 3. Tringle"
<<"\n";
cout << " Enter choice :";
cin>>ch ;
switch(ch)
{
case 1 :cout << "Enter radius of
the circle:" ;
cin >> rad;
area=3.14 * rad * rad;
cout <<
"\n" << "the area of the circle is "<< area
<< "\n";
break;
case 2 :cout
<< "Enter length and breadth of rectangle :" ;
cin >> len >> bre;
area =len * bre;
cout <<
"\n" << "the area of ractangle is "<<area
<< "\n";
break;
case 3 :cout << "Enter two base and hight
of triangle is" << area <<"\n";
cin >> base>>hight;
area=1/2*base*hight;
cout <<
"\n" << "the area of the tringle is" << area
<<"\n";
break;
default :cout <<"wrong choice
wrong";
break;
}
getch();
}
Write a program to print a pattern
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1; i<10; i++)
{
cout<<"\n";
for(j=1;j<=i;j=j+1)
cout<<"*";
}
getch();
}
(q) wap to print 3*3 matrix.
#include<iostream.h>
#include<conio.h>
void main()
{
int n[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<”enter a no”;
cin>>n[i][j];
}}
for(i=0;i<3;i++)
{
Cout<<”\n”;
for(j=0;j<3;j++)
cout<<n[i][j]<<”\t”;
}}
getch();
}
class & object
classàa class is a method to bind the data and functions together in a single unit.the variables and functions enclosed in a class are called data members and members functions.while defining a class, created a new abstract data type that can be treated like other built in data type is. the variable of a class is called an object.these objects allow to access class members for processing.
a class is defined by the
keyword class. the format of class declaration is
class
class name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
};
privateàprivate
data members are not accessible to the outside world, out of the class. by
default members of a class are private.
publicà the
members which are declared as public can be accessed from outside the class
also.
for example
class smmti
{
private:
int roll;
char nm [20];
public:
void input ( );
void output ( );
};
member function definition:- a member
function can be declared in two ways:
[1] inside
class definitionàa
member function can be defined like any other function,using the beginning”{“
and ending “}”. these function replace the function declartion made by the actual
function definition inside the class.
the follwing example is used to declare the member function inside the
class declaration
#include<iostream.h>
#include<conio.h>
class smmti
{
private:
int
roll;
char nm[20];
public:
void input()
void output()
{
Cout<<”enter your name”;
Cin.getline(nm,20);
cout<<”enter roll”;
cin>>roll;
}
void output()
{
cout<<nm<<roll;
getch();
}};
void main()
{
smmti kk;
kk.input();
kk.output();
}
[2] outside class definition using scope
resolution operator(: :)àin
outside class definition , member functions are declared outside the class
declaration.the function prototype within the body of a class and then it is
declareet define first outside the body of class.the following example show how
to declare member function outside the class declaration
class smmti
{
private:
int roll;
public:
void input();
void display();
};
void smmti : : input()
{
cout<<”enter roll”;
cin>>roll;
}
void smmti: : display()
{
cout<<roll;
getch();
}
object à an
object is an instance of a class in which it is by using the class name created
is of the same class type .the syntax for defining an object is given below
class
name object name;
(q) wap enter a name,roll and two subject
marks calculate total marks print name,roll and total marks.
#include<iostream.h>
#include<conio.h>
class student
{
private:
int r,d,w,tot;
char nm[22];
public:
void input();
void output();
};
void student::input()
{
cout<<”enter a name”;
cin.getline(nm,22);
cout<<”enter your roll”;
cin>>r;
cout<<”enter two subject marks”;
cin>>d>>w;
tot=d+w;
}
void student:output()
{
cout<<nm<<endl;
cout<<r<<endl;
cout<<tot;
getch();
}
void main()
{
student kk;
kk.input();
kk.output();
}
(q) wap enter a name,dept,basic pay if bp>10000 then
ta,da=10% else ta,da=5% calculate total pay print name,dept,bp and tp.
#include<iostream.h>
#include<conio.h>
class student
{
private:
int bp,tp,ta,da;
char nm[22];
char dept[22];
public:
void input();
void output();
};
void student::input()
{
cout<<”enter a name”;
cin.getline(nm,22);
cout<<”enter your dept”;
cin.getline(dept,22);
cout<<”enter your basic pay”;
cin>>bp;
}
void student::output()
{
if(bp>10000)
{
ta=bp*10/100;
da=bp*10/100;
}
else
{
ta=bp*5/100;
da=bp*5/100;
}
tp=bp+ta+da;
cout<<nm<<endl;
cout<<dept<<endl;
cout<<bp<<endl;
cout<<tp;
getch( );
}
void main ( )
{
student ak;
ak.input();
ak.output();
}
(q)wap enter name, roll and three subject marks and
calculate total,average and grade if average>80 then grade=”a” if
average>70 then grade=”b” if average>60 then grade=”c” otherwise
grade=”fail” print name,roll,total,average and grade.
#include<iostream.h>
#include<conio.h>
class student
{
private:
int r,a,b,c,tot,ave;
char nm[22];
char g[2];
public:
void input();
void output();
};
void student::input()
{
cout<<”enter your name”;
cin.getline(nm,22);
cout<<”enter your roll”;
cin>>r;
cout<<”enter your three subject marks”;
cin>>a>>b>>c;
tot=a+b+c;
ave=tot/3;
}
void student::output()
{
cout<<”name”<<nm<<endl;
cout<<”roll”<<r<<endl;
cout<<”total”<<tot<<endl;
cout<<”average”<<ave<<endl;
if(ave>80)
{
cout<<”grade=a”;
}
else if(ave>70)
{
cout<<”grade=b”;
}
else if(ave>60)
{
cout<<”grade=c”;
}
else
{
cout<<”grade=fail”;
}
getch();
}
void main()
{
student ar;
ar.input();
ar.output();
}
Function:- a function is a subprogram that acts on data
and often returns a value. a function is a named units of a group of program statements.
This unit can be invoked from other parts of the program. The most important
reason to use functions is to make program handling easier as only a small part
of the program is dealt with at a time. Another reason to use function is to
reduce program size. Functions make a program more readable and understandable
to a programmer thereby making program management much easier.
types of functions
[a] built-in or library function àthese functions are
part of the compiler package.these are part of standard library made availabe
by the compiler.for example pow (), strlen (),sqrt() etc are library function
[2] user defined functionà the user defined functions are created by you, ex the
programmer. these functions are created as per requirments of your programs.
parts of functions
[a] function
definition à it define above
the main function .
syntax
type function
name (parameter list)
[b]accessing function or declearationà where it call
syntax
function
name(parameter);
[c] bodyà outside of main
program
syntax
type
function name (parameter)
(q) wap enter two no then print the add,sub,mul and div
using the function.
#include<iostream.h>
#include<conio.h>
void add(int a,int b);
void sub(int a,int b);
void multi(int a,int b);
void divi(int a,int b);
void main()
{
int a,b;
cout<<”enter two no”;
cin>>a>>b;
add(a,b);
sub(a,b);
multi(a,b);
divi(a,b);
}
void add(int a,int b)
{
cout<<”sum of no”<<a+b<<endl;
}
void sub(int a,int b)
{
cout<<”sub of no”<<a-b<<endl;
}
void multi(int a,int b)
{
cout<<”multi of no”<<a*b<<endl;
}
void divi(int a,int b)
{
cout<<”divi of no”<<a/b<<endl;
getch();
}
(q) wap to enter a no then print the factorial no using
the function.
#include<iostream.h>
#include<conio.h>
void fact(int a);
void main()
{
int a;
cout<<”enter a no”;
cin>>a;
fact(a);
}
void fact(int a)
{
int f=1;
while(a!=0)
{
f=f*a;
a--;
}
cout<<”factorial no”<<f;
getch();
}
(q) wap to enter a no then print the reverse no using the
function.
#include<iostream.h>
#include<conio.h>
void reverse(int a);
void main()
{
int n;
cout<<”enter a no”;
cin>>n;
reverse(n);
}
void reverse(int a)
{
int p=0,r=0;
while(a!=0)
{
p=(a%10);
r=r*10+p;
a=a/10;
cout<<”reverse no”<<r;
getch();
}
(Q)#include<iostream.h>
#include<conio.h>
void interest(int p, int t, int r);
void main()
{
int p, r, t;
cout<<"Enter
the principle ";
cin>>p;
cout<<"Enter
the rate ";
cin>>r;
cout<<"Enter
the time ";
cin>>t;
interest(
p, t,
r);
}
void interest(int p, int t, int r)
{
int i;
i=(p*t*r)/100;
cout<<"Simple
interest is "<<i<<endl;
getch();
}
(q) wap to enter a no then print the sum of reverse no,
count the digit of no using function.
#include<iostream.h>
#include<conio.h>
void sum(int a);
void reverse(int a);
void count(int a);
void main()
{
int n;
cout<<”enter a no”;
cin>>n;
sum(n);
reverse(n);
count(n);
}
void reverse(int a)
{
int 0,r=0;
while(a!=0)
{
p=(a%10);
r=r*10+p;
a=a/10;
cout<<”reverse no”<<r<<endl;
}
void sum(int a)
{
int s=0,p=0;
while(a!=0)
{
p=(a%10);
s=s+p;
a=a/10;
}
cout<<”sum of digit no”<<s<<endl;
}
void count(int a)
int p=0,k=0
while(a!=0)
{
p=(a%10);
k++;
a=a/10;
}
cout<<”count of digit no”<<k;
getch();
}
pointer
pointerà a pointer is a
memory variable that stores a memory address. Pointers can have an name that is
legal for other variables and it is declared in the same fashion like other
variable but it is always denoted by ‘*’ operator.
advantage
of pointer
[a]
pointer save the memory space.
[b]
execution time with pointer is faster because data is manipulated with the
address, direct access to memory location.
[c]
pointers are used with data structures. They are useful for representing two
dimensional and multi dimensional arrays.
declaration of pointer
example
int
*n; (integer to
pointer)
float
*f; (float to pointer)
char
*nm; (character to pointer)
[1]wap
to declare a pointer. display the value and address of the variable using
pointer?
#include<iostream.h>
#include<conio.h>
void
main()
{
int *n;
int k=10;
n=&k;
clrscr
();
cout<<
*n;
cout<<&n;
getch();
}
(q) wap to enter ten no in array then print the sum of
array using pointer.
#include<iostream.h>
#include<conio.h>
void main()
{
int *n,i,*s=0,j;
for(i=0;i<10;i++)
{
cout<<”enter ten no”;
cin>>*(n+i);
}
for(j=0;j<10;j++)
{
*s=*(s)+*(n+j);
}
cout<<”sum of array”<<*s;
getch();
}
structurueàa
structure is a collection of logically related data items. it is a method of
grouping data of different data types,in c++, a structure can be defined by the
keyword struct followed by its name and a body enclosed in
culry braces and terminated by a semicolon.the varibles of a structure can be
of different types like int, float, char , double etc. the variable
declared in the structure are called the members of the structure . the syntax
for declaring a structure is as follows
struct <struct tag >
{
type of member variable;
}variable of structure can be defined here;
(q) wap to enter roll,name,and two subject marks print
name,roll and total marks using structure.
#include<iostream.h>
#include<conio.h>
struct student
{
int r,d,w;
char nm[22];
}st;
void main()
{
clrscr();
cout<<”enter your no”;
cin.getline(st.nm,22);
cout<<”enter your roll no”;
cin>>st.r;
cout<<”enter your two marks”;
cin>>st.d>>st.w;
cout<<”roll no”<<st.r<<endl;
cout<<”name”<<st.nm<<endl;
cout<<”dos”<<st.d<<endl;
cout<<”window”<<st.w;
getch();
}
(q) wap enter name,roll and three subject marks calculate
total and average marks print name,roll ,total and average marks using
structure.
#include<iostream.h>
#include<conio.h>
struct student
{
int r,tot,ave,m,e,h;
char nm[22];
}st;
void main()
{
clrscr();
cout<<”enter your name”;
cin.getline(st.nm,22);
cout<<”enter your roll”;
cin>>st.r;
cout<<”enter your three marks”;
cin>>st.m>>st.e>>st.h;
st.tot=st.m+st.e+st.h;
st.ave=st.tot/3;
cout<<”roll no”<<st.r<<endl;
cout<<”name”<<st.nm<<endl;
cout<<”total”<<st.tot<<endl;
cout<<”average”<<st.ave<<endl;
cout<<”math”<<st.m<<endl;
cout<<”english”<<st.e<<endl;
cout<<”hindi”<<st.h;
getch();
}
(q) wap to enter two no then print the smallest no using
structure.
#include<iostream.h>
#include<conio.h>
struct student
{
int m,n;
}st;
void main()
{
clrscr();
cout<<”enter two no”;
cin>>st.m>>st.n;
if(st.m<st.n)
{
cout<<” m no is smallest”<<st.m;
}
else
{
cout<<”n no is smallest”<<st.n;
}
getch();
}
No comments:
Post a Comment