Sunteți pe pagina 1din 52

1.

Write a C++ Program to enter velocity, acceleration and time and print final velocity using
the formula : v = u + a * t.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int v,u,a,t;
cout << "Enter the velocity, acceleration, time as integers : " << endl;
cin>>u>>a>>t;
v=ua!t;
cout << ""he #inal velocity is " << v << "." << endl;
getch();
$
2. Write a C++ Program to enter a string and find its length.
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main()
{
clrscr();
int slength;
char %&'(); **+llo,ing the user to in-ut a ma%imum o# '. characters.
cout << "Enter the string : " << endl;
cin>>%;
slength=strlen(%);
cout << ""he length o# the string " << % << " is " << slength << "." << endl;
getch();
$
. Write a C++ Program to enter an integer and output it in the reversed form.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
long int num(,num/,rnum=.;
cout << "Enter an integer : " << endl;
cin>>num(;
num/=num(;
do
{
rnum=rnum!(.;
int digit=num(0(.;
rnum=digit;
num(*=(.;
$
,hile(num();
cout << ""he integer you ty-ed is " << num/ << "." << endl;
cout << ""he reversed integer is " << rnum << "." << endl;
getch();
$
!. Write a C++ Program to compute the fi"onacci series.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int a,1,%,y,num(,ct;
a=.;
1=(;
cout << "Enter the num1er o# terms (less than /2) : " << endl;
cin>>num(;
cout << a << endl;
cout << 1 << endl;
#or(ct=(;ct<=num(3/;ct)
{
%=a1;
cout << % << endl;
y=a;
a=1;
1=%;
$
getch();
$
#. Write a C++ Program to identify if an input is a sym"ol, digit or character.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
char charac;
cout << "Enter your in-ut : " << endl;
cin>>charac;
i#(((charac>=4+4)55(charac<=464))77((charac>=4a4)55(charac<=484)))
cout << "9our in-ut " << charac << " is a character." << endl;
else i#((charac>=4.4)55(charac<=4:4))
cout << "9our in-ut " << charac << " is a digit." << endl;
else
cout << "9our in-ut " << charac << " is a sym1ol." << endl;
getch();
$
$. Program to enter a sentence and output the num"er of
uppercase % lo&ercase consonants, uppercase % lo&ercase vo&els in sentence.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
char line&'.);
int num1er;o#;vo,els,uc,lc,uv,lv;
uc=lc=uv=lv=.;
cout << "Enter your sentence : " << endl;
cin.getline(line,'.);
#or(int %=.; line&%)<=4=.4;%)
{
i#(line&%)==4+477line&%)==4E477line&%)==4>477line&%)==4?477line&%)==4@4)
uv;
else i#(line&%)==4a477line&%)==4e477line&%)==4i477line&%)==4o477line&%)==4u4)
lv;
else i#(line&%)>A255line&%)<=:.)
uc;
else i# (line&%)>=:B55line&%)<=(//)
lc;
$
**Crinting the out-ut.
cout << "@--ercase Donsonants = " << uc << "." << endl;
cout << "Eo,ercase Donsonants = " << lc << "." << endl;
cout << "@--ercase Fo,els = " << uv << "." << endl;
cout << "Eo,ercase Fo,els = " << lv << "." << endl;
num1er;o#;vo,els=uvlv;
cout << "Gum1er o# vo,els = " << num1er;o#;vo,els << endl;
getch();
$
'. Program to enter 1( integers in a single)dimension array and then print out the array in
ascending order.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int array&(.),t;
#or(int %=.;%<(.;%)
{
cout << "Enter >nteger Go. " << %( << " : " << endl;
cin>>array&%);
$
#or (%=.;%<(.;%)
{
#or(int y=.;y<:;y)
{
i#(array&y)>array&y())
{
t=array&y);
array&y)=array&y();
array&y()=t;
$
$
$
cout << "+rray in ascending order is : ";
#or (%=.;%<(.;%)
cout << endl << array&%);
getch();
$
*. Program to convert temperatures from Celsius to +ahrenheit and vice versa.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int choice;
#loat ctem-,#tem-;
cout << "(.Delsius to Hahrenheit" << endl;
cout << "/.Hahrenheit to Delsius" << endl;
cout << "Dhoose 1et,een ( 5 / : " << endl;
cin>>choice;
i# (choice==()
{
cout << "Enter the tem-erature in Delsius : " << endl;
cin>>ctem-;
#tem-=((.'!ctem-)I/;
cout << ""em-erature in Hahrenheit = " << #tem- << endl;
$
else
{
cout << "Enter the tem-erature in Hahrenheit : " << endl;
cin>>#tem-;
ctem-=(#tem-3I/)*(.';
cout << ""em-erature in Delsius = " << ctem- << endl;
$
getch();
$
,. Program to find the sum of either of the diagonals of a ! - ! matri-.
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int %;
int +&J)&J),sum=.; **Keading the matri%.
cout << "Enter the elements o# the matri% : " << endl;
#or(int y=.;y<J;y)
#or (int %=.;%<J;%)
{
cout << "Element " << %( << ", " << y( << " : ";
cin>>+&%)&y);
$
**Lum o# either o# the diagonal elements.
#or(%=.;%<J;%)
#or(y=.;y<J;y)
i#(%==y)
sum=+&%)&y);
else i#(y==J3((());
sum=+&%)&y);
cout << "Lum o# either o# the diagonal elements is : " << sum;
getch();
$
1(. C++ code to s&ap t&o varia"les &ithout using third varia"le.
#include<iostream>
using names-ace std;
int main()
{
int var(, var/;
cout<<"Enter value #or #irst integer: ";
cin>>var(;
cout<<"Enter value #or second integer: ";
cin>>var/;
cout<<" Falues Me#ore s,a--ing: "<<endl;
cout<<"Hirst >nteger ="<<var(<<endl;
cout<<"Lecond >nterger ="<<var/<<endl;
var(=var(var/;
var/=var(3var/;
var(=var(3var/;
cout<<" Falues +#ter s,a--ing: "<<endl;
cout<<"Hirst >nteger ="<<var(<<endl;
cout<<"Lecond >nterger ="<<var/<<endl;
return .;
$
11. .imple c++ code to find num"er is palindrome or not:
#include<iostream>
using names-ace std;
int main()
{
int -alindrome, reverse=.;
cout<<"Enter num1er: ";
cin>>-alindrome;
int num=.,Ney=-alindrome;
#or(int i=(;-alindrome<=.;i){
num=-alindrome0(.;
-alindrome=-alindrome*(.;
reverse=num(reverse!(.);
$
i#(reverse==Ney){
cout<<Ney<<" is a Calindrome Gum1er";
$
else{
cout<<Ney<<"is G?" a Calindrome Gum1er";
$
return .;
$
12. C++ program &hich ta/es input a num"er and chec/ &hether it is 0rmstrong 1um"er or
not.
#include<iostream>
using names-ace std;
int main()
{
int armstrong=.,num=.,result=.,checN;
cout<<"Enter Gum1er to #ind it is an +rmstrong num1erO";
cin>>num;
checN=num;
#or(int i=(;num<=.;i){
armstrong=num0(.;
num=num*(.;
armstrong=armstrong!armstrong!armstrong;
result=resultarmstrong;
$
i#(result==checN){
cout<<checN<<" is an +rmstrong Gum1er";
$
else{
cout<<checN<<" is G?" an +rmstrong Gum1er";
$
return .;
$
Phat is +rmstrong num1erO
+ num1er in ,hich the sum o# cu1e o# its individual digits is eQual to the num1er itsel# is called
+rmstrong num1er
Hor E%am-le: (RI 2RI IRI = (2I
J ! J ! J . ! . ! . B ! B ! B = J.B is an +rmstrong num1er.
1. 2ecursion in c++ programming
>t is one o# the most annoying or di##icult conce-t #or c -rogramming #or 1eginners. 9es it is
di##icult #or a 1eginner to understand 1ecause sometimes its e%ecution goes in de-th ,here things
1ecomes com-licated and a 1eginners #eel di##icult to understand it.
+ #unction calls it sel# until a certain condition is true
+ recursive #unction has t,o im-ortant -arts
(. Mase case, Lto--ing state, sto--ing condition
/. Hunctions call its sel# ,ith a s-eci#ic conditions (Kecalling condition)
Phen a #unction calls occur in a -rogram it -ushes into the stacN. Lo i# a #unction is calling itsel#
again and again (Kecursive Dalling) it ,ill 1e -ushing again and again on the stacN every time it
,ill -ush ne, resources ,ill 1e allocated #or a ne, call.
Mase Dase:
>ts a condition ,hen it 1ecomes true or e%ecutes #unction sto- to call it sel#
Kecalling Dondition:
+ condition in ,hich #unction recalls it sel# ,ith a s-eci#ic -arameters or it de-ends on the reQuired
result ,e ,ant to get.
Eets ,rite a sim-le e%am-le to understand the ho, recursive #unction ,orNs.
>n this e%am-le an integer 4n4 is -assing to a #unction in ,hich its value is incrementing 1y one in
recalling condition.
Phen its value 1ecomes (( or greater than (. (1ase case) it returns all calls.
#include<iostream>
using names-ace std;
void recursive;#unction(int n){
i#(n>(.) **1ase case
return;
else{
cout<<"Kecursive Hunction call num1er "<<n<<endl;
recursive;#unction(n=n(); ** here #unction is calling it sel#
$
$
int main(){
int n=(;
recursive;#unction(n); ** #unction call
return .;
$
1!. C++ Program to +ind the 3inary 4alue of 5ecimal 1um"er 6sing for loop
#include<iostream>
#include <stdio.h>
using names-ace std;
int main()
{
int n,%,a, c, N;
cout<<"Enter an integer in decimal num1er system";
cin>>%;
n=%;
cout<<"Minary Falue ?H Siven Gum1er >s: ";
#or( a=(;n<=.;a)
{
n=n*/;
$
a=a3/;
#or (c = a; c >= .; c33)
{
N = % >> c;
i# (N 5 ()
cout<<"(";
else
cout<<".";
$
return .;
$
1#. C++ program to display hours minutes and seconds in "oth 12 and 2! hours format:
/J Tours #ormat : /I:I.:(/
Ltandard #ormat : ((:I.:(/ -m
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int hours,mins,seconds,%;
cout<<"Enter hours=";
cin>>hours;
cout<<"=nEnter minutes=";
cin>>mins;
cout<<"=nEnter seconds=";
cin>>seconds;
i#(hours > /J)
{
cout<<">nvalid Entery";
$
else
{
cout<<"=n/J Tours Hormat=n";
cout<<"Tours : Uins : Leconds=n"<<" "<<hours<<" : "<<mins<<" :
"<<seconds<<"=n";

i#(hours > (/)
{
hours=hours3(/;
cout<<"(/ Tours Hormat=n";
cout<<"Tours : Uins : Leconds=n"<<" "<<hours<<" : "<<mins<<" : "<<seconds;
$
else
{
cout<<"(/ Tours Hormat=n";
cout<<"Tours : Uins : Leconds=n"<<" "<<hours<<": "<<mins<<" : "<<seconds;
$
$
$ ** end o# main
1$. C++ program to find greatest num"er "et&een three num"ers
#include<iostream>
using names-ace std;
int main()
{
int num(,num/,numI;
cout<<" Enter value #or #irst num1er";
cin>>num(;
cout<<" Enter value #or second num1er";
cin>>num/;
cout<<" Enter value #or third num1er";
cin>>numI;
i#(num(>num/55num(>numI)
{
cout<<" Hirst num1er is greatest:"<<endl<<",hicN is= "<<num(;
$
else i#(num/>num(55num/>numI)
{
cout<<" Lecond num1er is greatest"<<endl<<",hicN is= "<<num/;
$
else
{
cout<<" "hird num1er is greatest"<<endl<<",hicN is= "<<numI;
$
return .;
$
1'. C++ Program 7o +ind Prime 1um"ers
#include<iostream.h>
#include<conio.h>
void main()
{
**clrscr();
int num1er,count=.;
cout<<"EG"EK G@UMEK "? DTEDV >" >L CK>UE ?K G?" ";
cin>>num1er;
#or(int a=(;a<=num1er;a)
{
i#(num1er0a==.)
{
count;
$
$
i#(count==/)
{
cout<<" CK>UE G@UMEK =n";
$
else
{
cout<<" G?" + CK>UE G@UMEK =n";
$
**getch();
$
1*. Write a program to print the pattern
C++ Program to print half pyramid as using * as sho&n in figure "elo&.
*
* *
* * *
* * * *
* * * * *
#include <iostream>
using names-ace std;
int main()
{
int i,W,ro,s;
cout<<"Enter the num1er o# ro,s: ";
cin>>ro,s;
#or(i=(;i<=ro,s;i)
{
#or(W=(;W<=i;W)
{
cout<<"! ";
$
cout<<"=n";
$
return .;
$
C++ Program to print half pyramid as using num"ers as sho&n in figure "elo&.
1
1 2
1 2
1 2 !
1 2 ! #
#include <iostream>
using names-ace std;
int main()
{
int i,W,ro,s;
cout<<"Enter the num1er o# ro,s: ";
cin>>ro,s;
#or(i=(;i<=ro,s;i)
{
#or(W=(;W<=i;W)
{
cout<<W<<" ";
$
cout<<"=n";
$
return .;
$
C++ Program to print triangle of characters as "elo&
0
3 3
C C C
5 5 5 5
8 8 8 8 8
#include <iostream>
using names-ace std;
int main()
{
int i,W;
char in-ut,tem-=4+4;
cout<<"Enter u--ercase character you ,ant in triange at last ro,: ";
cin>>in-ut;
#or(i=(;i<=(in-ut34+4();i)
{
#or(W=(;W<=i;W)
cout<<tem-<<" ";
tem-;
cout<<endl;
$
return .;
$
C++ Program 7o 5isplay inverted half pyramid using * and num"ers
C++ Program to print inverted half pyramid using * as sho&n "elo&.
* * * * *
* * * *
* * *
* *
*
#include <iostream>
using names-ace std;
int main()
{
int i,W,ro,s;
cout<<"Enter the num1er o# ro,s: ";
cin>>ro,s;
#or(i=ro,s;i>=(;33i)
{
#or(W=(;W<=i;W)
{
cout<<"! ";
$
cout<<"=n";
$
return .;
$
C++ Program to print inverted half pyramid as using num"ers as sho&n "elo&.
1 2 ! #
1 2 !
1 2
1 2
1
#include <iostream>
using names-ace std;
int main()
{
int i,W,ro,s;
cout<<"Enter the num1er o# ro,s: ";
cin>>ro,s;
#or(i=ro,s;i>=(;33i)
{
#or(W=(;W<=i;W)
{
cout<<W<<" ";
$
cout<<"=n";
$
return .;
$
C++ Program 7o display the pyramid of * and digits
C++ program to print pyramid using *.

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
#include <iostream>
using names-ace std;
int main()
{
int i,s-ace,ro,s,N;
cout<<"Enter the num1er o# ro,s: ";
cin>>ro,s;
#or(i=(;i<=ro,s;i)
{
#or(s-ace=(;s-ace<=ro,s3i;s-ace)
{
cout<<" ";
$
,hile(N<=/!i3()
{
cout<<"! ";
N;
$
N=.;
cout<<"=n";
$
return .;
$
C++ program to print the pyramid of digits in pattern as "elo&.
1
2 2
! # !
! # $ ' $ # !
# $ ' * , * ' $ #
#include <iostream>
using names-ace std;
int main()
{
int i,s-ace,ro,s,N=.,count=.,count(=.;
cout<<"Enter the num1er o# ro,s: ";
cin>>ro,s;
#or(i=(;i<=ro,s;i)
{
#or(s-ace=(;s-ace<=ro,s3i;s-ace)
{
cout<<" ";
count;
$
,hile(N<=/!i3()
{
i# (count<=ro,s3()
{
cout<<iN<<" ";
count;
$
else
{
count(;
cout<<iN3/!count(<<" ";
$
N;
$
count(=count=N=.;
cout<<"=n";
$
return .;
$
C++ program to display reverse pyramid.
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
#include <iostream>
using names-ace std;
int main()
{
int ro,s,i,W,s-ace;
cout<<"Enter num1er o# ro,s: ";
cin>>ro,s;
#or(i=ro,s;i>=(;33i)
{
#or(s-ace=.;s-ace<ro,s3i;s-ace)
cout<<" ";
#or(W=i;W<=/!i3(;W)
cout<<"! ";
#or(W=.;W<i3(;W)
cout<<"! ";
cout<<endl;
$
return .;
$
C++ Program to 5ra& Pascal9s triangle as "elo&:
1
1 1
1 2 1
1 1
1 ! $ ! 1
1 # 1( 1( # 1
#include <iostream>
using names-ace std;
int main()
{
int ro,s,coe#=(,s-ace,i,W;
cout<<"Enter num1er o# ro,s: ";
cin>>ro,s;
#or(i=.;i<ro,s;i)
{
#or(s-ace=(;s-ace<=ro,s3i;s-ace)
cout<<" ";
#or(W=.;W<=i;W)
{
i# (W==.77i==.)
coe#=(;
else
coe#=coe#!(i3W()*W;
cout<<" "<<coe#;
$
cout<<endl;
$
$
C++ Program to display +loyd9s 7riangle.
1
2
! # $
' * , 1(
#include <iostream>
using names-ace std;
int main()
{
int ro,s,i,W,N=.;
cout<<"Enter num1er o# ro,s: ";
cin>>ro,s;
#or(i=(;i<=ro,s;i)
{
#or(W=(;W<=i;W)
cout<<NW<<" ";
N;
cout<<endl;
$
return .;
$
1,. Write a c++ program to add t&o matri- using 2)5 arrays.
#include<iostream>
using names-ace std;
int main(){
**@sing const int #or array si8e
const int ro,=/,col=/;
** i# not use const error #ound
cout<<"Li8e o# Uatrices : "<<ro,<<" X "<<col<<endl;
cout<<"Enter Falue Hor Hirst Uatri%:"<<endl;
int #irst&ro,)&col), second&ro,)&col);
int i,W;
#or( i=.;i<ro,;i){
cout<<"Enter value #or ro, num1er: "<<i(<<endl;
#or( W=.;W<col;W){
cin>>#irst&i)&W);
$
$
cout<<"=n=n=nEnter Falue Hor Lecond Uatri%:"<<endl;
#or( i=.;i<ro,;i){
cout<<"Enter value #or ro, num1er: "<<i(<<endl;
#or( W=.;W<col;W){
cin>>second&i)&W);
$
$
** Kesultant Uatri%
#or( i=.;i<ro,;i){
#or( W=.;W<col;W){
#irst&i)&W)=#irst&i)&W)second&i)&W);
$
$
cout<<"=n=n=t=tKesultant Uatri%:"<<endl;
#or( i=.;i<ro,;i){
cout<< endl;
#or( W=.;W<col;W){
cout<<"=t=t"<<#irst&i)&W)<<" ";
$
$
return .;
$
2(. Program for multiplication of t&o matrices in c++
"he 1elo, -rogram gives multi-lication o# t,o matrices.
>n this #irst ,e enter order o# matri% + then entered elements #or matri% +, same -rocedure #or
matri% M.
Hrom a1ove Uatri% + o# order ( X J and Uatri% M o# order J X J then Uulti-lication o# Uatri% +
and M o# order ( X J.
Lu--ose Uatri% + o# order ( X I and Uatri% M o# order J X J then no. o# column o# matri% + i.e. I
and no. o# ro,s matri% M i.e. J is not match so it gives out-ut as Uulti-lication is not -ossi1le.
#include<iostream.h>
#include<conio.h>

int main()
{
clrscr();
int a&(.)&(.), 1&(.)&(.),c&(.)&(.);
int %,y,i,W,m,n;


cout<<"=nEnter the num1er o# ro,s and columns #or Uatri% +:::=n=n";
cin>>%>>y;

** % denotes num1er ro,s in matri% +
** y denotes num1er columns in matri% +

cout<<"=n=nEnter elements #or Uatri% + :::=n=n";

#or(i=.;i<%;i)
{
#or(W=.;W<y;W)
{
cin>>a&i)&W);
$
cout<<"=n";
$



cout<<"=n=nUatri% + :=n=n";

#or(i=.;i<%;i)
{
#or(W=.;W<y;W)
{
cout<<"=t"<<a&i)&W);
$
cout<<"=n=n";
$

cout<<"=n33333333333333333333333333333333333333333333333333333333333=n";

cout<<"=nEnter the num1er o# ro,s and columns #or Uatri% M:::=n=n";
cin>>m>>n;

** m denotes num1er ro,s in matri% M
** n denotes num1er columns in matri% M


cout<<"=n=nEnter elements #or Uatri% M :::=n=n";

#or(i=.;i<m;i)
{
#or(W=.;W<n;W)
{
cin>>1&i)&W);
$
cout<<"=n";
$


cout<<"=n=nUatri% M :=n=n";

#or(i=.;i<m;i)
{
#or(W=.;W<n;W)
{
cout<<"=t"<<1&i)&W);
$
cout<<"=n=n";
$

i#(y==m)
{

#or(i=.;i<%;i)
{
#or(W=.;W<n;W)
{
c&i)&W)=.;
#or(int N=.;N<m;N)
{
c&i)&W)=c&i)&W)a&i)&N)!1&N)&W);
$
$
$

cout<<"=n33333333333333333333333333333333333333333333333333333333333=n";

cout<<"=n=nUulti-lication o# Uatri% + and Uatri% M :=n=n";

#or(i=.;i<%;i)
{
#or(W=.;W<n;W)
{
cout<<"=t"<<c&i)&W);
$
cout<<"=n=n";
$
$
else
{
cout<<"=n=nUulti-lication is not -ossi1le";
$

getch();
return .;
$
21. C++ program ) :atri- ;perations
"his c -rogram im-lements the #ollo,ing matri% o-erations:
Y +ddition
Y Uulti-lication
Y "rans-osition
Y Zeterminant
Y Lingular or not
#include<iostream.h>
#include<conio.h>
void main()
{
int a&2)&2),1&2)&2),t&2)&2),i,W,N,l,m,n,8,c&2)&2),%,y; **declarations
clrscr();
**@ser in-uts
cout<<"enter the order o# the matri% > ";
cin>>%;
**Uatri%(
cout<<endl<<"enter the (st matri% elements > ";
#or(i=.;i<%;i)
{
#or(W=.;W<%;W)
{
cin>>a&i)&W);
$
$
**Uatri%/
cout<<endl<<"enter the /nd matri% elements > ";
#or(i=.;i<%;i)
{
#or(W=.;W<%;W)
{
cin>>1&i)&W);
$
$

**Lum Dalculation
cout<<endl<<"L@U...."<<endl;
#or(i=.;i<%;i)
{
#or(W=.;W<%;W)
{
cout<<" "<<a&i)&W)1&i)&W);
$
cout<<endl;
$

**@ser >n-ut
cout<<"enter the order o# the J matrices > ";
cin>>N;
cin>>l;
cin>>m;
cin>>n;
cout<<endl<<"enter the (st matri% elements > ";
#or(i=.;i<N;i)
{
#or(W=.;W<l;W)
{
cin>>a&i)&W);
$
$
cout<<endl<<"enter the /nd matri% elements > ";
#or(i=.;i<m;i)
{
#or(W=.;W<n;W)
{
cin>>1&i)&W);
$
$

**Croduct Dalculation
i#(l==m)
{
#or(i=.;i<N;i)
{
#or(W=.;W<n;W)
{
c&i)&W)=.;
#or(8=.;8<l;8)
{
c&i)&W)=c&i)&W)(a&i)&8)!1&8)&W));
$
$
$ $
else
{
cout<<"invalid data....";
$

**Zis-lay Kesult
cout<<endl<<"CK?Z@D"..."<<endl;
#or(i=.;i<N;i)
{
#or(W=.;W<n;W)
{
cout<<" "<<c&i)&W);
$
cout<<endl;
$

**@ser >n-ut
cout<<"enter the order o# the matri% > ";
cin>>%;
cin>>y;
cout<<endl<<"enter the matri% elements > ";
#or(i=.;i<%;i)
{
#or(W=.;W<y;W)
{
cin>>a&i)&W);
$
$
cout<<"matri%..."<<endl;
#or(i=.;i<%;i)
{
#or(W=.;W<y;W)
{
cout<<" "<<a&i)&W);
$
cout<<endl;
$

**"rans-ose
cout<<endl<<"trans-ose..."<<endl;
#or(i=.;i<y;i)
{
#or(W=.;W<%;W)
{
t&i)&W)=a&W)&i);
$
$
#or(i=.;i<y;i)
{
#or(W=.;W<%;W)
{
cout<<" "<<t&i)&W);
$
cout<<endl;
$

**@ser >n-ut
cout<<endl<<"enter the elements o# a I!I matri% > ";
#or(i=.;i<I;i)
{
#or(W=.;W<I;W)
{
cin>>a&i)&W);
$
$
**Dalculation o# Zeterminant
8=(a&.)&.)!(a&()&()!a&/)&/))3(a&/)&()!a&()&/)))3(a&.)&()!(a&()&.)!a&/)&/))3(a&/)&.)!a&()&/)))(a&.)
&/)!(a&()&.)!a&/)&())3(a&/)&.)!a&()&()));
cout<<endl<<"determinant = "<<8;

**"o checN i# it is a singular matri%
i#(8==.)
{
cout<<endl<<"this is a singular matri%...";
$
else
{
cout<<endl<<"this is not a singular matri%...";
$
getch();
$
;67P67:
Enter the order o# the matri% > /
Enter the (
st
matri% elements >
(
(
(
(
Enter the second matri% elements >
(
(
(
(
L@U [.
/ /
/ /
Enter the order o# the / matrices >
/
I
I
/
Enter the (
st
matri% elements >
(
(
(
(
(
(
Enter the /
nd
matri% elements >
(
(
(
(
(
(
CK?Z@D" [.
I I
I I
Enter the order o# the matri% >
/
I
Enter the matri% elements >
(
/
I
/
(
/
Uatri% [
( / I
/ ( /
"rans-ose [
( /
/ (
I /
Enter the elements o# a I!I matri% >
(
/
I
J
2
A
B
'
:
Zeterminant=/'
"his is not a singular matri% [.
22. Program for su"traction of t&o matrices in c++
>n these -rogram ,e com-ute su1traction o# t,o matrices + and M ,hich has same order.
Hirst ,e enter order o# matri% then enter num1ers #or matri% + and M.
>n this elements o# matri% M su1tract #rom corres-onding elements o# matri% + .
+#ter that gives resultant matri%.
Lu--ose matrices have order I\I.
Uatri% + :
: ' B
A 2 J
: A J
Uatri% M :
( / I
J I /
B ( A
Kesultant Uatri% is su1traction o# matrices + and M :
' A J
/ / /
/ 2 3/
#include<iostream.h>
#include<conio.h>

int main()
{
clrscr();
int a&(.)&(.);
int 1&(.)&(.);
int %,y,i,W;


cout<<"=nEnter the num1er o# ro,s and columns :::=n=n";
cin>>%>>y;

cout<<"=n=nEnter elements #or Uatri% + :::=n=n";

#or(i=.;i<%;i)
{
#or(W=.;W<y;W)
{
cin>>a&i)&W);
$
cout<<"=n";
$

cout<<"=n=nEnter elements #or Uatri% M :::=n=n";

#or(i=.;i<%;i)
{
#or(W=.;W<y;W)
{
cin>>1&i)&W);
$
cout<<"=n";
$


cout<<"=n=nUatri% + :=n=n";

#or(i=.;i<%;i)
{
#or(W=.;W<y;W)
{
cout<<"=t"<<a&i)&W);
$
cout<<"=n=n";
$

cout<<"=n=nUatri% M :=n=n";

#or(i=.;i<%;i)
{
#or(W=.;W<y;W)
{
cout<<"=t"<<1&i)&W);
$
cout<<"=n=n";
$


cout<<"=n=nLu1traction o# Uatri% + and Uatri% M :=n=n";

#or(i=.;i<%;i)
{
#or(W=.;W<y;W)
{
cout<<"=t"<<a&i)&W)31&i)&W);
$
cout<<"=n=n";
$

getch();
return .;
$
C).trings <Character 0rrays=
L"K>GS: >t is an array o# ty-e char.
Lynta% #or declaration
char <array*string name> &ma%. num1er o# characters to 1e stored ();
"he num1er o# elements that can 1e stored in a string is al,ays n3(, i# the si8e o# the array s-eci#ied
is n. "his is 1ecause ( 1yte is reserved #or the G@EE character 4=.4 i.e. 1acNslash 8ero. + string is
al,ays terminated ,ith the G@EE character.
8-ample:
char str&'.);
>n the a1ove e%am-le, str can 1e used to store a string ,ith B: characters.
>nitiali8ing a string
+ string can 1e initiali8ed to a constant value ,hen it is declared.
char str& ) = "Sood";
?r
char str&)={4S4,4o4,4o4,4d4,4=.4$;
Tere. 4S4 ,ill 1e stored in str&.), 4o4 in str&() and so on.
Gote: Phen the value is assigned to the com-lete string at once, the com-uter automatically inserts
the G@EE character at the end o# the string. Mut, i# it is done character 1y character, then ,e have to
insert it at the end o# the string.
Keading strings ,ith*,ithout em1edded 1lanNs
"o read a string ,ithout 1lanNs cin can 1e used
cin>>str;
"o read a string ,ith 1lanNs cin.getline() or gets() can 1e used.
cin.getline(str,'.);
3?r3
gets(str);
Crinting strings
cout and -uts() can 1e used to -rint a string.
cout<<str:
?r
-uts(str);
Gote: Hor gets( ) and -uts(), the header #ile stdio.h has to 1e included. -uts() can 1e used to dis-lay
only strings. >t taNes a line #eed a#ter -rinting the string.
cin
gets()
>t can 1e used to taNe in-ut o# a value o# any data ty-e. >t can 1e used to taNe in-ut o# a string.
>t taNes the ,hite s-ace i.e. a 1lanN, a ta1, or a ne, line character as a string terminator. >t does not
taNe the ,hite s-ace i.e. a 1lanN, a ta1, or a ne, line character, as a string terminator.
>t reQuires header #ile iostream.h >t reQuires the header #ile stdio.h
E%am-le:
char L&'.);
cout<<"Enter a string:];
cin>>L; E%am-le:
char L&'.);
cout<<"Enter a string:";
gets(L);

cout
-uts()
>t can 1e used to dis-lay the value o# any data ty-e. >t can 1e used to dis-lay the value o# a
string.
>t does not taNe a line #eed a#ter dis-laying the string. >t taNes a line #eed a#ter dis-laying the
string.
>t reQuires the header #ile iostream.h >t reQuires the header #ile stdio.h
E%am-le:
char L&'.)="Dom-uters";
cout<<L<<L;
?ut-ut:
Dom-utersDom-uters E%am-le:
char L&'.)="Dom-uters";
-uts(L);
-uts(L);
?ut-ut:
Dom-uters
Dom-uters
Dounting the num1er o# characters in a string and -rinting it 1acN,ards
#include<iostream.h>
#include<stdio.h>
int main( )
{
char str&'.);
cout<<"Enter a string:";
gets(str);
#or(int l=.; str&l)<=4=.4;l); **Eoo- to #ind length
cout<<""he length o# the string is : "<<l<<endl ;
#or(int i=l3(;i>=.;i33) **Eoo- to dis-lay the string 1acN,ards
cout<<str&i);
return .;
$
Hunction to count the num1er o# ,ords in a string
void count(char L&))
{
int ,ords=.;
#or(int i=.;L&i)<=4=.4;i)
{
i# (L&i)==4 4)
,ords; **DhecNing #or s-aces
$
cout<<""he num1er o# ,ords="<<,ords(<<endl;
$
Hunction to #ind the length o# a string
int length(char L& ))
{
#or(int i=.;L&i)<=4=.4;i);
return i;
$
Hunction to co-y the contents o# string L/ to L(
void co-y(char L(& ), char L/& ))
{
#or(int i=.;L/&i)<=4=.4;i)
L(&i)=L/&i);
L(&i)=4=.4;
$
Hunction to concatenate the contents o# string L/ to L(
void concat(char L(& ), char L/& ))
{
#or(int l=.;L(&l)<=4=.4;l);
#or(int i=.;L/&i)<=4=.4;i)
L(&l)=L/&i);
L(&l)=4=.4;
$
Hunction to com-are strings L"K( to L"K/.
"he #unction returns a value>. i# **L"K(>L"K/, a value<. i# L"K(<L"K/, and value . i#
L"K(=L"K/
int com-are(char L"K(& ),char L"K/&))
{
#or(int >=.;L"K(&>)==L"K/&>) 55 L"K(&>)<=4=.455L"K/&>)<=4=.4; >);
return L"K(&>)3L"K/&>);
$
"o reverse the contents o# string L and store it in string Kev
void Keverse(char L&), char Kev&))
{
#or(int D(=.; L&D()<=4=.4; D();
D(33;
#or(int D/=.;D(>=.;D/,D(33)
Kev&D/)=L&D();
Kev&D/)=4=.4;
$
Hunction to checN ,hether a string L is a -alindrome or not
int Calin(char L&))
{
#or(int E=.;L&E)<=4=.4;E); **"o #ind length
#or(int D=.;(D<E*/) 55 (L&D)==L&E3D3());D);
return (D==E*/)O(:.; **Keturns ( i# Calindrome else .
$
Hunction to change the case o# string L to u--ercase
void @--er(char L&))
{
#or(int i=.;L&i)<=4=.4;i)
L&i) = (L&i)>=4a4 55 L&i)<=484)O(L&i)3I/):L&i);
$
Hunction to change the case o# string L to lo,er case
void Eo,er(char L&))
{
#or(int i=.;L&i)<=4=.4;i)
L&i) = (L&i)>=4+4 55 L&i)<=464)O(L&i)I/):L&i);
$
Hunction to e%tract n characters #rom le#t side o# the string and store it in a di##erent string.
E%am-le: J characters #rom EGF>K?GUEG"=EGF>
int LEe#t(char L& ), int n, char result& ))
{
#or(int l=.;L&l)<=4=.4;l);
i#(n<=>) **characters e%tracted should 1e <=length
{
#or(int i=.;i<n;i)
result&i)=L&i);
result&i)=4=.4;
return (;
$
else
return .;
$
Hunction to e%tract n characters #rom right side o# the string and store it in a di##erent string.
E%am-le: J characters #rom EGF>K?GUEG"=UEG"
int LKight(char L& ), int n, char result& ))
{
#or(int l=.;L&l)<=4=.4;l);
i#(n<=>) **characters e%tracted should 1e <=length
{
#or(int W=.;i=l3n;L&i)<=^*.^;i,W)
result&W)=L&i);
result&W)=4=.4;
return (;
$
else
return .;
$
Hunction to e%tract n characters #rom s-eci#ied location loc o# the string and store it in a di##erent
string.
E%am-le: J characters #rom third location in string EGF>K?GUEG"= F>K?
int su1string(char L& ), int n, int loc, char result& ))
{
#or(int l=.;L&l)<=4=.4;l);
i#(n<=>) **characters e%tracted should 1e <=length
{
#or(int W=.;i=l3n;L&i)<=^*.^;i,W)
result&W)=L&i);
result&W)=4=.4;
return (;
$
else
return .;
$
Write a program to find the length of string.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
cout<<"Enter a string:";
gets(str);
#or(int i=.;str&i)<=4=.4;i);
cout<<"Eenght o# string is :"<<i;
getch();
return .;
$
Write a program to display string from "ac/&ard.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
cout<<"Enter a string:";
gets(str);
#or(int l=.; str&l)<=4=.4;l); **Eoo- to #ind the length o# the string
#or(int i=l3(;i>=.;i33) **Eoo- to dis-lay the string 1acN,ards
cout<<str&i);
getch();
return .;
$
Write a program to count num"er of &ords in string.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
int ,ords=.;
cout<<"Enter a string:";
gets(str);
#or(int i=.;str&i)<=4=.4;i)
{
i# (str&i)==4 4)
,ords; **DhecNing #or s-aces
$
cout<<""he num1er o# ,ords="<<,ords(<<endl;
getch();
return .;
$
Write a program to concatenate one string contents to another.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str(&'.),str/&'.);
cout<<"Enter #irst string :";
gets(str();
cout<<"Enter second string :";
gets(str/);
#or(int l=.;str(&l)<=4=.4;l);
#or(int i=.;str/&i)<=4=.4;i)
str(&l)=str/&i);
str(&l)=4=.4;
cout<<"=n"he #irst string a#ter adding second string content is=n=n"<<str(;
getch();
return .;
$
Write a C++ program to compare t&o strings they are e-act e>ual or not.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char L"K(&'.),L"K/&'.);
cout<<"Enter #irst string:";
gets(L"K();
cout<<"Enter second string:";
gets(L"K/);
#or (int >=.; L"K(&>)==L"K/&>) 55 L"K(&>)<= 4=.4 55 L"K/&>) <= 4=.4; >);
i#(L"K(&>)3L"K/&>)==.)
cout<<"Ltrings are eQual";
else
cout<<"Ltrings are not eQual";
getch();
return .;
$
Write a program to chec/ a string is palindrome or not.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
cout<<"Enter a string:";
gets(str);
#or(int E=.;str&E)<=4=.4;E); **"o #ind length o# the string
#or(int D=.;(D<E*/) 55 (str&D)==str&E3D3());D);
i#(D==E*/)
cout<<"Calindrome";
else
cout<<"Got a -alindrome";
getch();
return .;
$
Write a program to find a su"string &ithin a string. ?f found display its starting position.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str(&'.),str/&'.);
cout<<"Enter #irst string :";
gets(str();
cout<<"Enter second string :";
gets(str/);
#or(int l=.;str/&l)<=4=.4;l); **#inding length o# string /
#or(int i=.,W=.;str(&i)<=4=.455 str/&W)<=4=.4;i)
i#(str(&i)==str/&W))
W;
else
W=.;
i#(W==l)
cout<<"Lu1string #ound at -osition "<<i3W(;
else
cout<<"Lu1string not #ound";
getch();
return .;
$
Write a C++ program to reverse a string.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
int tem-;
cout<<"Enter string :";
gets(str);
#or(int l=.;str&l)<=4=.4;l); **#inding length o# string
#or(int i=.,W=l3(;i<l*/;i,W33)
{
tem-=str&i);
str&i)=str&W);
str&W)=tem-;
$
cout<<"Keverse Ltring is=n"<<str;
getch();
return .;
$
Write a program to convert a string in lo&ercase.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
cout<<"Enter a string:";
gets(str);
#or(int i=.;str&i)<=4=.4;i)
str&i) = (str&i)>=4+4 55 str&i)<=464)O(str&i)I/):str&i);
cout<<str;
getch();
return .;
$
Write a program to convert a string in uppercase.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
cout<<"Enter a string:";
gets(str);
#or(int i=.;str&i)<=4=.4;i)
str&i) = (str&i)>=4a4 55 str&i)<=484)O(str&i)3I/):str&i);
cout<<str;
getch();
return .;
$
2. Write a program to find the length of string.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
cout<<"Enter a string:";
gets(str);
#or(int i=.;str&i)<=4=.4;i);
cout<<"Eenght o# string is :"<<i;
getch();
return .;
$
2!. Write a program to display string from "ac/&ard.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
cout<<"Enter a string:";
gets(str);
#or(int l=.; str&l)<=4=.4;l); **Eoo- to #ind the length o# the string
#or(int i=l3(;i>=.;i33) **Eoo- to dis-lay the string 1acN,ards
cout<<str&i);
getch();
return .;
$
2#. Write a program to count num"er of &ords in string.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
int ,ords=.;
cout<<"Enter a string:";
gets(str);
#or(int i=.;str&i)<=4=.4;i)
{
i# (str&i)==4 4)
,ords; **DhecNing #or s-aces
$
cout<<""he num1er o# ,ords="<<,ords(<<endl;
getch();
return .;
$
2$. Write a program to concatenate one string contents to another.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str(&'.),str/&'.);
cout<<"Enter #irst string :";
gets(str();
cout<<"Enter second string :";
gets(str/);
#or(int l=.;str(&l)<=4=.4;l);
#or(int i=.;str/&i)<=4=.4;i)
str(&l)=str/&i);
str(&l)=4=.4;
cout<<"=n"he #irst string a#ter adding second string content is=n=n"<<str(;
getch();
return .;
$
2'. Write a program to concatenate one string contents to another.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str(&'.),str/&'.);
cout<<"Enter #irst string :";
gets(str();
cout<<"Enter second string :";
gets(str/);
#or(int l=.;str(&l)<=4=.4;l);
#or(int i=.;str/&i)<=4=.4;i)
str(&l)=str/&i);
str(&l)=4=.4;
cout<<"=n"he #irst string a#ter adding second string content is=n=n"<<str(;
getch();
return .;
$
2*. Write a program to chec/ a string is palindrome or not.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
cout<<"Enter a string:";
gets(str);
#or(int E=.;str&E)<=4=.4;E); **"o #ind length o# the string
#or(int D=.;(D<E*/) 55 (str&D)==str&E3D3());D);
i#(D==E*/)
cout<<"Calindrome";
else
cout<<"Got a -alindrome";
getch();
return .;
$
2,. Write a program to find a su"string &ithin a string. ?f found display its starting position.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str(&'.),str/&'.);
cout<<"Enter #irst string :";
gets(str();
cout<<"Enter second string :";
gets(str/);
#or(int l=.;str/&l)<=4=.4;l); **#inding length o# string /
#or(int i=.,W=.;str(&i)<=4=.455 str/&W)<=4=.4;i)
i#(str(&i)==str/&W))
W;
else
W=.;
i#(W==l)
cout<<"Lu1string #ound at -osition "<<i3W(;
else
cout<<"Lu1string not #ound";
getch();
return .;
$

(. Write a C++ program to reverse a string.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
int tem-;
cout<<"Enter string :";
gets(str);
#or(int l=.;str&l)<=4=.4;l); **#inding length o# string
#or(int i=.,W=l3(;i<l*/;i,W33)
{
tem-=str&i);
str&i)=str&W);
str&W)=tem-;
$
cout<<"Keverse Ltring is=n"<<str;
getch();
return .;
$
1. Write a program to convert a string in lo&ercase.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
cout<<"Enter a string:";
gets(str);
#or(int i=.;str&i)<=4=.4;i)
str&i) = (str&i)>=4+4 55 str&i)<=464)O(str&i)I/):str&i);
cout<<str;
getch();
return .;
$
2. Write a program to convert a string in uppercase.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main( )
{
clrscr( );
char str&'.);
cout<<"Enter a string:";
gets(str);
#or(int i=.;str&i)<=4=.4;i)
str&i) = (str&i)>=4a4 55 str&i)<=484)O(str&i)3I/):str&i);
cout<<str;
getch();
return .;
$
. .tructures
1. Write a C program to accept records of the different states using array of
structures. The structure should contain char state, population, literacy rate, and income.
Display the state whose literacy rate is highest and whose income is highest.
4 #include<stdio.h>
5 #defne M 50
6
7 struct state {
8 char name[50];
9 lon int !o!ulation;
"0 #oat literac$%ate;
"" #oat income;
"& ' st[M]; () arra$ o* structure )(
13
"4 int main+, {
"5 int i- n- ml- mi- ma.imum/iterac$%ate- ma.imum0ncome;
"1 #oat rate;
"7 ml 2 mi 2 3";
"8 ma.imum/iterac$%ate 2 ma.imum0ncome 2 0;
19
&0 !rint*+45nter ho6 man$ states74,;
&" scan*+48d4- 9n,;
22
&: *or +i 2 0; i < n; i;;, {
&4 !rint*+4<n5nter state 8d details 74- i,;
25
&1 !rint*+4<n5nter state name 7 4,;
&7 scan*+48s4- 9st[i].name,;
28
&9 !rint*+4<n5nter total !o!ulation 7 4,;
:0 scan*+48ld4- 9st[i].!o!ulation,;
31
:& !rint*+4<n5nter total literar$ rate 7 4,;
:: scan*+48*4- 9rate,;
:4 st[i].literac$%ate 2 rate;
35
:1 !rint*+4<n5nter total income 7 4,;
:7 scan*+48*4- 9st[i].income,;
:8 '
39
40 *or +i 2 0; i < n; i;;, {
4" i* +st[i].literac$%ate >2 ma.imum/iterac$%ate, {
4& ma.imum/iterac$%ate 2 st[i].literac$%ate;
4: ml;;;
44 '
45 i* +st[i].income > ma.imum0ncome, {
41 ma.imum0ncome 2 st[i].income;
47 mi;;;
48 '
49 '
50
5" !rint*+4<n=tate 6ith hihest literar$ rate 78s4- st[ml].name,;
5& !rint*+4<n=tate 6ith hihest income 78s4- st[mi].name,;
53
54 return +0,;
55 '
56
2. Calculate Size of Structure in C Programming
#include<stdio.h>

struct stud {
int roll;
char name["0];
int mar>s;
';

int main+, {
int si?e;
struct stud s;

si?e 2 si?eo*+s,;
!rint*+4n=i?e o* =tructure 7 8d4- si?e,;

return+0,;
'
3. Write a program to use structure within union, display the contents of structure
elements.
#include<stdio.h>
#include<conio.h>

@oid main+, {
struct student {
char name[:0];
char se.;
int rollno;
#oat !ercentae;
';

union details {
struct student st;
';
union details set;

!rint*+45nter details74,;

!rint*+4<n5nter name 7 4,;
scan*+48s4- set.st.name,;
!rint*+4<n5nter roll no 7 4,;
scan*+48d4- 9set.st.rollno,;

#ushall+,;

!rint*+4<n5nter se. 7 4,;
scan*+48c4- 9set.st.se.,;
!rint*+4<n5nter !ercentae 74,;
scan*+48*4- 9set.st.!ercentae,;

!rint*+4<nAhe student details are 7 <n4,;
!rint*+4<name 7 8s4- set.st.name,;
!rint*+4<n%ollno 7 8d4- set.st.rollno,;
!rint*+4<n=e. 7 8c4- set.st.se.,;
!rint*+4<nBercentae 7 8*4- set.st.!ercentae,;

etch+,;
'
. The annual e!amination is conducted for "# students for three su$%ects. Write a
program to read the data and determine the following&
'a( Total mar)s o$tained $y each student.
'$( The highest mar)s in each su$%ect and the *oll +o. of the student who secured it.
'c( The student who o$tained the highest total mar)s.
#include<stdio.h>
#defne =0C5 50

struct student {
char name[:0];
int rollno;
int suD[:];
';

@oid main+, {
int i- E- ma.- count- total- n- a[=0C5]- ni;
struct student st[=0C5];
clrscr+,;

!rint*+45nter ho6 man$ students7 4,;
scan*+48d4- 9n,;

() *or loo! to read the names and roll numDers)(
*or +i 2 0; i < n; i;;, {
!rint*+4<n5nter name and roll numDer *or student 8d 7 4- i,;
scan*+48s4- 9st[i].name,;
scan*+48d4- 9st[i].rollno,;
'

() *or loo! to read ith studentFs Eth suDEect)(
*or +i 2 0; i < n; i;;, {
*or +E 2 0; E <2 &; E;;, {
!rint*+4<n5nter mar>s o* student 8d *or suDEect 8d 7 4- i- E,;
scan*+48d4- 9st[i].suD[E],;
'
'

() +i, *or loo! to calculate total mar>s oDtained D$ each student)(

*or +i 2 0; i < n; i;;, {
total 2 0;
*or +E 2 0; E < :; E;;, {
total 2 total ; st[i].suD[E];
'
!rint*+4<nAotal mar>s oDtained D$ student 8s are 8dn4- st[i].name-total,;
a[i] 2 total;
'

() +ii, *or loo! to list out the studentFs roll numDers 6ho
ha@e secured the hihest mar>s in each suDEect )(

() roll numDer 6ho secured the hihest mar>s )(

*or +E 2 0; E < :; E;;, {
ma. 2 0;
*or +i 2 0; i < n; i;;, {
i* +ma. < st[i].suD[E], {
ma. 2 st[i].suD[E];
ni 2 i;
'
'
!rint*+4<n=tudent 8s ot ma.imum mar>s 2 8d in =uDEect 7
8d4-st[ni].name- ma.- E,;
'

ma. 2 0;

*or +i 2 0; i < n; i;;, {
i* +ma. < a[i], {
ma. 2 a[i];
ni 2 i;
'
'

!rint*+4<n8s oDtained the total hihest mar>s.4- st[ni].name,;
etch+,;
'
1. @ive the output of the follo&ing program
#include<iostream.h>
struct Ci%el
{
int D,K;
$;
void Zis-lay(Ci%el C)
{
cout<<"Dol]<<C.D<<"Ko,"<<C.K<<endl;
$
void main()
{
Ci%el X={J.,2.$,9,6;
6=X;
X.D=(.;
9=6;
9.D=(.;
9.K=/.;
6.D3=(2;
Zis-lay(X);
Zis-lay(9);
Zis-lay(6);
$
2. +ind the output of the follo&ing program:
#include <iostream.h>
struct CE+9
{
int Lcore, Monus;
$;
void Dalculate(CE+9 5C, int G=(.)
{
C.Lcore;
C.Monus=G;
$
void main()
{
CE+9 CE={(.,(2$;
Dalculate(CE,2);
cout<<CE.Lcore<<]:]<<CE.Monus<<endl;
Dalculate(CE);
cout<<CE.Lcore<<]:]<<CE.Monus<<endl;
Dalculate(CE,(2);
cout<<CE.Lcore<<]:]<<CE.Monus<<endl;
$
. +ind the output of the follo&ing program:
#include<iostream.h>
struct UyMo%
{
int Eength, Mreadth, Teight;
$;
void Zimension (UyMo% U)
{
cout<<U.Eength<<"%"<<U.Mreadth<<"%";
cout<<U.Teight<<endl;
$
void main ()
{
UyMo% M(={(.,(2,2$, M/, MI;
M(.Teight;
Zimension(M();
MI = M(;
MI.Eength;
MI.Mreadth;
Zimension(MI);
M/ = MI;
M/.Teight=2;
M/.Eength33;
Zimension(M/);
$
!. 2e&rite the follo&ing program after removing the syntactical errors <if any=. 6nderline
each correction.
#include <iostream.h>
struct Ci%els
{
int Dolor, Ltyle;
$
void Lho,Coint(Ci%els C)
{
cout<<C.Dolor,C.Ltyle<<endl;
$
void main()
{
Ci%els Coint(=(2,I);
Lho,Coint(Coint();
Ci%els Coint/=Coint(;
Dolor.Coint(=/;
Lho,Coint(Coint/);
$
2. Zeclare a structure to re-resent a com-le% num1er (a num1er having a real -art and imaginary
-art). Prite D #unctions to add, su1tract, multi-ly and divide t,o com-le% num1ers.
A. +n array stores details o# /2 students (rollno, name, marNs in three su1Wect). Prite a -rogram to
create such an array and -rint out a list o# students ,ho have #ailed in more than one su1Wect.
!. Write the output of the follo&ing program :
#include <iostream.h>
void X(int 5+, int 5M)
{
+ = + M;
M = + 3 M;
+=+3M;
$
void main()
{
int a = J, 1 =(';
X(a,1);
cout<<a<<],]<<1;
$
Write the output of the follo&ing program:
#include <iostream.h>
void X(int +, int 5M)
{
+ = +M;
M = +3M;
+ = +3M;
$
void main()
{
int a=J, 1=l';
X(a,1);
cout<< a <<],]<<1;
$
Write the output of the follo&ing program:
# include <iostream.h>
void E%ecute(int 5M, int D=(..)
{
int "EUC = M D;
M = "EUC;
i# (D == (..)
cout<<"EUC<<" "<<M<<" "<<D<<endl;
$
void main()
{
int U= :., G = (.;
E%ecute(U);
cout << U << _ _<< G << endl;
E%ecute(U,G);
cout << U<<_ "<<G<< endl;
$
@ive the output of the follo&ing program
# include <iostream.h>
int glo1al = (.;
void #unc(int 5%, int y)
{
% = % 3 y;
y = % ! (.;
cout << % << << y << 4=n4;
$
void main()
{
int glo1al = B:
#unc (::glo1al, glo1al);
cout << glo1al <<],] << ::glo1al <<^=n^;
#unc(glo1al,:: glo1al);
cout<< glo1al << _,]<<::glo1al<<`=n^;
$
Write the output of the follo&ing program :
# include <iostream.h>
static int i=(..;
void a1c()
{
static int i=';
cout<< "#irst=" <<i;
$
main()
{
static int i = /;
a1c();
cout << "second =] << i << endl;
a1c();
$
Write the output of the follo&ing program:
# include <iostream.h>
int #unc(int 5%, int y = (.)
{
i# (%0y == .)
return %;
else
return y33;
$
void main()
{
int -=/., Q=/I;
Q=#unc (-,Q);
cout << - << " " << " " << Q << endl;
-=#unc (Q);
cout<< - << " " << " " << Q << endl;
Q=#unc (-);
cout << - << " " << " " << Q << endl;
$
#. Write a program using function &hich accept t&o integers as an argument and return its
sum. Call this function from main< = and print the results in main< =. solution
#include<iostream.h>
#include<conio.h>
int sum(int, int);
int main()
{
int %,y,s;
cout<<"Enter #irst num1er : ";
cin>>%;
cout<<"Enter second num1er : ";
cin>>y;
s=sum(%,y);
cout<<""he sum is : "<<s;
getch();
return .;
$
int sum(int a, int 1)
{
int total;
total=a1;
return total;
$
$. Write a function to calculate the factorial value of any integer as an argument. Call this
function from main< = and print the results in main< =. solution
#include<iostream.h>
#include<conio.h>
int #actorial(int);
int main()
{
int %,#;
cout<<"Enter num1er : ";
cin>>%;
#=#actorial(%);
cout<<""he #actorial is :"<<#;
getch();
return .;
$
int #actorial(int a)
{
int #act=(;
,hile(a>=()
{
#act=#act!a;
a33;
$
return #act;
$
'. Write a function that receives t&o num"ers as an argument and display all prime num"ers
"et&een these t&o num"ers. Call this function from main< =. solution

#include<iostream.h>
#include<conio.h>
void sho,-rime(int, int);
int main()
{
int %,y;
cout<<"Enter #irst num1er : ";
cin>>%;
cout<<"Enter second num1er : ";
cin>>y;
sho,-rime(%,y);
getch();
return .;
$
void sho,-rime(int a, int 1)
{
int #lag;
#or(int i=a(;i<=1;i)
{
#lag=.;
#or(int W=/;W<i;W)
{
i#(i0W==.)
{
#lag=(;
1reaN;
$
$
i#(#lag==. 55 i>()
cout<<i<<endl;
$
$
*. 2aising a num"er to a po&er p is the same as multiplying n "y itself p times. Write a
function called po&er that ta/es t&o arguments, a dou"le value for n and an int value for p,
and return the result as dou"le value. 6se default argument of 2 for p, so that if this argument
is omitted the num"er &ill "e s>uared. Write the main function that gets value from the user
to test po&er function. .olution
#include<iostream.h>
#include<conio.h>
dou1le -o,er(dou1le,int=/);
int main()
{
int -;
dou1le n,r;
cout<<"Enter num1er : ";
cin>>n;
cout<<"Enter e%-onent : ";
cin>>-;
r=-o,er(n,-);
cout<<"Kesult is "<<r;
r=-o,er(n);
cout<<"=nKesult ,ithout -assing e%-onent is "<<r;
getch();
return .;
$
dou1le -o,er(dou1le a, int 1)
{
dou1le %=(;
#or(int i=(;i<=1;i)
%=%!a;
return %;
$
,. Write a function called AeroBsmall<= that has t&o integer arguments "eing passed "y
reference and sets the smaller of the t&o num"ers to (. Write the main program to access the
function. .olution
#include<iostream.h>
#include<conio.h>
void 8ero;small(int 5,int 5);
int main()
{
int %,y;
cout<<"Enter #irst num1er : ";
cin>>%;
cout<<"Enter second num1er : ";
cin>>y;
8ero;small(%,y);
cout<<"Hirst num1er is : "<<%;
cout<<"=nLecond num1er is : "<<y;
getch();
return .;
$
void 8ero;small(int 5a, int 51)
{
i#(a<1)
a=.;
else
1=.;
$

S-ar putea să vă placă și