C Introduction11
C Introduction11
Programming
Conventional Programming using high level languages
such as cobol,fortran and C is commonly known as
procedure oriented programming (POP).
Procedure oriented programming basically consists of
writing list of instructions for the computer to follow,and
organizing these groups into functions.
int main()
{
cout<<“Hello World.\n”;
Return 0;
}
Every C++ program must have a main().
Like C,the C++ statements must terminate with semi-
colons;
Comments
The double slash comment is basically a single line
comment.Multi-line comments can be written as
follows.
//This is an example of
//C++ programs to illustrate
/*
*/
Output Statement
The only statement in the above example is an output
statement.
Numerals:0,1,2,3,4,5,6,7,8,9.
Albhabets:a,b,…..z
A,B,……….Z
Arithmetic Operators:=,-,*,/,%(mod)
Special characters
( ) { } [ ] < >
= ! $ ? . , : ;
‘ “ & | ^ ~ ` #
\ Blank - _ / * % @
Identifier
An identifier is an unlimited sequence of characters
consisting of letters,digits or underscore.The first
character is either underscore or letter.An identifier
cannot be a reserved keyword.
\t Horizontal tab
\n Newline
\v Vertical tab
\a Audible bell
\f Formfeed
\r Carriage return
\0 Null character
Data types
Data types defines a set of values and a set of
operations that can be applied on those values which
are governed by :
Void function1(void);
Qualifiers
Type Bytes Range
Short 2 -32768 to 32767
Unsigned short 2 0 -65,536
int
Unsigned int 2 0-
>+4,294,967,295
Long int 4 -2,147,483,648 -
>+2,147,483,647
Signed char 1 -128->+127
Unsigned char 1 0->+255
Long double 10 3.410-4932 to 3.4
*10 4932
Constants in C++
A constant is a value of any type which remains the
same and can never change,during the program
execution.
Float area=3.14159*rad*rad;
Declaration and initialization can be done
simeltenously.
Float average;
Average = sum/I;
Float average=sum/I;
Reference Variable
C++ introduces a new kind of variable called as the
reference variable.
Example
Float total=100;
Float &sum=total;
Total is float type variable that has already been
declared.;Sum is the alternative name declared to
represent the variable total.Both the variables refer to
the same data object in the memory.
Cout<<total;
And
Cout<<Sum;
Both print the value 100;
The statement
Total=total+10;
% Modulus(Re 9%8 1
mainder)
Unary operator
Unary operators need only one operand .The two
unary operators are ++,--.
similarly
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
One mistake done by programmers is in using =
assignment operator in place of relational operator ==.
Operator Meaning
! Not
&& And
|| Or
Conditional operator
Consider
Exp1 ? Exp2 : Exp3; where Exp1, Exp2, and Exp3 are
expressions. Notice the use and placement of the colon.
int a;float b;
b=(float)a;
Assignment operators
Term Expression Equivalent
+= y+=x y=y+x;
-= X-=5 X=x-5;
/= x/=y X=x/y;
*= X*=y X=x*y;
%= X%=y X=x%y;
Scope resolution operator
Variables declared inside a block are local variables
and scope of the local variable is block scope.These
variables only exist inside the specific function that
create them.They are unknown to other functions and
to the main program.
while(condition)
{
statement(s);
}
Here, statement(s) may be a single statement or a
block of statements.
Here first assigns count the value 19, assigns incr the
value 10, then adds 1 to count, and finally, assigns var
the value of the rightmost expression, count+1, which
is 20.
#include <iostream>
using namespace std;
int main()
{
int i, j;
j = 10;
i = (j++, j+100, 999+j);
cout << i;
return 0;
}
Editor in C++
An Editor is an program much like a Word Processor
that you use to edit the Source Code of any program
you write.
Cout<<“m=“<<m<<endl;
Cout<<“n=“<<n<<endl;
Cout<<“p=“<<p<<endl;
Cout<<setw(5)<<sum<<endl;
}
void show() // function definition
{
……… //function body
}
When function is called control is transferred to the
first statement in the function body.
int temp;
temp=x;
x=y;
y=temp;
cout<<"\n in swap "<<"x="<<x<<" "<<"y="<<y<<endl;
}
Call by Reference
}
#include<iostream.h>
2011 board
using namespace std;
question
int a=30;
void trial(int &x,int y,int *z)
{
a/=x;
y+=a;
*z=a+y;
x+=5;
cout<<a<<" "<<x<<" "<<y<<" "<<*z<<endl;
}
int main()
{
int a=5,b=10;
trial(::a,a,&b);
cout<<::a<<" "<<a<<" "<<b<<endl;
trial(b,::a,&a);
cout<<::a<<" "<<a<<" "<<b<<endl;
}
#include<iostream.h>
Prelims 2016
int g=20;
void func(int &x,int y)
{
x=x-y;
y=x*10;
cout<<x<<“,”<<y<<endl;
-13,-130
}
void main()
-13,20
{ 33,330
int g=7;
func(g,::g); -13,33
cout<<g<<“,”<<::g<<endl;
func(::g,g);
cout<<g<<“,”<<::g<<endl;
}
#include<iostream.h> Board 2016
int a=3;
void demo(int x,int y,int &z)
{
a+=x+y;
z=a+y;
y+=x;
cout<<x<<y<<z<<endl;
3 5 10
}
void main() 8 2 10
{
int a=2,b=5;
demo(::a,a,b);
8 10 20
cout<<::a<<a<<b<<endl;
demo(::a,a,b); 18 2 20
cout<<::a<<a<<b<<endl;
}
#include<iostream.h> 2014 board
using namespace std;
int x=10;
void pass(int &a,int b,int &c)
{
int x=4;
c+=x;
a*=::x;
b+=c;
cout<<a<<" "<<b<<" "<<c<<endl;
}
int main()
{
int y=1,x=2;
pass(y,::x,x);
cout<<x<<" "<<y<<" "<<::x<<endl;
pass(::x,x,y);
cout<<x<<" "<<y<<" "<<" "<<::x<<endl;
}
Board 2017
#include<iostream.h>
int global=10;
void func(int &x,int y)
{
x=x-y;
y=x*10;
cout<<x<<","<<y<<"\n";
}
int main()
{
int global=7;
func(::global,global);
cout<<global<<","<<::global<,endl;
func(global,::global);
cout<<global<<","<<::global<<endl;
}
#include<iostream.h>
using namespace std;
int main()
{
void execute(int &b,int c=100);
int m=90,n=10;
execute(m);
cout<<m<<" "<<n<<"\n";
execute(m,n);
cout<<m<<" "<<n<<"\n";
}
void execute(int &b,int c)
{
int t=b+c;
b=b+t;
if (c==100)
cout<<t<<" "<<b<<" "<<c<<"\n";
}
#include<iostream.h>
using namespace std;
void withdef(int hisnum=30)
{
for (int i=20;i<=hisnum;i+=5)
cout<<i<<",";
cout<<endl;
}
void control(int&mynum)
{
mynum=mynum+10;
withdef();
}
int main()
{
int yournum=20;
control(yournum);
withdef();
cout<<"number="<<yournum<<endl;
}
Return by reference
Int & max(int &x,int &y)
{
If (x>y)
Return x;
Else
Return y;
}
Return by reference
A function call can also return a reference. Since the
return type of max is int&,the function returns
refernce to x or y and not the values.
Inline function-header
{
Function -body
}
We should exercise care before making a function
inline.
Amount(5000,7)
Cout<<add(5,10);
Cout<<add(15,10.0)
Cout<<add(12.5,7.5)
Cout<<add(5,10,15)
Cout<<add(0.75,5)
#include <iostream>
using namespace std;
void display(int);
void display(float);
void display(int, float);
int main()
{
int a = 5;
float b = 5.5;
display(a);
display(a, b);
display(b);
}
void display(int var)
{
cout << "Integer number: " << var << endl;
}
void display(float var)
{
cout << "Float number: " << var << endl;
}
void display(int var1, float var2)
{
cout << "Integer number: " << var1; cout << " and float number:" << var2;
}
#include <iostream>
Int volume(int s)
Using namespace std; {
Return(s*s*s);
Int volume(int);
}
Double volume(double,int)
Long volume(long,int,int);
Double volume(double r,int h)
{
Int main() Return(3.14519*r*r*h)
{ }
Cout<<volume(10);
Long volume(long l,int b,int h)
Cout<<volume(2.5,8); {
Return(l*b*h);
Cout<<volume(100,75,15);
} }
Passing array as parameters
}
Output Based example
# include <iostream.h>
using namespace std;
void ChangeArray (int Number, int ARR[], int Size)
{
for (int L=0; L<Size; L++)
if (L<Number)
ARR[L]+=L;
else
ARR[L]*=L;
}
void Show(int ARR [], int size)
{
for (int L=0; L<size; L++)
(L%2!=0) ? cout<<ARR[L] <<"#" : cout<<ARR[L] <<endl;
}
int main ( )
{
int Array [] = {30,20,40,10,60,50};
ChangeArray (3, Array, 6);
Show (Array,6);
}
Built in functions
<string.h>
Strlen()
Strcat(str1,str2);
String.h
Strcpy()
Copies the contents of one string into another
Char str1[20]=“hello”;
Char str2[20]=“world”;
Strcpy(str1,str2);
Cout<<str1;
Output:world.
<ctype.h>
Isalnum()
Int isalnum( c )
True if c is a letter or digit.
Ex : if (isalnum(‘a’))
Cout<<“Yes it is alphanumeric”;
Ctype.h
Isdigit()
Int isdigit( c )
True if c is a digit 0 -9
If (isdigit(7))
Cout<<“It is a digit”;
Ctype.h
islower()
int islower( c )
true if c is a lowercase letter
ex if (islower (‘a’))
cout<<it is a lower case letter”;
Ctype.h
isupper()
int isupper( c )
true if c is an uppercase letter.
if(isupper( ‘c’ )
cout<<“it is an upper case letter;
Ctype.h
tolower( )
int tolower( c )
returns lowercase version of c if there is one,otherwise it
returns the character unchanged.
ex. cout<<tolower(‘a’)
output :a
Ctype.h
toupper()
int toupper( c )
ex:cout<<toupper(‘a’);
output :a
Ctype.h
isalpha()
int isalpha( c )
true if c is a letter.
if(isalpha (‘a))
cout<<“yes it is a alphabet”;
Ctype.h
isspace()
int isspace( c )
true if c is a whitespace character (space,tab,vertical
tab,formfeed,carriage return or new line)
if (isspace(“\n”))
cout<<“it is newline”;
Ctype.h
isspace()
int isspace( c )
true if c is a whitespace character (space,tab,vertical
tab,formfeed,carriage return or new line)
if (isspace(“\n”))
cout<<“it is newline”;
Math.h
Abs()
pow(x,y)
double sin(double x)
Math.h
The C library function double log(double x) returns
the natural logarithm of x.
double log(double x)
Conio.h
getch()
#include<iostream.h>
#include<conio.h>
int main()
{
cout << "Enter a character";
getch();
}
Conio.h
clrscr()-This is used for clearing the output screen i.e
console
char x[10];
gets(x)
Stdio.h
puts()
outputs a string
char x[10]=“hello”;
puts(x);
Stdio.h
Getchar()-Reads a character from the input device.
char x;
x=getchar();
Stdio.h
putchar()
char x=‘a’;
putchar(x);
Limitations of structures
C does not allow the struct data type to be treated like
built in types.
Example
struct complex
{
float x;
float y;
};
Struct complex c1,c2,c3;
1)Class declarations
2)Class functions declarations
General form of class declaration is
Class class_name
{
Private:
Variable declarations;
Function declarations;
Public:
Variable declarations;
Functions declarations;
};
The body of the class is enclosed within braces and
terminated by semicolon.
Fruit mango;
1)Class declarations
2)Class functions declarations
General form of class declaration is
Class class_name
{
Private:
Variable declarations;
Function declarations;
Public:
Variable declarations;
Functions declarations;
};
The body of the class is enclosed within braces and
terminated by semicolon.
Public:
Void getdata(int a,float b) //functions declarations
Void putdata (void); //using prototype
Item x;
Item x,y,z;
Class item
{
}x,y,z;
Accessing class members
Object-name.function-name(actual-arguments);
For example the function call
x.getdata(100,75.5);
X.putdata();
Getdata(100,75,5);
Has no meaning.
X.putdata();
Example
Class xyz
{
Int x;
Int y;
Public:
Int z;
};
Xyz p
P.x=0;
P.z=10
#include<iostream.h> Void main ()
Class sample {
{ private: Sample obj;
int rollno; Obj.enter();
char name[20]; Obj.show();
int marks; }
public:
void enter()
{
cout<<“\n enter roll no”;
cin>>rollno;
cout<<“Enter name\n”;
cin>>name;
cout<<“enter marks\n”
cin>>marks;
}
void show()
{
cout<<“\n roll number”<<rollno;
cout<<“\n name”<<name;
cout<<“\n marks”<<marks;
}
};
Sending arguments to member
function of class
It is possible to send values to member functions
through the function calls just the way it is done in
ordinary function calls.
Public:
Cout<<number<<“\n”;
Cout<<cost<<“\n”;
}
};
When a function is defined inside a class,it is treated
as an inline function.
class employee
{
char name[30];
float age;
public:
void getdata();
void putdata();
};
The identifier employee is a user-defined data type
and can be used to create objects that relate to
different categories of employees.
manager[i].putdata();
name
Manager[0]
age
name
Manager[1]
age
name
Manager[2]
age
#include<iostream.h>
class employee const int size=3;
{ int main()
char name[30]; {
float age; employee manager[size];
public: for(int i=0; i<size; i++)
void getdata(void); {
void putdata(void); cout<<“\n details of
}; manager”<<i+1<<\n;
void employee::getdata() manager[i].gettdata();
{ }
cout<<“enter name:”; for(int i=0; i<size; i++)
cin>>name; {
cout<<“enter age”; cout<<“\n manager”<<i+1<<\n;
cin>>age; manager[i].putdata();
} }
void employee::putdata() }
{
cout<<“name:”<<name<<“\n”;
cout<<“age”;<<age<<“\n”;
}
Objects as Function arguments
Rectangle R1,R2,R3; };
R1.set(5,7);
R2.set(2,3);
R1.Display();
R2.Display();
R3 = R1.Sum(R2);
R3.Display();
}
Classes within classes (Nested Classes)
cout<<tno<<":"<<tripno<<":"<
<personcount<<endl;
}
};
#include<iostream.h>
using namespace std;
void execute(int &x,int y=150)
{
int temp=x+y;
x+=temp;
if(y!=200)
cout<<temp<<"\t"<<x<<"\t"<<y<<endl;
}
int main()
{
int a=50,b=20;
execute(b);
cout<<a<<"\t"<<b<<endl;
execute(b,a);
cout<<a<<"\t"<<b<<endl;
}
#include<iostream.h>
class aroundus Void main()
{ {
int place,humidity,temp; aroundus a,b(5);
public: a.hot(10);
aroundus(int p=2) a.justsee();
{ b.humid(15);
place=p; b.hot(2);
humidity=60; b.justsee();
temp=20; a.humid(5);
} a.justsee();
void hot(int t) }
{
temp +=t;
}
void humid(int h)
{
humidity+=h;
}
void justsee()
{
cout<<place<<“:”<<temp<<“&”<<humidity<<“%”<<endl;
}
};
Write an equivalent while loop for the
following for loop
for(int i=2,sum=0;i<20;i+i+2)
sum+=i;
i=2;sum=0;
while(i<=20)
{
sum+=i;
i=i+2;
}
Rewrite the following code using a for loop
int i=99;
while (i>=0)
{
cout<<“half of this is “<<i<<endl;
i=i/2;
}
Rewrite the following code using a while loop
int i,x=0;
for(i=0;x<5;++i)
x+=i;
Rewrite the following code using switch
if(ch==’p’)
physcics++;
else if (ch==’m’)
maths++;
else if (ch==’b’)
biology++;
else if (ch==’c’)
chemistry++;
else
unknown++;
switch(ch)
Rewrite the following with a switch
Evaluate the output of the following statements
2)Int a=50,b=10,c;cout<<c=(a>45)?a:b;
3)Int a=2,b,b=++a;cout<<a;cout<<b<<cout++a<<b++
Give the output of the following
#include<iostream.h>
Int main()
{
C
Ca
char *str=“CALIFORNIA”; Cal
for(int i=0;str[i ]!=‘\0’;i++)
{ Cali
for(int j=0;j<=i;j++) Calif
cout<<str[j]; Califo
cout<<endl;
} Califor
} Californ
Californi
calofornia
#include<iostream.h>
int main()
{
int z[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
int a,b;
for(a=0;a<3;++a)
for(b=0;b<4;++b)
if(z[a][b]%2==1)
z[a][b]--;
for(a=0;a<3;++a)
{
cout<<endl;
for(b=0;b<4;++b)
cout<<z[a][b]<<"\t";
}
}
Convert the following if else code into its equivalent switch case
Char code;
Cin>>code;
If(code==‘A’)
Cout<<“Accountant”;
Else if (code==‘C’||code==‘G’)
Cout<<“GradeIV”;
Else if (code==‘F’)
Cout<<“Financial Advisor”;
Else
Cout<<“Wrong code”;
Write a program in C++ to display the following pattern for N number of
lines .
*
* * *
* * * * *
cout<<endl;
}
}
Write a user defined function named pattern() which accepts an integer number
as parameter which represents the total number of lines to be generated to
display a pattern.
Eg if n=4
*
* *
* *
* * * * * * *
Void pattern(int n)
{
int I,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=2*i-1;j++)
if(j==1||j==2*i-1||i==n)
cout<<“*”;
else
cout<<“ ”;
cout<<endl;
}
}
Switch(code)
{
case’A’:
cout<<“Accountant”;
break;
case ‘C’:
case ‘G’:
cout<<“Grade IV”;
Break;
case’F’:
cout<<“Financial Advisor”;
break;
default:
cout<<“Wrong Choice”;
}
Write a user defined function in C++ to display the following pattern for N
number of lines .
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
Prelims 2016
Generate the following pattern in C++
1234
5678
9012
3456
#include<iostream.h>
int main()
{
int n,i,j,a=1;
cout<<"Enter the number of rows : ";
cin>>n;
cout<<"The number pattern is : \n";
for(i=1; i<=n; i++)
{
for(j=1; j<=4; j++)
{
cout<<a;
if(a<9)
a++;
else
a=0;
}
cout<<endl;
}
}
1
2 3
3 45
4 567
5 6789
#include<iostream.h>
using namespace std;
int main()
{
int i,j,a,n;
cout<<"Enter number of rows : ";
cin>>n;
for(i=1;i<=n;i++)
{
a=i;
for(j=1;j<=i;j++)
{
cout<<a<<" ";
a++;
}
cout<<"\n";
}
}
Void pattern(int n)
{
int n,j,k,p;
cout<<"enter n"<<endl;
cin>>n;
for(int i=1;i<=n;i++)
{
for( j=i;j<=n;j++)
cout<<" ";
for( k=i;k<=2*i-1;k++)
cout<<k;
for( p=k-2;p>=i;p--)
cout<<p;
cout<<endl;
}
}
Write a complete C program to accept a positive number N.The program
should count and display the number of even digits present in the number.
#include<iostream.h>
using namespace std;
int main() Prelims 2015
{
long int n;
int count=0;
cout<<"Enter any positive number \n ";
cin>>n;
while(n>0)
{
int d=n%10;
if(d%2==0)
{
cout<<d<<endl;
count++;
}
n=n/10;
}
cout<<"Number of even digits \n"<<count;
}
Write a C program to find the sum of the following series.
1+(1+2)+(1+2+3)+(1+2+3+4)+……….+upto N terms.
#include<stdio.h>
int main() Prelims 2016
{
int n,sum,i,j,t;
printf("Enter how many number of terms you want to add \n");
scanf("%d",&n);
sum=0;
for( i=1;i<=n;i++)
{
t=0;
for( j=1;j<=i;j++)
t=t+j;
sum=sum+t;
}
printf("Sum of first %d terms is %d\n",n,sum);
}
Write a complete C++ program to generate the following pattern
1
2 4
1 3 5
2 4 6 8
Write a C++ program to generate the following pattern
*
**
***
****
*****
#include<iostream>
using namespace std;
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
cout<<' ';
for(k=1;k<=i;k++)
cout<<'*';
cout<<endl;
}
}
#include<iostream.h>
using namespace std;
int main()
{
int i,x;
for(int i=1;i<=4;i++)
{
if(i%2==0)
x=2;
else
x=1;
for(int j=1;j<=i;j++,x+=2)
{
cout<<x<<" ";
}
cout<<endl;
}
}
Write a complete C++ program to generate the following
pattern
Board 2016
If n=4
10 9 8 7
6 5 4
3 2
1
#include<iostream.h>
void main()
{
int n,k;
cout<<“enter the number of lines “;
cin>>n;
k=n*(n+1)/2;
for(int i=n;i>0;i--)
{
for(int j=0;j<i;j++)
{
cout<<k<<“ “;
k--;
}
cout<<endl;
}
}
Find the Output
#include<iostream.h>
#include<string.h>
#include<ctype.h>
void newtext(char string[],int &position)
{
int length=strlen(string);
for(;position<length-2;position++)
string[position]=toupper(string[position]);
}
int main()
{
int loc=3;
char message[]="Silver Zone";
newtext(message,loc);
cout<<message<<"#"<<loc;
}
Write a c++ program to check if number entered is prime number
#include<iostream.h>
using namespace std;
int main()
{
int n,temp,flag=0;
cout<<"enter the number";
cin>>n;
temp=n;
for(int i=2;i<n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
cout<<" prime";
else
cout<<"not prime";
}
Define a class play in C++ with the following specifications
Public members:
printf("Emirp number");
}
else
printf("non emirp number");
}
int main()
#include<iostream.h>
{
void modify(int &a,int b=10)
disp(3);
{
disp(4);
if(b%10==0)
modify(2,20);
a+=5;
}
for(int i=5;i<=a;i++)
cout<<b++<<":";
cout<<endl;
}
void disp(int x)
{
if(x%3==0)
modify(x);
else
10:11:12:13
}
modify(x,3);
20:21:22
#include<iostream.h>
using namespace std;
int main()
{
int numbers[]={2,4,8,10};
int *ptr=numbers;
for(int c=0;c<3;c++)
{
cout<<*ptr<<"@";
ptr++;
}
cout<<endl;
for(int c=0;c<4;c++)
{
(*ptr)*=2;
--ptr;
}
for(int c=0;c<4;c++)
cout<<numbers[c]<<"#";
cout<<endl;
}
Determine the output of the following program:
#include<iostream.h>
Void indirect(int temp=20)
{
for(int i=1;i<=temp;i+=5)
cout<<i<<“ ”;
cout<<endl;
}
Void direct(int &num) 1,6,11,16,21,26
{
num+=10; 1,6,11,16
indirect(num);
} 30
Void main()
{
int number=20;
direct(number);
indirect();
cout<<number<<endl;
}
Consider the following program
#include<iostream.h>
Class complex
{ Define inline member function product that
int real,imag; multiplies objects sent as parameter
public:
void show()
{
cout<<endl<<real<<“+”<<imag;
}
complex(int x,int y)
{
real=x;
imag=y;
}
};
Int main()
{
complex a(12,20),b(23,40)
complex c;
c.product();
a.show();
b.show();
c.show();
}
Void product(complex x,complex y)
{
real=x.real*y.real-x.imag*y.imag;
imag=x.real*y.imag+y.real;
}
#include <iostream.h>
int main()
{
int row, c, n, temp;
cout<<"Enter the number of rows\n";
cin>>n;
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
for ( c = 1 ; c < temp ; c++ )
cout<<" "; // space
temp--;
for ( c = 1 ; c <= 2*row - 1 ; c++ )
cout<<"*";
cout<<"\n";
}
}
Define a class report in C++ with the following specifications
Private members
1)Stream – array of 10 characters
2)Mno –of type integer
3)name-array of 30 characters
4)div-01 character
5)grade-01 character
6)remark-array of 30 characters
7)getremark()- function which assigns remark based on the following
conditions
Remark Condition
CONTD
Public:
1)Input()- function to ask and store the value of mno,name,class,div,grade and call
getremark() function to assign remark
2)Display()-Function to display all the data members.
Class report
{
char stream[10],name[20],remark[20];
int rno;
char div,grade;
void getremark();
public:
void input();
void display();
};
Void report::input()
{
cout<<“enter stream\n”;
gets(stream);
cout<<“Enter name\n”;
gets(name);
cout<<“Enter roll number\n”;
cin>>rno;
cout<<“enter division\n”;
cin>>div;
cout<<“enter the grade\n”;
cin>>grade;
};
Void report::getremark()
{
if(grade==‘A’||grade==‘B’||grade==‘C’||grade==‘D’||grade==‘E’||grade==‘F’
||grade==‘G’)
strcpy(remark,”Qualify for next class”);
else
strcpy(remark,”Needs Improvement”);
}
Void report::display()
{
cout<<“\nStream:-”<<stream;
cout<<“\nName:-”<<name;
cout<<“\nRemark”<<remark;
cout<<“\nRoll No”<<rno;
cout<<“\n Division”<<div;
cout<<“\n Grade”<<Grade;
}
#include<iostream.h>
int m=2;
int setvalue(int &x,int y)
{
x=m*x-x/y;
y=m*y-x%y;
}
return y; 6 4 4 16
int main()
{
int m=4,p,q;
p=setvalue(m,::m);
q=setvalue(::m,p);
cout<<"\n"<<m<<" "<<::m<<" "<<p<<" "<<q;
}
Define a class named EGG with the following specfications
Private:
1)Doz of type integer
2)No of type integer
3)Define a member function getdata which accepts values for all the data members
4)Define a member function sum() which accepts two objects as parameters and
initialises data members as per the summation of dozons of both the objects (refer
example below)
5)Define a member function show()in public visibility label to display values of data
members of all the objects .Further write appropriate main () function.
Example
}
void sum(egg t1,egg t2);
};
void egg::sum(egg t1,egg t2)
{
no=t1.no+t2.no;
doz=no/12;
no=no%12;
doz=doz+t1.doz+t2.doz;
}
int main()
{
egg t1,t2,t3;
t1.getdata();
t2.getdata();
t3.sum(t1,t2);
t1.show();
t2.show();
t3.show();
}
Write a complete procedural C++ program to print all two digit special numbers.
Note:A special number is a number in which adding sum of f digits to the product
of digits gives the original number.
36+13=49
#include<iostream.h>
Int main()
{
int x,sum,product,digit;
cout<<“\n”;
for(int i=10;i<=99;i++)
{
x=i;
sum=0; product=1;
while(x!=0)
{
digit=x%10;
sum=sum+digit;
product=product*digit;
x=x/10;
}
if(i==sum+product)
cout<<i<<“,”;
}
}
Write a user defined function named design() in c++ which accepts two character
variables x,y and a positive integer variable n as parameter and generates the
following pattern for n lines.
*
##
* * *
## # #
* * * * *
Void design(char x,char y,int n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
if(i%2!=0)
cout<<x<<“ ”;
else
cout<<y<<“ ”;
}
}
}
Write a short note on precedence and associativity of
operators in C++
Associativity:-Direction of execution of
operators from left to right or right to left in
same precedence group.
#include<iostream.h>
using namespace std;
void revert(int &num,int last=2)
{
last=(last%2==0)?last+1:last-1;
for(int c=1;c<=last;c++)
num+=c;
}
int main()
{
int a=20,b=4;
revert(a,b);
cout<<a<<"&"<<b<<endl;
b--;
revert(a,b);
cout<<a<<"#"<<b<<endl;
revert(b);
cout<<a<<"#"<<b<<endl;
}
Prelims 2018
1
2 4
3 6 9
4 8 12 16
Void pattern(int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
cout<<i*j<<“ ”;
cout<<endl;
}
}